Test VIRL connection.
[csit.git] / resources / libraries / python / topology.py
index 2b202e5..1034160 100644 (file)
@@ -43,6 +43,14 @@ class NodeType(object):
     # Traffic Generator (this node has traffic generator on it)
     TG = 'TG'
 
+class NodeSubTypeTG(object):
+    #T-Rex traffic generator
+    TREX = 'TREX'
+    # Moongen
+    MOONGEN = 'MOONGEN'
+    #IxNetwork
+    IXNET = 'IXNET'
+
 DICT__nodes = load_topo_from_yaml()
 
 
@@ -187,7 +195,7 @@ class Topology(object):
 
         interface_dict = {}
         list_mac_address = self.convert_mac_to_number_list(mac_address)
-        logger.trace(list_mac_address.__str__())
+        logger.trace(str(list_mac_address))
         for interface in interfaces_list:
             # TODO: create vat json integrity checking and move there
             if "l2_address" not in interface:
@@ -246,6 +254,9 @@ class Topology(object):
             if_mac = ifc['mac_address']
             interface_dict = self._extract_vpp_interface_by_mac(interface_list,
                                                                 if_mac)
+            if not interface_dict:
+                raise Exception('Interface {0} not found by MAC {1}'.
+                        format(ifc, if_mac))
             ifc['name'] = interface_dict["interface_name"]
             ifc['vpp_sw_index'] = interface_dict["sw_if_index"]
 
@@ -315,7 +326,7 @@ class Topology(object):
         This method updates the topology dictionary by querying interface lists
         of all nodes mentioned in the topology dictionary.
         It does this by dumping interface list to json output from all devices
-        using vpe_api_test, and pairing known information from topology
+        using vpp_api_test, and pairing known information from topology
         (mac address/pci address of interface) to state from VPP.
         For TG/linux nodes add interface name only.
         """
@@ -345,6 +356,22 @@ class Topology(object):
 
         return None
 
+    @staticmethod
+    def get_interface_mac_by_port_key(node, port_key):
+        """Get MAC address for the interface based on port key.
+
+        :param node: Node to get interface mac on.
+        :param port_key: Dictionary key name of interface.
+        :type node: dict
+        :type port_key: str
+        :return: Return MAC or None if not found.
+        """
+        for port_name, port_data in node['interfaces'].iteritems():
+            if port_name == port_key:
+                return port_data['mac_address']
+
+        return None
+
     @staticmethod
     def get_interface_mac(node, interface):
         """Get MAC address for the interface.
@@ -364,6 +391,45 @@ class Topology(object):
 
         return None
 
+    @staticmethod
+    def get_adjacent_node_and_interface_by_key(nodes_info, node, port_key):
+        """Get node and interface adjacent to specified interface
+        on local network.
+
+        :param nodes_info: Dictionary containing information on all nodes
+        in topology.
+        :param node: Node that contains specified interface.
+        :param port_key: Interface port key.
+        :type nodes_info: dict
+        :type node: dict
+        :type port_key: str
+        :return: Return (node, interface info) tuple or None if not found.
+        :rtype: (dict, dict)
+        """
+        link_name = None
+       # get link name where the interface belongs to
+        for port_name, port_data in node['interfaces'].iteritems():
+            if port_name == 'mgmt':
+                continue
+            if port_name == port_key:
+                link_name = port_data['link']
+                break
+
+        if link_name is None: 
+            return None
+
+        # find link
+        for node_data in nodes_info.values():
+            # skip self
+            if node_data['host'] == node['host']:
+                continue
+            for interface, interface_data \
+                    in node_data['interfaces'].iteritems():
+                if 'link' not in interface_data:
+                    continue
+                if interface_data['link'] == link_name:
+                    return node_data, node_data['interfaces'][interface]
+
     @staticmethod
     def get_adjacent_node_and_interface(nodes_info, node, interface_name):
         """Get node and interface adjacent to specified interface
@@ -584,3 +650,20 @@ class Topology(object):
                           'DUT1_BD_LINKS': dut1_bd_links,
                           'DUT2_BD_LINKS': dut2_bd_links}
         return topology_links
+
+    @staticmethod
+    def is_tg_node(node):
+        """Find out whether the node is TG
+
+        :param node: node to examine
+        :return: True if node is type of TG; False otherwise
+        """
+        return node['type'] == NodeType.TG
+
+    @staticmethod
+    def get_node_hostname(node):
+        """
+        :param node: node dictionary
+        :return: host name as 'str' type
+        """
+        return node['host']
\ No newline at end of file