3 Authors: Baohua Yang@IBM, Denghui Huang@IBM
10 from restlib import * # noqa
11 from testmodule import TestModule
16 class SwitchManager(TestModule):
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'
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)
29 The name is suggested to match the NB API.
30 list all nodes and their properties
33 r = super(self.__class__, self).read(suffix)
37 def get_node(self, suffix):
39 The name is suggested to match the NB API.
40 list nodeconnector and properties of a node.
42 r = super(self.__class__, self).read(suffix)
46 def add_property_to_node(self, node_type, node_id, property, value):
48 Add a property to given node.
50 suffix = 'node/' + node_type + '/' + node_id + '/property'
51 super(self.__class__, self).update(suffix + '/' + property + '/' + str(value))
53 def remove_property_from_node(self, node_type, node_id, property):
55 Remove a property from given node.
57 suffix = 'node/' + node_type + '/' + node_id + '/property'
58 super(self.__class__, self).delete(suffix + '/' + property)
60 def add_property_to_nodeconnector(self, node_type, node_id, nc_type, nc_id, property, value):
62 Add a property to given node.
64 suffix = 'nodeconnector/' + node_type + '/' + node_id + '/' + nc_type + '/' + nc_id + '/property'
65 super(self.__class__, self).update(suffix + '/' + property + '/' + str(value))
67 def remove_property_from_nodeconnector(self, node_type, node_id, nc_type, nc_id, property):
69 Add a property to given node.
71 suffix = 'nodeconnector/' + node_type + '/' + node_id + '/' + nc_type + '/' + nc_id + '/property'
72 super(self.__class__, self).delete(suffix + '/' + property)
74 def test_list_nodes(self):
76 The name is suggested to match the NB API.
77 list all nodes and their properties
78 >>> SwitchManager().test_list_nodes()
83 t = super(self.__class__, self).extract_properties(r, 'nodeProperties', 'node')
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]
90 def test_node_property_operations(self, node_type, node_id, property, value):
92 Test the add,remove,show actions on node properties.
94 >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:01','description','Switch1')
96 >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:02','description','Switch2')
98 >>> SwitchManager().test_node_property_operations('OF','00:00:00:00:00:00:00:03','description','Switch3')
102 # current node properties should not include description
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)
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)
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]
121 def test_nodeconnector_property_operations(self, node_type, node_id, nc_type, nc_id, property, value):
123 Test the add,remove,show actions on nodeconnector properties.
125 >>> SwitchManager().test_nodeconnector_property_operations(
126 'OF','00:00:00:00:00:00:00:01','OF','1','bandwidth',1000)
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]