{
    "name"  : "端口光电属性是否正确配置",
    "problems" : {
        "exception" : {
            "desc"      : "【问题】检测到端口${0}光电属性配置错误",
            "advice"    : ["【建议】建议重新配置端口光电属性 "
                          ,"配置示例： "
                          ,"\tRuijie#config "
                          ,"\tRuijie(config)#interface ${1} "
                          ,"\tRuijie(config-if)#medium-type ${2} "
                          ,"\tRuijie(config-if)#end"]
        }
    }
}
---------------------------code-------------------------------
#端口光电属性是否正确配置
param = {}
param["status"] = precondition('status')
param["name"] = precondition('name')
if_raw_type = ""
def jumpOut(proType):
    name = param["name"]
    if proType == undefined || proType == null:
        return 'skip'
    end
    if proType == 'exception':
        add_problem('exception', [name, name, if_raw_type])
        return 'warn'
    end
    return 'success'
end

def second(response, if_raw_type):
    reg = /\w+[ ]*[\r\n]+(?:Not eligible for medium-type change|% Unknown command)/
    responseArr = response.match(reg)
    if responseArr != null:
        cli("end")
        return "skip"
    else:
        sleep(2000)
        name = param["name"]
        cmd = format('show interface %s status',name)
        response = cli(cmd)
        if isInvalidCli(response):
            add_reason('无效指令：show interface')
            return 'skip' 
        end
        reg = /------[\r\n]+[^\r\n]*?\s{2,}(\w+)\s{2,}/
        statusArr = response.match(reg)
        if statusArr != null :
            status = statusArr[1]
            if status == 'up' :
                cmd = format('medium-type %s', if_raw_type)
                cli(cmd)
                cli("end")
                if if_raw_type == 'copper' :
                    if_switch_type = 'fiber'
                else :
                    if_switch_type = 'copper'
                end
                restore_cmd = []
                restore_cmd.push('end')
                restore_cmd.push('config')
                cmd = 'interface ' + name
                restore_cmd.push(cmd)
                cmd = 'medium-type ' + if_switch_type
                restore_cmd.push(cmd)
                restore_cmd.push('end')
                add_problem('exception', [name,if_switch_type], restore_cmd, [])
                return 'warn'
            else :
                cmd = format('medium-type %s', if_raw_type)
                cli(cmd)
                cli("end")
                set_share_value('manual', 'true')
                return 'skip'
            end             
        else:
            res = jumpOut(null)
            return "skip"            
        end
    end
end

def first(response):
    reg = /------[\r\n]+[^\r\n]*?[ ]+(\w+)[ ]*[\r\n]/
    responseArr = response.match(reg)
    if responseArr != null:
        if_raw_type = responseArr[1]
        if if_raw_type == 'copper' :
            if_switch_type = 'fiber'
        else if if_raw_type == 'fiber' :
            if_switch_type = 'copper'
        end
        cli("configure")
        name = param["name"]
        cmd = format('interface %s', name)
        cli(cmd)
        cmd = format('medium-type %s', if_switch_type)
        response = cli(cmd)
        res = second(response, if_raw_type)
        return res
    else:
        res = jumpOut(null)
        return "skip"
    end
    
end

def start():
    name = param["name"]
    cmd = format('show interface %s status', name)
    response = cli(cmd)
    if isInvalidCli(response):
        add_reason('无效指令：show interface')
        return 'skip' 
    end
    res = first(response)    
    return res
end

start()


