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