def verify_bgp_neighbor_rst(dut, neigh_ip): bgp_neigh_get_url = "/restconf/data/openconfig-network-instance:network-instances/network-instance=default/protocols/" \ "protocol=BGP,bgp/bgp/neighbors/neighbor={}".format(neigh_ip) try: res_bgp_get = st.rest_read(dut, bgp_neigh_get_url) state = res_bgp_get['output'][u'openconfig-network-instance:neighbor'][ 0][u'state'][u'session-state'] if not state.encode('UTF-8') == 'ESTABLISHED': return False except Exception as e: st.log(e) return False return True
def config_max_sessions(dut, **kwargs): """ API to configure max mirror sessions Author: Chaitanya Vella ([email protected]) :param dut: :param kwargs: {"cli_ype":"rest","data":[{"name":"Mirror1","src_ip":"10.20.3.1","dst_ip":"10.23.3.5", "gre_type":"0x855","dscp":16,"ttl":5,"queue":6,"dst_port":"Ethernet28","src_port":"Ethernet20", "direction":"rx/tx"},{"name":"Mirror2","dst_port":"Ethernet20","src_port":"Ethernet22","direction":"rx"}, {"name":"Mirror3","dst_port":"Ethernet26","src_port":"Ethernet22","direction":"tx"}],"action":"config"} :return: response/False """ cli_type = kwargs.get("cli_type", "rest") if cli_type == "rest": status = 204 data = kwargs.get("data") action = data.get("action", "config") rest_url = "/restconf/data/{}".format(YANG_MODEL) if action == "config": if data.get("data"): rest_data = dict() rest_data[YANG_MODEL] = dict() rest_data[YANG_MODEL]["MIRROR_SESSION"] = dict() rest_data[YANG_MODEL]["MIRROR_SESSION"][ "MIRROR_SESSION_LIST"] = make_list(data.get("data")) response = st.rest_modify(dut, rest_url, rest_data) else: st.log("Required data not found -- {}".format(data)) return False elif action == "unconfig": response = st.rest_delete(dut, rest_url) elif action == "get": response = st.rest_read(dut, rest_url) status = 200 else: st.log("Unsupporte ACTION -- {}".format(action)) return False if response and response["status"] == status: return response else: st.log("RESPONSE -- {}".format(response)) return False else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False
def show_interface(dut, interface_name=None, cli_type=""): """ API to show sflow interface configuration Author: Chaitanya Vella ([email protected]) :param dut: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) cli_type = "klish" if cli_type in ["rest-put", "rest-patch"] else cli_type output = list() if cli_type == "klish" or cli_type == "click": command = "show sflow interface" if interface_name: command = "{} | grep {}".format(command, interface_name) return st.show(dut, command, type=cli_type) elif cli_type == "rest": if not interface_name: url = REST_URI else: url = "{}/SFLOW_SESSION/SFLOW_SESSION_TABLE".format(REST_URI) result = st.rest_read(dut, url, SFLOW_SESSION_LIST=interface_name) if result and result.get("status") == 200 and result.get("output"): if YANG_MODULE in result["output"]: data = result["output"][YANG_MODULE] if data.get("SFLOW_SESSION_TABLE").get("SFLOW_SESSION_LIST"): for intf_list in data.get("SFLOW_SESSION_TABLE").get( "SFLOW_SESSION_LIST"): response = dict() response["sampling_rate"] = intf_list.get( "sample_rate") response["admin_status"] = intf_list.get("admin_state") response["interface"] = intf_list.get("ifname") if response: output.append(response) else: st.log("{} not observed in ouput".format(YANG_MODULE)) else: st.log("REST show INTERFACE GET CALL --- {}".format(output)) return output else: st.log("UNSUPPORTED CLI TYPE {}".format(cli_type)) return output
def verify_max_sessions(dut, **kwargs): """ API to verify max sessions Author: Chaitanya Vella ([email protected]) :param dut: :param kwargs: :return: {"cli_ype":"rest","data":[{"name":"Mirror1","src_ip":"10.20.3.1","dst_ip":"10.23.3.5", "gre_type":"0x855","dscp":16,"ttl":5,"queue":6,"dst_port":"Ethernet28","src_port":"Ethernet20", "direction":"rx/tx"},{"name":"Mirror2","dst_port":"Ethernet20","src_port":"Ethernet22","direction":"rx"}, {"name":"Mirror3","dst_port":"Ethernet26","src_port":"Ethernet22","direction":"tx"}]} """ st.log("KWARGS -- {}".format(kwargs)) cli_type = kwargs.get("cli_type", "rest") if cli_type == "rest": rest_url = "/restconf/data/{}".format(YANG_MODEL) verify_data = kwargs.get("data").get("data") if not verify_data: st.log("DATA TO VERIFY IS NOT PROVIDED -- {}".format(verify_data)) return False response = st.rest_read(dut, rest_url) if response and response["status"] == 200: data = response["output"][YANG_MODEL]["MIRROR_SESSION"][ "MIRROR_SESSION_LIST"] if not data: st.log("DATA IN RESPONSE IS EMPTY -- {}".format(data)) return False for session_data in verify_data: output = filter_and_select(data, session_data.keys(), session_data) st.log("FILTER RESPONSE -- {}".format(output)) if not output: return False return True else: st.log("RESPONSE -- {}".format(response)) return False else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return False
def show(dut, cli_type=""): """ API to show sflow configuration Author: Chaitanya Vella ([email protected]) :param dut: :return: {u'agent_ip': '10.0.0.10', 'collectors': [{'port': '6343', 'collector_ip': '10.100.12.13'}, {'port': '6344', 'collector_ip': '10.144.1.2'}], u'collectors_cnt': '2', u'state': 'enabled', u'agent_id': 'loopback0', u'polling_interval': '20'} """ cli_type = st.get_ui_type(dut, cli_type=cli_type) cli_type = "klish" if cli_type in ["rest-put", "rest-patch"] else cli_type result = dict() if cli_type == "klish" or cli_type == "click": command = "show sflow" output = st.show(dut, command, type=cli_type) if output: result["collectors"] = list() for data in output: for key, value in data.items(): if value != "": if key not in [ "collector_ip", "collector_port", "collector_name" ]: result[key] = value else: result["collectors"].append({ "collector_name": data["collector_name"], "collector_ip": data["collector_ip"], "port": data["collector_port"] }) if result: result[ "collectors"] = utils_obj.remove_duplicate_dicts_from_list( result["collectors"]) else: return False elif cli_type == "rest": output = st.rest_read(dut, REST_URI) if output and output.get("status") == 200 and output.get("output"): if YANG_MODULE in output["output"]: data = output["output"][YANG_MODULE] if "SFLOW" in data: for key, value in data["SFLOW"].items(): if isinstance(value, list): for attributes in value: result.update( {"state": attributes.get("admin_state")}) result.update( {"agent_id": attributes.get("agent_id")}) result.update({ "polling_interval": attributes.get("polling_interval") }) result.update( {"sflow_key": attributes.get("sflow_key")}) if attributes.get("agent_id"): ip_address = get_interface_ip_address( dut, attributes.get("agent_id")) if ip_address: ip, _ = ip_address[0]['ipaddr'].split( '/') result.update({"agent_ip": ip}) if "SFLOW_COLLECTOR" in data: result.update({ "collectors_cnt": len(data["SFLOW_COLLECTOR"]["SFLOW_COLLECTOR_LIST"]) }) result.update({"collectors": list()}) for value in data["SFLOW_COLLECTOR"][ "SFLOW_COLLECTOR_LIST"]: collector_data = dict() collector_data.update({ "port": value.get("collector_port", DEFAULT_COLLECTOR_PORT) }) collector_data.update( {"collector_ip": value.get("collector_ip")}) collector_data.update( {"collector_name": value.get("collector_name")}) st.log("COLLECTORS {}".format(collector_data)) result["collectors"].append(collector_data) else: st.log("{} not observed in ouput".format(YANG_MODULE)) else: st.log("REST show GET CALL --- {}".format(output)) else: st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type)) return result
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