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