Adapts PLA to new SOL006 NSD descriptors format
[osm/PLA.git] / osm_pla / test / test_server.py
index 56cf212..9d7582f 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import asyncio
-# import platform
-# import random
+import copy
 import os
 import sys
-# import unittest
 from unittest import TestCase, mock
 from unittest.mock import Mock
 
-# import pkg_resources
 import yaml
 
 from osm_pla.placement.mznplacement import NsPlacementDataFactory, MznPlacementConductor
@@ -197,9 +194,7 @@ list_of_vims = [{"_id": "73cd1a1b-333e-4e29-8db2-00d23bd9b644", "vim_user": "adm
                  "schema_version": "1.1", "config": {}}]
 
 # FIXME this is not correct re mgmt-network setting.
-nsd_from_db = {"_id": "15fc1941-f095-4cd8-af2d-1000bd6d9eaa", "short-name": "three_vnf_constrained_nsd_low",
-               "name": "three_vnf_constrained_nsd_low", "version": "1.0",
-               "description": "Placement constraints NSD",
+nsd_from_db = {"_id": "15fc1941-f095-4cd8-af2d-1000bd6d9eaa",
                "_admin": {"modified": 1567672251.7531693,
                           "storage": {"pkg-dir": "ns_constrained_nsd", "fs": "local",
                                       "descriptor": "ns_constrained_nsd/ns_constrained_nsd.yaml",
@@ -209,33 +204,62 @@ nsd_from_db = {"_id": "15fc1941-f095-4cd8-af2d-1000bd6d9eaa", "short-name": "thr
                           "projects_write": ["0a5d0c5b-7e08-48a1-a686-642a038bbd70"], "operationalState": "ENABLED",
                           "userDefinedData": {}, "created": 1567672251.7531693,
                           "projects_read": ["0a5d0c5b-7e08-48a1-a686-642a038bbd70"]},
-               "constituent-vnfd": [{"vnfd-id-ref": "cirros_vnfd_v2", "member-vnf-index": "one"},
-                                    {"vnfd-id-ref": "cirros_vnfd_v2", "member-vnf-index": "two"},
-                                    {"vnfd-id-ref": "cirros_vnfd_v2", "member-vnf-index": "three"}],
-               "id": "three_vnf_constrained_nsd_low", "vendor": "ArctosLabs",
-               "vld": [{"type": "ELAN", "short-name": "ns_constrained_nsd_low_vld1",
-                        "link-constraint": [{"constraint-type": "LATENCY", "value": "100"},
-                                            {"constraint-type": "JITTER", "value": "30"}],
-                        "vim-network-name": "external", "mgmt-network": True,
-                        "id": "three_vnf_constrained_nsd_low_vld1",
-                        "vnfd-connection-point-ref": [
-                            {"vnfd-id-ref": "cirros_vnfd_v2", "member-vnf-index-ref": "one",
-                             "vnfd-connection-point-ref": "vnf-cp0"},
-                            {"vnfd-id-ref": "cirros_vnfd_v2",
-                             "member-vnf-index-ref": "two",
-                             "vnfd-connection-point-ref": "vnf-cp0"}],
-                        "name": "ns_constrained_nsd_vld1"},
-                       {"type": "ELAN", "short-name": "ns_constrained_nsd_low_vld2",
-                        "link-constraint": [{"constraint-type": "LATENCY", "value": "50"},
-                                            {"constraint-type": "JITTER", "value": "30"}],
-                        "vim-network-name": "lanretxe", "mgmt-network": True,
-                        "id": "three_vnf_constrained_nsd_low_vld2",
-                        "vnfd-connection-point-ref": [
-                            {"vnfd-id-ref": "cirros_vnfd_v2", "member-vnf-index-ref": "two",
-                             "vnfd-connection-point-ref": "vnf-cp0"},
-                            {"vnfd-id-ref": "cirros_vnfd_v2", "member-vnf-index-ref": "three",
-                             "vnfd-connection-point-ref": "vnf-cp0"}],
-                        "name": "ns_constrained_nsd_vld2"}]}
+               'id': 'three_vnf_constrained_nsd_low',
+               'name': 'three_vnf_constrained_nsd_low',
+               'description': 'Placement constraints NSD',
+               'designer': 'ArctosLabs',
+               'version': '1.0',
+               'vnfd-id': ['cirros_vnfd_v2'],
+               'df': [{
+                   'id': 'default-df',
+                   'vnf-profile': [{
+                       'id': 'one',
+                       'vnfd-id': 'cirros_vnfd_v2',
+                       'virtual-link-connectivity': [{
+                           'virtual-link-profile-id': 'three_vnf_constrained_nsd_low_vld1',
+                           'constituent-cpd-id': [{
+                               'constituent-base-element-id': 'one',
+                               'constituent-cpd-id': 'vnf-cp0-ext'
+                           }]
+                       }]
+                   }, {
+                       'id': 'two',
+                       'vnfd-id': 'cirros_vnfd_v2',
+                       'virtual-link-connectivity': [{
+                           'virtual-link-profile-id': 'three_vnf_constrained_nsd_low_vld1',
+                           'constituent-cpd-id': [{
+                               'constituent-base-element-id': 'two',
+                               'constituent-cpd-id': 'vnf-cp0-ext'
+                           }]
+                       }, {
+                           'virtual-link-profile-id': 'three_vnf_constrained_nsd_low_vld2',
+                           'constituent-cpd-id': [{
+                               'constituent-base-element-id': 'two',
+                               'constituent-cpd-id': 'vnf-cp0-ext'
+                           }]
+                       }]
+                   }, {
+                       'id': 'three',
+                       'vnfd-id': 'cirros_vnfd_v2',
+                       'virtual-link-connectivity': [{
+                           'virtual-link-profile-id': 'three_vnf_constrained_nsd_low_vld2',
+                           'constituent-cpd-id': [{
+                               'constituent-base-element-id': 'three',
+                               'constituent-cpd-id': 'vnf-cp0-ext'
+                           }]
+                       }]
+                   }]
+               }],
+               'virtual-link-desc': [{
+                   'id': 'three_vnf_constrained_nsd_low_vld1',
+                   'mgmt-network': True,
+                   'vim-network-name': 'external'
+               }, {
+                   'id': 'three_vnf_constrained_nsd_low_vld2',
+                   'mgmt-network': True,
+                   'vim-network-name': 'lanretxe'
+               }],
+            }
 
 
 ######################################################
@@ -264,13 +288,13 @@ class TestServer(TestCase):
         FIXME temporary, we will need more control over vim_urls and _id for test purpose - make a generator
         :return: vim_url and _id as dict, i.e. extract these from vim_accounts data
         """
-        return {_['vim_url']: _['_id'] for _ in list_of_vims}
+        return {_['name']: _['_id'] for _ in list_of_vims}
 
     def _produce_ut_vnf_price_list(self):
         price_list_file = "vnf_price_list.yaml"
         with open(str(Path(price_list_file))) as pl_fd:
             price_list_data = yaml.safe_load_all(pl_fd)
-            return {i['vnfd']: {i1['vim_url']: i1['price'] for i1 in i['prices']} for i in next(price_list_data)}
+            return {i['vnfd']: {i1['vim_name']: i1['price'] for i1 in i['prices']} for i in next(price_list_data)}
 
     def _populate_pil_info(self, file):
         """
@@ -313,6 +337,18 @@ class TestServer(TestCase):
         _ = server._get_nsd(nslcmop_record_wo_pinning['operationParams']['nsdId'])
         server.db.get_one.assert_called_with("nsds", {'_id': nslcmop_record_wo_pinning['operationParams']['nsdId']})
 
+    def test__create_vnf_id_maps(self):
+        server = self.serverSetup()
+        server.db = Mock()
+        expected_mvi2mzn = {'one': 'VNF0', 'two': 'VNF1', 'three': 'VNF2'}
+        expected_mzn2mvi = {'VNF0': 'one', 'VNF1': 'two', 'VNF2': 'three'}
+
+        nsd_for_test = copy.deepcopy(nsd_from_db)
+        mvi2mzn, mzn2mvi = server._create_vnf_id_maps(nsd_for_test)
+
+        self.assertDictEqual(expected_mvi2mzn, mvi2mzn, 'Faulty mzn2member-vnf-index mapping')
+        self.assertDictEqual(expected_mzn2mvi, mzn2mvi, 'Faulty mzn2member-vnf-index mapping')
+
     def test__get_vim_accounts(self):  # OK
         server = self.serverSetup()
         server.db = Mock()
@@ -322,10 +358,16 @@ class TestServer(TestCase):
 
     def test__get_vnf_price_list(self):
         server = self.serverSetup()
-        pl = server._get_vnf_price_list(Path(self._adjust_path('./vnf_price_list.yaml')))
-        self.assertIs(type(pl), dict, "price list not a dictionary")
-        for k, v in pl.items():
+        pl1 = server._get_vnf_price_list(Path(self._adjust_path('./vnf_price_list.yaml')))
+        self.assertIs(type(pl1), dict, "price list not a dictionary")
+        for k, v in pl1.items():
+            self.assertIs(type(v), dict, "price list values not a dict")
+
+        pl2 = server._get_vnf_price_list(Path(self._adjust_path('./vnf_price_list_keys.yaml')), 'hackfest_project_a')
+        self.assertIs(type(pl2), dict, "price list not a dictionary")
+        for k, v in pl2.items():
             self.assertIs(type(v), dict, "price list values not a dict")
+        self.assertEqual(pl1, pl2, "non-project and project price lists differ")
 
     def test__get_pil_info(self):
         server = self.serverSetup()
@@ -356,7 +398,9 @@ class TestServer(TestCase):
     @mock.patch.object(Server, '_get_nslcmop')
     @mock.patch.object(Server, '_get_vnf_price_list')
     @mock.patch.object(Server, '_get_pil_info')
-    def test_get_placement(self, mock_get_pil_info, mock_get_vnf_price_list, mock__get_nslcmop, mock__get_nsd,
+    @mock.patch.object(Server, '_get_projects')
+    def test_get_placement(self, mock_get_projects, mock_get_pil_info, mock_get_vnf_price_list, mock__get_nslcmop,
+                           mock__get_nsd,
                            mock__get_vim_accounts,
                            mock_create_ns_placement_data,
                            mock_do_placement_computation):
@@ -364,19 +408,21 @@ class TestServer(TestCase):
         run _get_placement and check that things get called as expected
         :return:
         """
-        placement_ret_val = [{'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'one'},
-                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'two'},
-                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'three'}]
+        placement_ret_val = [{'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'VNF0'},
+                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'VNF1'},
+                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'VNF2'}]
         server = self.serverSetup()
 
         server.msgBus.aiowrite = _async_mock()
-        mock__get_nsd.return_value = nsd_from_db
+        nsd_for_test = copy.deepcopy(nsd_from_db)
+        mock__get_nsd.return_value = nsd_for_test
         mock__get_vim_accounts.return_value = list_of_vims
 
         # FIXME need update to match nslcmop, not for test but for consistency
         mock_do_placement_computation.return_value = placement_ret_val
         _run(server.get_placement(nslcmop_record_wo_pinning['id']))
 
+        self.assertTrue(mock_get_projects.called, '_get_projects not called as expected')
         self.assertTrue(mock_get_vnf_price_list.called, '_get_vnf_price_list not called as expected')
         self.assertTrue(mock_get_pil_info.called, '_get_pil_info not called as expected')
         self.assertTrue(mock__get_nslcmop.called, '_get_nslcmop not called as expected')
@@ -422,7 +468,9 @@ class TestServer(TestCase):
     @mock.patch.object(Server, '_get_nslcmop')
     @mock.patch.object(Server, '_get_vnf_price_list')
     @mock.patch.object(Server, '_get_pil_info')
-    def test_get_placement_with_pinning(self, mock_get_pil_info, mock_get_vnf_price_list, mock__get_nslcmop,
+    @mock.patch.object(Server, '_get_projects')
+    def test_get_placement_with_pinning(self, mock_get_projects, mock_get_pil_info, mock_get_vnf_price_list,
+                                        mock__get_nslcmop,
                                         mock__get_nsd, mock__get_vim_accounts,
                                         mock_create_ns_placement_data,
                                         mock_do_placement_computation):
@@ -430,19 +478,21 @@ class TestServer(TestCase):
         run _get_placement and check that things get called as expected
         :return:
         """
-        placement_ret_val = [{'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'one'},
-                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'two'},
-                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'three'}]
+        placement_ret_val = [{'vimAccountId': 'bbbbbbbb-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'VNF0'},
+                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'VNF1'},
+                             {'vimAccountId': 'aaaaaaaa-38f5-438d-b8ee-3f93b3531f87', 'member-vnf-index': 'VNF2'}]
         server = self.serverSetup()
 
         server.msgBus.aiowrite = _async_mock()
-        mock__get_nsd.return_value = nsd_from_db
+        nsd_for_test = copy.deepcopy(nsd_from_db)
+        mock__get_nsd.return_value = nsd_for_test
         mock__get_vim_accounts.return_value = list_of_vims
 
         # FIXME need update to match nslcmop, not for test but for consistency
         mock_do_placement_computation.return_value = placement_ret_val
         _run(server.get_placement(nslcmop_record_w_pinning['id']))
 
+        self.assertTrue((mock_get_projects.called, '_get_projects not called as expected'))
         self.assertTrue(mock_get_vnf_price_list.called, '_get_vnf_price_list not called as expected')
         self.assertTrue(mock_get_pil_info.called, '_get_pil_info not called as expected')
         self.assertTrue(mock__get_nslcmop.called, '_get_nslcmop not called as expected')
@@ -498,7 +548,8 @@ class TestServer(TestCase):
         server = self.serverSetup()
 
         server.msgBus.aiowrite = _async_mock()
-        mock__get_nsd.return_value = nsd_from_db
+        nsd_for_test = copy.deepcopy(nsd_from_db)
+        mock__get_nsd.return_value = nsd_for_test
         mock__get_nsd.side_effect = RuntimeError('kaboom!')
         mock__get_vim_accounts.return_value = list_of_vims
         mock_do_placement_computation.return_value = \