{
    "name"  : "端口是否使用非锐捷光模块",
    "problems" : {
        "fake" : {
            "desc"      : "【问题】端口${0}使用非锐捷光模块，可能导致端口状态异常",
            "advice"    : "【建议】更换锐捷光模块"
        }
    },
    "if_to_i2cbus" : {
        "M18000-24GT20SFP4XS-ED":{"45":21,"46":22,"47":23,"48":24},
        "M8600E-24GT20SFP4XS-ED":{"45":21,"46":22,"47":23,"48":24},
        "M7800E-24GT20SFP4XS-ED":{"45":21,"46":22,"47":23,"48":24}
    }
}
---------------------------code-------------------------------
#端口是否使用非锐捷光模块
param = {}
param["status"] = precondition('status')
param["type"] = precondition('type')
param["name"] = precondition('name')
if_to_i2cbus = get_configure_value('if_to_i2cbus')
_if_slot = undefined
_if_port = undefined
_if_date = undefined
_if_dev_id = undefined
_if_name = undefined
_slotNames = undefined
_i2c_bus = undefined
_fiber_sn = undefined
_fiber_pwd = undefined

def jumpOut(message, proType):
    name = param["name"]
    if message == "UNCKECK" || message == "NONEED":
        set_share_value("checkFiberInsert_power", true)
        return "skip"
    end
    if proType == undefined:
        return 'skip'
    end
    if proType == "fake":
        addProblem("fake", [name])
        return 'warn'
    end
    return 'success'
end
def GetFiberCode(rawData):
    if rawData == undefined:
        return null
    end
    chars = rawData.raplace(/0x/g, ' ')
    chars = chars.trim()
    chars = chars.split(/\s+/)
    buf = []
    i = 0
    while i < chars.length:
        stringTemp = chars[i]
        stringTemp = '0x' + stringTemp
        buf[i] = parse_int(stringTemp)
        i = i + 1
    end
    return buf
end
def IsAllZero(buf) :
    i = 0
    while i < buf.length:
        if buf[i] != 0:
            return false
        end
        i = i + 1
    end
    return true
end

def processFiber(fiber_sn, fiber_pwd, if_date):
    sn = GetFiberCode(fiber_sn)
    pwd = GetFiberCode(fiber_pwd)
    if pwd != null:
        isMatch = false
        res = IsAllZero(pwd)
        if !res :
           isMatch = ValidateFiber(sn, 0, pwd)
        end
        if !isMatch:
            if if_date != undefined:
                date1 = dateFormat(if_date)
                date2 = dateFormat("2015-1-1")
                if date1 < date2 :
                    isMatch = true
                else:
                    isMatch = false                
                end
            end
        end
        if !isMatch:
            return false
        end
        return true
    end
end
def fourth():
    reg = /Last Read:[ ]*[\r\n]+([^\r\n]*)/
    cmd = format('./hw_test.bin i2c_rd %s 0x50 0x44 0 0x10', _i2c_bus)
    response = cli(cmd)
    _fiber_sn = response.match(reg)
    if _fiber_sn == null:
        res = jumpOut("UNCKECK",null)
        return res
    end
    _fiber_sn = _fiber_sn[1]
    cmd = format('./hw_test.bin i2c_rd %s 0x50 0x60 0 0x10', _i2c_bus)
    response = cli(cmd)
    _fiber_pwd = response.match(reg)
    if _fiber_pwd == null:
        res = jumpOut("UNCKECK",null)
        return res
    end
    _fiber_pwd = _fiber_pwd[1]
    re = processFiber(_fiber_sn, _fiber_pwd, _if_date)
    if re == false :
        res = jumpOut(null, "fake")
        return res
    else :
        return "success"
    end
end

def third(response):
    response = cli("cd sbin")
    response = cli("insmod dram_hwtest.ko")
    res = fourth()
    return res
end

def GetSlotNameById(names, slotId) :
        key = "slot_" + slotId
        if names[key] == undefined:
            return null
        else:
            return names[key]
        end
end
def GetI2cBusId_N18K(if_to_i2cbus, deviceMode, names, slotId, ifId) :
    slotName = GetSlotNameById(names,slotId)
    if slotName == null:
        return ifId
    end
    temp1 = if_to_i2cbus[slotName]
    temp2 = if_to_i2cbus[slotName][ifId]
    if temp1 != undefined && temp2 != undefined:
        return temp2
    else:
        return ifId
    end
