Remove variables for genius
[integration/test.git] / csit / libraries / Topologynew.py
1 """
2 Library for the robot based system test tool of the OpenDaylight project.
3 Authors: Baohua Yang@IBM, Denghui Huang@IBM
4 Updated: 2013-11-10
5 """
6 import re
7 from robot.libraries.BuiltIn import BuiltIn
8 import Common
9
10
11 class Topologynew(object):
12     """
13     Topology class provide topology database and provide many method to get property of topology.
14
15     node_boilerplate = {u'type': u'MD_SAL', u'id': u'openflow:%d'}
16     """
17
18     topo_nodes_db = [
19         [],
20         [{"type": "MD_SAL", "id": "openflow:1"}],
21         [
22             {"type": "MD_SAL", "id": "openflow:1"},
23             {"type": "MD_SAL", "id": "openflow:2"},
24             {"type": "MD_SAL", "id": "openflow:3"},
25         ],
26     ]
27
28     def __init__(self):
29         self.builtin = BuiltIn()
30
31     def get_nodes_from_topology(self, topo_level):
32         """
33         get nodes from topology database by topology tree level
34         """
35         if isinstance(topo_level, str) or isinstance(topo_level, unicode):
36             if topo_level.isdigit():
37                 topo_level = int(topo_level)
38                 if topo_level <= 0:
39                     return None
40                 return self.topo_nodes_db[topo_level]
41             else:
42                 return None
43         elif isinstance(topo_level, int):
44             if topo_level <= 0:
45                 return None
46             return self.topo_nodes_db[topo_level]
47         else:
48             return None
49
50     def get_nodes_from_tree_topo(self, topo, exceptroot="0"):
51         """
52         This function generates a dictionary that contains type and id of each node.
53         It follows tree level topology.
54         @parameter topo: either an interer (in this case, depth is set and fanout will be 2)
55                          or a string in format of "(a,b)"   (a and b are integers and they
56                          stands for depth and fanout respectively)
57         @return array of dicitonary objects that contains info about each node
58         """
59         depth = 0
60         fanout = 2
61         if isinstance(topo, str) or isinstance(topo, unicode):
62             t = tuple(int(v) for v in re.findall("[0-9]+", topo))
63             if len(t) == 1:
64                 depth = t[0]
65             elif len(t) == 2:
66                 depth = t[0]
67                 fanout = t[1]
68             else:
69                 return None  # topology consists of two parameters: depth and fanout
70         elif isinstance(topo, int):
71             depth = topo
72         else:
73             return None  # topo parameter is not given in a desired way
74
75         num_nodes = Common.num_of_nodes(depth, fanout)
76         nodelist = []
77         for i in xrange(1, num_nodes + 1):
78             temp = {"id": "00:00:00:00:00:00:00:%s" % format(i, "02x"), "type": "OF"}
79             nodelist.append(temp)
80         if int(exceptroot):
81             del nodelist[0]
82         return nodelist
83
84     def get_ids_of_leaf_nodes(self, fanout, depth):
85         """
86         For a tree structure, it numerates leaf nodes
87         by following depth-first strategy
88         @parameter  fanout: fanout of tree
89         @parameter  depth:  total depth of a tree
90         @return     leafnodes:  list of ids of leaf nodes
91         """
92         leafnodes = []
93         self._enumerate_nodes(0, 1, 1, fanout, depth - 1, leafnodes)
94         return leafnodes
95
96     def _enumerate_nodes(
97         self, currentdepth, nodeid, currentbranch, fanout, depth, leafnodes
98     ):
99         if currentdepth == depth:
100             leafnodes.append("00:00:00:00:00:00:00:%s" % format(nodeid, "02x"))
101             return 1
102         nodes = 1
103         for i in xrange(1, fanout + 1):
104             nodes += self._enumerate_nodes(
105                 currentdepth + 1, nodeid + nodes, i, fanout, depth, leafnodes
106             )
107         return nodes
108
109
110 if __name__ == "__main__":
111     topologynew = Topologynew()
112     print(topologynew.get_nodes_from_tree_topo("(2,3)"))