{
    "name"  : "是否软件故障导致端口无法LINKUP",
    "problems" : {
        "manual" : {
            "desc"      : "【问题】端口${0}上的寄存器配置错误。",
            "advice"    : "【建议】建议升级到10.4(3b17)p5最新版本"
        },
        "register": {
            "desc"      : "【问题】检测到端口${0}内部参数配置错误。",
            "advice"    : ["【建议】建议升级11.0(4)B1的最新版本。或者将至少一个端口退出聚合口规避该问题。 "
                          ,"\t规避方法示例： "
                          ,"\t\tRuijie(config)#interface ${1} " 
                          ,"\t\tRuijie(config-if-GigabitEthernet 1/1)#no port-group "
                          ,"\t\tRuijie(config-if-GigabitEthernet 1/1)#en "
                          ,"\t\tRuijie#write"]
        }
    },
    "slot_map" : {
        "S8606" : {"1":"10","2":"40","3":"50","4":"80"},
        "S8610" : {"1":"10","2":"20","3":"30","4":"40","5":"50","6":"60","7":"70","8":"80"},
        "S8614" : {"1":"10","2":"20","3":"30","4":"40","5":"50","6":"60","7":"70","8":"80","9":"110","10":"120","11":"130","12":"140"}
    }
}
---------------------------code-------------------------------
#是否软件故障导致端口无法LINKUP
_linePairExp = ''
_linePairOutput = ''
param = {}
param["status"] = precondition('status')
param["type"] = precondition('type')
param["name"] = precondition('name')
slot_map = get_configure_value('slot_map')
dev_type = undefined
if_slot = undefined
if_port = undefined

def jumpOut(proType):
    name = param["name"]
    if proType == null:
        return 'skip'
    end
    if proType == 'manual':
        add_problem('manual', [name])
        return 'warn'
    end
    if proType == 'register': 
        add_problem('register', [name,name])
        return 'warn'
    end
    return 'success'
end

def second(): 
    response = cli('show run')
    reg = format('interface \\w+ (?:\\d+/)?%s/\\d+[\\r\\n][^!]*!', if_slot) 
    reg = newRegExp(reg, 'ig')
    slot_ifs = []
    intfs = response.match(reg)
    if intfs != null:
        i = 0
        while i < intfs.length:
            intfTemp = intfs[i]
            intfTemp = intfTemp.match(/port-group/i)
            if intfTemp == null:
                slot_ifs.push(intfTemp)
            end
            i = i + 1
        end
    end
    if slot_ifs.length == 0: 
        return "success"
    end
    response = cli('show interface status') 
    reg = format('\\w+[ ](?:\\d+/)?%s/\\d+ [^\\r\\n]*', if_slot) 
    reg = newRegExp(reg, 'ig')
    slot_ifs = []
    intfs = response.match(reg)
    if intfs != null:
        i = 0
        while i < intfs.length:
            intfTemp = intfs[i]
            intfTemp = intfTemp.match(/[ ]down[ ]/i)
            if intfTemp == null:
                slot_ifs.push(intfTemp)
            end
            i = i + 1
        end
        if slot_ifs.length > 0: 
            return "success"
        else:
            res = jumpOut('manual')
            return res
        end
    end
end

def first_one():
    response = cli('ddev') 
    if_phy_val1 = undefined
    if_phy_val2 = undefined
    cmd = undefined
    reg = undefined
    matchStr = undefined
    reg = format('[\\r\\n ]([a-zA-Z0-9]*)\\([^)]*\\)[ ]+%s\\s', if_port)
    reg = newRegExp(reg)    
    matchStr = response.match(reg)
    if matchStr == null:
        return 'skip'
    end
    if_inner_name = matchStr[1]
    cmd1 = format('phy int %s 0x1f 0', if_inner_name)
    cmd2 = format('phy int %s 0', if_inner_name)
    response = cli(cmd1,'>')
    response = cli(cmd2,'>')
    matchStr = response.match(/Reg \d+:[ ]*([0-9x]*)[ ]*[\r\n]+/i)
    if matchStr == null:
        return 'skip'
    end
    if_phy_val1 = matchStr[1] 
    cmd1 = format('phy int %s 0x1f 0xffe0', if_inner_name)
    cmd2 = format('phy int 0x10', if_inner_name)
    response = cli(cmd1,'>')
    response = cli(cmd2,'>')
    matchStr = response.match(/Reg \d+:[ ]*([0-9x]*)[ ]*[\r\n]+/i)
    if matchStr == null:
        return 'skip'
    end
    if_phy_val2 = matchStr[1]
    response = cli('cons off')
    response = cli('dexit')
    response = cli('no debug ssp enable slot all')
    key1 = aAndB(if_phy_val1, 0x4800)
    key2 = aAndB(if_phy_val2, 0x4800)
    if key1 != 0 || key2 != 0 :
        res = jumpOut('register')
    else:
        res = second()
        return res
    end
    
end
def first():
    if_name = param["name"]
    if_slot = if_name.match(/(?:\d+\/)?(\d+)\/\d+/)
    if_port = if_name.match(/(?:\d+\/)+(\d+)/) 
    if if_slot.length > 1 && if_port.length > 1:
        if_slot = if_slot[1]
        if_port = if_port[1]
    else:
        return 'skip'
    end 
    if dev_type == '10x':
        deviceModel = get_device_model()
        if_inner_slot = ''
        if slot_map[deviceModel] == undefined:
            if_inner_slot = if_slot
            return "skip"
        else:
            if_inner_slot = slot_map[deviceModel]
        end
        response = cli('debug ssp enable slot all')
        response1 = cli('sd', '>')
        cmd = format('su %s', if_inner_slot) 
        response2 = cli(cmd, '>')
        response3 = cli('cons on')
        if isInvalidCli(response1) || isInvalidCli(response2) || isInvalidCli(response3):
            response = cli('cons off', '>')
            response = cli('dexit')
            response = cli('no debug ssp enable slot all')
            add_reason('无效指令：interface')
            return 'skip' 
        else:
            res = first_one()
            return res
        end
    end
end

def start():
    if param["status"] == 'up':
        res = jumpOut(null)
        add_reason('端口处于up状态')
        return res
    end
    deviceModel = get_device_model() 
    softVesion = get_device_softvesion()
    if softVesion.indexOf("10.") != -1:
        dev_type = '10x'
    else :
        dev_type = '11x'
    end
    if deviceModel.match(/s86\d{2}$/i) != null: 
        response = cli('show version | include System software version')
        str = response.match(/Release\((\w+)\)/i)
        if str != null && str[1] < '183033':
            #dev_type = '10x'
            res = first()
            return res
        else:
            return 'success'
        end
    else if deviceModel.match(/(s78\d{2}e|s86\d{2}e|n18\d{3})$/i) != null:
        str = get_device_softvesion() 
        str = str.match(/1[0-1][\.]0[^\[]*\[(\d+)/) 
        if str != null && str[1] < '20150805': 
            #dev_type = '11x'
            res = second() 
            return res
        else:
            return 'success'
        end
    else:
        return 'success'
    end
end

start()





















