{
    "name"  : "端口是否正确插入网线",
    "problems" : {
        "open" : {
            "desc"      : "【问题】检测到端口${0}上的网线线序对${1}状态为断路",
            "advice"    : "【建议】建议重新插紧网线或者更换网线"
        },
        "short" : {
            "desc"      : "【问题】检测到端口${0}上的网线线序对${1}状态为短路",
            "advice"    : "【建议】建议更换网线"
        },
        "crosstalk" : {
            "desc"      : "【问题】检测到端口${0}上的网线线序对${1}状态为接触不良",
            "advice"    : "【建议】建议更换网线"
        },
        "toolong" : {
            "desc"      : "【问题】检测到端口${0}上的网线部署距离大于100米",
            "advice"    : "【建议】正常的网线部署距离必须小于100米，如果距离超过100米，建议在2台设备中间使用小交换机或者hub作为中转设备，保证2个端口间的网线距离小于100米或者调整设备位置或网线部署，使网线长度小于100米。" 
        }
    }
}
---------------------------code-------------------------------
#端口是否正确插入网线 
param = {}
param["status"] = precondition('status')
param["type"] = precondition('type')
param["name"] = precondition('name')
_linePairExp = ''
_linePairOutput = ''

def jumpOut(proType):
    name = param["name"]
    if proType == null:
        return 'skip'
    end
    if proType == 'open':
        add_problem('open', [name, _linePairOutput])
        return 'warn'
    end
    if proType == 'short':
        add_problem('short', [name, _linePairOutput])
        return 'warn'
    end
    if proType == 'crosstalk':
        add_problem('crosstalk', [name, _linePairOutput])
        return 'warn'
    end
    if proType == 'toolong':
        add_problem('toolong', [name])
        return 'warn'
    end
    return 'success'
end

def third(response):
    _linePairOutput = ''
    cli('end')
    reg = format("[\\r\\n]+(?:(?:([%s])[ ]+open[ ]+\\d+)|(cable-state:[ ]*open[ ]))", _linePairExp) 
    reg = newRegExp(reg, 'ig')
    isError = false
    matchStr = reg.exec(response)
    while matchStr != null:
        isError = true
        matchStrTemp = matchStr[1]
        if matchStrTemp != undefined:
            if _linePairOutput != '':
                _linePairOutput = _linePairOutput + ','
            end
            _linePairOutput = _linePairOutput + matchStrTemp
        end
        matchStr = reg.exec(response) 
    end
    if isError: 
        res = jumpOut('open')
        #return res
    end  
    
    isError1 = false
    reg = format("[\\r\\n]+(?:(?:([%s])[ ]+short[ ]+\\d+)|(cable-state:[ ]*short[ ]))", _linePairExp)
    reg = newRegExp(reg, 'ig')
    matchStr = reg.exec(response)
    while matchStr != null:
        isError1 = true
        matchStrTemp = matchStr[1]
        if matchStrTemp != undefined:
            if _linePairOutput != '':
                _linePairOutput = _linePairOutput + ','
            end
            _linePairOutput = _linePairOutput + matchStrTemp
        end
        matchStr  = reg.exec(response)
    end
    if isError1:
        res = jumpOut('short')
        #return res
    end    
    
    isError2 = false
    reg = format("[\\r\\n]+(?:(?:([%s])[ ]+crosstalk[ ]+\\d+)|(cable-state:[ ]*crosstalk[ ]))", _linePairExp)
    reg = newRegExp(reg, 'ig')
    matchStr = reg.exec(response)
    while matchStr != null:
        isError2 = true
        matchStrTemp = matchStr[1]
        if matchStrTemp != undefined:
            if _linePairOutput != '':
                _linePairOutput = _linePairOutput + ','
            end
            _linePairOutput = _linePairOutput + matchStrTemp
        end
        matchStr  = reg.exec(response)
    end
    if isError2:
        res = jumpOut('crosstalk')
        #return res
    end  
    
    isError3 = false
    reg = format("[\\r\\n]+(?:(?:[%s][ ]+\\w+[ ]+)|(?:cable-state:[ ]*\\w+[ ]+length\\(meters\\):[ ]*))([1-9]\\d{2}|\\d{4,})", _linePairExp)
    reg = newRegExp(reg, 'ig')
    matchStr = reg.exec(response)
    if matchStr != null:
        isError3 = true
        res = jumpOut('toolong')
        #return res
    end
    if isError || isError1 || isError2 || isError3 :
        return "warn"
    end
    return 'success'
end

def first():
    response = cli('line-detect detail')
    if isInvalidCli(response):
        response = cli('line-detect')
        if isInvalidCli(response):
            add_reason('无效指令：line-detect')
            return 'skip' 
        end
        res = third(response)
        return res
    else:
        res = third(response)
        return res            
    end
end

def start():
    status = precondition('status')
    type = precondition('type')
    if (status == 'up' || type != 'copper'):
        res = jumpOut(null)
        add_reason('接口为up或者端口不是copper类型')
        return res
    end
    _linePairExp = 'A-Z'
    paramName = precondition('name')
    if paramName.search(/fastethernet/i) != -1:
        _linePairExp = 'A-B'
    end
    intCmd = format('interface %s', paramName)
    response = cli('configure')
    response = cli(intCmd)
    if isInvalidCli(response):
        add_reason('无效指令：interface')
        return 'skip' 
    end
    res = first()
    return res
end

start() 


