def config_bgp_router_rst(dut, asn, config="add"): bgp_router_create_data = { "openconfig-network-instance:global": { "config": { "as": asn } } } bgp_router_create_url = "/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,bgp/bgp" bgp_router_del_url = "/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,bgp/bgp" if config == "add": try: res_create = st.rest_create(dut, path=bgp_router_create_url, data=bgp_router_create_data) except Exception as e: st.log(e) return False if not res_create["status"] in [200, 201, 204]: st.error("Failed to configure the BGP Router with {} through REST". format(asn)) return False elif config == "del": try: res_delete = st.rest_delete(dut, path=bgp_router_del_url) except Exception as e: st.log(e) return False if not res_delete["status"] in [200, 201, 204]: st.error( "Failed to un-configure the BGP Router with {} through REST". format(asn)) return False return True
def config_coppgroup_copptrap_viarest(dut, **kwargs): """ Author: Gangadhara Sahu ([email protected]) To configure classifier of type CoPP :param dut: :param classifier: :param action_group: :param config: :return bool: Example : copp.config_coppgroup_copptrap_viarest(vars.D2,classifier_name="class1", \ protocol_trap_id=["arp_req","arp_resp"],copp_action_group="copp-group-arp", \ trap_priority="10",trap_queue="10",cir="3500",cbs="3570",trap_action="trap") """ #action = kwargs['trap_action']; priority=int(kwargs['trap_priority']);queue=int(kwargs['trap_queue']) cir=kwargs['cir'];cbs=kwargs['cbs'];name=kwargs['copp_action_group'] if 'police_meter_type' not in kwargs: ptype = "PACKETS" else: ptype = kwargs['police_meter_type'] if 'police_mode' not in kwargs: pmode = "SR_TCM" else: pmode = kwargs['police_mode'] trap_action = kwargs['trap_action'].upper() class1 = kwargs['classifier_name'] if isinstance(kwargs['protocol_trap_id'],list): trap1 = "" for id in kwargs['protocol_trap_id']: if kwargs['protocol_trap_id'].index(id) == 0: trap1 = id else: trap1 = trap1 + "," + id else: trap1 = kwargs['protocol_trap_id'] rest_urls = st.get_datastore(dut, "rest_urls") rest_url = rest_urls['copp_global'] rest_data = {"openconfig-copp-ext:copp-groups":{"copp-group":[{"name":name,"config": \ {"cbs":cbs,"cir":cir,"meter-type":ptype,"mode":pmode,"queue":queue,"trap-priority":priority, \ "trap-action":trap_action}}]}, \ "openconfig-copp-ext:copp-traps":{"copp-trap":[{"name":class1, \ "config":{"trap-ids":trap1,"trap-group":class1}}]} } output=st.rest_create(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error("Failed to configure copp group and copp trap in {} due to bad request {} seen for REST command".format(dut,output["status"])) return False else: st.log("PASS: Rest operation for copp group and copp trap config return status {}".format(output['status'])) return True
def config_ip_addr_rst(dut, ip_addr, pre_len, interface, family, config="add"): ip_addr_create_data = { "openconfig-if-ip:address": [{ "ip": ip_addr, "config": { "ip": ip_addr, "prefix-length": pre_len } }] } if family == "ipv4": ip_addr_create_url = "/restconf/data/openconfig-interfaces:interfaces/interface={}/subinterfaces/subinterface=0/openconfig-if-ip:ipv4/addresses".format( interface) ip_addr_delete_url = "/restconf/data/openconfig-interfaces:interfaces/interface={}/subinterfaces/subinterface=0/openconfig-if-ip:ipv4/addresses".format( interface) else: ip_addr_create_url = "/restconf/data/openconfig-interfaces:interfaces/interface={}/subinterfaces/subinterface=0/openconfig-if-ip:ipv6/addresses".format( interface) ip_addr_delete_url = "/restconf/data/openconfig-interfaces:interfaces/interface={}/subinterfaces/subinterface=0/openconfig-if-ip:ipv6/addresses".format( interface) if config == "add": try: res_create = st.rest_create(dut, path=ip_addr_create_url, data=ip_addr_create_data) except Exception as e: st.log(e) return False if not res_create["status"] in [200, 201, 204]: st.error( "Failed to configure the {} routing interface with {} through REST" .format(family, ip_addr)) return False elif config == "del": try: res_delete = st.rest_delete(dut, path=ip_addr_delete_url) except Exception as e: st.log(e) return False if not res_delete["status"] in [200, 201, 204]: st.error( "Failed to un-configure the {} routing interface with {} through rst" .format(family, ip_addr)) return False return True
def config_domain(dut, domain_id, **kwargs): ''' Author: [email protected] :param dut: :param domain_id: Mclag domain_id :param local_ip: Mclag peer1 IP :param peer_ip: Mclag peer2 IP :param kwargs: optional parameters can be <local_ip|peer_ip|peer_interface|config|cli_type> :return: usage: config_domain(dut1,10, local_ip="10.10.10.1", peer_ip="10.10.10.2", delay_restore_timer="60") config_domain(dut1,10, local_ip="10.10.10.1", peer_ip="10.10.10.2", peer_interface='Ethernet0001') config_domain(dut1,10, config='del') ''' ### Optional parameters processing local_ip = kwargs.get('local_ip', None) peer_ip = kwargs.get('peer_ip', None) peer_intf = kwargs.get('peer_interface', None) delay_restore_timer = kwargs.get('delay_restore_timer', None) config = kwargs.get('config', 'add') cli_type = kwargs.pop('cli_type', st.get_ui_type(dut, **kwargs)) if cli_type == 'click': cmd = "config mclag {} {}".format(config, domain_id) if config == 'add': if 'local_ip' not in kwargs or 'peer_ip' not in kwargs: st.error("Mandatory parameters local_ip and peer_ip not found") return False cmd += " {} {}".format(local_ip, peer_ip) if 'peer_interface' in kwargs: cmd += ' {}'.format(peer_intf) #cmd += ' \n' output = st.config(dut, cmd) if "Missing argument" in output: st.error("Argument Missing") return False if "invalid peer ip address" in output: st.error("Invalid peer_ip address") return False if "invalid local ip address" in output: st.error("Invalid local_ip address") return False if "interface name is invalid" in output: st.error("Invalid peer interface") return False elif cli_type == 'klish': config = 'no ' if config == 'del' else '' if config == '': cmd = "mclag domain {}".format(domain_id) if 'local_ip' in kwargs: cmd = cmd + "\n" + "source-ip {}".format(local_ip) if 'peer_ip' in kwargs: cmd = cmd + "\n" + "peer-ip {}".format(peer_ip) if 'peer_interface' in kwargs: pintf = get_interface_number_from_name(peer_intf) cmd = cmd + "\n" + "peer-link {} {}".format( pintf['type'], pintf['number']) if 'delay_restore_timer' in kwargs: cmd = cmd + "\n" + "delay-restore {}".format( delay_restore_timer) cmd = cmd + "\n" + "exit" elif config == 'no ': if 'local_ip' in kwargs or 'peer_ip' in kwargs or 'peer_interface' in kwargs or 'delay_restore_timer' in kwargs: cmd = "mclag domain {}".format(domain_id) if 'local_ip' in kwargs: cmd = cmd + "\n" + "{}source-ip".format(config) if 'peer_ip' in kwargs: cmd = cmd + "\n" + "{}peer-ip".format(config) if 'peer_interface' in kwargs: cmd = cmd + "\n" + "{}peer-link".format(config) if 'delay_restore_timer' in kwargs: cmd = cmd + "\n" + "{}delay-restore".format(config) cmd = cmd + "\n" + "exit" else: cmd = "{}mclag domain {}".format(config, domain_id) output = st.config(dut, cmd, type="klish", conf=True) if "Could not connect to Management REST Server" in output: st.error("klish mode not working.") return False elif cli_type in ["rest-put", "rest-patch"]: if config == 'del': rest_urls = st.get_datastore(dut, 'rest_urls') rest_url_del = rest_urls['mclag_config_domain'].format( int(domain_id)) output = st.rest_delete(dut, rest_url_del) if not output["status"] in [200, 204]: st.error( "Failed to delete the mclag domain using REST in {} due to bad request {}" .format(dut, output["status"])) return False else: st.log( "PASS: Rest delete mclag domain return status {}".format( output['status'])) return True if 'local_ip' in kwargs: rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['mclag_config_all'] rest_data = { "openconfig-mclag:mclag-domains": { "mclag-domain": [{ "domain-id": int(domain_id), "config": { "domain-id": int(domain_id), "source-address": local_ip } }] } } output = st.rest_create(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error( "Failed to configure using POST source-address in {} due to bad request {} seen for REST command" .format(dut, output["status"])) return False else: st.log( "PASS: Rest operation using POST for source-address return status {}" .format(output['status'])) if 'peer_ip' in kwargs: rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['mclag_config_peer_ip'].format(int(domain_id)) rest_data = {"openconfig-mclag:peer-address": peer_ip} output = st.rest_modify(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error( "Failed to configure peer-address in {} due to bad request {} seen for REST command" .format(dut, output["status"])) return False else: st.log( "PASS: Rest operation for peer-address return status {}". format(output['status'])) if 'peer_interface' in kwargs: rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['mclag_config_peer_link'].format( int(domain_id)) rest_data = {"openconfig-mclag:peer-link": peer_intf} output = st.rest_modify(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error( "Failed to configure peer-link in {} due to bad request {} seen for REST command" .format(dut, output["status"])) return False else: st.log("PASS: Rest operation for peer-link return status {}". format(output['status'])) if 'delay_restore_timer' in kwargs: rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['mclag_config_delay_restore'].format( int(domain_id)) rest_data = { "openconfig-mclag:delay-restore": int(delay_restore_timer) } output = st.rest_modify(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error( "Failed to configure delay_restore_timer in {} due to bad request {} seen for REST command" .format(dut, output["status"])) return False else: st.log( "PASS: Rest operation for delay_restore_timer return status {}" .format(output['status'])) if 'keepalive_interval' in kwargs: rest_urls = st.get_datastore(dut, 'rest_urls') rest_url = rest_urls['mclag_config_keepalive'].format( int(domain_id)) rest_data = { "openconfig-mclag:keepalive-interval": int(kwargs['keepalive_interval']) } output = st.rest_modify(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error( "Failed to configure keepalive-interval in {} due to bad request {} seen for REST command" .format(dut, output["status"])) return False else: st.log( "PASS: Rest operation for keepalive-interval return status {}" .format(output['status'])) if 'session_timeout' in kwargs: rest_url = rest_urls['mclag_config_session_timeout'].format( int(domain_id)) rest_data = { "openconfig-mclag:session-timeout": int(kwargs['session_timeout']) } output = st.rest_modify(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error( "Failed to configure session-timeout in {} due to bad request {} seen for REST command" .format(dut, output["status"])) return False else: st.log( "PASS: Rest operation for session-timeout return status {}" .format(output['status'])) if 'mclag_system_mac' in kwargs: rest_url = rest_urls['mclag_config_mclag_system_mac'].format( int(domain_id)) rest_data = { "openconfig-mclag:mclag-system-mac": kwargs['mclag_system_mac'] } output = st.rest_modify(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error( "Failed to configure mclag-system-mac in {} due to bad request {} seen for REST command" .format(dut, output["status"])) return False else: st.log( "PASS: Rest operation for mclag-system-mac return status {}" .format(output['status'])) for arg1 in kwargs: if arg1 not in [ 'mclag_system_mac', 'session_timeout', 'keepalive_interval', 'delay_restore_timer', 'peer_interface', 'peer_ip', 'local_ip' ]: st.error("ARG {} is not supported through REST".format(arg1)) return True
def config_bgp_neighbor_rst(dut, **kwargs): neigh_ip = kwargs.get('neigh_ip') local_asn = kwargs.get('local_asn') remote_asn = kwargs.get('remote_asn') peer_type = kwargs.get('peer_type') family = kwargs.get('family') config = kwargs.get('config') if family == "ipv4": afi_safi_name = "openconfig-bgp-types:IPV4_UNICAST" else: afi_safi_name = "openconfig-bgp-types:IPV6_UNICAST" bgp_ipv4_neighbor_create_data = { "openconfig-network-instance:neighbor": [{ "neighbor-address": neigh_ip, "config": { "neighbor-address": neigh_ip, "enabled": True, "peer-as": remote_asn, "local-as": local_asn, "peer-type": peer_type }, "afi-safis": { "afi-safi": [{ "afi-safi-name": afi_safi_name, "config": { "afi-safi-name": afi_safi_name, "enabled": True } }] }, "timers": { "config": { "connect-retry": "60" } } }] } bgp_neighbor_create_url = "/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,bgp/bgp/neighbors" bgp_neighbor_delete_url = "/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/protocol=BGP,bgp/bgp/neighbors" if config == "add": try: res_create = st.rest_create(dut, path=bgp_neighbor_create_url, data=bgp_ipv4_neighbor_create_data) except Exception as e: st.log(e) return False if not res_create["status"] in [200, 201, 204]: st.error("Failed to configure the {} BGP neighbor {} through REST". format(family, neigh_ip)) return False elif config == "del": try: res_delete = st.rest_delete(dut, path=bgp_neighbor_delete_url) except Exception as e: st.log(e) return False if not res_delete["status"] in [200, 201, 204]: st.error( "Failed to un-configure the {} BGP neighbor {} through REST". format(family, neigh_ip)) return False return True
def rest_operation(dut, **kwargs): op = kwargs.get("http_method") url = kwargs.get("rest_url") data = kwargs.get("json_data") timeout = kwargs.get("timeout", 5) log_msg = [] status_map = {200 : "Rest operation successful", 201 : "Rest operation successful", 204 : "Rest operation successful", 400 : "Bad Request", 401 : "Unauthorized", 403 : "Forbidden", 404 : "Page not found", 405 : "Method not allowed", 409 : "Conflict", 415 : "Unsupported Media Type", 500 : "Internal Server Error"} retval = {} rest_result = True log_msg.append("[{}] -- HTTP METHOD : {}".format(dut, op.upper())) log_msg.append("URL : {}".format(url)) if data: log_msg.append("PAYLOAD : {}".format(data)) if not op or not url: st.log("Please provide http_method: {} or rest_url: {}".format(op,url)) return False op = op.lower() if op in ["get","delete"]: params = {"path":url, "rest_timeout":timeout} elif op in ["post", "put", "patch"]: params = {"path": url, "data":data,"rest_timeout": timeout} else: st.log("Please provide valid Http method") return False if kwargs.get("username"): params.update({"rest_username":kwargs.get("username")}) if kwargs.get("password"): params.update({"rest_password":kwargs.get("password")}) for iteration in range(1,5): try: if op == "get": retval = st.rest_read(dut, **params) elif op == "post": retval = st.rest_create(dut, **params) elif op == "put": retval = st.rest_update(dut, **params) elif op == "delete": retval = st.rest_delete(dut, **params) elif op == "patch": retval = st.rest_modify(dut, **params) else: st.log("Please provide valid Http method") return False break except Exception as e: if iteration > 2: credentials = st.get_credentials(dut) st.rest_init(dut, credentials[0], credentials[1], credentials[2]) if op == "get": tout = 180 if int(timeout) < 180 else timeout st.log("Setting timeout to {} sec".format(tout)) params.update({"rest_timeout": tout}) st.error(e) if "url" in retval.keys(): host_ip = re.findall(r'([0-9]+(?:\.[0-9]+){3})', retval["url"]) if host_ip: log_msg.insert(1, "HOST IP : {}".format(host_ip[0])) if "status" in retval.keys(): log_msg.append("STATUS : {} - {}".format(retval["status"], status_map[retval["status"]])) rest_result = True if retval["status"] in [200, 201, 204] else False if op == "get": if "output" in retval.keys(): log_msg.append("OUTPUT : {}".format(retval["output"])) if rest_result: st.log("{}".format(", ".join(log_msg))) else: st.error("{}".format(", ".join(log_msg))) return retval
def config_copp_action_group(dut, **kwargs): """ Author: Gangadhara Sahu ([email protected]) To configure classifier of type CoPP :param dut: :param copp_action_group: :param trap_action: :param trap_priority: :param trap_queue: :param police_meter_type: :param police_mode: :param cir: :param cbs: :param pir: :param pbs: :param config: :return bool: Example : config_copp_action_group(dut1,copp_action_group="copp-user-arp",config="no") config_copp_action_group(dut1,copp_action_group="copp-user-arp", trap_action="trap",trap_priority="3",trap_queue="3", police_meter_type="pps",police_mode="sr_tcm", cir="6000",cbs="6000") config_copp_action_group(vars.D2,copp_action_group="copp-group-arp",trap_priority="10",trap_queue="10",cir="3500",cbs="3570", trap_action="TRAP",cli_type="rest-put",config="yes") config_copp_action_group(vars.D2,copp_action_group="copp-group-arp",trap_priority="10",trap_queue="10",cir="3500",cbs="3570", trap_action="TRAP",cli_type="rest-put",config="no") """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut,**kwargs)) config_cmd = 'no' if kwargs.get('config','yes').lower() == 'no' else 'yes' if cli_type == 'klish': command = [] if config_cmd == 'no': command.append("no copp-action {}".format(kwargs['copp_action_group'])) else: command.append("copp-action {}".format(kwargs['copp_action_group'])) if "trap_action" in kwargs: command.append("set trap-action {}".format(kwargs['trap_action'])) if "trap_priority" in kwargs: command.append("set trap-priority {}".format(kwargs['trap_priority'])) if "trap_queue" in kwargs: command.append("set trap-queue {}".format(kwargs['trap_queue'])) if "police_meter_type" in kwargs: command.append("police meter-type {}".format(kwargs['police_meter_type'])) if "police_mode" in kwargs: command.append("police mode {} red drop".format(kwargs['police_mode'])) if "cir" in kwargs and "cbs" in kwargs and "pir" not in kwargs: command.append("police cir {} cbs {}".format(kwargs['cir'],kwargs['cbs'])) if "cir" in kwargs and "cbs" in kwargs and "pir" in kwargs and "pbs" in kwargs: command.append("police cir {} cbs {} pir {} pbs {}".format(kwargs['cir'],kwargs['cbs'],kwargs['pir'],kwargs['pbs'])) command.append("exit") st.config(dut, command,type="klish") elif cli_type in ['rest-put','rest-patch']: if config_cmd == 'no': rest_urls = st.get_datastore(dut, "rest_urls") rest_url = rest_urls['copp_group_config'].format(kwargs['copp_action_group']) output=st.rest_delete(dut, path=rest_url) if output["status"] not in [200, 204, 201]: st.error("Failed to delete classifier in {} due to bad request {} seen for REST command".format(dut,output["status"])) return False else: st.log("PASS: Rest operation for classifier delete return status {}".format(output['status'])) return True else: #action = kwargs['trap_action'] priority=int(kwargs['trap_priority']);queue=int(kwargs['trap_queue']) cir=kwargs['cir'];cbs=kwargs['cbs'];name=kwargs['copp_action_group'] if 'police_meter_type' not in kwargs: ptype = "PACKETS" else: ptype = kwargs['police_meter_type'] if 'police_mode' not in kwargs: pmode = "SR_TCM" else: pmode = kwargs['police_mode'] trap_action = kwargs['trap_action'].upper() rest_urls = st.get_datastore(dut, "rest_urls") rest_url = rest_urls['copp_global'] rest_data = {"openconfig-copp-ext:copp-groups":{"copp-group":[{"name":name,"config": \ {"cbs":cbs,"cir":cir,"meter-type":ptype,"mode":pmode,"queue":queue,"trap-priority":priority, \ "trap-action":trap_action}}]}} st.rest_create(dut, path=rest_url, data=rest_data) output=st.rest_create(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error("Failed to configure copp action in {} due to bad request {} seen for REST command".format(dut,output["status"])) return False else: st.log("PASS: Rest operation for copp action config return status {}".format(output['status'])) return True
def config_copp_classifier(dut, **kwargs): """ Author: Gangadhara Sahu ([email protected]) To configure classifier of type CoPP :param dut: :param classifier_name: :param protocol_trap_id: :param config: :return bool: Example : config_copp_classifier(dut1) config_copp_classifier(dut1,classifier_name="copp-system-arp",protocol_trap_id=["arp_req","arp_resp"],copp_group="copp-user-arp-action") config_copp_classifier(vars.D2,classifier_name="class1",protocol_trap_id=["arp_req","arp_resp"],cli_type="rest-put",config="yes") config_copp_classifier(vars.D2,classifier_name="class1",protocol_trap_id=["arp_req","arp_resp"],cli_type="rest-put",config="no") """ cli_type = kwargs.pop('cli_type', st.get_ui_type(dut,**kwargs)) config_cmd = 'no' if kwargs.get('config','yes').lower() == 'no' else 'yes' if cli_type == 'klish': command = [] if config_cmd == 'no': command.append("no class-map {}".format(kwargs['classifier_name'])) else: command.append("class-map {} match-type copp".format(kwargs['classifier_name'])) for id in kwargs['protocol_trap_id']: command.append("match protocol {}".format(id)) command.append("exit") st.config(dut, command,type="klish") elif cli_type in ['rest-put','rest-patch']: class1 = kwargs['classifier_name'] if config_cmd == 'no': bind_class_action_copp_policy(dut,classifier=class1,config="no") rest_urls = st.get_datastore(dut, "rest_urls") rest_url = rest_urls['copp_trap_config'].format(class1) output=st.rest_delete(dut, path=rest_url) if output["status"] not in [200, 204, 201]: st.error("Failed to delete classifier in {} due to bad request {} seen for REST command".format(dut,output["status"])) return False else: st.log("PASS: Rest operation for classifier delete return status {}".format(output['status'])) return True if isinstance(kwargs['protocol_trap_id'],list): trap1 = "" for id in kwargs['protocol_trap_id']: if kwargs['protocol_trap_id'].index(id) == 0: trap1 = id else: trap1 = trap1 + "," + id else: trap1 = kwargs['protocol_trap_id'] copp_group = kwargs['copp_group'] rest_urls = st.get_datastore(dut, "rest_urls") rest_url = rest_urls['copp_trap_global'] rest_data = {"openconfig-copp-ext:copp-trap":[{"name":class1,"config":{"trap-ids":trap1,"trap-group":copp_group}}]} output=st.rest_create(dut, path=rest_url, data=rest_data) if output["status"] not in [200, 204, 201]: st.error("Failed to configure classifier in {} due to bad request {} seen for REST command".format(dut,output["status"])) return False else: st.log("PASS: Rest operation for classifier config return status {}".format(output['status'])) return True