end
def recycle(response, index_recycle, debugSsaCmd):
    index_recycle = index_recycle + 1
    if index_recycle > 5: 
        res = third()
        return res
    end
    response = cli(debugSsaCmd)
    response = cli('sh')
    reg = "devid:[ ]*" + _if_dev_id
    reg = newRegExp(reg, 'ig')
    if response.match(reg) != null:
        res = third()
        return res
    else:
        response = cli("exit",">")
        response = cli("\u001d","t")
        response = cli("e")
        recycle(response, index_recycle, debugSsaCmd)    
    end
end
def second():
    index_recycle = 0
    deviceMode = get_device_model()
    _i2c_bus = GetI2cBusId_N18K(if_to_i2cbus, deviceMode, _slotNames, _if_slot, _if_port)
    debugSsaCmd = format('debug-ssa %s', _if_slot)
    response = cli("run-system-shell")
    recycle(response, index_recycle, debugSsaCmd)
end

def start():
    response = cli('show version slot')
    if isInvalidCli(response):
        add_reason('无效指令：show version slot')
        return 'skip' 
    end
    reg = /[\r\n+[ ]+[a-zA-Z0-9]+[ ]+(\d)+[ ]+\d+[ ]+(M18000-44SFP4XS-ED|M18000-08XS-ED|M18000-48XS-DB|M18000-24XS4QXS-DB|M18000-24GT20SFP4XS-ED|M8600E-24GT20SFP4XS-ED|M8600E-08XS-ED|M7800E-24GT20SFP4XS-ED|M7800E-44SFP4XS-ED|M7800E-08XS-ED|M7800E-24SFP\/8GT-EB|M7800E-24GT\/8SFP-EB)/g
    _slotNames = {}
    matchResult = reg.exec(response)
    while matchResult != null:
        index = 'slot ' + matchResult[1]
        _slotNames[index] = matchResult[2]
        matchResult = reg.exec(response)
    end
    name = param["name"]
    cmd = format('show interface %s transceiver', name)
    response = cli(cmd)
    if isInvalidCli(response):
        add_reason('无效指令：show interface transceiver')
        return 'skip' 
    end
    if response.match(/Transceiver Type[ ]*:[ ]*[a-zA-Z0-9\-_]*?unknow/i) != null || response.match(/Transceiver Type[ ]*:[ ]*[a-zA-Z0-9\-_]*?Copper/i) != null: 
        res = jumpOut(null,'fake')
        return res
    end
    cmd = format('show interface %s transceiver manuinfo', name)
    response = cli(cmd)
    if isInvalidCli(response):
        add_reason('无效指令：show interface transceiver')
        return 'skip' 
    end
    reg = /Vendor Name[ ]*:[ ]*\b(?:RU[I\u0004]?JIE|INNOLIGHT)\b[^=]*?Manufacturing Date[ ]*:[ ]*([0-9\-]*)/i
    matchStr = response.match(reg)
    if matchStr != null:
        _if_date = matchStr[1] 
    end  
    reg = /[ ]*Vendor Name[ ]*:[^\r\n]*(XGIGA|FIBERXON|Star-cn|Gigalight|Volec Inc.|Tyco|FINISAR CORP.|AVAGO|OPNEXT|WTD|HG GENUINE)/i
    if response.match(reg) != null:
        res = jumpOut("NONEED",null)
        return res
    end
    reg = /[ ]*Vendor Name[ ]*:[ ]*(?!RU[I\u0004]JIE|INNOLIGHT)\S+/i 
    if response.match(reg) != null :
        res = jumpOut(null, 'fake')
        return res
    end
    _if_name = param["name"]
    _if_dev_id = _if_name.match(/(\d+)\/\d+\/\d+/)
    _if_slot = _if_name.match(/(?:\d+\/)?(\d+)\/\d+/)
    _if_port = _if_name.match(/(?:\d+\/)+(\d+)/)
    if _if_dev_id.length > 1 && _if_slot.length > 1 && _if_port.length >length :
        _if_dev_id = _if_dev_id[1]
        _if_slot = _if_slot[1]
        _if_port = _if_port[1]
        res = second() 
        return res
    else:
        res = jumpOut("UNCKECK",null)
        return res
    end
    
end

key = "checkFiberInsert_support_N18K"
if get_share_value(key) != true:
    return "skip"
end

start()


