Rm resmgr.grant releated contents
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / tests / tests.py
index c3d3008..a88e05d 100644 (file)
@@ -19,6 +19,7 @@ import mock
 from django.test import TestCase, Client
 from rest_framework import status
 
+from lcm.ns_vnfs.biz.grant_vnfs import GrantVnfs
 from lcm.pub.database.models import VLInstModel, NfInstModel, JobModel, NSInstModel, VmInstModel, \
     OOFDataModel, VNFCInstModel, PortInstModel, CPInstModel, SubscriptionModel
 from lcm.pub.exceptions import NSLCMException
@@ -32,15 +33,13 @@ from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
 from lcm.ns_vnfs.biz.scale_vnfs import NFManualScaleService
 from lcm.ns_vnfs.biz.subscribe import SubscriptionDeletion
 from lcm.ns_vnfs.biz.terminate_nfs import TerminateVnfs
-from lcm.ns_vnfs.enum import VNF_STATUS, LIFE_CYCLE_OPERATION, RESOURCE_CHANGE_TYPE, VNFC_CHANGE_TYPE, INST_TYPE, \
-    NETWORK_RESOURCE_TYPE
+from lcm.ns_vnfs.enum import VNF_STATUS, LIFE_CYCLE_OPERATION, RESOURCE_CHANGE_TYPE, VNFC_CHANGE_TYPE, \
+    INST_TYPE, NETWORK_RESOURCE_TYPE
 from lcm.ns_vnfs.biz.place_vnfs import PlaceVnfs
-from lcm.pub.msapi import resmgr
 from lcm.ns_vnfs.tests.test_data import vnfm_info, vim_info, vnf_place_request
 from lcm.ns_vnfs.tests.test_data import nf_package_info, nsd_model_dict, subscription_response_data
 from lcm.ns_vnfs.biz.create_vnfs import CreateVnfs
-from lcm.ns_vnfs.biz import create_vnfs
-from lcm.ns_vnfs.biz.grant_vnfs import GrantVnfs
+from lcm.ns_vnfs.biz import create_vnfs, grant_vnf
 from lcm.ns_vnfs.biz.update_vnfs import NFOperateService
 from lcm.ns_vnfs.biz.verify_vnfs import VerifyVnfs
 from lcm.ns.enum import OWNER_TYPE
@@ -866,7 +865,6 @@ class TestGrantVnfsViews(TestCase):
     #     }
     #     response = self.client.post(self.url, data=data)
     #     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
