c5f7038ad7b8daac6e50a3937eb690d9e19ecd8e
[osm/RO.git] / RO-VIM-openstack / osm_rovim_openstack / tests / test_vimconn_openstack.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2017 Intel Corporation.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: nfvlabs@tid.es
20 ##
21
22 """
23 This module contains unit tests for the OpenStack VIM connector
24 Run this directly with python2 or python3.
25 """
26
27 import copy
28 import unittest
29
30 import mock
31 from neutronclient.v2_0.client import Client
32 from osm_ro_plugin import vimconn
33 from osm_rovim_openstack.vimconn_openstack import vimconnector
34
35 __author__ = "Igor D.C."
36 __date__ = "$23-aug-2017 23:59:59$"
37
38
39 class TestSfcOperations(unittest.TestCase):
40 def setUp(self):
41 # instantiate dummy VIM connector so we can test it
42 self.vimconn = vimconnector(
43 "123",
44 "openstackvim",
45 "456",
46 "789",
47 "http://dummy.url",
48 None,
49 "user",
50 "pass",
51 )
52
53 def _test_new_sfi(
54 self,
55 create_sfc_port_pair,
56 sfc_encap,
57 ingress_ports=["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
58 egress_ports=["230cdf1b-de37-4891-bc07-f9010cf1f967"],
59 ):
60 # input to VIM connector
61 name = "osm_sfi"
62 # + ingress_ports
63 # + egress_ports
64 # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
65 correlation = "nsh"
66 if sfc_encap is not None:
67 if not sfc_encap:
68 correlation = None
69
70 # what OpenStack is assumed to respond (patch OpenStack"s return value)
71 dict_from_neutron = {
72 "port_pair": {
73 "id": "3d7ddc13-923c-4332-971e-708ed82902ce",
74 "name": name,
75 "description": "",
76 "tenant_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
77 "project_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
78 "ingress": ingress_ports[0] if len(ingress_ports) else None,
79 "egress": egress_ports[0] if len(egress_ports) else None,
80 "service_function_parameters": {"correlation": correlation},
81 }
82 }
83 create_sfc_port_pair.return_value = dict_from_neutron
84
85 # what the VIM connector is expected to
86 # send to OpenStack based on the input
87 dict_to_neutron = {
88 "port_pair": {
89 "name": name,
90 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
91 "egress": "230cdf1b-de37-4891-bc07-f9010cf1f967",
92 "service_function_parameters": {"correlation": correlation},
93 }
94 }
95
96 # call the VIM connector
97 if sfc_encap is None:
98 result = self.vimconn.new_sfi(name, ingress_ports, egress_ports)
99 else:
100 result = self.vimconn.new_sfi(name, ingress_ports, egress_ports, sfc_encap)
101
102 # assert that the VIM connector made the expected call to OpenStack
103 create_sfc_port_pair.assert_called_with(dict_to_neutron)
104 # assert that the VIM connector had the expected result / return value
105 self.assertEqual(result, dict_from_neutron["port_pair"]["id"])
106
107 def _test_new_sf(self, create_sfc_port_pair_group):
108 # input to VIM connector
109 name = "osm_sf"
110 instances = [
111 "bbd01220-cf72-41f2-9e70-0669c2e5c4cd",
112 "12ba215e-3987-4892-bd3a-d0fd91eecf98",
113 "e25a7c79-14c8-469a-9ae1-f601c9371ffd",
114 ]
115
116 # what OpenStack is assumed to respond (patch OpenStack"s return value)
117 dict_from_neutron = {
118 "port_pair_group": {
119 "id": "3d7ddc13-923c-4332-971e-708ed82902ce",
120 "name": name,
121 "description": "",
122 "tenant_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
123 "project_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
124 "port_pairs": instances,
125 "group_id": 1,
126 "port_pair_group_parameters": {
127 "lb_fields": [],
128 "ppg_n_tuple_mapping": {
129 "ingress_n_tuple": {},
130 "egress_n_tuple": {},
131 },
132 },
133 }
134 }
135 create_sfc_port_pair_group.return_value = dict_from_neutron
136
137 # what the VIM connector is expected to
138 # send to OpenStack based on the input
139 dict_to_neutron = {
140 "port_pair_group": {
141 "name": name,
142 "port_pairs": [
143 "bbd01220-cf72-41f2-9e70-0669c2e5c4cd",
144 "12ba215e-3987-4892-bd3a-d0fd91eecf98",
145 "e25a7c79-14c8-469a-9ae1-f601c9371ffd",
146 ],
147 }
148 }
149
150 # call the VIM connector
151 result = self.vimconn.new_sf(name, instances)
152
153 # assert that the VIM connector made the expected call to OpenStack
154 create_sfc_port_pair_group.assert_called_with(dict_to_neutron)
155 # assert that the VIM connector had the expected result / return value
156 self.assertEqual(result, dict_from_neutron["port_pair_group"]["id"])
157
158 def _test_new_sfp(self, create_sfc_port_chain, sfc_encap, spi):
159 # input to VIM connector
160 name = "osm_sfp"
161 classifications = [
162 "2bd2a2e5-c5fd-4eac-a297-d5e255c35c19",
163 "00f23389-bdfa-43c2-8b16-5815f2582fa8",
164 ]
165 sfs = [
166 "2314daec-c262-414a-86e3-69bb6fa5bc16",
167 "d8bfdb5d-195e-4f34-81aa-6135705317df",
168 ]
169
170 # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
171 correlation = "nsh"
172 chain_id = 33
173 if spi:
174 chain_id = spi
175
176 # what OpenStack is assumed to respond (patch OpenStack"s return value)
177 dict_from_neutron = {
178 "port_chain": {
179 "id": "5bc05721-079b-4b6e-a235-47cac331cbb6",
180 "name": name,
181 "description": "",
182 "tenant_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
183 "project_id": "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c",
184 "chain_id": chain_id,
185 "flow_classifiers": classifications,
186 "port_pair_groups": sfs,
187 "chain_parameters": {"correlation": correlation},
188 }
189 }
190 create_sfc_port_chain.return_value = dict_from_neutron
191
192 # what the VIM connector is expected to
193 # send to OpenStack based on the input
194 dict_to_neutron = {
195 "port_chain": {
196 "name": name,
197 "flow_classifiers": [
198 "2bd2a2e5-c5fd-4eac-a297-d5e255c35c19",
199 "00f23389-bdfa-43c2-8b16-5815f2582fa8",
200 ],
201 "port_pair_groups": [
202 "2314daec-c262-414a-86e3-69bb6fa5bc16",
203 "d8bfdb5d-195e-4f34-81aa-6135705317df",
204 ],
205 "chain_parameters": {"correlation": correlation},
206 }
207 }
208 if spi:
209 dict_to_neutron["port_chain"]["chain_id"] = spi
210
211 # call the VIM connector
212 if sfc_encap is None:
213 if spi is None:
214 result = self.vimconn.new_sfp(name, classifications, sfs)
215 else:
216 result = self.vimconn.new_sfp(name, classifications, sfs, spi=spi)
217 else:
218 if spi is None:
219 result = self.vimconn.new_sfp(name, classifications, sfs, sfc_encap)
220 else:
221 result = self.vimconn.new_sfp(
222 name, classifications, sfs, sfc_encap, spi
223 )
224
225 # assert that the VIM connector made the expected call to OpenStack
226 create_sfc_port_chain.assert_called_with(dict_to_neutron)
227 # assert that the VIM connector had the expected result / return value
228 self.assertEqual(result, dict_from_neutron["port_chain"]["id"])
229
230 def _test_new_classification(self, create_sfc_flow_classifier, ctype):
231 # input to VIM connector
232 name = "osm_classification"
233 definition = {
234 "ethertype": "IPv4",
235 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
236 "protocol": "tcp",
237 "source_ip_prefix": "192.168.2.0/24",
238 "source_port_range_max": 99,
239 "source_port_range_min": 50,
240 }
241
242 # what OpenStack is assumed to respond (patch OpenStack"s return value)
243 dict_from_neutron = {"flow_classifier": copy.copy(definition)}
244 dict_from_neutron["flow_classifier"][
245 "id"
246 ] = "7735ec2c-fddf-4130-9712-32ed2ab6a372"
247 dict_from_neutron["flow_classifier"]["name"] = name
248 dict_from_neutron["flow_classifier"]["description"] = ""
249 dict_from_neutron["flow_classifier"][
250 "tenant_id"
251 ] = "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c"
252 dict_from_neutron["flow_classifier"][
253 "project_id"
254 ] = "130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c"
255 create_sfc_flow_classifier.return_value = dict_from_neutron
256
257 # what the VIM connector is expected to
258 # send to OpenStack based on the input
259 dict_to_neutron = {"flow_classifier": copy.copy(definition)}
260 dict_to_neutron["flow_classifier"]["name"] = "osm_classification"
261
262 # call the VIM connector
263 result = self.vimconn.new_classification(name, ctype, definition)
264
265 # assert that the VIM connector made the expected call to OpenStack
266 create_sfc_flow_classifier.assert_called_with(dict_to_neutron)
267 # assert that the VIM connector had the expected result / return value
268 self.assertEqual(result, dict_from_neutron["flow_classifier"]["id"])
269
270 @mock.patch.object(Client, "create_sfc_flow_classifier")
271 def test_new_classification(self, create_sfc_flow_classifier):
272 self._test_new_classification(
273 create_sfc_flow_classifier, "legacy_flow_classifier"
274 )
275
276 @mock.patch.object(Client, "create_sfc_flow_classifier")
277 def test_new_classification_unsupported_type(self, create_sfc_flow_classifier):
278 self.assertRaises(
279 vimconn.VimConnNotSupportedException,
280 self._test_new_classification,
281 create_sfc_flow_classifier,
282 "h265",
283 )
284
285 @mock.patch.object(Client, "create_sfc_port_pair")
286 def test_new_sfi_with_sfc_encap(self, create_sfc_port_pair):
287 self._test_new_sfi(create_sfc_port_pair, True)
288
289 @mock.patch.object(Client, "create_sfc_port_pair")
290 def test_new_sfi_without_sfc_encap(self, create_sfc_port_pair):
291 self._test_new_sfi(create_sfc_port_pair, False)
292
293 @mock.patch.object(Client, "create_sfc_port_pair")
294 def test_new_sfi_default_sfc_encap(self, create_sfc_port_pair):
295 self._test_new_sfi(create_sfc_port_pair, None)
296
297 @mock.patch.object(Client, "create_sfc_port_pair")
298 def test_new_sfi_bad_ingress_ports(self, create_sfc_port_pair):
299 ingress_ports = [
300 "5311c75d-d718-4369-bbda-cdcc6da60fcc",
301 "a0273f64-82c9-11e7-b08f-6328e53f0fa7",
302 ]
303 self.assertRaises(
304 vimconn.VimConnNotSupportedException,
305 self._test_new_sfi,
306 create_sfc_port_pair,
307 True,
308 ingress_ports=ingress_ports,
309 )
310 ingress_ports = []
311 self.assertRaises(
312 vimconn.VimConnNotSupportedException,
313 self._test_new_sfi,
314 create_sfc_port_pair,
315 True,
316 ingress_ports=ingress_ports,
317 )
318
319 @mock.patch.object(Client, "create_sfc_port_pair")
320 def test_new_sfi_bad_egress_ports(self, create_sfc_port_pair):
321 egress_ports = [
322 "230cdf1b-de37-4891-bc07-f9010cf1f967",
323 "b41228fe-82c9-11e7-9b44-17504174320b",
324 ]
325 self.assertRaises(
326 vimconn.VimConnNotSupportedException,
327 self._test_new_sfi,
328 create_sfc_port_pair,
329 True,
330 egress_ports=egress_ports,
331 )
332 egress_ports = []
333 self.assertRaises(
334 vimconn.VimConnNotSupportedException,
335 self._test_new_sfi,
336 create_sfc_port_pair,
337 True,
338 egress_ports=egress_ports,
339 )
340
341 @mock.patch.object(vimconnector, "get_sfi")
342 @mock.patch.object(Client, "create_sfc_port_pair_group")
343 def test_new_sf(self, create_sfc_port_pair_group, get_sfi):
344 get_sfi.return_value = {"sfc_encap": True}
345 self._test_new_sf(create_sfc_port_pair_group)
346
347 @mock.patch.object(vimconnector, "get_sfi")
348 @mock.patch.object(Client, "create_sfc_port_pair_group")
349 def test_new_sf_inconsistent_sfc_encap(self, create_sfc_port_pair_group, get_sfi):
350 get_sfi.return_value = {"sfc_encap": "nsh"}
351 self.assertRaises(
352 vimconn.VimConnNotSupportedException,
353 self._test_new_sf,
354 create_sfc_port_pair_group,
355 )
356
357 @mock.patch.object(Client, "create_sfc_port_chain")
358 def test_new_sfp_with_sfc_encap(self, create_sfc_port_chain):
359 self._test_new_sfp(create_sfc_port_chain, True, None)
360
361 @mock.patch.object(Client, "create_sfc_port_chain")
362 def test_new_sfp_without_sfc_encap(self, create_sfc_port_chain):
363 self._test_new_sfp(create_sfc_port_chain, False, None)
364 self._test_new_sfp(create_sfc_port_chain, False, 25)
365
366 @mock.patch.object(Client, "create_sfc_port_chain")
367 def test_new_sfp_default_sfc_encap(self, create_sfc_port_chain):
368 self._test_new_sfp(create_sfc_port_chain, None, None)
369
370 @mock.patch.object(Client, "create_sfc_port_chain")
371 def test_new_sfp_with_sfc_encap_spi(self, create_sfc_port_chain):
372 self._test_new_sfp(create_sfc_port_chain, True, 25)
373
374 @mock.patch.object(Client, "create_sfc_port_chain")
375 def test_new_sfp_default_sfc_encap_spi(self, create_sfc_port_chain):
376 self._test_new_sfp(create_sfc_port_chain, None, 25)
377
378 @mock.patch.object(Client, "list_sfc_flow_classifiers")
379 def test_get_classification_list(self, list_sfc_flow_classifiers):
380 # what OpenStack is assumed to return to the VIM connector
381 list_sfc_flow_classifiers.return_value = {
382 "flow_classifiers": [
383 {
384 "source_port_range_min": 2000,
385 "destination_ip_prefix": "192.168.3.0/24",
386 "protocol": "udp",
387 "description": "",
388 "ethertype": "IPv4",
389 "l7_parameters": {},
390 "source_port_range_max": 2000,
391 "destination_port_range_min": 3000,
392 "source_ip_prefix": "192.168.2.0/24",
393 "logical_destination_port": None,
394 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
395 "destination_port_range_max": None,
396 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
397 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
398 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
399 "name": "fc1",
400 }
401 ]
402 }
403
404 # call the VIM connector
405 filter_dict = {"protocol": "tcp", "ethertype": "IPv4"}
406 result = self.vimconn.get_classification_list(filter_dict.copy())
407
408 # assert that VIM connector called OpenStack with the expected filter
409 list_sfc_flow_classifiers.assert_called_with(**filter_dict)
410 # assert that the VIM connector successfully
411 # translated and returned the OpenStack result
412 self.assertEqual(
413 result,
414 [
415 {
416 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
417 "name": "fc1",
418 "description": "",
419 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
420 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
421 "ctype": "legacy_flow_classifier",
422 "definition": {
423 "source_port_range_min": 2000,
424 "destination_ip_prefix": "192.168.3.0/24",
425 "protocol": "udp",
426 "ethertype": "IPv4",
427 "l7_parameters": {},
428 "source_port_range_max": 2000,
429 "destination_port_range_min": 3000,
430 "source_ip_prefix": "192.168.2.0/24",
431 "logical_destination_port": None,
432 "destination_port_range_max": None,
433 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
434 },
435 }
436 ],
437 )
438
439 def _test_get_sfi_list(self, list_port_pair, correlation, sfc_encap):
440 # what OpenStack is assumed to return to the VIM connector
441 list_port_pair.return_value = {
442 "port_pairs": [
443 {
444 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
445 "description": "",
446 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
447 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
448 "service_function_parameters": {"correlation": correlation},
449 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
450 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
451 "name": "osm_sfi",
452 }
453 ]
454 }
455
456 # call the VIM connector
457 filter_dict = {"name": "osm_sfi", "description": ""}
458 result = self.vimconn.get_sfi_list(filter_dict.copy())
459
460 # assert that VIM connector called OpenStack with the expected filter
461 list_port_pair.assert_called_with(**filter_dict)
462 # assert that the VIM connector successfully
463 # translated and returned the OpenStack result
464 self.assertEqual(
465 result,
466 [
467 {
468 "ingress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
469 "description": "",
470 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
471 "egress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
472 "sfc_encap": sfc_encap,
473 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
474 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
475 "name": "osm_sfi",
476 }
477 ],
478 )
479
480 @mock.patch.object(Client, "list_sfc_port_pairs")
481 def test_get_sfi_list_with_sfc_encap(self, list_sfc_port_pairs):
482 self._test_get_sfi_list(list_sfc_port_pairs, "nsh", True)
483
484 @mock.patch.object(Client, "list_sfc_port_pairs")
485 def test_get_sfi_list_without_sfc_encap(self, list_sfc_port_pairs):
486 self._test_get_sfi_list(list_sfc_port_pairs, None, False)
487
488 @mock.patch.object(Client, "list_sfc_port_pair_groups")
489 def test_get_sf_list(self, list_sfc_port_pair_groups):
490 # what OpenStack is assumed to return to the VIM connector
491 list_sfc_port_pair_groups.return_value = {
492 "port_pair_groups": [
493 {
494 "port_pairs": [
495 "08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2",
496 "0d63799c-82d6-11e7-8deb-a746bb3ae9f5",
497 ],
498 "description": "",
499 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
500 "port_pair_group_parameters": {},
501 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
502 "id": "f4a0bde8-82d5-11e7-90e1-a72b762fa27f",
503 "name": "osm_sf",
504 }
505 ]
506 }
507
508 # call the VIM connector
509 filter_dict = {"name": "osm_sf", "description": ""}
510 result = self.vimconn.get_sf_list(filter_dict.copy())
511
512 # assert that VIM connector called OpenStack with the expected filter
513 list_sfc_port_pair_groups.assert_called_with(**filter_dict)
514 # assert that the VIM connector successfully
515 # translated and returned the OpenStack result
516 self.assertEqual(
517 result,
518 [
519 {
520 "sfis": [
521 "08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2",
522 "0d63799c-82d6-11e7-8deb-a746bb3ae9f5",
523 ],
524 "description": "",
525 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
526 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
527 "id": "f4a0bde8-82d5-11e7-90e1-a72b762fa27f",
528 "name": "osm_sf",
529 }
530 ],
531 )
532
533 def _test_get_sfp_list(self, list_sfc_port_chains, correlation, sfc_encap):
534 # what OpenStack is assumed to return to the VIM connector
535 list_sfc_port_chains.return_value = {
536 "port_chains": [
537 {
538 "port_pair_groups": [
539 "7d8e3bf8-82d6-11e7-a032-8ff028839d25",
540 "7dc9013e-82d6-11e7-a5a6-a3a8d78a5518",
541 ],
542 "flow_classifiers": [
543 "1333c2f4-82d7-11e7-a5df-9327f33d104e",
544 "1387ab44-82d7-11e7-9bb0-476337183905",
545 ],
546 "description": "",
547 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
548 "chain_parameters": {"correlation": correlation},
549 "chain_id": 40,
550 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
551 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
552 "name": "osm_sfp",
553 }
554 ]
555 }
556
557 # call the VIM connector
558 filter_dict = {"name": "osm_sfp", "description": ""}
559 result = self.vimconn.get_sfp_list(filter_dict.copy())
560
561 # assert that VIM connector called OpenStack with the expected filter
562 list_sfc_port_chains.assert_called_with(**filter_dict)
563 # assert that the VIM connector successfully
564 # translated and returned the OpenStack result
565 self.assertEqual(
566 result,
567 [
568 {
569 "service_functions": [
570 "7d8e3bf8-82d6-11e7-a032-8ff028839d25",
571 "7dc9013e-82d6-11e7-a5a6-a3a8d78a5518",
572 ],
573 "classifications": [
574 "1333c2f4-82d7-11e7-a5df-9327f33d104e",
575 "1387ab44-82d7-11e7-9bb0-476337183905",
576 ],
577 "description": "",
578 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
579 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
580 "sfc_encap": sfc_encap,
581 "spi": 40,
582 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
583 "name": "osm_sfp",
584 }
585 ],
586 )
587
588 @mock.patch.object(Client, "list_sfc_port_chains")
589 def test_get_sfp_list_with_sfc_encap(self, list_sfc_port_chains):
590 self._test_get_sfp_list(list_sfc_port_chains, "nsh", True)
591
592 @mock.patch.object(Client, "list_sfc_port_chains")
593 def test_get_sfp_list_without_sfc_encap(self, list_sfc_port_chains):
594 self._test_get_sfp_list(list_sfc_port_chains, None, False)
595
596 @mock.patch.object(Client, "list_sfc_flow_classifiers")
597 def test_get_classification(self, list_sfc_flow_classifiers):
598 # what OpenStack is assumed to return to the VIM connector
599 list_sfc_flow_classifiers.return_value = {
600 "flow_classifiers": [
601 {
602 "source_port_range_min": 2000,
603 "destination_ip_prefix": "192.168.3.0/24",
604 "protocol": "udp",
605 "description": "",
606 "ethertype": "IPv4",
607 "l7_parameters": {},
608 "source_port_range_max": 2000,
609 "destination_port_range_min": 3000,
610 "source_ip_prefix": "192.168.2.0/24",
611 "logical_destination_port": None,
612 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
613 "destination_port_range_max": None,
614 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
615 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
616 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
617 "name": "fc1",
618 }
619 ]
620 }
621
622 # call the VIM connector
623 result = self.vimconn.get_classification("22198366-d4e8-4d6b-b4d2-637d5d6cbb7d")
624
625 # assert that VIM connector called OpenStack with the expected filter
626 list_sfc_flow_classifiers.assert_called_with(
627 id="22198366-d4e8-4d6b-b4d2-637d5d6cbb7d"
628 )
629 # assert that VIM connector successfully returned the OpenStack result
630 self.assertEqual(
631 result,
632 {
633 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
634 "name": "fc1",
635 "description": "",
636 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
637 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
638 "ctype": "legacy_flow_classifier",
639 "definition": {
640 "source_port_range_min": 2000,
641 "destination_ip_prefix": "192.168.3.0/24",
642 "protocol": "udp",
643 "ethertype": "IPv4",
644 "l7_parameters": {},
645 "source_port_range_max": 2000,
646 "destination_port_range_min": 3000,
647 "source_ip_prefix": "192.168.2.0/24",
648 "logical_destination_port": None,
649 "destination_port_range_max": None,
650 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
651 },
652 },
653 )
654
655 @mock.patch.object(Client, "list_sfc_flow_classifiers")
656 def test_get_classification_many_results(self, list_sfc_flow_classifiers):
657 # what OpenStack is assumed to return to the VIM connector
658 list_sfc_flow_classifiers.return_value = {
659 "flow_classifiers": [
660 {
661 "source_port_range_min": 2000,
662 "destination_ip_prefix": "192.168.3.0/24",
663 "protocol": "udp",
664 "description": "",
665 "ethertype": "IPv4",
666 "l7_parameters": {},
667 "source_port_range_max": 2000,
668 "destination_port_range_min": 3000,
669 "source_ip_prefix": "192.168.2.0/24",
670 "logical_destination_port": None,
671 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
672 "destination_port_range_max": None,
673 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
674 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
675 "id": "22198366-d4e8-4d6b-b4d2-637d5d6cbb7d",
676 "name": "fc1",
677 },
678 {
679 "source_port_range_min": 1000,
680 "destination_ip_prefix": "192.168.3.0/24",
681 "protocol": "udp",
682 "description": "",
683 "ethertype": "IPv4",
684 "l7_parameters": {},
685 "source_port_range_max": 1000,
686 "destination_port_range_min": 3000,
687 "source_ip_prefix": "192.168.2.0/24",
688 "logical_destination_port": None,
689 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
690 "destination_port_range_max": None,
691 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
692 "logical_source_port": "aaab0ab0-1452-4636-bb3b-11dca833fa2b",
693 "id": "3196bafc-82dd-11e7-a205-9bf6c14b0721",
694 "name": "fc2",
695 },
696 ]
697 }
698
699 # call the VIM connector
700 self.assertRaises(
701 vimconn.VimConnConflictException,
702 self.vimconn.get_classification,
703 "3196bafc-82dd-11e7-a205-9bf6c14b0721",
704 )
705
706 # assert the VIM connector called OpenStack with the expected filter
707 list_sfc_flow_classifiers.assert_called_with(
708 id="3196bafc-82dd-11e7-a205-9bf6c14b0721"
709 )
710
711 @mock.patch.object(Client, "list_sfc_flow_classifiers")
712 def test_get_classification_no_results(self, list_sfc_flow_classifiers):
713 # what OpenStack is assumed to return to the VIM connector
714 list_sfc_flow_classifiers.return_value = {"flow_classifiers": []}
715
716 # call the VIM connector
717 self.assertRaises(
718 vimconn.VimConnNotFoundException,
719 self.vimconn.get_classification,
720 "3196bafc-82dd-11e7-a205-9bf6c14b0721",
721 )
722
723 # assert the VIM connector called OpenStack with the expected filter
724 list_sfc_flow_classifiers.assert_called_with(
725 id="3196bafc-82dd-11e7-a205-9bf6c14b0721"
726 )
727
728 @mock.patch.object(Client, "list_sfc_port_pairs")
729 def test_get_sfi(self, list_sfc_port_pairs):
730 # what OpenStack is assumed to return to the VIM connector
731 list_sfc_port_pairs.return_value = {
732 "port_pairs": [
733 {
734 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
735 "description": "",
736 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
737 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
738 "service_function_parameters": {"correlation": "nsh"},
739 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
740 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
741 "name": "osm_sfi1",
742 },
743 ]
744 }
745
746 # call the VIM connector
747 result = self.vimconn.get_sfi("c121ebdd-7f2d-4213-b933-3325298a6966")
748
749 # assert the VIM connector called OpenStack with the expected filter
750 list_sfc_port_pairs.assert_called_with(
751 id="c121ebdd-7f2d-4213-b933-3325298a6966"
752 )
753 # assert the VIM connector successfully returned the OpenStack result
754 self.assertEqual(
755 result,
756 {
757 "ingress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
758 "egress_ports": ["5311c75d-d718-4369-bbda-cdcc6da60fcc"],
759 "sfc_encap": True,
760 "description": "",
761 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
762 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
763 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
764 "name": "osm_sfi1",
765 },
766 )
767
768 @mock.patch.object(Client, "list_sfc_port_pairs")
769 def test_get_sfi_many_results(self, list_sfc_port_pairs):
770 # what OpenStack is assumed to return to the VIM connector
771 list_sfc_port_pairs.return_value = {
772 "port_pairs": [
773 {
774 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
775 "description": "",
776 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
777 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
778 "service_function_parameters": {"correlation": "nsh"},
779 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
780 "id": "c121ebdd-7f2d-4213-b933-3325298a6966",
781 "name": "osm_sfi1",
782 },
783 {
784 "ingress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
785 "description": "",
786 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
787 "egress": "5311c75d-d718-4369-bbda-cdcc6da60fcc",
788 "service_function_parameters": {"correlation": "nsh"},
789 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
790 "id": "c0436d92-82db-11e7-8f9c-5fa535f1261f",
791 "name": "osm_sfi2",
792 },
793 ]
794 }
795
796 # call the VIM connector
797 self.assertRaises(
798 vimconn.VimConnConflictException,
799 self.vimconn.get_sfi,
800 "c0436d92-82db-11e7-8f9c-5fa535f1261f",
801 )
802
803 # assert that VIM connector called OpenStack with the expected filter
804 list_sfc_port_pairs.assert_called_with(
805 id="c0436d92-82db-11e7-8f9c-5fa535f1261f"
806 )
807
808 @mock.patch.object(Client, "list_sfc_port_pairs")
809 def test_get_sfi_no_results(self, list_sfc_port_pairs):
810 # what OpenStack is assumed to return to the VIM connector
811 list_sfc_port_pairs.return_value = {"port_pairs": []}
812
813 # call the VIM connector
814 self.assertRaises(
815 vimconn.VimConnNotFoundException,
816 self.vimconn.get_sfi,
817 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
818 )
819
820 # assert that VIM connector called OpenStack with the expected filter
821 list_sfc_port_pairs.assert_called_with(
822 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
823 )
824
825 @mock.patch.object(Client, "list_sfc_port_pair_groups")
826 def test_get_sf(self, list_sfc_port_pair_groups):
827 # what OpenStack is assumed to return to the VIM connector
828 list_sfc_port_pair_groups.return_value = {
829 "port_pair_groups": [
830 {
831 "port_pairs": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
832 "description": "",
833 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
834 "port_pair_group_parameters": {},
835 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
836 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
837 "name": "osm_sf1",
838 }
839 ]
840 }
841
842 # call the VIM connector
843 result = self.vimconn.get_sf("b22892fc-82d9-11e7-ae85-0fea6a3b3757")
844
845 # assert that VIM connector called OpenStack with the expected filter
846 list_sfc_port_pair_groups.assert_called_with(
847 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
848 )
849 # assert that VIM connector successfully returned the OpenStack result
850 self.assertEqual(
851 result,
852 {
853 "description": "",
854 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
855 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
856 "sfis": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
857 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
858 "name": "osm_sf1",
859 },
860 )
861
862 @mock.patch.object(Client, "list_sfc_port_pair_groups")
863 def test_get_sf_many_results(self, list_sfc_port_pair_groups):
864 # what OpenStack is assumed to return to the VIM connector
865 list_sfc_port_pair_groups.return_value = {
866 "port_pair_groups": [
867 {
868 "port_pairs": ["08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2"],
869 "description": "",
870 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
871 "port_pair_group_parameters": {},
872 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
873 "id": "aabba8a6-82d9-11e7-a18a-d3c7719b742d",
874 "name": "osm_sf1",
875 },
876 {
877 "port_pairs": ["0d63799c-82d6-11e7-8deb-a746bb3ae9f5"],
878 "description": "",
879 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
880 "port_pair_group_parameters": {},
881 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
882 "id": "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
883 "name": "osm_sf2",
884 },
885 ]
886 }
887
888 # call the VIM connector
889 self.assertRaises(
890 vimconn.VimConnConflictException,
891 self.vimconn.get_sf,
892 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
893 )
894
895 # assert that VIM connector called OpenStack with the expected filter
896 list_sfc_port_pair_groups.assert_called_with(
897 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
898 )
899
900 @mock.patch.object(Client, "list_sfc_port_pair_groups")
901 def test_get_sf_no_results(self, list_sfc_port_pair_groups):
902 # what OpenStack is assumed to return to the VIM connector
903 list_sfc_port_pair_groups.return_value = {"port_pair_groups": []}
904
905 # call the VIM connector
906 self.assertRaises(
907 vimconn.VimConnNotFoundException,
908 self.vimconn.get_sf,
909 "b22892fc-82d9-11e7-ae85-0fea6a3b3757",
910 )
911
912 # assert that VIM connector called OpenStack with the expected filter
913 list_sfc_port_pair_groups.assert_called_with(
914 id="b22892fc-82d9-11e7-ae85-0fea6a3b3757"
915 )
916
917 @mock.patch.object(Client, "list_sfc_port_chains")
918 def test_get_sfp(self, list_sfc_port_chains):
919 # what OpenStack is assumed to return to the VIM connector
920 list_sfc_port_chains.return_value = {
921 "port_chains": [
922 {
923 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
924 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
925 "description": "",
926 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
927 "chain_parameters": {"correlation": "nsh"},
928 "chain_id": 40,
929 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
930 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
931 "name": "osm_sfp1",
932 }
933 ]
934 }
935
936 # call the VIM connector
937 result = self.vimconn.get_sfp("821bc9be-82d7-11e7-8ce3-23a08a27ab47")
938
939 # assert that VIM connector called OpenStack with the expected filter
940 list_sfc_port_chains.assert_called_with(
941 id="821bc9be-82d7-11e7-8ce3-23a08a27ab47"
942 )
943 # assert that VIM connector successfully returned the OpenStack result
944 self.assertEqual(
945 result,
946 {
947 "service_functions": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
948 "classifications": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
949 "description": "",
950 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
951 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
952 "sfc_encap": True,
953 "spi": 40,
954 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
955 "name": "osm_sfp1",
956 },
957 )
958
959 @mock.patch.object(Client, "list_sfc_port_chains")
960 def test_get_sfp_many_results(self, list_sfc_port_chains):
961 # what OpenStack is assumed to return to the VIM connector
962 list_sfc_port_chains.return_value = {
963 "port_chains": [
964 {
965 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
966 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
967 "description": "",
968 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
969 "chain_parameters": {"correlation": "nsh"},
970 "chain_id": 40,
971 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
972 "id": "821bc9be-82d7-11e7-8ce3-23a08a27ab47",
973 "name": "osm_sfp1",
974 },
975 {
976 "port_pair_groups": ["7d8e3bf8-82d6-11e7-a032-8ff028839d25"],
977 "flow_classifiers": ["1333c2f4-82d7-11e7-a5df-9327f33d104e"],
978 "description": "",
979 "tenant_id": "8f3019ef06374fa880a0144ad4bc1d7b",
980 "chain_parameters": {"correlation": "nsh"},
981 "chain_id": 50,
982 "project_id": "8f3019ef06374fa880a0144ad4bc1d7b",
983 "id": "5d002f38-82de-11e7-a770-f303f11ce66a",
984 "name": "osm_sfp2",
985 },
986 ]
987 }
988
989 # call the VIM connector
990 self.assertRaises(
991 vimconn.VimConnConflictException,
992 self.vimconn.get_sfp,
993 "5d002f38-82de-11e7-a770-f303f11ce66a",
994 )
995
996 # assert that VIM connector called OpenStack with the expected filter
997 list_sfc_port_chains.assert_called_with(
998 id="5d002f38-82de-11e7-a770-f303f11ce66a"
999 )
1000
1001 @mock.patch.object(Client, "list_sfc_port_chains")
1002 def test_get_sfp_no_results(self, list_sfc_port_chains):
1003 # what OpenStack is assumed to return to the VIM connector
1004 list_sfc_port_chains.return_value = {"port_chains": []}
1005
1006 # call the VIM connector
1007 self.assertRaises(
1008 vimconn.VimConnNotFoundException,
1009 self.vimconn.get_sfp,
1010 "5d002f38-82de-11e7-a770-f303f11ce66a",
1011 )
1012
1013 # assert that VIM connector called OpenStack with the expected filter
1014 list_sfc_port_chains.assert_called_with(
1015 id="5d002f38-82de-11e7-a770-f303f11ce66a"
1016 )
1017
1018 @mock.patch.object(Client, "delete_sfc_flow_classifier")
1019 def test_delete_classification(self, delete_sfc_flow_classifier):
1020 result = self.vimconn.delete_classification(
1021 "638f957c-82df-11e7-b7c8-132706021464"
1022 )
1023 delete_sfc_flow_classifier.assert_called_with(
1024 "638f957c-82df-11e7-b7c8-132706021464"
1025 )
1026 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1027
1028 @mock.patch.object(Client, "delete_sfc_port_pair")
1029 def test_delete_sfi(self, delete_sfc_port_pair):
1030 result = self.vimconn.delete_sfi("638f957c-82df-11e7-b7c8-132706021464")
1031 delete_sfc_port_pair.assert_called_with("638f957c-82df-11e7-b7c8-132706021464")
1032 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1033
1034 @mock.patch.object(Client, "delete_sfc_port_pair_group")
1035 def test_delete_sf(self, delete_sfc_port_pair_group):
1036 result = self.vimconn.delete_sf("638f957c-82df-11e7-b7c8-132706021464")
1037 delete_sfc_port_pair_group.assert_called_with(
1038 "638f957c-82df-11e7-b7c8-132706021464"
1039 )
1040 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1041
1042 @mock.patch.object(Client, "delete_sfc_port_chain")
1043 def test_delete_sfp(self, delete_sfc_port_chain):
1044 result = self.vimconn.delete_sfp("638f957c-82df-11e7-b7c8-132706021464")
1045 delete_sfc_port_chain.assert_called_with("638f957c-82df-11e7-b7c8-132706021464")
1046 self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464")
1047
1048
1049 if __name__ == "__main__":
1050 unittest.main()