{
    "name"  : "端口是否使用非锐捷光模块",
    "problems" : {
        "fake" : {
            "desc"      : "【问题】端口${0}使用非锐捷光模块，可能导致端口状态异常",
            "advice"    : "【建议】更换锐捷光模块"
        }
    },
    "if_to_i2cbus" : {
        "S6220-48XS4QXS":{"1/1":"15","1/2":"14","1/3":"16","1/4":"17"}
    }
}
---------------------------code-------------------------------
#端口是否使用非锐捷光模块
param = {}
param["status"] = precondition('status')
param["type"] = precondition('type')
param["name"] = precondition('name')
if_to_i2cbus = get_configure_value('if_to_i2cbus')
_if_name = undefined
_if_nano_name = undefined
_if_slot = undefined
_if_date = undefined
_if_port = undefined
_i2c_num = 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 third():
    cmd = format('read %s 50 60 16', _i2c_bus)
    response = cli(cmd)
    response = cli("q", ":")
    response = cli("l", "#")
    response = cli("exit", "#")
    _fiber_pwd = response.match(/[\r\n]+((0x[0-9a-zA-Z]{1,2}[ ]+){15}(0x[0-9a-zA-Z]{1,2}))[ ]*[\r\n]+/)
    if _fiber_pwd.length > 1:
        _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
    else:
        res = jumpOut("UNCKECK",null)
        return res
    end
end

def second():
    response = cli(_i2c_num, ":")
    cmd = format('read %s 50 44 16 ', _i2c_bus)
    response = cli(cmd, ":")
    _fiber_sn = response.match(/[\r\n]+((0x[0-9a-zA-Z]{1,2}[ ]+){15}(0x[0-9a-zA-Z]{1,2}))[ ]*[\r\n]+/)
    if _fiber_sn.length > 1:
        _fiber_sn = _fiber_sn[1]
        res = third()
        return res  
    else:
        res = jumpOut("UNCKECK",null)
        return res        
    end

end
def GetI2cBusId(if_to_i2cbus, deviceMode, ifName):
    if if_to_i2cbus[deviceMode] == undefined:
        return null
    end
    dict = if_to_i2cbus[deviceMode]
    if dict[ifName] == undefined:
        return null
    else:
        return dict[ifName]
    end
end
def GetI2cBusId_6220(if_to_i2cbus, deviceMode, ifName):
    if ifName.match(/^0\//) != null :
        ifIdxTemp = ifName.substring(2)
        ifIdx = parse_int(ifIdxTemp)
        if ifIdx % 2 == 0 :
            res = (16 + ifIdx) + ''
            return res
        end
            res = (18 + ifIdx) + ''
            return res   
        res = GetI2cBusId(if_to_i2cbus, deviceMode, ifName)      
        return res        
    end
end
def first():
    deviceMode = get_device_model()
    _i2c_bus =GetI2cBusId_6220(if_to_i2cbus, deviceMode, _if_nano_name)
    response = cli("run-system-shell")
    response = cli("dfd_debug",":")
    _i2c_num = response.match(/[\r\n]+(\d+)\s+dfd_i2c/)
    if _i2c_num.length > 1:
        _i2c_num = _i2c_num[1]
        res = second()
        return res
    else:
        res = jumpOut("UNCKECK",null)
        return res        
    end
end

def start():
    name = param["name"]
    cmd = format('show interface %s transceiver manuinfo', name)
    response = cli(cmd)
    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_nano_name = _if_name.match(/((?:\d+\/)+\d+)/)
    _if_slot = _if_name.match(/(?:\d+\/)?(\d+)\/\d+/)
    _if_port = _if_name.match(/(?:\d+\/)+(\d+)/)     
    if _if_nano_name.length > 1 && _if_slot.length > 1 && _if_port.length > 1:
        _if_nano_name = _if_nano_name[1]
        _if_slot = _if_slot[1]
        _if_port = _if_port[1]
    else:
        res = jumpOut("UNCKECK",null)
        return res
    end    
    res = first()
    return res
 end

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

start()


