Merge "Fix pep8 violations in csit/libraries/Topology.py"
[integration/test.git] / test / csit / libraries / Common.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-14
5 """
6 import collections
7 import xml.etree.ElementTree as ET
8
9 '''
10 Common constants and functions for the robot framework.
11 '''
12
13 def collection_should_contain(collection, *members):
14     """
15     Fail if not every members is in the collection.
16     """
17     if not isinstance(collection, collections.Iterable):
18         return False
19     for m in members:
20         if m not in collection:
21             return False
22     else:
23         return True
24
25 def combine_strings(*strings):
26     """
27     Combines the given `strings` together and returns the result.
28     The given strings are not altered by this keyword.
29     """
30     result = ''
31     for s in strings:
32         if isinstance(s,str) or isinstance(s,unicode):
33             result += s
34     if result == '':
35         return None
36     else:
37         return result
38
39         
40 def compare_xml(xml1, xml2):
41     """
42     compare the two XML files to see if they contain the same data
43     but could be if different order.
44     It just split the xml in to lines and just check the line is in
45     the other file 
46     """
47     for line in xml1.rstrip().split('\n'):
48         if line not in xml2.rstrip().split('\n'):
49             return False
50
51     for line in xml2.rstrip().split('\n'):
52         if line not in xml1.rstrip().split('\n'):
53             return False
54
55     return True
56
57 def num_of_nodes(depth, fanout):
58     '''returns num of switches of a mininet with tree topology 
59     with particular depth and fanout parameters
60     '''
61     result = 0
62     for i in xrange(depth):
63         result += fanout**i
64     return result    
65
66 def num_of_links_for_node(nodeid, leaflist, fanout):
67     '''
68     If the given node is a leaf node, there will be an only one link for it
69     and nodeid will be represented 2 times in topology
70     If the given node is not a leaf node, then there will be fanout+1 links
71     for it and nodeid will be represented (fanout+1)*2 times in topology
72     
73     p.s. root node is excluded.
74     '''
75     if nodeid in leaflist:
76         return 1
77     return (fanout+1)
78
79 if __name__ == '__main__':
80         print num_of_nodes(3,4)
81         pass