{
    "name"  : "端口是否使用非锐捷光模块",
    "problems" : {
        "fake" : {
            "desc"      : "【问题】端口${0}使用非锐捷光模块，可能导致端口状态异常",
            "advice"    : "【建议】更换锐捷光模块"
        }
    },
    "if_to_i2cbus" : {
        "all":{"any":"0"}
    },
    "if_to_sfpsw" : {
        "S2910-48GT4XS-E":{"49":"b","50":"7","51":"e","52":"d"},
        "S2910-24GT4XS-E":{"25":"e","26":"d","27":"b","28":"7"}
    }
}
---------------------------code-------------------------------
#端口是否使用非锐捷光模块
param = {}
param["status"] = precondition('status')
param["type"] = precondition('type')
param["name"] = precondition('name')
if_to_i2cbus = get_configure_value('if_to_i2cbus')
if_to_sfpsw = get_configure_value('if_to_sfpsw')
_if_name = undefined
_if_nano_name = undefined
_if_slot = undefined
_if_port = undefined
_i2c_num = undefined
_i2c_bus = undefined
_if_date = undefined
_fiber_sn = undefined
_fiber_pwd = undefined
_cpld_num = undefined
_cpld_val = 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 processCpldValReset(cpldVal) :
    endIndex = cpldVal.length - 1
    str = cpldVal.substring(0, endIndex)
    str = str + "f"
    return  str
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)
    _cpld_val = processCpldValReset(_cpld_val)
    response = cli("q",":")
    response = cli(_cpld_num,":")
    cmd = "write 5 " + _cpld_val
    response = cli(cmd,":")
    response = cli("q",":")
    response = cli("11")
    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]
    else:
        res = jumpOut("UNCKECK",null)
        return res
    end
    re = processFiber(_fiber_sn, _fiber_pwd, _if_date)
    if re == false:
        res = jumpOut(null, "fake")
        return res
    end
    return "success"
    
end
def processCpldVal(cpldVal, spf_sw) :
    endIndex = cpldVal.length - 1
    str = cpldVal.substring(0, endIndex)
    str = str + spf_sw
    return  str
end
def GetSfpSw(if_to_sfpsw, deviceMode, ifid) :
    if deviceMode == undefined || if_to_sfpsw == undefined || if_to_sfpsw[deviceMode] == undefined :
        return null
    end
    dic = if_to_sfpsw[deviceMode]
    if dic[ifid] == undefined:
        return null
    end
    return dic[ifid]
end
def second():
    cmd1 = _cpld_num
    response = cli(cmd1,":")
    if isInvalidCli(response):
        add_reason('无效指令：')
        return 'skip' 
    end
    cmd2 = "read 5 1"
    response = cli(cmd2,":")
    if isInvalidCli(response):
        add_reason('无效指令：')
        return 'skip' 
    end
    matchStr = response.match(/[ ]*reg_val[ ]*=[ ]*(?:0x)?([0-9a-fA-F]+)/)
    if matchStr == null:
        return "skip"
    end
    _cpld_val = matchStr[1]
    deviceMode = get_device_model()
    sfpSw = GetSfpSw(if_to_sfpsw, deviceMode, _if_port)
    _cpld_val = processCpldVal(_cpld_val, sfpSw)
    cmd = 'write 5 ' + _cpld_val
    response = cli(cmd,":")
    response = cli('q',":")
    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 != null:
        _fiber_sn = _fiber_sn[1]
        res = third()
        return res
    else:
        res = jumpOut("UNCKECK",null)
        return res        
    end
end

def first():
    _i2c_bus = "0"
    response = cli("run-system-shell")
    response = cli("dfd_debug",":")
    _cpld_num = response.match(/[\r\n]+(\d+)\s+dfd_cpld/)
    _i2c_num = response.match(/[\r\n]+(\d+)\s+dfd_i2c/) 
    if _cpld_num.length > 1 && _i2c_num.length > 1:
        _cpld_num = _cpld_num[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_S2910"
if get_share_value(key) != true:
    return "skip"
end

start()


