go.ligato.io/vpp-agent/v3@v3.5.0/tests/robot/libraries/vpp_term.py (about)

     1  # input - output from sh int addr
     2  # output - list of words containing ip/prefix
     3  from robot.api import logger
     4  
     5  def Find_IPV4_In_Text(text):
     6      ipv4 = []
     7      for word in text.split():
     8          if (word.count('.') == 3) and (word.count('/') == 1):
     9              ipv4.append(word)
    10      return ipv4
    11  
    12  
    13  def Find_IPV6_In_Text(text):
    14      """Find and return all IPv6 addresses in the given string.
    15  
    16      :param text: string to search.
    17      :type text: str
    18  
    19      :return: IPv6 addresses found in string.
    20      :rtype: list of str
    21      """
    22  
    23      ipv6 = []
    24      for word in text.split():
    25          if (word.count(':') >= 2) and (word.count('/') == 1):
    26              ipv6.append(word)
    27      return ipv6
    28  
    29  
    30  # input - output from sh hardware interface_name
    31  # output - list of words containing mac
    32  def Find_MAC_In_Text(text):
    33      mac = ''
    34      for word in text.split():
    35          if (word.count(':') == 5):
    36              mac = word
    37              break
    38      return mac
    39  
    40  
    41  # input - output from sh ip arp command
    42  # output - state info list
    43  def parse_arp(info, intf, ip, mac):
    44      """Parse ARP list from vpp console and find a specific entry using the provided arguments.
    45  
    46      :param info: ARP list from VPP console.
    47      :param intf: VPP-internal name of the interface configured with this ARP entry.
    48      :param ip: IP address of the ARP entry.
    49      :param mac: MAC address of the ARP entry.
    50      :type info: str
    51      :type intf: str
    52      :type ip: str
    53      :type mac: str
    54      :returns: True if a matching entry is found, else False
    55      :rtype: bool
    56      """
    57  
    58      for line in info.splitlines():
    59              if intf in line and ip in line and mac in line:
    60                  print("ARP Found:"+line)
    61                  return True
    62      logger.debug("ARP not Found")
    63      return False
    64  
    65  
    66  # input - output from sh ip arp command
    67  # output - state info list
    68  def parse_neighbor(info, intf, ip, mac):
    69      """Parse neighbor list from vpp console and find a specific entry using the provided arguments.
    70  
    71      :param info: Neighbor list from VPP console.
    72      :param intf: VPP-internal name of the interface configured with this neighbor.
    73      :param ip: IP address of the neighbor entry.
    74      :param mac: MAC address of the neighbor entry.
    75      :type info: str
    76      :type intf: str
    77      :type ip: str
    78      :type mac: str
    79      :returns: True if a matching entry is found, else False
    80      :rtype: bool
    81      """
    82  
    83      for line in info.splitlines():
    84          if intf in line and ip in line and mac in line:
    85              print("Neighbor Found:"+line)
    86              return True
    87      logger.debug("Neighbor not Found")
    88      return False
    89  
    90  
    91  # input - output from sh ip arp command
    92  # output - state info list
    93  def parse_stn_rule(info):
    94      state = {}
    95      for line in info.splitlines():
    96          try:
    97              if "address" in line.strip().split()[0]:
    98                  state['ip_address'] = line.strip().split()[1]
    99              elif "iface" in line.strip().split()[0]:
   100                  state['iface'] = line.strip().split()[1]
   101              elif "next_node" in line.strip().split()[0]:
   102                  state['next_node'] = line.strip().split()[1]
   103          except IndexError:
   104              pass
   105  
   106      return state['ip_address'], state['iface'], state['next_node']
   107  
   108  
   109  def parse_memif_info(info):
   110      state = []
   111      sockets_line = []
   112      for line in info.splitlines():
   113          if line:
   114              try:
   115                  _ = int(line.strip().split()[0])
   116                  sockets_line.append(line)
   117              except ValueError:
   118                  pass
   119              if line.strip().split()[0] == "flags":
   120                  if "admin-up" in line:
   121                      state.append("enabled=1")
   122                  if "slave" in line:
   123                      state.append("role=slave")
   124                  if "connected" in line:
   125                      state.append("connected=1")
   126              if line.strip().split()[0] == "socket-id":
   127                  try:
   128                      socket_id = int(line.strip().split()[1])
   129                      state.append("id="+line.strip().split()[3])
   130                      for sock_line in sockets_line:
   131                          try:
   132                              num = int(sock_line.strip().split()[0])
   133                              if num == socket_id:
   134                                  state.append("socket=" + sock_line.strip().split()[-1])
   135                          except ValueError:
   136                              pass
   137                  except ValueError:
   138                      pass
   139      if "enabled=1" not in state:
   140          state.append("enabled=0")
   141      if "role=slave" not in state:
   142          state.append("role=master")
   143      if "connected=1" not in state:
   144          state.append("connected=0")
   145      return state