go.ligato.io/vpp-agent/v3@v3.5.0/tests/robot/libraries/vat_term.py (about) 1 import json 2 3 # input - json output from vxlan_tunnel_dump, src ip, dst ip, vni 4 # output - true if tunnel exists, false if not, interface index 5 def Check_VXLan_Tunnel_Presence(out, src, dst, vni): 6 out = out[out.find('['):out.rfind(']')+1] 7 data = json.loads(out) 8 present = False 9 if_index = -1 10 for iface in data: 11 if iface["src_address"] == src and iface["dst_address"] == dst and iface["vni"] == int(vni): 12 present = True 13 if_index = iface["sw_if_index"] 14 return present, if_index 15 16 # input - json output from sw_interface_dump, index 17 # output - interface name 18 def Get_Interface_Name(out, index): 19 out = out[out.find('['):out.rfind(']')+1] 20 data = json.loads(out) 21 name = "x" 22 for iface in data: 23 if iface["sw_if_index"] == int(index): 24 name = iface["interface_name"] 25 return name 26 27 # input - json output from sw_interface_dump, interface name 28 # output - index 29 def Get_Interface_Index(out, name): 30 out = out[out.find('['):out.rfind(']')+1] 31 data = json.loads(out) 32 index = -1 33 for iface in data: 34 if iface["interface_name"] == name: 35 index = iface["sw_if_index"] 36 return index 37 38 # input - json output from sw_interface_dump, index 39 # output - whole interface state 40 def Get_Interface_State(out, index): 41 out = out[out.find('['):out.rfind(']')+1] 42 data = json.loads(out) 43 state = -1 44 for iface in data: 45 if iface["sw_if_index"] == int(index): 46 state = iface 47 return state 48 49 # input - mac in dec from sw_interface_dump 50 # output - regular mac in hex 51 def Convert_Dec_MAC_To_Hex(mac): 52 hexmac=[] 53 for num in mac[:6]: 54 hexmac.append("%02x" % num) 55 hexmac = ":".join(hexmac) 56 return hexmac 57 58 # input - output from show memif intf command 59 # output - state info list 60 def Parse_Memif_Info(info): 61 state = [] 62 socket_id = '' 63 sockets_line = [] 64 for line in info.splitlines(): 65 if line: 66 try: 67 _ = int(line.strip().split()[0]) 68 sockets_line.append(line) 69 except ValueError: 70 pass 71 if (line.strip().split()[0] == "flags"): 72 if "admin-up" in line: 73 state.append("enabled=1") 74 if "slave" in line: 75 state.append("role=slave") 76 if "connected" in line: 77 state.append("connected=1") 78 if (line.strip().split()[0] == "socket-id"): 79 try: 80 socket_id = int(line.strip().split()[1]) 81 state.append("id="+line.strip().split()[3]) 82 for sock_line in sockets_line: 83 try: 84 num = int(sock_line.strip().split()[0]) 85 if (num == socket_id): 86 state.append("socket=" + sock_line.strip().split()[-1]) 87 except ValueError: 88 pass 89 except ValueError: 90 pass 91 if "enabled=1" not in state: 92 state.append("enabled=0") 93 if "role=slave" not in state: 94 state.append("role=master") 95 if "connected=1" not in state: 96 state.append("connected=0") 97 return state 98 99 # input - output from show br br_id detail command 100 # output - state info list 101 def Parse_BD_Details(details): 102 state = [] 103 details = "\n".join([s for s in details.splitlines(True) if s.strip("\r\n")]) 104 line = details.splitlines()[1] 105 if (line.strip().split()[6]) in ("on", "flood"): 106 state.append("unicast=1") 107 else: 108 state.append("unicast=0") 109 if (line.strip().split()[8]) == "on": 110 state.append("arp_term=1") 111 else: 112 state.append("arp_term=0") 113 return state 114 115 # input - node name, bd name, etcd dump converted to json, bridge domain dump 116 # output - list of interfaces (etcd names) in bd 117 def Parse_BD_Interfaces(node, bd, etcd_json, bd_dump): 118 interfaces = [] 119 bd_dump = json.loads(bd_dump) 120 etcd_json = json.loads(etcd_json) 121 for int in bd_dump[0]["sw_if"]: 122 bd_sw_if_index = int["sw_if_index"] 123 etcd_name = "none" 124 for key_data in etcd_json: 125 if key_data["node"] == node and key_data["type"] == "status" and "/interface/" in key_data["key"]: 126 if "if_index" in key_data["data"]: 127 if key_data["data"]["if_index"] == bd_sw_if_index: 128 etcd_name = key_data["data"]["name"] 129 interfaces.append("interface="+etcd_name) 130 if bd_dump[0]["bvi_sw_if_index"] != 4294967295: 131 bvi_sw_if_index = bd_dump[0]["bvi_sw_if_index"] 132 etcd_name = "none" 133 for key_data in etcd_json: 134 if key_data["node"] == node and key_data["type"] == "status" and "/interface/" in key_data["key"]: 135 if "if_index" in key_data["data"]: 136 if key_data["data"]["if_index"] == bvi_sw_if_index: 137 etcd_name = key_data["data"]["name"] 138 interfaces.append("bvi_int="+etcd_name) 139 else: 140 interfaces.append("bvi_int=none") 141 return interfaces 142 143 # input - bridge domain dump, interfaces indexes 144 # output - true if bd with int indexes exists, false id bd not exists 145 def Check_BD_Presence(bd_dump, indexes): 146 bd_dump = json.loads(bd_dump) 147 present = False 148 for bd in bd_dump: 149 bd_present = True 150 for index in indexes: 151 int_present = False 152 for bd_int in bd["sw_if"]: 153 if bd_int["sw_if_index"] == index: 154 int_present = True 155 if int_present == False: 156 bd_present = False 157 if bd_present == True: 158 present = True 159 return present 160