Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 1 | from random import randint |
| 2 | from time import time, sleep |
| 3 | |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 4 | from google.protobuf.json_format import MessageToDict |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 5 | from unittest import main, TestCase, skip |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 6 | from voltha.protos.device_pb2 import Device |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 7 | from tests.itests.voltha.rest_base import RestBase |
| 8 | from voltha.core.flow_decomposer import mk_simple_flow_mod, in_port, output |
| 9 | from voltha.protos import openflow_13_pb2 as ofp |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 10 | from common.utils.consulhelpers import get_endpoint_from_consul |
Richard Jankowski | 9ac4bbd | 2018-03-21 11:31:02 -0400 | [diff] [blame] | 11 | from structlog import get_logger |
Richard Jankowski | 8f52afb | 2018-03-29 14:19:11 -0400 | [diff] [blame] | 12 | from tests.itests.test_utils import get_pod_ip |
Richard Jankowski | 9ac4bbd | 2018-03-21 11:31:02 -0400 | [diff] [blame] | 13 | from testconfig import config |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 14 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 15 | LOCAL_CONSUL = "localhost:8500" |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 16 | |
Richard Jankowski | 9ac4bbd | 2018-03-21 11:31:02 -0400 | [diff] [blame] | 17 | log = get_logger() |
| 18 | |
| 19 | orch_env = 'docker-compose' |
| 20 | if 'test_parameters' in config and 'orch_env' in config['test_parameters']: |
| 21 | orch_env = config['test_parameters']['orch_env'] |
| 22 | log.debug('orchestration-environment', orch_env=orch_env) |
| 23 | |
| 24 | # Retrieve details of the REST entry point |
| 25 | if orch_env == 'k8s-single-node': |
| 26 | rest_endpoint = get_pod_ip('voltha') + ':8443' |
| 27 | elif orch_env == 'swarm-single-node': |
| 28 | rest_endpoint = 'localhost:8443' |
| 29 | else: |
| 30 | rest_endpoint = get_endpoint_from_consul(LOCAL_CONSUL, 'voltha-envoy-8443') |
| 31 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 32 | class GlobalRestCalls(RestBase): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 33 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 34 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 41 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 42 | # Construct the base_url |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 43 | base_url = 'https://' + rest_endpoint |
Richard Jankowski | 9ac4bbd | 2018-03-21 11:31:02 -0400 | [diff] [blame] | 44 | log.debug('global-rest-calls', base_url=base_url) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 45 | |
| 46 | def test_01_global_rest_apis(self): |
| 47 | # ~~~~~~~~~~~~~~~~~~~ GLOBAL TOP-LEVEL SERVICES~ ~~~~~~~~~~~~~~~~~~~~~~ |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 48 | # self._get_root() |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 49 | 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 Jankowski | 9ac4bbd | 2018-03-21 11:31:02 -0400 | [diff] [blame] | 69 | # TODO: Figure out why this test fails |
| 70 | # self._list_device_flows(device_id) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 71 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 77 | res = self.get('/', expected_content_type='text/html') |
| 78 | self.assertGreaterEqual(res.find('swagger'), 0) |
| 79 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 80 | def _get_schema(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 81 | res = self.get('/schema') |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 82 | self.assertEqual(set(res.keys()), {'protos', 'yang_from','swagger_from'}) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 83 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 84 | def _get_health(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 85 | res = self.get('/health') |
| 86 | self.assertEqual(res['state'], 'HEALTHY') |
| 87 | |
| 88 | # ~~~~~~~~~~~~~~~~~~~~~ TOP LEVEL VOLTHA OPERATIONS ~~~~~~~~~~~~~~~~~~~~~~~ |
| 89 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 90 | def _get_voltha(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 91 | res = self.get('/api/v1') |
| 92 | self.assertEqual(res['version'], '0.9.0') |
| 93 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 94 | def _list_voltha_instances(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 95 | res = self.get('/api/v1/instances') |
| 96 | self.assertEqual(len(res['items']), 1) |
| 97 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 98 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 102 | self.assertEqual(res['version'], '0.9.0') |
| 103 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 104 | 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 Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 110 | expected_http_code=200) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 111 | 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 Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 124 | self.post(path + '/enable', expected_http_code=200) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 125 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 177 | res = self.get('/api/v1/logical_devices') |
| 178 | self.assertGreaterEqual(len(res['items']), 1) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 179 | return res |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 180 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 181 | def _get_logical_device(self, id): |
| 182 | res = self.get('/api/v1/logical_devices/{}'.format(id)) |
| 183 | self.assertIsNotNone(res['datapath_id']) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 184 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 185 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 188 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 189 | def _list_and_update_logical_device_flows(self, id): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 190 | |
| 191 | # retrieve flow list |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 192 | res = self.get('/api/v1/logical_devices/{}/flows'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 193 | len_before = len(res['items']) |
| 194 | |
| 195 | # add some flows |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 196 | req = ofp.FlowTableUpdate( |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 197 | id=id, |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 198 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 206 | ] |
| 207 | ) |
| 208 | ) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 209 | res = self.post('/api/v1/logical_devices/{}/flows'.format(id), |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 210 | MessageToDict(req, preserving_proto_field_name=True), |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 211 | expected_http_code=200) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 212 | # TODO check some stuff on res |
| 213 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 214 | res = self.get('/api/v1/logical_devices/{}/flows'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 215 | len_after = len(res['items']) |
| 216 | self.assertGreater(len_after, len_before) |
| 217 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 218 | def _list_and_update_logical_device_flow_groups(self, id): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 219 | |
| 220 | # retrieve flow list |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 221 | res = self.get('/api/v1/logical_devices/{}/flow_groups'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 222 | len_before = len(res['items']) |
| 223 | |
| 224 | # add some flows |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 225 | req = ofp.FlowGroupTableUpdate( |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 226 | id=id, |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 227 | group_mod=ofp.ofp_group_mod( |
| 228 | command=ofp.OFPGC_ADD, |
| 229 | type=ofp.OFPGT_ALL, |
| 230 | group_id=len_before + 1, |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 231 | buckets=[ |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 232 | ofp.ofp_bucket( |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 233 | actions=[ |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 234 | ofp.ofp_action( |
| 235 | type=ofp.OFPAT_OUTPUT, |
| 236 | output=ofp.ofp_action_output( |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 237 | port=1 |
| 238 | ) |
| 239 | ) |
| 240 | ] |
| 241 | ) |
| 242 | ] |
| 243 | ) |
| 244 | ) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 245 | res = self.post('/api/v1/logical_devices/{}/flow_groups'.format(id), |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 246 | MessageToDict(req, preserving_proto_field_name=True), |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 247 | expected_http_code=200) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 248 | # TODO check some stuff on res |
| 249 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 250 | res = self.get('/api/v1/logical_devices/{}/flow_groups'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 251 | len_after = len(res['items']) |
| 252 | self.assertGreater(len_after, len_before) |
| 253 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 254 | def _list_devices(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 255 | res = self.get('/api/v1/devices') |
| 256 | self.assertGreaterEqual(len(res['items']), 2) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 257 | return res |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 258 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 259 | def _get_device(self, id): |
| 260 | res = self.get('/api/v1/devices/{}'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 261 | # TODO test result |
| 262 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 263 | def _list_device_ports(self, id): |
| 264 | res = self.get('/api/v1/devices/{}/ports'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 265 | self.assertGreaterEqual(len(res['items']), 2) |
| 266 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 267 | def _list_device_flows(self, id): |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 268 | # pump some flows into the logical device |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 269 | res = self.get('/api/v1/devices/{}/flows'.format(id)) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 270 | self.assertGreaterEqual(len(res['items']), 1) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 271 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 272 | def _list_device_flow_groups(self,id): |
| 273 | res = self.get('/api/v1/devices/{}/flow_groups'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 274 | self.assertGreaterEqual(len(res['items']), 0) |
| 275 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 276 | def _list_device_types(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 277 | res = self.get('/api/v1/device_types') |
| 278 | self.assertGreaterEqual(len(res['items']), 2) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 279 | return res |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 280 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 281 | def _get_device_type(self, dtype): |
| 282 | res = self.get('/api/v1/device_types/{}'.format(dtype)) |
| 283 | self.assertIsNotNone(res) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 284 | # TODO test the result |
| 285 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 286 | def _list_device_groups(self): |
| 287 | pass |
| 288 | # res = self.get('/api/v1/device_groups') |
| 289 | # self.assertGreaterEqual(len(res['items']), 1) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 290 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 291 | def _get_device_group(self): |
| 292 | pass |
| 293 | # res = self.get('/api/v1/device_groups/1') |
| 294 | # # TODO test the result |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 295 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 296 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 297 | @skip("Use of local rest calls is deprecated.") |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 298 | class TestLocalRestCalls(RestBase): |
| 299 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 300 | # Construct the base_url |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 301 | base_url = 'https://' + rest_endpoint |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 302 | |
| 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 324 | self.assertEqual(self.get('/api/v1/local')['version'], '0.9.0') |
| 325 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 326 | def _get_local_health(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 327 | d = self.get('/api/v1/local/health') |
| 328 | self.assertEqual(d['state'], 'HEALTHY') |
| 329 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 330 | def _list_local_adapters(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 331 | self.assertGreaterEqual( |
| 332 | len(self.get('/api/v1/local/adapters')['items']), 1) |
| 333 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 334 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 338 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 339 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 342 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 343 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 346 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 347 | def _list_and_update_local_logical_device_flows(self, id): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 348 | |
| 349 | # retrieve flow list |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 350 | res = self.get('/api/v1/local/logical_devices/{}/flows'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 351 | len_before = len(res['items']) |
| 352 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 353 | t0 = time() |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 354 | # add some flows |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 355 | for _ in xrange(10): |
| 356 | req = ofp.FlowTableUpdate( |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 357 | id=id, |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 358 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 368 | ) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 369 | self.post('/api/v1/local/logical_devices/{}/flows'.format(id), |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 370 | MessageToDict(req, preserving_proto_field_name=True), |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 371 | expected_http_code=200) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 372 | print time() - t0 |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 373 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 374 | res = self.get('/api/v1/local/logical_devices/{}/flows'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 375 | len_after = len(res['items']) |
| 376 | self.assertGreater(len_after, len_before) |
| 377 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 378 | def _list_and_update_local_logical_device_flow_groups(self, id): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 379 | |
| 380 | # retrieve flow list |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 381 | res = self.get('/api/v1/local/logical_devices/{' |
| 382 | '}/flow_groups'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 383 | len_before = len(res['items']) |
| 384 | |
| 385 | # add some flows |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 386 | req = ofp.FlowGroupTableUpdate( |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 387 | id=id, |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 388 | group_mod=ofp.ofp_group_mod( |
| 389 | command=ofp.OFPGC_ADD, |
| 390 | type=ofp.OFPGT_ALL, |
| 391 | group_id=len_before + 1, |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 392 | buckets=[ |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 393 | ofp.ofp_bucket( |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 394 | actions=[ |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 395 | ofp.ofp_action( |
| 396 | type=ofp.OFPAT_OUTPUT, |
| 397 | output=ofp.ofp_action_output( |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 398 | port=1 |
| 399 | ) |
| 400 | ) |
| 401 | ] |
| 402 | ) |
| 403 | ] |
| 404 | ) |
| 405 | ) |
| 406 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 407 | res = self.post('/api/v1/local/logical_devices/{' |
| 408 | '}/flow_groups'.format(id), |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 409 | MessageToDict(req, preserving_proto_field_name=True), |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 410 | expected_http_code=200) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 411 | # TODO check some stuff on res |
| 412 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 413 | res = self.get('/api/v1/local/logical_devices/{' |
| 414 | '}/flow_groups'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 415 | len_after = len(res['items']) |
| 416 | self.assertGreater(len_after, len_before) |
| 417 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 418 | def _list_local_devices(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 419 | res = self.get('/api/v1/local/devices') |
| 420 | self.assertGreaterEqual(len(res['items']), 2) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 421 | return res |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 422 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 423 | def _get_local_device(self, id): |
| 424 | res = self.get('/api/v1/local/devices/{}'.format(id)) |
| 425 | self.assertIsNotNone(res) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 426 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 427 | def _list_local_device_ports(self, id): |
| 428 | res = self.get('/api/v1/local/devices/{}/ports'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 429 | self.assertGreaterEqual(len(res['items']), 2) |
| 430 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 431 | def _list_local_device_flows(self, id): |
| 432 | res = self.get('/api/v1/local/devices/{}/flows'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 433 | self.assertGreaterEqual(len(res['items']), 0) |
| 434 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 435 | def _list_local_device_flow_groups(self, id): |
| 436 | res = self.get('/api/v1/local/devices/{}/flow_groups'.format(id)) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 437 | self.assertGreaterEqual(len(res['items']), 0) |
| 438 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 439 | def _list_local_device_types(self): |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 440 | res = self.get('/api/v1/local/device_types') |
| 441 | self.assertGreaterEqual(len(res['items']), 2) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 442 | return res |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 443 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 444 | def _get_local_device_type(self, type): |
| 445 | res = self.get('/api/v1/local/device_types/{}'.format(type)) |
| 446 | self.assertIsNotNone(res) |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 447 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 448 | 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 Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 452 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 453 | def _get_local_device_group(self): |
| 454 | pass |
| 455 | # res = self.get('/api/v1/local/device_groups/1') |
| 456 | # # TODO test the result |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 457 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 458 | class TestGlobalNegativeCases(RestBase): |
| 459 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 460 | # Construct the base_url |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 461 | base_url = 'https://' + rest_endpoint |
Richard Jankowski | 9ac4bbd | 2018-03-21 11:31:02 -0400 | [diff] [blame] | 462 | log.debug('global-negative-tests', base_url=base_url) |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 463 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 464 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~ NEGATIVE TEST CASES ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 465 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 466 | 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 Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 473 | self.get('/some_invalid_url', expected_http_code=404) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 474 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 475 | def _instance_not_found(self): |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 476 | self.get('/api/v1/instances/nay', expected_http_code=200, grpc_status=5) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 477 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 478 | def _logical_device_not_found(self): |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 479 | self.get('/api/v1/logical_devices/nay', expected_http_code=200, grpc_status=5) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 480 | |
khenaidoo | b96ee0a | 2017-06-28 15:39:16 -0400 | [diff] [blame] | 481 | def _device_not_found(self): |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 482 | self.get('/api/v1/devices/nay', expected_http_code=200, grpc_status=5) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 483 | |
| 484 | # TODO add more negative cases |
| 485 | |
| 486 | |
Zsolt Haraszti | 00d9a84 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 487 | if __name__ == '__main__': |
| 488 | main() |