{
    "name"  : "是否硬件故障导致端口无法LINKUP",
    "problems" : {
        "manual" : {
            "desc"      : "【问题】端口${0}存在硬件故障",
            "advice"    : ["【建议】"
                          ,"\t1. 如果设备上有空闲端口，可尝试更换端口使用。"
                          ,"\t2. 如果需要维修或更换设备，请联系4008-111-000。"]
        }
    },
    "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')
_if_name = undefined
_if_slot = undefined
_if_port = undefined
_if_inner_slot = undefined
_if_inner_name = undefined
_if_new_status = undefined

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

def second():
    if _if_new_status == undefined:
        return 'skip'
    end
    if _if_new_status == 'down':
        res = jumpOut('manual')
        return res
    end
    return 'success'
end

def first():
    portCmd = format('port %s lb\u003dphy', _if_inner_name)
    psCmd = format('ps %s', _if_inner_name)
    response = cli(portCmd, '>')
    if response.search(/port .*? lb\u003dphy(.|\r|\n)*>/i) == -1 :
        if softVersion.indexOf("11.") != -1:
            response = cli('exit', '#')
            response = cli('\u001d', 't')
            response = cli('e', '#')
            response = cli('exit', '#')
        else:
            response = cli('dexit', '#')
        end
        return "skip" 
    end
    sleep(2000)
    response = cli(psCmd, '>')
    if response.search(/ps (.|\r|\n)*>/i) == -1 :
        return "skip"
    end
    reg = format('[ ]+%s[ ]+(\\w+)', _if_inner_name)
    reg = newRegExp(reg,"i")
    matchStr = response.match(reg)
    if matchStr == null:
        return "skip"
    else:
        _if_new_status = matchStr[1]
        portCmd = format('port %s lb\u003dnone', _if_inner_name)
        response = cli(portCmd, '>')
        softVersion = get_device_softvesion()
        if softVersion.indexOf("11.") != -1:
            response = cli('exit', '#')
            response = cli('\u001d', 't')
            response = cli('e', '#')
            response = cli('exit', '#')
            res = second()
            return res
        else:
            response = cli('dexit', '#')
            res = second()
            return res
        end
    end
    
end

def start():
    if param["status"] == 'up':
        res = jumpOut(null)
        add_reason('端口处于up状态')
        return res
    end
    model = get_device_model()
    isSupport = isSupport(['S86','N18K'],model) 
    if !isSupport:
        res = jumpOut(null)
        return res
    end
    _if_name = param["name"]
    _if_slot = _if_name.match(/(?:\d+\/)?(\d+)\/\d+/)
    if _if_slot == null:
        res = jumpOut(null)
        return res
    end
    _if_slot = _if_slot[1]
    
    _if_port = _if_name.match(/(?:\d+\/)+(\d+)/) 
    if _if_port == null:
        res = jumpOut(null)
        return res
    end
    _if_port = _if_port[1]
    
    deviceMode = get_device_model()
    if _slot_map[deviceMode] == undefined:
        _if_inner_slot = _if_slot 
        return "skip"
    else:
        _if_inner_slot = _slot_map[deviceMode]
    end
    softVersion  = get_device_softvesion()
    if softVersion.indexOf('11.') != -1:
        response = cli('run-system-shell')
        debugSsaCmd = format('debug-ssa %s', _if_inner_slot)
        response = cli(debugSsaCmd, '>')
        response = cli('cons on', '>')
        response = cli('sd', '>')
        response = cli('ssaport ddev', '>')
        cli('exit', '>')
        reg = format('[\\r\\n]+[ ]+lport[ ]+%s[ ]*,[^\\r\\n]*? ([a-zA-Z0-9_]+)[ ]+mmu_port', _if_port)
        reg = newRegExp(reg)
        matchStr = response.match(reg)
        if matchStr == null:
            return 'skip'
        else:
            _if_inner_name = matchStr[1]
            res = first()
            return res
        end
    else:
        response = cli('debug ssp enable slot all', '>')
        if isInvalidCli(response):
            add_reason('无效指令：debug ssp enable slot all')
            return 'skip' 
        end     
        suCmd = format('su %s', _if_inner_slot)
        response = cli('sd', '>')
        response = cli(suCmd, '>')
        response = cli('cons on', '>')
        response = cli('ddev', '>') 
        if response.indexOf('excess arguments ignored beginning with') == -1:
            return 'skip'
        end
        reg = format('[\\r\\n ]([a-zA-Z0-9]*)\\([^)]*\\)[ ]+$${Attributes["if_port"]}\\s', _if_port)
        reg = newRegExp(reg)
        matchStr = response.match(reg)
        if matchStr == null:
            return 'skip'
        else:
            _if_inner_name = matchStr[1]
            res = first()
            return res
        end
    end
end

start()





















