Make pep8 more picky
[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     topo_nodes_db = [
18         [],
19         [{u'type': u'MD_SAL', u'id': u'openflow:1'}],
20         [{u'type': u'MD_SAL', u'id': u'openflow:1'},
21          {u'type': u'MD_SAL', u'id': u'openflow:2'},
22          {u'type': u'MD_SAL', u'id': u'openflow:3'}]
23     ]
24
25     def __init__(self):
26         self.builtin = BuiltIn()
27
28     def get_nodes_from_topology(self, topo_level):
29         '''
30         get nodes from topology database by topology tree level
31         '''
32         if isinstance(topo_level, str) or isinstance(topo_level, unicode):
33             if topo_level.isdigit():
34                 topo_level = int(topo_level)
35                 if topo_level <= 0:
36                     return None
37                 return self.topo_nodes_db[topo_level]
38             else:
39                 return None
40         elif isinstance(topo_level, int):
41             if topo_level <= 0:
42                 return None
43             return self.topo_nodes_db[topo_level]
44         else:
45             return None
46
47     def get_nodes_from_tree_topo(self, topo, exceptroot="0"):
48         '''
49         This function generates a dictionary that contains type and id of each node.
50         It follows tree level topology.
51         @parameter topo: either an interer (in this case, depth is set and fanout will be 2)
52                          or a string in format of "(a,b)"   (a and b are integers and they
53                          stands for depth and fanout respectively)
54         @return array of dicitonary objects that contains info about each node
55         '''
56         depth = 0
57         fanout = 2
58         if isinstance(topo, str) or isinstance(topo, unicode):
59             t = tuple(int(v) for v in re.findall("[0-9]+", topo))
60             if len(t) == 1:
61                 depth = t[0]
62             elif len(t) == 2:
63                 depth = t[0]
64                 fanout = t[1]
65             else:
66                 return None                 # topology consists of two parameters: depth and fanout
67         elif isinstance(topo, int):
68             depth = topo
69         else:
70             return None                     # topo parameter is not given in a desired way
71
72         num_nodes = Common.num_of_nodes(depth, fanout)
73         nodelist = []
74         for i in xrange(1, num_nodes + 1):
75             temp = {"id": "00:00:00:00:00:00:00:%s" % format(i, '02x'), "type": "OF"}
76             nodelist.append(temp)
77         if int(exceptroot):
78             del nodelist[0]
79         return nodelist
80
81     def get_ids_of_leaf_nodes(self, fanout, depth):
82         '''
83         For a tree structure, it numerates leaf nodes
84         by following depth-first strategy
85         @parameter  fanout: fanout of tree
86         @parameter  depth:  total depth of a tree
87         @return     leafnodes:  list of ids of leaf nodes
88         '''
89         leafnodes = []
90         self._enumerate_nodes(0, 1, 1, fanout, depth - 1, leafnodes)
91         return leafnodes
92
93     def _enumerate_nodes(self, currentdepth, nodeid, currentbranch, fanout, depth, leafnodes):
94         if currentdepth == depth:
95             leafnodes.append("00:00:00:00:00:00:00:%s" % format(nodeid, '02x'))
96             return 1
97         nodes = 1
98         for i in xrange(1, fanout + 1):
99             nodes += self._enumerate_nodes(currentdepth + 1, nodeid + nodes, i, fanout, depth, leafnodes)
100         return nodes
101
102 if __name__ == '__main__':
103     topologynew = Topologynew()
104     # print topologynew.get_nodes_from_tree_topo(2)
105     # print topologynew.get_nodes_from_tree_topo('2')
106     print topologynew.get_nodes_from_tree_topo('(2,3)')
107     # print topologynew.get_ids_of_leaf_nodes(2,2 )#, depth)