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