--- /dev/null
+# -*- coding: utf-8 -*-
+
+##
+# Copyright 2017 Intel Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact with: nfvlabs@tid.es
+##
+
+"""
+This module contains unit tests for the OpenStack VIM connector
+Run this directly with python2 or python3.
+"""
+
+import copy
+import unittest
+
+import mock
+from neutronclient.v2_0.client import Client
+
+from osm_ro import vimconn
+from osm_ro.vimconn_openstack import vimconnector
+
+
+__author__ = "Igor D.C."
+__date__ = "$23-aug-2017 23:59:59$"
+
+
+class TestSfcOperations(unittest.TestCase):
+ def setUp(self):
+ # instantiate dummy VIM connector so we can test it
+ self.vimconn = vimconnector(
+ '123', 'openstackvim', '456', '789', 'http://dummy.url', None,
+ 'user', 'pass')
+
+ def _test_new_sfi(self, create_port_pair, sfc_encap,
+ ingress_ports=['5311c75d-d718-4369-bbda-cdcc6da60fcc'],
+ egress_ports=['230cdf1b-de37-4891-bc07-f9010cf1f967']):
+ # input to VIM connector
+ name = 'osm_sfi'
+ # + ingress_ports
+ # + egress_ports
+ # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
+ correlation = 'mpls'
+ if sfc_encap is not None:
+ if not sfc_encap:
+ correlation = None
+
+ # what OpenStack is assumed to respond (patch OpenStack's return value)
+ dict_from_neutron = {'port_pair': {
+ 'id': '3d7ddc13-923c-4332-971e-708ed82902ce',
+ 'name': name,
+ 'description': '',
+ 'tenant_id': '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c',
+ 'project_id': '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c',
+ 'ingress': ingress_ports[0] if len(ingress_ports) else None,
+ 'egress': egress_ports[0] if len(egress_ports) else None,
+ 'service_function_parameters': {'correlation': correlation}
+ }}
+ create_port_pair.return_value = dict_from_neutron
+
+ # what the VIM connector is expected to
+ # send to OpenStack based on the input
+ dict_to_neutron = {'port_pair': {
+ 'name': name,
+ 'ingress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'egress': '230cdf1b-de37-4891-bc07-f9010cf1f967',
+ 'service_function_parameters': {'correlation': correlation}
+ }}
+
+ # call the VIM connector
+ if sfc_encap is None:
+ result = self.vimconn.new_sfi(name, ingress_ports, egress_ports)
+ else:
+ result = self.vimconn.new_sfi(name, ingress_ports, egress_ports,
+ sfc_encap)
+
+ # assert that the VIM connector made the expected call to OpenStack
+ create_port_pair.assert_called_with(dict_to_neutron)
+ # assert that the VIM connector had the expected result / return value
+ self.assertEqual(result, dict_from_neutron['port_pair']['id'])
+
+ def _test_new_sf(self, create_port_pair_group):
+ # input to VIM connector
+ name = 'osm_sf'
+ instances = ['bbd01220-cf72-41f2-9e70-0669c2e5c4cd',
+ '12ba215e-3987-4892-bd3a-d0fd91eecf98',
+ 'e25a7c79-14c8-469a-9ae1-f601c9371ffd']
+
+ # what OpenStack is assumed to respond (patch OpenStack's return value)
+ dict_from_neutron = {'port_pair_group': {
+ 'id': '3d7ddc13-923c-4332-971e-708ed82902ce',
+ 'name': name,
+ 'description': '',
+ 'tenant_id': '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c',
+ 'project_id': '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c',
+ 'port_pairs': instances,
+ 'group_id': 1,
+ 'port_pair_group_parameters': {
+ "lb_fields": [],
+ "ppg_n_tuple_mapping": {
+ "ingress_n_tuple": {},
+ "egress_n_tuple": {}
+ }}
+ }}
+ create_port_pair_group.return_value = dict_from_neutron
+
+ # what the VIM connector is expected to
+ # send to OpenStack based on the input
+ dict_to_neutron = {'port_pair_group': {
+ 'name': name,
+ 'port_pairs': ['bbd01220-cf72-41f2-9e70-0669c2e5c4cd',
+ '12ba215e-3987-4892-bd3a-d0fd91eecf98',
+ 'e25a7c79-14c8-469a-9ae1-f601c9371ffd']
+ }}
+
+ # call the VIM connector
+ result = self.vimconn.new_sf(name, instances)
+
+ # assert that the VIM connector made the expected call to OpenStack
+ create_port_pair_group.assert_called_with(dict_to_neutron)
+ # assert that the VIM connector had the expected result / return value
+ self.assertEqual(result, dict_from_neutron['port_pair_group']['id'])
+
+ def _test_new_sfp(self, create_port_chain, sfc_encap, spi):
+ # input to VIM connector
+ name = 'osm_sfp'
+ classifications = ['2bd2a2e5-c5fd-4eac-a297-d5e255c35c19',
+ '00f23389-bdfa-43c2-8b16-5815f2582fa8']
+ sfs = ['2314daec-c262-414a-86e3-69bb6fa5bc16',
+ 'd8bfdb5d-195e-4f34-81aa-6135705317df']
+
+ # TODO(igordc): must be changed to NSH in Queens (MPLS is a workaround)
+ correlation = 'mpls'
+ chain_id = 33
+ if sfc_encap is not None:
+ if not sfc_encap:
+ correlation = None
+ if spi:
+ chain_id = spi
+
+ # what OpenStack is assumed to respond (patch OpenStack's return value)
+ dict_from_neutron = {'port_chain': {
+ 'id': '5bc05721-079b-4b6e-a235-47cac331cbb6',
+ 'name': name,
+ 'description': '',
+ 'tenant_id': '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c',
+ 'project_id': '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c',
+ 'chain_id': chain_id,
+ 'flow_classifiers': classifications,
+ 'port_pair_groups': sfs,
+ 'chain_parameters': {'correlation': correlation}
+ }}
+ create_port_chain.return_value = dict_from_neutron
+
+ # what the VIM connector is expected to
+ # send to OpenStack based on the input
+ dict_to_neutron = {'port_chain': {
+ 'name': name,
+ 'flow_classifiers': ['2bd2a2e5-c5fd-4eac-a297-d5e255c35c19',
+ '00f23389-bdfa-43c2-8b16-5815f2582fa8'],
+ 'port_pair_groups': ['2314daec-c262-414a-86e3-69bb6fa5bc16',
+ 'd8bfdb5d-195e-4f34-81aa-6135705317df'],
+ 'chain_parameters': {'correlation': correlation}
+ }}
+ if spi:
+ dict_to_neutron['port_chain']['chain_id'] = spi
+
+ # call the VIM connector
+ if sfc_encap is None:
+ if spi is None:
+ result = self.vimconn.new_sfp(name, classifications, sfs)
+ else:
+ result = self.vimconn.new_sfp(name, classifications, sfs,
+ spi=spi)
+ else:
+ if spi is None:
+ result = self.vimconn.new_sfp(name, classifications, sfs,
+ sfc_encap)
+ else:
+ result = self.vimconn.new_sfp(name, classifications, sfs,
+ sfc_encap, spi)
+
+ # assert that the VIM connector made the expected call to OpenStack
+ create_port_chain.assert_called_with(dict_to_neutron)
+ # assert that the VIM connector had the expected result / return value
+ self.assertEqual(result, dict_from_neutron['port_chain']['id'])
+
+ def _test_new_classification(self, create_flow_classifier, ctype):
+ # input to VIM connector
+ name = 'osm_classification'
+ definition = {'ethertype': 'IPv4',
+ 'logical_source_port':
+ 'aaab0ab0-1452-4636-bb3b-11dca833fa2b',
+ 'protocol': 'tcp',
+ 'source_ip_prefix': '192.168.2.0/24',
+ 'source_port_range_max': 99,
+ 'source_port_range_min': 50}
+
+ # what OpenStack is assumed to respond (patch OpenStack's return value)
+ dict_from_neutron = {'flow_classifier': copy.copy(definition)}
+ dict_from_neutron['flow_classifier'][
+ 'id'] = '7735ec2c-fddf-4130-9712-32ed2ab6a372'
+ dict_from_neutron['flow_classifier']['name'] = name
+ dict_from_neutron['flow_classifier']['description'] = ''
+ dict_from_neutron['flow_classifier'][
+ 'tenant_id'] = '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c'
+ dict_from_neutron['flow_classifier'][
+ 'project_id'] = '130b1e97-b0f1-40a8-8804-b6ad9b8c3e0c'
+ create_flow_classifier.return_value = dict_from_neutron
+
+ # what the VIM connector is expected to
+ # send to OpenStack based on the input
+ dict_to_neutron = {'flow_classifier': copy.copy(definition)}
+ dict_to_neutron['flow_classifier']['name'] = 'osm_classification'
+
+ # call the VIM connector
+ result = self.vimconn.new_classification(name, ctype, definition)
+
+ # assert that the VIM connector made the expected call to OpenStack
+ create_flow_classifier.assert_called_with(dict_to_neutron)
+ # assert that the VIM connector had the expected result / return value
+ self.assertEqual(result, dict_from_neutron['flow_classifier']['id'])
+
+ @mock.patch.object(Client, 'create_flow_classifier')
+ def test_new_classification(self, create_flow_classifier):
+ self._test_new_classification(create_flow_classifier,
+ 'legacy_flow_classifier')
+
+ @mock.patch.object(Client, 'create_flow_classifier')
+ def test_new_classification_unsupported_type(self, create_flow_classifier):
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_classification,
+ create_flow_classifier, 'h265')
+
+ @mock.patch.object(Client, 'create_port_pair')
+ def test_new_sfi_with_sfc_encap(self, create_port_pair):
+ self._test_new_sfi(create_port_pair, True)
+
+ @mock.patch.object(Client, 'create_port_pair')
+ def test_new_sfi_without_sfc_encap(self, create_port_pair):
+ self._test_new_sfi(create_port_pair, False)
+
+ @mock.patch.object(Client, 'create_port_pair')
+ def test_new_sfi_default_sfc_encap(self, create_port_pair):
+ self._test_new_sfi(create_port_pair, None)
+
+ @mock.patch.object(Client, 'create_port_pair')
+ def test_new_sfi_bad_ingress_ports(self, create_port_pair):
+ ingress_ports = ['5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'a0273f64-82c9-11e7-b08f-6328e53f0fa7']
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_sfi,
+ create_port_pair, True, ingress_ports=ingress_ports)
+ ingress_ports = []
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_sfi,
+ create_port_pair, True, ingress_ports=ingress_ports)
+
+ @mock.patch.object(Client, 'create_port_pair')
+ def test_new_sfi_bad_egress_ports(self, create_port_pair):
+ egress_ports = ['230cdf1b-de37-4891-bc07-f9010cf1f967',
+ 'b41228fe-82c9-11e7-9b44-17504174320b']
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_sfi,
+ create_port_pair, True, egress_ports=egress_ports)
+ egress_ports = []
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_sfi,
+ create_port_pair, True, egress_ports=egress_ports)
+
+ @mock.patch.object(vimconnector, 'get_sfi')
+ @mock.patch.object(Client, 'create_port_pair_group')
+ def test_new_sf(self, create_port_pair_group, get_sfi):
+ get_sfi.return_value = {'sfc_encap': 'mpls'}
+ self._test_new_sf(create_port_pair_group)
+
+ @mock.patch.object(vimconnector, 'get_sfi')
+ @mock.patch.object(Client, 'create_port_pair_group')
+ def test_new_sf_inconsistent_sfc_encap(self, create_port_pair_group,
+ get_sfi):
+ get_sfi.return_value = {'sfc_encap': 'nsh'}
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_sf, create_port_pair_group)
+
+ @mock.patch.object(Client, 'create_port_chain')
+ def test_new_sfp_with_sfc_encap(self, create_port_chain):
+ self._test_new_sfp(create_port_chain, True, None)
+
+ @mock.patch.object(Client, 'create_port_chain')
+ def test_new_sfp_without_sfc_encap(self, create_port_chain):
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_sfp,
+ create_port_chain, False, None)
+ self.assertRaises(vimconn.vimconnNotSupportedException,
+ self._test_new_sfp,
+ create_port_chain, False, 25)
+
+ @mock.patch.object(Client, 'create_port_chain')
+ def test_new_sfp_default_sfc_encap(self, create_port_chain):
+ self._test_new_sfp(create_port_chain, None, None)
+
+ @mock.patch.object(Client, 'create_port_chain')
+ def test_new_sfp_with_sfc_encap_spi(self, create_port_chain):
+ self._test_new_sfp(create_port_chain, True, 25)
+
+ @mock.patch.object(Client, 'create_port_chain')
+ def test_new_sfp_default_sfc_encap_spi(self, create_port_chain):
+ self._test_new_sfp(create_port_chain, None, 25)
+
+ @mock.patch.object(Client, 'list_flow_classifier')
+ def test_get_classification_list(self, list_flow_classifier):
+ # what OpenStack is assumed to return to the VIM connector
+ list_flow_classifier.return_value = {'flow_classifiers': [
+ {'source_port_range_min': 2000,
+ 'destination_ip_prefix': '192.168.3.0/24',
+ 'protocol': 'udp',
+ 'description': '',
+ 'ethertype': 'IPv4',
+ 'l7_parameters': {},
+ 'source_port_range_max': 2000,
+ 'destination_port_range_min': 3000,
+ 'source_ip_prefix': '192.168.2.0/24',
+ 'logical_destination_port': None,
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'destination_port_range_max': None,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'logical_source_port': 'aaab0ab0-1452-4636-bb3b-11dca833fa2b',
+ 'id': '22198366-d4e8-4d6b-b4d2-637d5d6cbb7d',
+ 'name': 'fc1'}]}
+
+ # call the VIM connector
+ filter_dict = {'protocol': 'tcp', 'ethertype': 'IPv4'}
+ result = self.vimconn.get_classification_list(filter_dict.copy())
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_flow_classifier.assert_called_with(**filter_dict)
+ # assert that the VIM connector successfully
+ # translated and returned the OpenStack result
+ self.assertEqual(result, [
+ {'id': '22198366-d4e8-4d6b-b4d2-637d5d6cbb7d',
+ 'name': 'fc1',
+ 'description': '',
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'ctype': 'legacy_flow_classifier',
+ 'definition': {
+ 'source_port_range_min': 2000,
+ 'destination_ip_prefix': '192.168.3.0/24',
+ 'protocol': 'udp',
+ 'ethertype': 'IPv4',
+ 'l7_parameters': {},
+ 'source_port_range_max': 2000,
+ 'destination_port_range_min': 3000,
+ 'source_ip_prefix': '192.168.2.0/24',
+ 'logical_destination_port': None,
+ 'destination_port_range_max': None,
+ 'logical_source_port': 'aaab0ab0-1452-4636-bb3b-11dca833fa2b'}
+ }])
+
+ def _test_get_sfi_list(self, list_port_pair, correlation, sfc_encap):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair.return_value = {'port_pairs': [
+ {'ingress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'egress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'service_function_parameters': {'correlation': correlation},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'c121ebdd-7f2d-4213-b933-3325298a6966',
+ 'name': 'osm_sfi'}]}
+
+ # call the VIM connector
+ filter_dict = {'name': 'osm_sfi', 'description': ''}
+ result = self.vimconn.get_sfi_list(filter_dict.copy())
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_pair.assert_called_with(**filter_dict)
+ # assert that the VIM connector successfully
+ # translated and returned the OpenStack result
+ self.assertEqual(result, [
+ {'ingress_ports': ['5311c75d-d718-4369-bbda-cdcc6da60fcc'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'egress_ports': ['5311c75d-d718-4369-bbda-cdcc6da60fcc'],
+ 'sfc_encap': sfc_encap,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'c121ebdd-7f2d-4213-b933-3325298a6966',
+ 'name': 'osm_sfi'}])
+
+ @mock.patch.object(Client, 'list_port_pair')
+ def test_get_sfi_list_with_sfc_encap(self, list_port_pair):
+ self._test_get_sfi_list(list_port_pair, 'nsh', True)
+
+ @mock.patch.object(Client, 'list_port_pair')
+ def test_get_sfi_list_without_sfc_encap(self, list_port_pair):
+ self._test_get_sfi_list(list_port_pair, None, False)
+
+ @mock.patch.object(Client, 'list_port_pair_group')
+ def test_get_sf_list(self, list_port_pair_group):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair_group.return_value = {'port_pair_groups': [
+ {'port_pairs': ['08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2',
+ '0d63799c-82d6-11e7-8deb-a746bb3ae9f5'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'port_pair_group_parameters': {},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'f4a0bde8-82d5-11e7-90e1-a72b762fa27f',
+ 'name': 'osm_sf'}]}
+
+ # call the VIM connector
+ filter_dict = {'name': 'osm_sf', 'description': ''}
+ result = self.vimconn.get_sf_list(filter_dict.copy())
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_pair_group.assert_called_with(**filter_dict)
+ # assert that the VIM connector successfully
+ # translated and returned the OpenStack result
+ self.assertEqual(result, [
+ {'instances': ['08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2',
+ '0d63799c-82d6-11e7-8deb-a746bb3ae9f5'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'f4a0bde8-82d5-11e7-90e1-a72b762fa27f',
+ 'name': 'osm_sf'}])
+
+ def _test_get_sfp_list(self, list_port_chain, correlation, sfc_encap):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_chain.return_value = {'port_chains': [
+ {'port_pair_groups': ['7d8e3bf8-82d6-11e7-a032-8ff028839d25',
+ '7dc9013e-82d6-11e7-a5a6-a3a8d78a5518'],
+ 'flow_classifiers': ['1333c2f4-82d7-11e7-a5df-9327f33d104e',
+ '1387ab44-82d7-11e7-9bb0-476337183905'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'chain_parameters': {'correlation': correlation},
+ 'chain_id': 40,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': '821bc9be-82d7-11e7-8ce3-23a08a27ab47',
+ 'name': 'osm_sfp'}]}
+
+ # call the VIM connector
+ filter_dict = {'name': 'osm_sfp', 'description': ''}
+ result = self.vimconn.get_sfp_list(filter_dict.copy())
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_chain.assert_called_with(**filter_dict)
+ # assert that the VIM connector successfully
+ # translated and returned the OpenStack result
+ self.assertEqual(result, [
+ {'service_functions': ['7d8e3bf8-82d6-11e7-a032-8ff028839d25',
+ '7dc9013e-82d6-11e7-a5a6-a3a8d78a5518'],
+ 'classifications': ['1333c2f4-82d7-11e7-a5df-9327f33d104e',
+ '1387ab44-82d7-11e7-9bb0-476337183905'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'sfc_encap': sfc_encap,
+ 'spi': 40,
+ 'id': '821bc9be-82d7-11e7-8ce3-23a08a27ab47',
+ 'name': 'osm_sfp'}])
+
+ @mock.patch.object(Client, 'list_port_chain')
+ def test_get_sfp_list_with_sfc_encap(self, list_port_chain):
+ self._test_get_sfp_list(list_port_chain, 'nsh', True)
+
+ @mock.patch.object(Client, 'list_port_chain')
+ def test_get_sfp_list_without_sfc_encap(self, list_port_chain):
+ self._test_get_sfp_list(list_port_chain, None, False)
+
+ @mock.patch.object(Client, 'list_flow_classifier')
+ def test_get_classification(self, list_flow_classifier):
+ # what OpenStack is assumed to return to the VIM connector
+ list_flow_classifier.return_value = {'flow_classifiers': [
+ {'source_port_range_min': 2000,
+ 'destination_ip_prefix': '192.168.3.0/24',
+ 'protocol': 'udp',
+ 'description': '',
+ 'ethertype': 'IPv4',
+ 'l7_parameters': {},
+ 'source_port_range_max': 2000,
+ 'destination_port_range_min': 3000,
+ 'source_ip_prefix': '192.168.2.0/24',
+ 'logical_destination_port': None,
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'destination_port_range_max': None,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'logical_source_port': 'aaab0ab0-1452-4636-bb3b-11dca833fa2b',
+ 'id': '22198366-d4e8-4d6b-b4d2-637d5d6cbb7d',
+ 'name': 'fc1'}
+ ]}
+
+ # call the VIM connector
+ result = self.vimconn.get_classification(
+ '22198366-d4e8-4d6b-b4d2-637d5d6cbb7d')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_flow_classifier.assert_called_with(
+ id='22198366-d4e8-4d6b-b4d2-637d5d6cbb7d')
+ # assert that VIM connector successfully returned the OpenStack result
+ self.assertEqual(result,
+ {'id': '22198366-d4e8-4d6b-b4d2-637d5d6cbb7d',
+ 'name': 'fc1',
+ 'description': '',
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'ctype': 'legacy_flow_classifier',
+ 'definition': {
+ 'source_port_range_min': 2000,
+ 'destination_ip_prefix': '192.168.3.0/24',
+ 'protocol': 'udp',
+ 'ethertype': 'IPv4',
+ 'l7_parameters': {},
+ 'source_port_range_max': 2000,
+ 'destination_port_range_min': 3000,
+ 'source_ip_prefix': '192.168.2.0/24',
+ 'logical_destination_port': None,
+ 'destination_port_range_max': None,
+ 'logical_source_port':
+ 'aaab0ab0-1452-4636-bb3b-11dca833fa2b'}
+ })
+
+ @mock.patch.object(Client, 'list_flow_classifier')
+ def test_get_classification_many_results(self, list_flow_classifier):
+ # what OpenStack is assumed to return to the VIM connector
+ list_flow_classifier.return_value = {'flow_classifiers': [
+ {'source_port_range_min': 2000,
+ 'destination_ip_prefix': '192.168.3.0/24',
+ 'protocol': 'udp',
+ 'description': '',
+ 'ethertype': 'IPv4',
+ 'l7_parameters': {},
+ 'source_port_range_max': 2000,
+ 'destination_port_range_min': 3000,
+ 'source_ip_prefix': '192.168.2.0/24',
+ 'logical_destination_port': None,
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'destination_port_range_max': None,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'logical_source_port': 'aaab0ab0-1452-4636-bb3b-11dca833fa2b',
+ 'id': '22198366-d4e8-4d6b-b4d2-637d5d6cbb7d',
+ 'name': 'fc1'},
+ {'source_port_range_min': 1000,
+ 'destination_ip_prefix': '192.168.3.0/24',
+ 'protocol': 'udp',
+ 'description': '',
+ 'ethertype': 'IPv4',
+ 'l7_parameters': {},
+ 'source_port_range_max': 1000,
+ 'destination_port_range_min': 3000,
+ 'source_ip_prefix': '192.168.2.0/24',
+ 'logical_destination_port': None,
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'destination_port_range_max': None,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'logical_source_port': 'aaab0ab0-1452-4636-bb3b-11dca833fa2b',
+ 'id': '3196bafc-82dd-11e7-a205-9bf6c14b0721',
+ 'name': 'fc2'}
+ ]}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnConflictException,
+ self.vimconn.get_classification,
+ '3196bafc-82dd-11e7-a205-9bf6c14b0721')
+
+ # assert the VIM connector called OpenStack with the expected filter
+ list_flow_classifier.assert_called_with(
+ id='3196bafc-82dd-11e7-a205-9bf6c14b0721')
+
+ @mock.patch.object(Client, 'list_flow_classifier')
+ def test_get_classification_no_results(self, list_flow_classifier):
+ # what OpenStack is assumed to return to the VIM connector
+ list_flow_classifier.return_value = {'flow_classifiers': []}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnNotFoundException,
+ self.vimconn.get_classification,
+ '3196bafc-82dd-11e7-a205-9bf6c14b0721')
+
+ # assert the VIM connector called OpenStack with the expected filter
+ list_flow_classifier.assert_called_with(
+ id='3196bafc-82dd-11e7-a205-9bf6c14b0721')
+
+ @mock.patch.object(Client, 'list_port_pair')
+ def test_get_sfi(self, list_port_pair):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair.return_value = {'port_pairs': [
+ {'ingress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'egress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'service_function_parameters': {'correlation': 'nsh'},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'c121ebdd-7f2d-4213-b933-3325298a6966',
+ 'name': 'osm_sfi1'},
+ ]}
+
+ # call the VIM connector
+ result = self.vimconn.get_sfi('c121ebdd-7f2d-4213-b933-3325298a6966')
+
+ # assert the VIM connector called OpenStack with the expected filter
+ list_port_pair.assert_called_with(
+ id='c121ebdd-7f2d-4213-b933-3325298a6966')
+ # assert the VIM connector successfully returned the OpenStack result
+ self.assertEqual(result,
+ {'ingress_ports': [
+ '5311c75d-d718-4369-bbda-cdcc6da60fcc'],
+ 'egress_ports': [
+ '5311c75d-d718-4369-bbda-cdcc6da60fcc'],
+ 'sfc_encap': True,
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'c121ebdd-7f2d-4213-b933-3325298a6966',
+ 'name': 'osm_sfi1'})
+
+ @mock.patch.object(Client, 'list_port_pair')
+ def test_get_sfi_many_results(self, list_port_pair):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair.return_value = {'port_pairs': [
+ {'ingress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'egress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'service_function_parameters': {'correlation': 'nsh'},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'c121ebdd-7f2d-4213-b933-3325298a6966',
+ 'name': 'osm_sfi1'},
+ {'ingress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'egress': '5311c75d-d718-4369-bbda-cdcc6da60fcc',
+ 'service_function_parameters': {'correlation': 'nsh'},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'c0436d92-82db-11e7-8f9c-5fa535f1261f',
+ 'name': 'osm_sfi2'}
+ ]}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnConflictException,
+ self.vimconn.get_sfi,
+ 'c0436d92-82db-11e7-8f9c-5fa535f1261f')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_pair.assert_called_with(
+ id='c0436d92-82db-11e7-8f9c-5fa535f1261f')
+
+ @mock.patch.object(Client, 'list_port_pair')
+ def test_get_sfi_no_results(self, list_port_pair):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair.return_value = {'port_pairs': []}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnNotFoundException,
+ self.vimconn.get_sfi,
+ 'b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_pair.assert_called_with(
+ id='b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+
+ @mock.patch.object(Client, 'list_port_pair_group')
+ def test_get_sf(self, list_port_pair_group):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair_group.return_value = {'port_pair_groups': [
+ {'port_pairs': ['08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'port_pair_group_parameters': {},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'aabba8a6-82d9-11e7-a18a-d3c7719b742d',
+ 'name': 'osm_sf1'}
+ ]}
+
+ # call the VIM connector
+ result = self.vimconn.get_sf('b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_pair_group.assert_called_with(
+ id='b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+ # assert that VIM connector successfully returned the OpenStack result
+ self.assertEqual(result,
+ {'instances': [
+ '08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'aabba8a6-82d9-11e7-a18a-d3c7719b742d',
+ 'name': 'osm_sf1'})
+
+ @mock.patch.object(Client, 'list_port_pair_group')
+ def test_get_sf_many_results(self, list_port_pair_group):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair_group.return_value = {'port_pair_groups': [
+ {'port_pairs': ['08fbdbb0-82d6-11e7-ad95-9bb52fbec2f2'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'port_pair_group_parameters': {},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'aabba8a6-82d9-11e7-a18a-d3c7719b742d',
+ 'name': 'osm_sf1'},
+ {'port_pairs': ['0d63799c-82d6-11e7-8deb-a746bb3ae9f5'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'port_pair_group_parameters': {},
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': 'b22892fc-82d9-11e7-ae85-0fea6a3b3757',
+ 'name': 'osm_sf2'}
+ ]}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnConflictException,
+ self.vimconn.get_sf,
+ 'b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_pair_group.assert_called_with(
+ id='b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+
+ @mock.patch.object(Client, 'list_port_pair_group')
+ def test_get_sf_no_results(self, list_port_pair_group):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_pair_group.return_value = {'port_pair_groups': []}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnNotFoundException,
+ self.vimconn.get_sf,
+ 'b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_pair_group.assert_called_with(
+ id='b22892fc-82d9-11e7-ae85-0fea6a3b3757')
+
+ @mock.patch.object(Client, 'list_port_chain')
+ def test_get_sfp(self, list_port_chain):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_chain.return_value = {'port_chains': [
+ {'port_pair_groups': ['7d8e3bf8-82d6-11e7-a032-8ff028839d25'],
+ 'flow_classifiers': ['1333c2f4-82d7-11e7-a5df-9327f33d104e'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'chain_parameters': {'correlation': 'nsh'},
+ 'chain_id': 40,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': '821bc9be-82d7-11e7-8ce3-23a08a27ab47',
+ 'name': 'osm_sfp1'}]}
+
+ # call the VIM connector
+ result = self.vimconn.get_sfp('821bc9be-82d7-11e7-8ce3-23a08a27ab47')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_chain.assert_called_with(
+ id='821bc9be-82d7-11e7-8ce3-23a08a27ab47')
+ # assert that VIM connector successfully returned the OpenStack result
+ self.assertEqual(result,
+ {'service_functions': [
+ '7d8e3bf8-82d6-11e7-a032-8ff028839d25'],
+ 'classifications': [
+ '1333c2f4-82d7-11e7-a5df-9327f33d104e'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'sfc_encap': True,
+ 'spi': 40,
+ 'id': '821bc9be-82d7-11e7-8ce3-23a08a27ab47',
+ 'name': 'osm_sfp1'})
+
+ @mock.patch.object(Client, 'list_port_chain')
+ def test_get_sfp_many_results(self, list_port_chain):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_chain.return_value = {'port_chains': [
+ {'port_pair_groups': ['7d8e3bf8-82d6-11e7-a032-8ff028839d25'],
+ 'flow_classifiers': ['1333c2f4-82d7-11e7-a5df-9327f33d104e'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'chain_parameters': {'correlation': 'nsh'},
+ 'chain_id': 40,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': '821bc9be-82d7-11e7-8ce3-23a08a27ab47',
+ 'name': 'osm_sfp1'},
+ {'port_pair_groups': ['7d8e3bf8-82d6-11e7-a032-8ff028839d25'],
+ 'flow_classifiers': ['1333c2f4-82d7-11e7-a5df-9327f33d104e'],
+ 'description': '',
+ 'tenant_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'chain_parameters': {'correlation': 'nsh'},
+ 'chain_id': 50,
+ 'project_id': '8f3019ef06374fa880a0144ad4bc1d7b',
+ 'id': '5d002f38-82de-11e7-a770-f303f11ce66a',
+ 'name': 'osm_sfp2'}
+ ]}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnConflictException,
+ self.vimconn.get_sfp,
+ '5d002f38-82de-11e7-a770-f303f11ce66a')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_chain.assert_called_with(
+ id='5d002f38-82de-11e7-a770-f303f11ce66a')
+
+ @mock.patch.object(Client, 'list_port_chain')
+ def test_get_sfp_no_results(self, list_port_chain):
+ # what OpenStack is assumed to return to the VIM connector
+ list_port_chain.return_value = {'port_chains': []}
+
+ # call the VIM connector
+ self.assertRaises(vimconn.vimconnNotFoundException,
+ self.vimconn.get_sfp,
+ '5d002f38-82de-11e7-a770-f303f11ce66a')
+
+ # assert that VIM connector called OpenStack with the expected filter
+ list_port_chain.assert_called_with(
+ id='5d002f38-82de-11e7-a770-f303f11ce66a')
+
+ @mock.patch.object(Client, 'delete_flow_classifier')
+ def test_delete_classification(self, delete_flow_classifier):
+ result = self.vimconn.delete_classification(
+ '638f957c-82df-11e7-b7c8-132706021464')
+ delete_flow_classifier.assert_called_with(
+ '638f957c-82df-11e7-b7c8-132706021464')
+ self.assertEqual(result, '638f957c-82df-11e7-b7c8-132706021464')
+
+ @mock.patch.object(Client, 'delete_port_pair')
+ def test_delete_sfi(self, delete_port_pair):
+ result = self.vimconn.delete_sfi(
+ '638f957c-82df-11e7-b7c8-132706021464')
+ delete_port_pair.assert_called_with(
+ '638f957c-82df-11e7-b7c8-132706021464')
+ self.assertEqual(result, '638f957c-82df-11e7-b7c8-132706021464')
+
+ @mock.patch.object(Client, 'delete_port_pair_group')
+ def test_delete_sf(self, delete_port_pair_group):
+ result = self.vimconn.delete_sf('638f957c-82df-11e7-b7c8-132706021464')
+ delete_port_pair_group.assert_called_with(
+ '638f957c-82df-11e7-b7c8-132706021464')
+ self.assertEqual(result, '638f957c-82df-11e7-b7c8-132706021464')
+
+ @mock.patch.object(Client, 'delete_port_chain')
+ def test_delete_sfp(self, delete_port_chain):
+ result = self.vimconn.delete_sfp(
+ '638f957c-82df-11e7-b7c8-132706021464')
+ delete_port_chain.assert_called_with(
+ '638f957c-82df-11e7-b7c8-132706021464')
+ self.assertEqual(result, '638f957c-82df-11e7-b7c8-132706021464')
+
+
+if __name__ == '__main__':
+ unittest.main()
##
'''
-osconnector implements all the methods to interact with openstack using the python-client.
+osconnector implements all the methods to interact with openstack using the python-neutronclient.
+
+For the VNF forwarding graph, The OpenStack VIM connector calls the
+networking-sfc Neutron extension methods, whose resources are mapped
+to the VIM connector's SFC resources as follows:
+- Classification (OSM) -> Flow Classifier (Neutron)
+- Service Function Instance (OSM) -> Port Pair (Neutron)
+- Service Function (OSM) -> Port Pair Group (Neutron)
+- Service Function Path (OSM) -> Port Chain (Neutron)
'''
-__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research"
-__date__ ="$22-jun-2014 11:19:29$"
+__author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C."
+__date__ = "$22-sep-2017 23:59:59$"
import vimconn
import json
-import yaml
import logging
import netaddr
import time
import random
import sys
import re
+import copy
from novaclient import client as nClient, exceptions as nvExceptions
from keystoneauth1.identity import v2, v3
netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
}
+supportedClassificationTypes = ['legacy_flow_classifier']
+
#global var to have a timeout creating and deleting volumes
volume_timeout = 60
server_timeout = 300
self.insecure = self.config.get("insecure", False)
if not url:
- raise TypeError, 'url param can not be NoneType'
+ raise TypeError('url param can not be NoneType')
self.persistent_info = persistent_info
self.availability_zone = persistent_info.get('availability_zone', None)
self.session = persistent_info.get('session', {'reload_client': True})
else:
net['type']='bridge'
+ def __classification_os2mano(self, class_list_dict):
+ """Transform the openstack format (Flow Classifier) to mano format
+ (Classification) class_list_dict can be a list of dict or a single dict
+ """
+ if isinstance(class_list_dict, dict):
+ class_list_ = [class_list_dict]
+ elif isinstance(class_list_dict, list):
+ class_list_ = class_list_dict
+ else:
+ raise TypeError(
+ "param class_list_dict must be a list or a dictionary")
+ for classification in class_list_:
+ id = classification.pop('id')
+ name = classification.pop('name')
+ description = classification.pop('description')
+ project_id = classification.pop('project_id')
+ tenant_id = classification.pop('tenant_id')
+ original_classification = copy.deepcopy(classification)
+ classification.clear()
+ classification['ctype'] = 'legacy_flow_classifier'
+ classification['definition'] = original_classification
+ classification['id'] = id
+ classification['name'] = name
+ classification['description'] = description
+ classification['project_id'] = project_id
+ classification['tenant_id'] = tenant_id
+
+ def __sfi_os2mano(self, sfi_list_dict):
+ """Transform the openstack format (Port Pair) to mano format (SFI)
+ sfi_list_dict can be a list of dict or a single dict
+ """
+ if isinstance(sfi_list_dict, dict):
+ sfi_list_ = [sfi_list_dict]
+ elif isinstance(sfi_list_dict, list):
+ sfi_list_ = sfi_list_dict
+ else:
+ raise TypeError(
+ "param sfi_list_dict must be a list or a dictionary")
+ for sfi in sfi_list_:
+ sfi['ingress_ports'] = []
+ sfi['egress_ports'] = []
+ if sfi.get('ingress'):
+ sfi['ingress_ports'].append(sfi['ingress'])
+ if sfi.get('egress'):
+ sfi['egress_ports'].append(sfi['egress'])
+ del sfi['ingress']
+ del sfi['egress']
+ params = sfi.get('service_function_parameters')
+ sfc_encap = False
+ if params:
+ correlation = params.get('correlation')
+ if correlation:
+ sfc_encap = True
+ sfi['sfc_encap'] = sfc_encap
+ del sfi['service_function_parameters']
+
+ def __sf_os2mano(self, sf_list_dict):
+ """Transform the openstack format (Port Pair Group) to mano format (SF)
+ sf_list_dict can be a list of dict or a single dict
+ """
+ if isinstance(sf_list_dict, dict):
+ sf_list_ = [sf_list_dict]
+ elif isinstance(sf_list_dict, list):
+ sf_list_ = sf_list_dict
+ else:
+ raise TypeError(
+ "param sf_list_dict must be a list or a dictionary")
+ for sf in sf_list_:
+ del sf['port_pair_group_parameters']
+ sf['sfis'] = sf['port_pairs']
+ del sf['port_pairs']
+
+ def __sfp_os2mano(self, sfp_list_dict):
+ """Transform the openstack format (Port Chain) to mano format (SFP)
+ sfp_list_dict can be a list of dict or a single dict
+ """
+ if isinstance(sfp_list_dict, dict):
+ sfp_list_ = [sfp_list_dict]
+ elif isinstance(sfp_list_dict, list):
+ sfp_list_ = sfp_list_dict
+ else:
+ raise TypeError(
+ "param sfp_list_dict must be a list or a dictionary")
+ for sfp in sfp_list_:
+ params = sfp.pop('chain_parameters')
+ sfc_encap = False
+ if params:
+ correlation = params.get('correlation')
+ if correlation:
+ sfc_encap = True
+ sfp['sfc_encap'] = sfc_encap
+ sfp['spi'] = sfp.pop('chain_id')
+ sfp['classifications'] = sfp.pop('flow_classifiers')
+ sfp['service_functions'] = sfp.pop('port_pair_groups')
+
+ # placeholder for now; read TODO note below
+ def _validate_classification(self, type, definition):
+ # only legacy_flow_classifier Type is supported at this point
+ return True
+ # TODO(igordcard): this method should be an abstract method of an
+ # abstract Classification class to be implemented by the specific
+ # Types. Also, abstract vimconnector should call the validation
+ # method before the implemented VIM connectors are called.
+
def _format_exception(self, exception):
'''Transform a keystone, nova, neutron exception into a vimconn exception'''
if isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed
)):
- raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
- elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
+ raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
+ elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
neExceptions.NeutronException, nvExceptions.BadRequest)):
raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception))
elif isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound)):
#Fake subnet is required
subnet_rand = random.randint(0, 255)
ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
- if 'ip_version' not in ip_profile:
+ if 'ip_version' not in ip_profile:
ip_profile['ip_version'] = "IPv4"
subnet = {"name":net_name+"-subnet",
"network_id": new_net["network"]["id"],
error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
- '''
+ '''
net_dict={}
for net_id in net_list:
net = {}
else:
net["status"] = "OTHER"
net["error_msg"] = "VIM status reported " + net_vim['status']
-
+
if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
net['status'] = 'DOWN'
try:
# if interface["dedicated"]=="yes":
# raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
# #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
-
+
#create flavor
- new_flavor=self.nova.flavors.create(name,
- ram,
- vcpus,
+ new_flavor=self.nova.flavors.create(name,
+ ram,
+ vcpus,
flavor_data.get('disk',1),
is_public=flavor_data.get('is_public', True)
)
except IOError as e: #can not open the file
raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
http_code=vimconn.HTTP_Bad_Request)
-
+
def delete_image(self, image_id):
'''Deletes a tenant image from openstack VIM. Returns the old id
'''
self._format_exception(e)
def get_image_id_from_path(self, path):
- '''Get the image id from image path in the VIM database. Returns the image_id'''
+ '''Get the image id from image path in the VIM database. Returns the image_id'''
try:
self._reload_connection()
images = self.nova.images.list()
raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
self._format_exception(e)
-
+
def get_image_list(self, filter_dict={}):
'''Obtain tenant images from VIM
Filter_dict can be:
#metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
metadata = {}
-
+
self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
name, image_id, flavor_id, str(net_list_vim), description, str(metadata))
-
+
security_groups = self.config.get('security_groups')
if type(security_groups) is str:
security_groups = ( security_groups, )
console_dict = server.get_spice_console(console_type)
else:
raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
-
+
console_dict1 = console_dict.get("console")
if console_dict1:
console_url = console_dict1.get("url")
if protocol_index < 0 or port_index<0 or suffix_index<0:
return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
console_dict={"protocol": console_url[0:protocol_index],
- "server": console_url[protocol_index+2:port_index],
- "port": console_url[port_index:suffix_index],
- "suffix": console_url[suffix_index+1:]
+ "server": console_url[protocol_index+2:port_index],
+ "port": console_url[port_index:suffix_index],
+ "suffix": console_url[suffix_index+1:]
}
protocol_index += 2
return console_dict
raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
-
+
except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
self._format_exception(e)
vm['error_msg'] = str(e)
vm_dict[vm_id] = vm
return vm_dict
-
+
def action_vminstance(self, vm_id, action_dict):
'''Send and action over a VM instance from VIM
Returns the vm_id if the action was successfully sent to the VIM'''
self._reload_connection()
server = self.nova.servers.find(id=vm_id)
if "start" in action_dict:
- if action_dict["start"]=="rebuild":
+ if action_dict["start"]=="rebuild":
server.rebuild()
else:
if server.status=="PAUSED":
elif console_type == "spice-html5":
console_dict = server.get_spice_console(console_type)
else:
- raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
+ raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
http_code=vimconn.HTTP_Bad_Request)
try:
console_url = console_dict["console"]["url"]
if protocol_index < 0 or port_index<0 or suffix_index<0:
raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
console_dict2={"protocol": console_url[0:protocol_index],
- "server": console_url[protocol_index+2 : port_index],
- "port": int(console_url[port_index+1 : suffix_index]),
- "suffix": console_url[suffix_index+1:]
+ "server": console_url[protocol_index+2 : port_index],
+ "port": int(console_url[port_index+1 : suffix_index]),
+ "suffix": console_url[suffix_index+1:]
}
- return console_dict2
+ return console_dict2
except Exception as e:
raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
-
+
return vm_id
except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
self._format_exception(e)
"start_ID < end_ID ".format(vlanID_range))
#NOT USED FUNCTIONS
-
+
def new_external_port(self, port_data):
#TODO openstack if needed
'''Adds a external port to VIM'''
'''Returns the port identifier'''
- return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
-
+ return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
+
def connect_port_network(self, port_id, network_id, admin=False):
#TODO openstack if needed
'''Connects a external port to a network'''
'''Returns status code of the VIM response'''
- return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
-
+ return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
+
def new_user(self, user_name, user_passwd, tenant_id=None):
'''Adds a new user to openstack VIM'''
'''Returns the user identifier'''
#if reaching here is because an exception
if self.debug:
self.logger.debug("new_user " + error_text)
- return error_value, error_text
+ return error_value, error_text
def delete_user(self, user_id):
'''Delete a user from openstack VIM'''
'''Returns the user identifier'''
if self.debug:
- print "osconnector: Deleting a user from VIM"
+ print("osconnector: Deleting a user from VIM")
try:
self._reload_connection()
self.keystone.users.delete(user_id)
#TODO insert exception vimconn.HTTP_Unauthorized
#if reaching here is because an exception
if self.debug:
- print "delete_tenant " + error_text
+ print("delete_tenant " + error_text)
return error_value, error_text
-
+
def get_hosts_info(self):
'''Get the information of deployed hosts
Returns the hosts content'''
if self.debug:
- print "osconnector: Getting Host info from VIM"
+ print("osconnector: Getting Host info from VIM")
try:
h_list=[]
self._reload_connection()
#TODO insert exception vimconn.HTTP_Unauthorized
#if reaching here is because an exception
if self.debug:
- print "get_hosts_info " + error_text
- return error_value, error_text
+ print("get_hosts_info " + error_text)
+ return error_value, error_text
def get_hosts(self, vim_tenant):
'''Get the hosts and deployed instances
#TODO insert exception vimconn.HTTP_Unauthorized
#if reaching here is because an exception
if self.debug:
- print "get_hosts " + error_text
- return error_value, error_text
-
+ print("get_hosts " + error_text)
+ return error_value, error_text
+ def new_classification(self, name, ctype, definition):
+ self.logger.debug(
+ 'Adding a new (Traffic) Classification to VIM, named %s', name)
+ try:
+ new_class = None
+ self._reload_connection()
+ if ctype not in supportedClassificationTypes:
+ raise vimconn.vimconnNotSupportedException(
+ 'OpenStack VIM connector doesn\'t support provided '
+ 'Classification Type {}, supported ones are: '
+ '{}'.format(ctype, supportedClassificationTypes))
+ if not self._validate_classification(ctype, definition):
+ raise vimconn.vimconnException(
+ 'Incorrect Classification definition '
+ 'for the type specified.')
+ classification_dict = definition
+ classification_dict['name'] = name
+
+ new_class = self.neutron.create_flow_classifier(
+ {'flow_classifier': classification_dict})
+ return new_class['flow_classifier']['id']
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ self.logger.error(
+ 'Creation of Classification failed.')
+ self._format_exception(e)
+
+ def get_classification(self, class_id):
+ self.logger.debug(" Getting Classification %s from VIM", class_id)
+ filter_dict = {"id": class_id}
+ class_list = self.get_classification_list(filter_dict)
+ if len(class_list) == 0:
+ raise vimconn.vimconnNotFoundException(
+ "Classification '{}' not found".format(class_id))
+ elif len(class_list) > 1:
+ raise vimconn.vimconnConflictException(
+ "Found more than one Classification with this criteria")
+ classification = class_list[0]
+ return classification
+
+ def get_classification_list(self, filter_dict={}):
+ self.logger.debug("Getting Classifications from VIM filter: '%s'",
+ str(filter_dict))
+ try:
+ self._reload_connection()
+ if self.api_version3 and "tenant_id" in filter_dict:
+ filter_dict['project_id'] = filter_dict.pop('tenant_id')
+ classification_dict = self.neutron.list_flow_classifier(
+ **filter_dict)
+ classification_list = classification_dict["flow_classifiers"]
+ self.__classification_os2mano(classification_list)
+ return classification_list
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ self._format_exception(e)
+
+ def delete_classification(self, class_id):
+ self.logger.debug("Deleting Classification '%s' from VIM", class_id)
+ try:
+ self._reload_connection()
+ self.neutron.delete_flow_classifier(class_id)
+ return class_id
+ except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
+ ksExceptions.ClientException, neExceptions.NeutronException,
+ ConnectionError) as e:
+ self._format_exception(e)
+ def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
+ self.logger.debug(
+ "Adding a new Service Function Instance to VIM, named '%s'", name)
+ try:
+ new_sfi = None
+ self._reload_connection()
+ correlation = None
+ if sfc_encap:
+ # TODO(igordc): must be changed to NSH in Queens
+ # (MPLS is a workaround)
+ correlation = 'mpls'
+ if len(ingress_ports) != 1:
+ raise vimconn.vimconnNotSupportedException(
+ "OpenStack VIM connector can only have "
+ "1 ingress port per SFI")
+ if len(egress_ports) != 1:
+ raise vimconn.vimconnNotSupportedException(
+ "OpenStack VIM connector can only have "
+ "1 egress port per SFI")
+ sfi_dict = {'name': name,
+ 'ingress': ingress_ports[0],
+ 'egress': egress_ports[0],
+ 'service_function_parameters': {
+ 'correlation': correlation}}
+ new_sfi = self.neutron.create_port_pair({'port_pair': sfi_dict})
+ return new_sfi['port_pair']['id']
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ if new_sfi:
+ try:
+ self.neutron.delete_port_pair_group(
+ new_sfi['port_pair']['id'])
+ except Exception:
+ self.logger.error(
+ 'Creation of Service Function Instance failed, with '
+ 'subsequent deletion failure as well.')
+ self._format_exception(e)
+
+ def get_sfi(self, sfi_id):
+ self.logger.debug(
+ 'Getting Service Function Instance %s from VIM', sfi_id)
+ filter_dict = {"id": sfi_id}
+ sfi_list = self.get_sfi_list(filter_dict)
+ if len(sfi_list) == 0:
+ raise vimconn.vimconnNotFoundException(
+ "Service Function Instance '{}' not found".format(sfi_id))
+ elif len(sfi_list) > 1:
+ raise vimconn.vimconnConflictException(
+ 'Found more than one Service Function Instance '
+ 'with this criteria')
+ sfi = sfi_list[0]
+ return sfi
+
+ def get_sfi_list(self, filter_dict={}):
+ self.logger.debug("Getting Service Function Instances from "
+ "VIM filter: '%s'", str(filter_dict))
+ try:
+ self._reload_connection()
+ if self.api_version3 and "tenant_id" in filter_dict:
+ filter_dict['project_id'] = filter_dict.pop('tenant_id')
+ sfi_dict = self.neutron.list_port_pair(**filter_dict)
+ sfi_list = sfi_dict["port_pairs"]
+ self.__sfi_os2mano(sfi_list)
+ return sfi_list
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ self._format_exception(e)
+
+ def delete_sfi(self, sfi_id):
+ self.logger.debug("Deleting Service Function Instance '%s' "
+ "from VIM", sfi_id)
+ try:
+ self._reload_connection()
+ self.neutron.delete_port_pair(sfi_id)
+ return sfi_id
+ except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
+ ksExceptions.ClientException, neExceptions.NeutronException,
+ ConnectionError) as e:
+ self._format_exception(e)
+
+ def new_sf(self, name, sfis, sfc_encap=True):
+ self.logger.debug("Adding a new Service Function to VIM, "
+ "named '%s'", name)
+ try:
+ new_sf = None
+ self._reload_connection()
+ correlation = None
+ if sfc_encap:
+ # TODO(igordc): must be changed to NSH in Queens
+ # (MPLS is a workaround)
+ correlation = 'mpls'
+ for instance in sfis:
+ sfi = self.get_sfi(instance)
+ if sfi.get('sfc_encap') != correlation:
+ raise vimconn.vimconnNotSupportedException(
+ "OpenStack VIM connector requires all SFIs of the "
+ "same SF to share the same SFC Encapsulation")
+ sf_dict = {'name': name,
+ 'port_pairs': sfis}
+ new_sf = self.neutron.create_port_pair_group({
+ 'port_pair_group': sf_dict})
+ return new_sf['port_pair_group']['id']
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ if new_sf:
+ try:
+ self.neutron.delete_port_pair_group(
+ new_sf['port_pair_group']['id'])
+ except Exception:
+ self.logger.error(
+ 'Creation of Service Function failed, with '
+ 'subsequent deletion failure as well.')
+ self._format_exception(e)
+
+ def get_sf(self, sf_id):
+ self.logger.debug("Getting Service Function %s from VIM", sf_id)
+ filter_dict = {"id": sf_id}
+ sf_list = self.get_sf_list(filter_dict)
+ if len(sf_list) == 0:
+ raise vimconn.vimconnNotFoundException(
+ "Service Function '{}' not found".format(sf_id))
+ elif len(sf_list) > 1:
+ raise vimconn.vimconnConflictException(
+ "Found more than one Service Function with this criteria")
+ sf = sf_list[0]
+ return sf
+
+ def get_sf_list(self, filter_dict={}):
+ self.logger.debug("Getting Service Function from VIM filter: '%s'",
+ str(filter_dict))
+ try:
+ self._reload_connection()
+ if self.api_version3 and "tenant_id" in filter_dict:
+ filter_dict['project_id'] = filter_dict.pop('tenant_id')
+ sf_dict = self.neutron.list_port_pair_group(**filter_dict)
+ sf_list = sf_dict["port_pair_groups"]
+ self.__sf_os2mano(sf_list)
+ return sf_list
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ self._format_exception(e)
+
+ def delete_sf(self, sf_id):
+ self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
+ try:
+ self._reload_connection()
+ self.neutron.delete_port_pair_group(sf_id)
+ return sf_id
+ except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
+ ksExceptions.ClientException, neExceptions.NeutronException,
+ ConnectionError) as e:
+ self._format_exception(e)
+
+ def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
+ self.logger.debug("Adding a new Service Function Path to VIM, "
+ "named '%s'", name)
+ try:
+ new_sfp = None
+ self._reload_connection()
+ if not sfc_encap:
+ raise vimconn.vimconnNotSupportedException(
+ "OpenStack VIM connector only supports "
+ "SFC-Encapsulated chains")
+ # TODO(igordc): must be changed to NSH in Queens
+ # (MPLS is a workaround)
+ correlation = 'mpls'
+ sfp_dict = {'name': name,
+ 'flow_classifiers': classifications,
+ 'port_pair_groups': sfs,
+ 'chain_parameters': {'correlation': correlation}}
+ if spi:
+ sfp_dict['chain_id'] = spi
+ new_sfp = self.neutron.create_port_chain({'port_chain': sfp_dict})
+ return new_sfp["port_chain"]["id"]
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ if new_sfp:
+ try:
+ self.neutron.delete_port_chain(new_sfp['port_chain']['id'])
+ except Exception:
+ self.logger.error(
+ 'Creation of Service Function Path failed, with '
+ 'subsequent deletion failure as well.')
+ self._format_exception(e)
+
+ def get_sfp(self, sfp_id):
+ self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
+ filter_dict = {"id": sfp_id}
+ sfp_list = self.get_sfp_list(filter_dict)
+ if len(sfp_list) == 0:
+ raise vimconn.vimconnNotFoundException(
+ "Service Function Path '{}' not found".format(sfp_id))
+ elif len(sfp_list) > 1:
+ raise vimconn.vimconnConflictException(
+ "Found more than one Service Function Path with this criteria")
+ sfp = sfp_list[0]
+ return sfp
+
+ def get_sfp_list(self, filter_dict={}):
+ self.logger.debug("Getting Service Function Paths from VIM filter: "
+ "'%s'", str(filter_dict))
+ try:
+ self._reload_connection()
+ if self.api_version3 and "tenant_id" in filter_dict:
+ filter_dict['project_id'] = filter_dict.pop('tenant_id')
+ sfp_dict = self.neutron.list_port_chain(**filter_dict)
+ sfp_list = sfp_dict["port_chains"]
+ self.__sfp_os2mano(sfp_list)
+ return sfp_list
+ except (neExceptions.ConnectionFailed, ksExceptions.ClientException,
+ neExceptions.NeutronException, ConnectionError) as e:
+ self._format_exception(e)
+
+ def delete_sfp(self, sfp_id):
+ self.logger.debug(
+ "Deleting Service Function Path '%s' from VIM", sfp_id)
+ try:
+ self._reload_connection()
+ self.neutron.delete_port_chain(sfp_id)
+ return sfp_id
+ except (neExceptions.ConnectionFailed, neExceptions.NeutronException,
+ ksExceptions.ClientException, neExceptions.NeutronException,
+ ConnectionError) as e:
+ self._format_exception(e)