go.ligato.io/vpp-agent/v3@v3.5.0/tests/robot/libraries/interface/interface_generic.py (about) 1 2 def get_interface_index_from_api(data, name): 3 """Process data from sw_interface_dump API and return index 4 of the interface specified by name. 5 6 :param data: Output of interface dump API call. 7 :param name: Name of the interface to find. 8 :type data: list 9 :type name: str 10 :returns: Index of the specified interface. 11 :rtype: int 12 :raises RuntimeError: If the interface is not found. 13 """ 14 15 for iface in data: 16 if iface["sw_interface_details"]["interface_name"] == name: 17 return iface["sw_interface_details"]["sw_if_index"] 18 else: 19 raise RuntimeError( 20 "Interface with name {name} not found in dump. " 21 "Dumped data: {data}".format(name=name, data=data)) 22 23 24 def get_interface_name_from_api(data, index): 25 """Process data from sw_interface_dump API and return index 26 of the interface specified by name. 27 28 :param data: Output of interface dump API call. 29 :param index: Index of the interface to find. 30 :type data: list 31 :type index: int 32 :returns: Name of the specified interface. 33 :rtype: str 34 :raises RuntimeError: If the interface is not found. 35 """ 36 37 for iface in data: 38 if iface["sw_interface_details"]["sw_if_index"] == index: 39 return iface["sw_interface_details"]["interface_name"] 40 else: 41 raise RuntimeError( 42 "Interface with index {index} not found in dump. " 43 "Dumped data: {data}".format(index=index, data=data)) 44 45 46 def get_interface_state_from_api(data, index=-1, name="_"): 47 """Process data from sw_interface_dump API and return state 48 of the interface specified by name or index. 49 50 :param data: Output of interface dump API call. 51 :param index: Index of the interface to find. 52 :param name: Name of the interface to find. 53 :type data: list 54 :type index: int 55 :type name: str 56 :returns: State of the specified interface. 57 :rtype: str 58 :raises RuntimeError: If the interface is not found. 59 """ 60 61 if index == -1 and name == "_": 62 raise ValueError("Provide either an interface index or a name.") 63 64 for iface in data: 65 if iface["sw_interface_details"]["sw_if_index"] == int(index)\ 66 or iface["sw_interface_details"]["interface_name"] == name: 67 return iface["sw_interface_details"] 68 else: 69 raise RuntimeError( 70 "Interface with index {index} or name {name} not found in dump.\n " 71 "Dumped data:\n {data}".format(index=index, name=name, data=data)) 72 73 74 def convert_str_to_mac_address(hex_mac): 75 """Just add colons in the right places.""" 76 77 hex_pairs = [hex_mac[(x*2):((x+1)*2)] for x in range(6)] 78 79 mac = "{0}:{1}:{2}:{3}:{4}:{5}".format(*hex_pairs) 80 81 return mac