{
    "name"  : "是否正确配置DHCP SNOOPING",
    "problems" : {
        "dhcp12" : {
            "desc"      : "【问题】未配置DHCP SNOOPING功能，这可能造成地址冲突，影响下联设备获取地址",
            "advice"    : ["【建议】配置DHCP SNOOPING "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#ip dhcp snooping"
                          ,"\tRuijie(config)#interface 上联口"
                          ,"\tRuijie(config-if)#ip dhcp snooping trust"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },
        "dhcp34" : {
            "desc"      : "【问题】未配置上联口",
            "advice"    : ["【建议】给上联口配置与认证服务器IP地址相同网段的地址 "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#interface XX 上联口"
                          ,"\tRuijie(config)#no switchport"
                          ,"\tRuijie(config-if)#ip address IP地址"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        },
        "dhcp56" : {
            "desc"      : "【问题】端口 ${0} 未配置DHCP SNOOPING TRUST功能，这可能造成地址冲突，影响下联设备获取地址",
            "advice"    : ["【建议】配置DHCP SNOOPING "
                          ,"配置示例："
                          ,"\tRuijie#config"
                          ,"\tRuijie(config)#interface 上联口"
                          ,"\tRuijie(config)#ip dhcp snooping trust"
                          ,"\tRuijie(dhcp-config)#end"
                          ,"\tRuijie#write"]
        }
    }
}
---------------------------code-------------------------------
#是否正确配置DHCP SNOOPING
param = {}
param["role"] = precondition('role')
param["serverIp"] = precondition('serverIp')
no_support_dev = false
deviceMode = get_device_model()
if deviceMode == 'RG-S2900G-E/P' || deviceMode == 'RG-S2600G-I' :
    no_support_dev = true
end
all_server_ip = []
ints = []
not_snooping_trust_ints = []
dhcp_server_addrs = []
times = 0
if no_support_dev:
    return "success"
end
def check_int_snooping_trust():
    length = ints.length
    if times < length:
        intTmp = ints[times]
        cmd = 'show running-config interface ' + intTmp
        response = cli(cmd)
        if response.search("Invalid input") != -1:
            return "skip"
        end
        exist = false
        results = response.split(/\r?\n/ig)
        i = 1
        while i < results.length:
            resultsTmp = results[i]
            if resultsTmp.search('ip dhcp snooping trust') != -1:
                exist = true
                break
            end
            i = i + 1
        end
        if exist == fasle:
            intsTmp = ints[times]
            not_snooping_trust_ints.push(intsTmp)
        end
        times = times + 1
        res = check_int_snooping_trust()
        return res
    else:
        str = not_snooping_trust_ints.join(',')
        add_problem("dhcp56",str)
        return "warn"
    end
end
def get_port_config():
    response = cli('show ip interface brief')
    results = response.split(/\r?\n/ig)
    exist = false
    i = 2
    while i < results.length:
        resultsTmp = results[i]
        arr = resultsTmp.split(/\s\s+/ig)
        if arr.length < 3:
            continue;
        end
        int = arr[0]
        ip_pri = arr[1]
        ip_sec = arr[2]   
        if ip_pri.search("no address") != -1:
            continue;
        end
        k = 0
        while k < all_server_ip.length:
            ipStr = cidrSubnet(ip_pri)
            all_server_ipTmp = all_server_ip[k]
            if ipStr.contains(all_server_ipTmp) :
                exist = true
                ints.push(int)
                break
            end
            k = k + 1
        end
        i = i + 1
    end
    if exist == false:
        add_problem("dhcp34")
        return "warn"
    else :
        res = check_int_snooping_trust()
        return res
    end
end
def put_uqiue_to(data):
    exist = false
    k = 0 
    while k < all_server_ip.length:
        serverIpTmp = all_server_ip[k]
        if data == serverIpTmp:
            exist = true
            break
        end
        k = k + 1
    end
    if !exist:
        all_server_ip.push(data)
    end
end

def get_all_serverip():
    response = cli("show run | include ip helper-address")
    results = response.split(/\r?\n/ig)
    exist = false    
    i = 1
    while i < results.length:
        resultsTmp = results[i]
        if resultsTmp.search("ip helper-address") != -1 :
            exist = true
            resultsTmp = resultsTmp.trim()
            dhcp_server_addr_arr = resultsTmp.split(' ')
            index = dhcp_server_addr_arr.length -1
            dhcp_server_addr = dhcp_server_addr_arr[index]
            dhcp_server_addrs.push(dhcp_server_addr)
        end
        i = i + 1
    end
    i = 0 
    while i < dhcp_server_addrs.length:
        dhcp_server_addrTmp = dhcp_server_addrs[i]
        put_uqiue_to(dhcp_server_addrTmp)
        i = i + 1
    end
    
    i = 0 
    serverIp = param["serverIp"]
    while i < serverIp.length:
        serverIpTmp = serverIp[i]
        put_uqiue_to(serverIpTmp)
        i = i + 1
    end
    
    if all_server_ip.length == 0:
        return "success"
    else :
        res = get_port_config()
        return res
    end     
end

role = param["role"]
if role == 'relay' :
    response = cli("show run | include dhcp snooping")
    results = response.split(/\r?\n/ig)
    exist = false    
    i = 1
    while i < results.length:    # @fanbh 这边有必要去拆成数组再一个一个的找呢？直接输出内容进行search不是更简洁？
        resultsTmp = results[i]
        if resultsTmp.search("dhcp snooping") != -1 :
            exist = true   # @fanbh 缺少break
        end
        i = i + 1
    end
    if exist == false:
        add_problem("dhcp12")
        return "warn"
    else :
        res = get_all_serverip()
        return res
    end
else :
    return "success"
end

return "success"
