-
     @mock.patch.object(restcall, "call_req")
     def test_nf_grant_view_when_add_resource(self, mock_call_req):
         mock_vals = {
@@ -930,7 +928,7 @@ class TestGrantVnfViews(TestCase):
         OOFDataModel.objects.all().delete()
         NfInstModel.objects.all().delete()
 
-    @mock.patch.object(resmgr, "grant_vnf")
+    @mock.patch.object(grant_vnf, "vim_connections_get")
     def test_vnf_grant_view(self, mock_grant):
         resmgr_grant_resp = {
             "vim": {
@@ -949,7 +947,7 @@ class TestGrantVnfViews(TestCase):
         self.assertEqual(response.data["vimAssets"]["computeResourceFlavours"][0]["vimFlavourId"], "flavor_id_001")
 
     @mock.patch.object(restcall, "call_req")
-    @mock.patch.object(resmgr, "grant_vnf")
+    @mock.patch.object(grant_vnf, "vim_connections_get")
     def test_exec_grant_when_add_resources_success(self, mock_grant, mock_call_req):
         mock_vals = {
             "/api/catalog/v1/vnfpackages/package_id_001":
@@ -986,7 +984,7 @@ class TestGrantVnfViews(TestCase):
 
     @mock.patch.object(time, "sleep")
     @mock.patch.object(restcall, "call_req")
-    @mock.patch.object(resmgr, "grant_vnf")
+    @mock.patch.object(grant_vnf, "vim_connections_get")
     def test_exec_grant_when_add_resources_but_no_off(self, mock_grant, mock_call_req, mock_sleep):
         NfInstModel(mnfinstid="add_resources_but_no_off", nfinstid="vnf_inst_id_002",
                     package_id="package_id_002").save()
@@ -1023,7 +1021,7 @@ class TestGrantVnfViews(TestCase):
         }]
         self.assertEqual(resp["vimConnections"], vimConnections)
 
-    @mock.patch.object(resmgr, "grant_vnf")
+    @mock.patch.object(grant_vnf, "vim_connections_get")
     def test_exec_grant_when_resource_template_in_add_resources(self, mock_grant):
         resmgr_grant_resp = {
             "vim": {
@@ -1041,7 +1039,7 @@ class TestGrantVnfViews(TestCase):
         self.assertEqual(resp["vimAssets"]["computeResourceFlavours"][0]["vimFlavourId"], "flavor_id_001")
 
     @mock.patch.object(restcall, "call_req")
-    @mock.patch.object(resmgr, "grant_vnf")
+    @mock.patch.object(grant_vnf, "vim_connections_get")
     def test_exec_grant_when_remove_resources_success(self, mock_grant, mock_call_req):
         mock_vals = {
             "/api/catalog/v1/vnfpackages/package_id_001":
@@ -1083,7 +1081,7 @@ class TestGrantVnfViews(TestCase):
 
     @mock.patch.object(time, "sleep")
     @mock.patch.object(restcall, "call_req")
-    @mock.patch.object(resmgr, "grant_vnf")
+    @mock.patch.object(grant_vnf, "vim_connections_get")
     def test_exec_grant_when_remove_resources_but_no_off(self, mock_grant, mock_call_req, mock_sleep):
         NfInstModel(mnfinstid="remove_resources_but_no_off", nfinstid="vnf_inst_id_002", package_id="package_id_002",
                     vnfm_inst_id="vnfm_id_002").save()
@@ -1123,7 +1121,7 @@ class TestGrantVnfViews(TestCase):
         }]
         self.assertEqual(resp["vimConnections"], vimConnections)
 
-    @mock.patch.object(resmgr, "grant_vnf")
+    @mock.patch.object(grant_vnf, "vim_connections_get")
     def test_exec_grant_when_resource_template_in_remove_resources(self, mock_grant):
         resmgr_grant_resp = {
             "vim": {
@@ -2271,6 +2269,7 @@ class TestVnfNotifyView(TestCase):
                                                           "resourceHandle": {
                                                               "vimConnectionId": "vim_connection_id",
                                                               "resourceId": "resource_id",
+                                                              "resourceProviderId": "resourceProviderId",
                                                               "tenant": "tenant",
                                                               "ipAddress": "ipAddress",
                                                               "macAddress": "macAddress",
@@ -2314,7 +2313,7 @@ class TestVnfNotifyView(TestCase):
                                           relatednetworkid="resource_id", vltype=0)
         self.assertIsInstance(vl_inst, VLInstModel)
         port_inst = PortInstModel.objects.get(networkid='networkId', subnetworkid='subnetId', vimid="vim_connection_id",
-                                              resourceid="resource_id", name="resource_id", instid="instId",
+                                              resourceid="resource_id", name="resourceProviderId", instid="instId",
                                               cpinstanceid="cp_instance_id", bandwidth='unknown',
                                               operationalstate='active', ipaddress="ipAddress", macaddress='macAddress',
                                               floatipaddress='unknown', serviceipaddress='unknown',
@@ -2413,6 +2412,7 @@ class TestVnfNotifyView(TestCase):
                                                           "resourceHandle": {
                                                               "vimConnectionId": "vim_connection_id",
                                                               "resourceId": "resource_id",
+                                                              "resourceProviderId": "resourceProviderId",
                                                               "tenant": "tenant",
                                                               "ipAddress": "ipAddress",
                                                               "macAddress": "macAddress",
@@ -2432,7 +2432,7 @@ class TestVnfNotifyView(TestCase):
                                           relatednetworkid="resource_id", vltype=0)
         self.assertIsInstance(vl_inst, VLInstModel)
         port_inst = PortInstModel.objects.get(networkid='networkId', subnetworkid='subnetId', vimid="vim_connection_id",
-                                              resourceid="resource_id", name="resource_id", instid="instId",
+                                              resourceid="resource_id", name="resourceProviderId", instid="instId",
                                               cpinstanceid="cp_instance_id", bandwidth='unknown',
                                               operationalstate='active', ipaddress="ipAddress", macaddress='macAddress',
                                               floatipaddress='unknown', serviceipaddress='unknown',
@@ -2482,3 +2482,81 @@ class TestVnfNotifyView(TestCase):
             self.assertEqual(1, 0)
         except Exception:
             self.assertEqual(1, 1)
+
+    @mock.patch.object(restcall, "call_req")
+    def test_handle_vnf_identifier_notification_when_save_ip_aai(self, mock_call_req):
+        l_interface_info_aai = {
+            "interface-name": "resourceProviderId",
+            "is-port-mirrored": False,
+            "resource-version": "1589506153510",
+            "in-maint": False,
+            "is-ip-unnumbered": False
+        }
+        l3_interface_ipv4_address_list = {
+            "l3-interface-ipv4-address": "ipAddress",
+            "resource-version": "1589527363970"
+        }
+        mock_vals = {
+            "/network/generic-vnfs/generic-vnf/%s/l-interfaces/l-interface/%s"
+            % ("test_vnf_notify", "resourceProviderId"):
+                [0, json.JSONEncoder().encode(l_interface_info_aai), "200"],
+            "/network/generic-vnfs/generic-vnf/%s/l-interfaces/l-interface/%s/l3-interface-ipv4-address-list/%s"
+            % ("test_vnf_notify", "resourceProviderId", "ipAddress"):
+                [0, json.JSONEncoder().encode(l3_interface_ipv4_address_list), "200"],
+            "/network/l3-networks/l3-network/%s" % "vl_instance_id":
+                [0, json.JSONEncoder().encode({}), "200"],
+
+        }
+
+        def side_effect(*args):
+            return mock_vals[args[4]]
+
+        mock_call_req.side_effect = side_effect
+
+        data = {
+            "id": "1111",
+            "notificationType": "VnfLcmOperationOccurrenceNotification",
+            "subscriptionId": "1111",
+            "timeStamp": "1111",
+            "notificationStatus": "START",
+            "operationState": "STARTING",
+            "vnfInstanceId": self.nf_inst_id,
+            "operation": "INSTANTIATE",
+            "isAutomaticInvocation": "1111",
+            "vnfLcmOpOccId": "1111",
+            "affectedVnfcs": [{"id": "vnfc_instance_id",
+                               "vduId": "vdu_id",
+                               "changeType": VNFC_CHANGE_TYPE.MODIFIED,
+                               "computeResource": {
+                                   "vimConnectionId": "vim_connection_id",
+                                   "resourceId": "resource_id"
+                               }}],
+            "affectedVirtualLinks": [{"id": "vl_instance_id",
+                                      "virtualLinkDescId": "virtual_link_desc_id",
+                                      "changeType": VNFC_CHANGE_TYPE.MODIFIED,
+                                      "networkResource": {
+                                          "vimLevelResourceType": "network",
+                                          "resourceId": "resource_id"
+                                      }}],
+            "changedExtConnectivity": [{"id": "virtual_link_instance_id",
+                                        "extLinkPorts": [{"cpInstanceId": "cp_instance_id",
+                                                          "id": "cpd_id",
+                                                          "resourceHandle": {
+                                                              "vimConnectionId": "vim_connection_id",
+                                                              "resourceId": "resource_id",
+                                                              "resourceProviderId": "resourceProviderId",
+                                                              "tenant": "tenant",
+                                                              "ipAddress": "ipAddress",
+                                                              "macAddress": "macAddress",
+                                                              "instId": "instId",
+                                                              "networkId": "networkId",
+                                                              "subnetId": "subnetId"
+                                                          }
+                                                          }],
+                                        "changeType": VNFC_CHANGE_TYPE.MODIFIED
+                                        }]
+        }
+        HandleVnfLcmOocNotification(self.vnfm_inst_id, self.m_nf_inst_id, data).do_biz()
+        url = '/api/nslcm/v2/ns/%s/vnfs/%s/Notify' % (self.vnfm_inst_id, self.m_nf_inst_id)
+        response = self.client.post(url, data)
+        self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)