Fix pep8 violations in Robot_Tool Variables.py
[integration/test.git] / test / tools / CSIT_Test / base / modules / switch_manager.py
1 """
2 CSIT test tools.
3 Authors: Baohua Yang@IBM, Denghui Huang@IBM
4 Updated: 2013-11-01
5 """
6
7 import sys
8
9 sys.path.append('..')
10 from restlib import *
11 from testmodule import TestModule
12
13 sys.path.remove('..')
14
15
16 class SwitchManager(TestModule):
17     """
18     Test for the switch manager, including read switch nodes.
19     Start 2-layer tree topology network. e.g., in Mininet, run  'sudo mn --controller=remote,ip=127.0.0.1 --mac --topo tree,2'
20     """
21
22     def __init__(self, restSubContext='/controller/nb/v2/switchmanager', user=DEFAULT_USER, password=DEFAULT_PWD,
23                  container=DEFAULT_CONTAINER, contentType='json', prefix=DEFAULT_PREFIX):
24         super(self.__class__, self).__init__(restSubContext, user, password, container, contentType, prefix)
25
26     def get_nodes(self):
27         """
28         The name is suggested to match the NB API.
29         list all nodes and their properties
30         """
31         suffix = 'nodes'
32         r = super(self.__class__, self).read(suffix)
33         if r:
34             return r
35
36     def get_node(self, suffix):
37         """
38         The name is suggested to match the NB API.
39         list nodeconnector and properties of a node.
40         """
41         r = super(self.__class__, self).read(suffix)
42         if r:
43             return r
44
45     def add_property_to_node(self, node_type, node_id, property, value):
46         """
47         Add a property to given node.
48         """
49         suffix = 'node/' + node_type + '/' + node_id + '/property'
50         r = super(self.__class__, self).update(suffix + '/' + property + '/' + str(value))
51
52     def remove_property_from_node(self, node_type, node_id, property):
53         """
54         Remove a property from given node.
55         """
56         suffix = 'node/' + node_type + '/' + node_id + '/property'
57         r = super(self.__class__, self).delete(suffix + '/' + property)
58
59     def add_property_to_nodeconnector(self, node_type, node_id, nc_type, nc_id, property, value):
60         """
61         Add a property to given node.
62         """
63         suffix = 'nodeconnector/' + node_type + '/' + node_id + '/' + nc_type + '/' + nc_id + '/property'
64         r = super(self.__class__, self).update(suffix + '/' + property + '/' + str(value))
65
66     def remove_property_from_nodeconnector(self, node_type, node_id, nc_type, nc_id, property):
67         """
68         Add a property to given node.
69         """
70         suffix = 'nodeconnector/' + node_type + '/' + node_id + '/' + nc_type + '/' + nc_id + '/property'
71         r = super(self.__class__, self).delete(suffix + '/' + property)
72
73     def test_list_nodes(self):
74         """
75         The name is suggested to match the NB API.
76         list all nodes and their properties
77         >>> SwitchManager().test_list_nodes()
78         True
79         """
80         result = []
81         r = self.get_nodes()
82         t = super(self.__class__, self).extract_properties(r, 'nodeProperties', 'node')
83         if t:
84             result.append({u'type': u'OF', u'id': u'00:00:00:00:00:00:00:01'} in t)
85             result.append({u'type': u'OF', u'id': u'00:00:00:00:00:00:00:02'} in t)
86             result.append({u'type': u'OF', u'id': u'00:00:00:00:00:00:00:03'} in t)
87             return result == [True, True, True]
88
89     def test_node_property_operations(self, node_type, node_id, property, value):
90         """
91         Test the add,remove,show actions on node properties.
92
93         >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:01','description','Switch1')
94         True
95         >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:02','description','Switch2')
96         True
97         >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:03','description','Switch3')
98         True
99         """
100         result = []
101         #current node properties should not include description
102         r = self.get_nodes()
103         v = [e['properties'].get(property) for e in r['nodeProperties'] if
104              e['node'] == {u'type': node_type, u'id': node_id}]
105         result.append(v == [{u'value': u'None'}] or v == [None])
106         #After adding, current node properties should include description
107         self.add_property_to_node(node_type, node_id, property, value)
108         r = self.get_nodes()
109         v = [e['properties'].get(property) for e in r['nodeProperties'] if
110              e['node'] == {u'type': node_type, u'id': node_id}]
111         result.append(v == [{u'value': value}])
112         #After removing, current node properties should not include description
113         self.remove_property_from_node(node_type, node_id, property)
114         r = self.get_nodes()
115         v = [e['properties'].get(property) for e in r['nodeProperties'] if
116              e['node'] == {u'type': node_type, u'id': node_id}]
117         result.append(v == [{u'value': u'None'}] or v == [None])
118         return result == [True, True, True]
119
120     def test_nodeconnector_property_operations(self, node_type, node_id, nc_type, nc_id, property, value):
121         """
122         Test the add,remove,show actions on nodeconnector properties.
123
124         >>> SwitchManager().test_nodeconnector_property_operations('OF','00:00:00:00:00:00:00:01','OF','1','bandwidth',1000)
125         True
126         """
127         result = []
128         node_suffix = 'node/' + node_type + '/' + node_id
129         #default bw should be 10000000000L
130         r = self.get_node(node_suffix)
131         default_value = [e['properties'][property] for e in r['nodeConnectorProperties'] if
132                          property in e['properties'] and e['nodeconnector'] == {
133                              u'node': {u'type': node_type, u'id': node_id}, u'type': nc_type, u'id': nc_id}]
134         #After setting, the value should be the value
135         self.add_property_to_nodeconnector(node_type, node_id, nc_type, nc_id, property, value)
136         r = self.get_node(node_suffix)
137         current_value = [e['properties'][property] for e in r['nodeConnectorProperties'] if
138                          property in e['properties'] and e['nodeconnector'] == {
139                              u'node': {u'type': node_type, u'id': node_id}, u'type': nc_type, u'id': nc_id}]
140         result.append(current_value == [{'value': value}])
141         #After removing, and restoring the default value, the bandwidth property should be default
142         self.remove_property_from_nodeconnector(node_type, node_id, nc_type, nc_id, property)
143         r = self.get_node(node_suffix)
144         v = [e['properties'][property] for e in r['nodeConnectorProperties'] if
145              property in e['properties'] and e['nodeconnector'] == {u'node': {u'type': node_type, u'id': node_id},
146                                                                     u'type': nc_type, u'id': nc_id}]
147         result.append(v == [])
148         self.add_property_to_nodeconnector(node_type, node_id, nc_type, nc_id, property, default_value[0]['value'])
149         r = self.get_node(node_suffix)
150         current_value = [e['properties'][property] for e in r['nodeConnectorProperties'] if
151                          property in e['properties'] and e['nodeconnector'] == {
152                              u'node': {u'type': node_type, u'id': node_id}, u'type': nc_type, u'id': nc_id}]
153         result.append(current_value == default_value)
154         return result == [True, True, True]