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