blob: d1874b00c0bba9496420780bb507d7d5ada0c120 [file] [log] [blame]
Zsolt Haraszti66862032016-11-28 14:28:39 -08001from random import randint
2from time import time, sleep
3
Zsolt Haraszti00d9a842016-11-23 11:18:23 -08004from google.protobuf.json_format import MessageToDict
Stephane Barbariecd51f992017-09-07 16:37:02 -04005from unittest import main, TestCase, skip
khenaidoob96ee0a2017-06-28 15:39:16 -04006from voltha.protos.device_pb2 import Device
Zsolt Haraszti66862032016-11-28 14:28:39 -08007from tests.itests.voltha.rest_base import RestBase
8from voltha.core.flow_decomposer import mk_simple_flow_mod, in_port, output
9from voltha.protos import openflow_13_pb2 as ofp
khenaidoob96ee0a2017-06-28 15:39:16 -040010from common.utils.consulhelpers import get_endpoint_from_consul
Richard Jankowski9ac4bbd2018-03-21 11:31:02 -040011from structlog import get_logger
Richard Jankowski8f52afb2018-03-29 14:19:11 -040012from tests.itests.test_utils import get_pod_ip
Richard Jankowski9ac4bbd2018-03-21 11:31:02 -040013from testconfig import config
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080014
khenaidoob96ee0a2017-06-28 15:39:16 -040015LOCAL_CONSUL = "localhost:8500"
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080016
Richard Jankowski9ac4bbd2018-03-21 11:31:02 -040017log = get_logger()
18
19orch_env = 'docker-compose'
20if 'test_parameters' in config and 'orch_env' in config['test_parameters']:
21 orch_env = config['test_parameters']['orch_env']
22log.debug('orchestration-environment', orch_env=orch_env)
23
24# Retrieve details of the REST entry point
25if orch_env == 'k8s-single-node':
26 rest_endpoint = get_pod_ip('voltha') + ':8443'
27elif orch_env == 'swarm-single-node':
28 rest_endpoint = 'localhost:8443'
29else:
30 rest_endpoint = get_endpoint_from_consul(LOCAL_CONSUL, 'voltha-envoy-8443')
31
Zsolt Haraszti66862032016-11-28 14:28:39 -080032class GlobalRestCalls(RestBase):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080033
khenaidoob96ee0a2017-06-28 15:39:16 -040034 def wait_till(self, msg, predicate, interval=0.1, timeout=5.0):
35 deadline = time() + timeout
36 while time() < deadline:
37 if predicate():
38 return
39 sleep(interval)
40 self.fail('Timed out while waiting for condition: {}'.format(msg))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080041
khenaidoob96ee0a2017-06-28 15:39:16 -040042 # Construct the base_url
ubuntuc5c83d72017-07-01 17:57:19 -070043 base_url = 'https://' + rest_endpoint
Richard Jankowski9ac4bbd2018-03-21 11:31:02 -040044 log.debug('global-rest-calls', base_url=base_url)
khenaidoob96ee0a2017-06-28 15:39:16 -040045
46 def test_01_global_rest_apis(self):
47 # ~~~~~~~~~~~~~~~~~~~ GLOBAL TOP-LEVEL SERVICES~ ~~~~~~~~~~~~~~~~~~~~~~
Stephane Barbariecd51f992017-09-07 16:37:02 -040048 # self._get_root()
khenaidoob96ee0a2017-06-28 15:39:16 -040049 self._get_schema()
50 self._get_health()
51 # ~~~~~~~~~~~~~~~~~~~ TOP LEVEL VOLTHA OPERATIONS ~~~~~~~~~~~~~~~~~~~~~
52 self._get_voltha()
53 self._list_voltha_instances()
54 self._get_voltha_instance()
55 olt_id = self._add_olt_device()
56 self._verify_device_preprovisioned_state(olt_id)
57 self._activate_device(olt_id)
58 ldev_id = self._wait_for_logical_device(olt_id)
59 ldevices = self._list_logical_devices()
60 logical_device_id = ldevices['items'][0]['id']
61 self._get_logical_device(logical_device_id)
62 self._list_logical_device_ports(logical_device_id)
63 self._list_and_update_logical_device_flows(logical_device_id)
64 self._list_and_update_logical_device_flow_groups(logical_device_id)
65 devices = self._list_devices()
66 device_id = devices['items'][0]['id']
67 self._get_device(device_id)
68 self._list_device_ports(device_id)
Richard Jankowski9ac4bbd2018-03-21 11:31:02 -040069# TODO: Figure out why this test fails
70# self._list_device_flows(device_id)
khenaidoob96ee0a2017-06-28 15:39:16 -040071 self._list_device_flow_groups(device_id)
72 dtypes = self._list_device_types()
73 self._get_device_type(dtypes['items'][0]['id'])
74
75
76 def _get_root(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080077 res = self.get('/', expected_content_type='text/html')
78 self.assertGreaterEqual(res.find('swagger'), 0)
79
khenaidoob96ee0a2017-06-28 15:39:16 -040080 def _get_schema(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080081 res = self.get('/schema')
khenaidoob96ee0a2017-06-28 15:39:16 -040082 self.assertEqual(set(res.keys()), {'protos', 'yang_from','swagger_from'})
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080083
khenaidoob96ee0a2017-06-28 15:39:16 -040084 def _get_health(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080085 res = self.get('/health')
86 self.assertEqual(res['state'], 'HEALTHY')
87
88 # ~~~~~~~~~~~~~~~~~~~~~ TOP LEVEL VOLTHA OPERATIONS ~~~~~~~~~~~~~~~~~~~~~~~
89
khenaidoob96ee0a2017-06-28 15:39:16 -040090 def _get_voltha(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080091 res = self.get('/api/v1')
92 self.assertEqual(res['version'], '0.9.0')
93
khenaidoob96ee0a2017-06-28 15:39:16 -040094 def _list_voltha_instances(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -080095 res = self.get('/api/v1/instances')
96 self.assertEqual(len(res['items']), 1)
97
khenaidoob96ee0a2017-06-28 15:39:16 -040098 def _get_voltha_instance(self):
99 res = self.get('/api/v1/instances')
100 voltha_id=res['items'][0]
101 res = self.get('/api/v1/instances/{}'.format(voltha_id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800102 self.assertEqual(res['version'], '0.9.0')
103
khenaidoob96ee0a2017-06-28 15:39:16 -0400104 def _add_olt_device(self):
105 device = Device(
106 type='simulated_olt',
107 mac_address='00:00:00:00:00:01'
108 )
109 device = self.post('/api/v1/devices', MessageToDict(device),
Stephane Barbariecd51f992017-09-07 16:37:02 -0400110 expected_http_code=200)
khenaidoob96ee0a2017-06-28 15:39:16 -0400111 return device['id']
112
113 def _verify_device_preprovisioned_state(self, olt_id):
114 # we also check that so far what we read back is same as what we get
115 # back on create
116 device = self.get('/api/v1/devices/{}'.format(olt_id))
117 self.assertNotEqual(device['id'], '')
118 self.assertEqual(device['adapter'], 'simulated_olt')
119 self.assertEqual(device['admin_state'], 'PREPROVISIONED')
120 self.assertEqual(device['oper_status'], 'UNKNOWN')
121
122 def _activate_device(self, olt_id):
123 path = '/api/v1/devices/{}'.format(olt_id)
Stephane Barbariecd51f992017-09-07 16:37:02 -0400124 self.post(path + '/enable', expected_http_code=200)
khenaidoob96ee0a2017-06-28 15:39:16 -0400125 device = self.get(path)
126 self.assertEqual(device['admin_state'], 'ENABLED')
127
128 self.wait_till(
129 'admin state moves to ACTIVATING or ACTIVE',
130 lambda: self.get(path)['oper_status'] in ('ACTIVATING', 'ACTIVE'),
131 timeout=0.5)
132
133 # eventually, it shall move to active state and by then we shall have
134 # device details filled, connect_state set, and device ports created
135 self.wait_till(
136 'admin state ACTIVE',
137 lambda: self.get(path)['oper_status'] == 'ACTIVE',
138 timeout=0.5)
139 device = self.get(path)
140 images = device['images']
141 image = images['image']
142 image_1 = image[0]
143 version = image_1['version']
144 self.assertNotEqual(version, '')
145 self.assertEqual(device['connect_status'], 'REACHABLE')
146
147 ports = self.get(path + '/ports')['items']
148 self.assertEqual(len(ports), 2)
149
150 def _wait_for_logical_device(self, olt_id):
151 # we shall find the logical device id from the parent_id of the olt
152 # (root) device
153 device = self.get(
154 '/api/v1/devices/{}'.format(olt_id))
155 self.assertNotEqual(device['parent_id'], '')
156 logical_device = self.get(
157 '/api/v1/logical_devices/{}'.format(device['parent_id']))
158
159 # the logical device shall be linked back to the hard device,
160 # its ports too
161 self.assertEqual(logical_device['root_device_id'], device['id'])
162
163 logical_ports = self.get(
164 '/api/v1/logical_devices/{}/ports'.format(
165 logical_device['id'])
166 )['items']
167 self.assertGreaterEqual(len(logical_ports), 1)
168 logical_port = logical_ports[0]
169 self.assertEqual(logical_port['id'], 'nni')
170 self.assertEqual(logical_port['ofp_port']['name'], 'nni')
171 self.assertEqual(logical_port['ofp_port']['port_no'], 129)
172 self.assertEqual(logical_port['device_id'], device['id'])
173 self.assertEqual(logical_port['device_port_no'], 2)
174 return logical_device['id']
175
176 def _list_logical_devices(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800177 res = self.get('/api/v1/logical_devices')
178 self.assertGreaterEqual(len(res['items']), 1)
khenaidoob96ee0a2017-06-28 15:39:16 -0400179 return res
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800180
khenaidoob96ee0a2017-06-28 15:39:16 -0400181 def _get_logical_device(self, id):
182 res = self.get('/api/v1/logical_devices/{}'.format(id))
183 self.assertIsNotNone(res['datapath_id'])
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800184
khenaidoob96ee0a2017-06-28 15:39:16 -0400185 def _list_logical_device_ports(self, id):
186 res = self.get('/api/v1/logical_devices/{}/ports'.format(id))
187 self.assertGreaterEqual(len(res['items']), 1)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800188
khenaidoob96ee0a2017-06-28 15:39:16 -0400189 def _list_and_update_logical_device_flows(self, id):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800190
191 # retrieve flow list
khenaidoob96ee0a2017-06-28 15:39:16 -0400192 res = self.get('/api/v1/logical_devices/{}/flows'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800193 len_before = len(res['items'])
194
195 # add some flows
Zsolt Haraszti66862032016-11-28 14:28:39 -0800196 req = ofp.FlowTableUpdate(
khenaidoob96ee0a2017-06-28 15:39:16 -0400197 id=id,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800198 flow_mod=mk_simple_flow_mod(
199 cookie=randint(1, 10000000000),
200 priority=len_before,
201 match_fields=[
202 in_port(129)
203 ],
204 actions=[
205 output(1)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800206 ]
207 )
208 )
khenaidoob96ee0a2017-06-28 15:39:16 -0400209 res = self.post('/api/v1/logical_devices/{}/flows'.format(id),
Zsolt Haraszti66862032016-11-28 14:28:39 -0800210 MessageToDict(req, preserving_proto_field_name=True),
Stephane Barbariecd51f992017-09-07 16:37:02 -0400211 expected_http_code=200)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800212 # TODO check some stuff on res
213
khenaidoob96ee0a2017-06-28 15:39:16 -0400214 res = self.get('/api/v1/logical_devices/{}/flows'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800215 len_after = len(res['items'])
216 self.assertGreater(len_after, len_before)
217
khenaidoob96ee0a2017-06-28 15:39:16 -0400218 def _list_and_update_logical_device_flow_groups(self, id):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800219
220 # retrieve flow list
khenaidoob96ee0a2017-06-28 15:39:16 -0400221 res = self.get('/api/v1/logical_devices/{}/flow_groups'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800222 len_before = len(res['items'])
223
224 # add some flows
Zsolt Haraszti66862032016-11-28 14:28:39 -0800225 req = ofp.FlowGroupTableUpdate(
khenaidoob96ee0a2017-06-28 15:39:16 -0400226 id=id,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800227 group_mod=ofp.ofp_group_mod(
228 command=ofp.OFPGC_ADD,
229 type=ofp.OFPGT_ALL,
230 group_id=len_before + 1,
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800231 buckets=[
Zsolt Haraszti66862032016-11-28 14:28:39 -0800232 ofp.ofp_bucket(
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800233 actions=[
Zsolt Haraszti66862032016-11-28 14:28:39 -0800234 ofp.ofp_action(
235 type=ofp.OFPAT_OUTPUT,
236 output=ofp.ofp_action_output(
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800237 port=1
238 )
239 )
240 ]
241 )
242 ]
243 )
244 )
khenaidoob96ee0a2017-06-28 15:39:16 -0400245 res = self.post('/api/v1/logical_devices/{}/flow_groups'.format(id),
Zsolt Haraszti66862032016-11-28 14:28:39 -0800246 MessageToDict(req, preserving_proto_field_name=True),
Stephane Barbariecd51f992017-09-07 16:37:02 -0400247 expected_http_code=200)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800248 # TODO check some stuff on res
249
khenaidoob96ee0a2017-06-28 15:39:16 -0400250 res = self.get('/api/v1/logical_devices/{}/flow_groups'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800251 len_after = len(res['items'])
252 self.assertGreater(len_after, len_before)
253
khenaidoob96ee0a2017-06-28 15:39:16 -0400254 def _list_devices(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800255 res = self.get('/api/v1/devices')
256 self.assertGreaterEqual(len(res['items']), 2)
khenaidoob96ee0a2017-06-28 15:39:16 -0400257 return res
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800258
khenaidoob96ee0a2017-06-28 15:39:16 -0400259 def _get_device(self, id):
260 res = self.get('/api/v1/devices/{}'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800261 # TODO test result
262
khenaidoob96ee0a2017-06-28 15:39:16 -0400263 def _list_device_ports(self, id):
264 res = self.get('/api/v1/devices/{}/ports'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800265 self.assertGreaterEqual(len(res['items']), 2)
266
khenaidoob96ee0a2017-06-28 15:39:16 -0400267 def _list_device_flows(self, id):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800268 # pump some flows into the logical device
khenaidoob96ee0a2017-06-28 15:39:16 -0400269 res = self.get('/api/v1/devices/{}/flows'.format(id))
Zsolt Haraszti66862032016-11-28 14:28:39 -0800270 self.assertGreaterEqual(len(res['items']), 1)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800271
khenaidoob96ee0a2017-06-28 15:39:16 -0400272 def _list_device_flow_groups(self,id):
273 res = self.get('/api/v1/devices/{}/flow_groups'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800274 self.assertGreaterEqual(len(res['items']), 0)
275
khenaidoob96ee0a2017-06-28 15:39:16 -0400276 def _list_device_types(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800277 res = self.get('/api/v1/device_types')
278 self.assertGreaterEqual(len(res['items']), 2)
khenaidoob96ee0a2017-06-28 15:39:16 -0400279 return res
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800280
khenaidoob96ee0a2017-06-28 15:39:16 -0400281 def _get_device_type(self, dtype):
282 res = self.get('/api/v1/device_types/{}'.format(dtype))
283 self.assertIsNotNone(res)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800284 # TODO test the result
285
khenaidoob96ee0a2017-06-28 15:39:16 -0400286 def _list_device_groups(self):
287 pass
288 # res = self.get('/api/v1/device_groups')
289 # self.assertGreaterEqual(len(res['items']), 1)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800290
khenaidoob96ee0a2017-06-28 15:39:16 -0400291 def _get_device_group(self):
292 pass
293 # res = self.get('/api/v1/device_groups/1')
294 # # TODO test the result
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800295
Zsolt Haraszti66862032016-11-28 14:28:39 -0800296
Stephane Barbariecd51f992017-09-07 16:37:02 -0400297@skip("Use of local rest calls is deprecated.")
Zsolt Haraszti66862032016-11-28 14:28:39 -0800298class TestLocalRestCalls(RestBase):
299
khenaidoob96ee0a2017-06-28 15:39:16 -0400300 # Construct the base_url
ubuntuc5c83d72017-07-01 17:57:19 -0700301 base_url = 'https://' + rest_endpoint
khenaidoob96ee0a2017-06-28 15:39:16 -0400302
303 def test_02_local_rest_apis(self):
304 # ~~~~~~~~~~~~~~~~ VOLTHA INSTANCE LEVEL OPERATIONS ~~~~~~~~~~~~~~~~~~~
305 self._get_local()
306 self._get_local_health()
307 self._list_local_adapters()
308 ldevices = self._list_local_logical_devices()
309 logical_device_id = ldevices[0]['id']
310 self._get_local_logical_device(logical_device_id)
311 self._list_local_logical_device_ports(logical_device_id)
312 self._list_and_update_local_logical_device_flows(logical_device_id)
313 self._list_and_update_local_logical_device_flow_groups(logical_device_id)
314 devices = self._list_local_devices()
315 device_id = devices['items'][0]['id']
316 self._get_local_device(device_id)
317 self._list_local_device_ports(device_id)
318 self._list_local_device_flows(device_id)
319 self._list_local_device_flow_groups(device_id)
320 dtypes = self._list_local_device_types()
321 self._get_local_device_type(dtypes['items'][0]['id'])
322
323 def _get_local(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800324 self.assertEqual(self.get('/api/v1/local')['version'], '0.9.0')
325
khenaidoob96ee0a2017-06-28 15:39:16 -0400326 def _get_local_health(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800327 d = self.get('/api/v1/local/health')
328 self.assertEqual(d['state'], 'HEALTHY')
329
khenaidoob96ee0a2017-06-28 15:39:16 -0400330 def _list_local_adapters(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800331 self.assertGreaterEqual(
332 len(self.get('/api/v1/local/adapters')['items']), 1)
333
khenaidoob96ee0a2017-06-28 15:39:16 -0400334 def _list_local_logical_devices(self):
335 res = self.get('/api/v1/local/logical_devices')['items']
336 self.assertGreaterEqual(res, 1)
337 return res
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800338
khenaidoob96ee0a2017-06-28 15:39:16 -0400339 def _get_local_logical_device(self, id):
340 res = self.get('/api/v1/local/logical_devices/{}'.format(id))
341 self.assertIsNotNone(res['datapath_id'])
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800342
khenaidoob96ee0a2017-06-28 15:39:16 -0400343 def _list_local_logical_device_ports(self, id):
344 res = self.get('/api/v1/local/logical_devices/{}/ports'.format(id))
345 self.assertGreaterEqual(len(res['items']), 1)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800346
khenaidoob96ee0a2017-06-28 15:39:16 -0400347 def _list_and_update_local_logical_device_flows(self, id):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800348
349 # retrieve flow list
khenaidoob96ee0a2017-06-28 15:39:16 -0400350 res = self.get('/api/v1/local/logical_devices/{}/flows'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800351 len_before = len(res['items'])
352
Zsolt Haraszti66862032016-11-28 14:28:39 -0800353 t0 = time()
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800354 # add some flows
Zsolt Haraszti66862032016-11-28 14:28:39 -0800355 for _ in xrange(10):
356 req = ofp.FlowTableUpdate(
khenaidoob96ee0a2017-06-28 15:39:16 -0400357 id=id,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800358 flow_mod=mk_simple_flow_mod(
359 cookie=randint(1, 10000000000),
360 priority=randint(1, 10000), # to make it unique
361 match_fields=[
362 in_port(129)
363 ],
364 actions=[
365 output(1)
366 ]
367 )
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800368 )
khenaidoob96ee0a2017-06-28 15:39:16 -0400369 self.post('/api/v1/local/logical_devices/{}/flows'.format(id),
Zsolt Haraszti66862032016-11-28 14:28:39 -0800370 MessageToDict(req, preserving_proto_field_name=True),
Stephane Barbariecd51f992017-09-07 16:37:02 -0400371 expected_http_code=200)
Zsolt Haraszti66862032016-11-28 14:28:39 -0800372 print time() - t0
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800373
khenaidoob96ee0a2017-06-28 15:39:16 -0400374 res = self.get('/api/v1/local/logical_devices/{}/flows'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800375 len_after = len(res['items'])
376 self.assertGreater(len_after, len_before)
377
khenaidoob96ee0a2017-06-28 15:39:16 -0400378 def _list_and_update_local_logical_device_flow_groups(self, id):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800379
380 # retrieve flow list
khenaidoob96ee0a2017-06-28 15:39:16 -0400381 res = self.get('/api/v1/local/logical_devices/{'
382 '}/flow_groups'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800383 len_before = len(res['items'])
384
385 # add some flows
Zsolt Haraszti66862032016-11-28 14:28:39 -0800386 req = ofp.FlowGroupTableUpdate(
Stephane Barbariecd51f992017-09-07 16:37:02 -0400387 id=id,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800388 group_mod=ofp.ofp_group_mod(
389 command=ofp.OFPGC_ADD,
390 type=ofp.OFPGT_ALL,
391 group_id=len_before + 1,
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800392 buckets=[
Zsolt Haraszti66862032016-11-28 14:28:39 -0800393 ofp.ofp_bucket(
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800394 actions=[
Zsolt Haraszti66862032016-11-28 14:28:39 -0800395 ofp.ofp_action(
396 type=ofp.OFPAT_OUTPUT,
397 output=ofp.ofp_action_output(
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800398 port=1
399 )
400 )
401 ]
402 )
403 ]
404 )
405 )
406
khenaidoob96ee0a2017-06-28 15:39:16 -0400407 res = self.post('/api/v1/local/logical_devices/{'
408 '}/flow_groups'.format(id),
Zsolt Haraszti66862032016-11-28 14:28:39 -0800409 MessageToDict(req, preserving_proto_field_name=True),
Stephane Barbariecd51f992017-09-07 16:37:02 -0400410 expected_http_code=200)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800411 # TODO check some stuff on res
412
khenaidoob96ee0a2017-06-28 15:39:16 -0400413 res = self.get('/api/v1/local/logical_devices/{'
414 '}/flow_groups'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800415 len_after = len(res['items'])
416 self.assertGreater(len_after, len_before)
417
khenaidoob96ee0a2017-06-28 15:39:16 -0400418 def _list_local_devices(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800419 res = self.get('/api/v1/local/devices')
420 self.assertGreaterEqual(len(res['items']), 2)
khenaidoob96ee0a2017-06-28 15:39:16 -0400421 return res
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800422
khenaidoob96ee0a2017-06-28 15:39:16 -0400423 def _get_local_device(self, id):
424 res = self.get('/api/v1/local/devices/{}'.format(id))
425 self.assertIsNotNone(res)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800426
khenaidoob96ee0a2017-06-28 15:39:16 -0400427 def _list_local_device_ports(self, id):
428 res = self.get('/api/v1/local/devices/{}/ports'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800429 self.assertGreaterEqual(len(res['items']), 2)
430
khenaidoob96ee0a2017-06-28 15:39:16 -0400431 def _list_local_device_flows(self, id):
432 res = self.get('/api/v1/local/devices/{}/flows'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800433 self.assertGreaterEqual(len(res['items']), 0)
434
khenaidoob96ee0a2017-06-28 15:39:16 -0400435 def _list_local_device_flow_groups(self, id):
436 res = self.get('/api/v1/local/devices/{}/flow_groups'.format(id))
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800437 self.assertGreaterEqual(len(res['items']), 0)
438
khenaidoob96ee0a2017-06-28 15:39:16 -0400439 def _list_local_device_types(self):
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800440 res = self.get('/api/v1/local/device_types')
441 self.assertGreaterEqual(len(res['items']), 2)
khenaidoob96ee0a2017-06-28 15:39:16 -0400442 return res
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800443
khenaidoob96ee0a2017-06-28 15:39:16 -0400444 def _get_local_device_type(self, type):
445 res = self.get('/api/v1/local/device_types/{}'.format(type))
446 self.assertIsNotNone(res)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800447
khenaidoob96ee0a2017-06-28 15:39:16 -0400448 def _list_local_device_groups(self):
449 pass
450 # res = self.get('/api/v1/local/device_groups')
451 # self.assertGreaterEqual(len(res['items']), 1)
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800452
khenaidoob96ee0a2017-06-28 15:39:16 -0400453 def _get_local_device_group(self):
454 pass
455 # res = self.get('/api/v1/local/device_groups/1')
456 # # TODO test the result
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800457
Zsolt Haraszti66862032016-11-28 14:28:39 -0800458class TestGlobalNegativeCases(RestBase):
459
khenaidoob96ee0a2017-06-28 15:39:16 -0400460 # Construct the base_url
ubuntuc5c83d72017-07-01 17:57:19 -0700461 base_url = 'https://' + rest_endpoint
Richard Jankowski9ac4bbd2018-03-21 11:31:02 -0400462 log.debug('global-negative-tests', base_url=base_url)
khenaidoob96ee0a2017-06-28 15:39:16 -0400463
Zsolt Haraszti66862032016-11-28 14:28:39 -0800464 # ~~~~~~~~~~~~~~~~~~~~~~~~~~ NEGATIVE TEST CASES ~~~~~~~~~~~~~~~~~~~~~~~~~~
465
khenaidoob96ee0a2017-06-28 15:39:16 -0400466 def test_03_negative_behavior(self):
467 self._invalid_url()
468 self._instance_not_found()
469 self._logical_device_not_found()
470 self._device_not_found()
471
472 def _invalid_url(self):
Stephane Barbariecd51f992017-09-07 16:37:02 -0400473 self.get('/some_invalid_url', expected_http_code=404)
Zsolt Haraszti66862032016-11-28 14:28:39 -0800474
khenaidoob96ee0a2017-06-28 15:39:16 -0400475 def _instance_not_found(self):
Stephane Barbariecd51f992017-09-07 16:37:02 -0400476 self.get('/api/v1/instances/nay', expected_http_code=200, grpc_status=5)
Zsolt Haraszti66862032016-11-28 14:28:39 -0800477
khenaidoob96ee0a2017-06-28 15:39:16 -0400478 def _logical_device_not_found(self):
Stephane Barbariecd51f992017-09-07 16:37:02 -0400479 self.get('/api/v1/logical_devices/nay', expected_http_code=200, grpc_status=5)
Zsolt Haraszti66862032016-11-28 14:28:39 -0800480
khenaidoob96ee0a2017-06-28 15:39:16 -0400481 def _device_not_found(self):
Stephane Barbariecd51f992017-09-07 16:37:02 -0400482 self.get('/api/v1/devices/nay', expected_http_code=200, grpc_status=5)
Zsolt Haraszti66862032016-11-28 14:28:39 -0800483
484 # TODO add more negative cases
485
486
Zsolt Haraszti00d9a842016-11-23 11:18:23 -0800487if __name__ == '__main__':
488 main()