Add code and testcase of lcm notify
authorying.yunlong <ying.yunlong@zte.com.cn>
Sat, 25 Feb 2017 07:07:22 +0000 (15:07 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Sat, 25 Feb 2017 07:07:22 +0000 (15:07 +0800)
Change-Id: I1fcb31e62827db3a53d57da7a80443ca55ca4f4e
Issue-Id: GVNFM-34
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py
lcm/lcm/nf/vnfs/tests/test_vnf_create.py
lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py
lcm/lcm/nf/vnfs/vnf_create/inst_vnf.py
lcm/lcm/pub/msapi/nfvolcm.py

index 9ec2a25..ceef5d9 100644 (file)
@@ -103,7 +103,8 @@ class TestNFTerminate(TestCase):
                                    localizationLanguage='EN_US', create_time=now_time())
         t1_apply_grant_result = [0, json.JSONEncoder().encode(
             {"vim": {"vimid": 'vimid_1', "accessinfo": {"tenant": 'tenantname_1'}}}), '200']
-        mock_call_req.side_effect = [t1_apply_grant_result]
+        t2_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
+        mock_call_req.side_effect = [t1_apply_grant_result, t2_lcm_notify_result]
         mock_call.return_value = None
         data = {"terminationType": "FORCEFUL",
                 "gracefulTerminationTimeout": 120}
index c14fa2f..92a8517 100644 (file)
@@ -161,7 +161,7 @@ class TestNFInstantiate(TestCase):
 
     @mock.patch.object(restcall, 'call_req')
     @mock.patch.object(api, 'call')
-    def test_instantiate_vnf_when_111(self, mock_call, mock_call_req):
+    def test_instantiate_vnf_success(self, mock_call, mock_call_req):
         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
@@ -169,7 +169,7 @@ class TestNFInstantiate(TestCase):
         r2_get_rawdata_from_catalog = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']
         r3_apply_grant_result = [0, json.JSONEncoder().encode(
             {"vim": {"vimid": 'vimid_1', "accessinfo": {"tenant": 'tenantname_1'}}}), '200']
-        r4_lcm_notify_result = [0, None, '200']
+        r4_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
         mock_call_req.side_effect = [r1_get_csarid_by_vnfdid, r2_get_rawdata_from_catalog,
                                      r3_apply_grant_result, r4_lcm_notify_result]
         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
index 9060ad3..b1e6ebf 100644 (file)
@@ -17,9 +17,9 @@ from threading import Thread
 
 from lcm.nf.vnfs.const import VNF_STATUS
 from lcm.pub.database.models import JobStatusModel, NfInstModel, VmInstModel, NetworkInstModel, StorageInstModel, \
-    FlavourInstModel, PortInstModel, SubNetworkInstModel
+    FlavourInstModel, PortInstModel, SubNetworkInstModel, VNFCInstModel
 from lcm.pub.exceptions import NFLCMException
-from lcm.pub.msapi.nfvolcm import apply_grant_to_nfvo
+from lcm.pub.msapi.nfvolcm import apply_grant_to_nfvo, notify_lcm_to_nfvo
 from lcm.pub.utils.jobutil import JobUtil
 from lcm.pub.utils.timeutil import now_time
 from lcm.pub.utils.values import ignore_case_get
@@ -43,14 +43,16 @@ class TermVnf(Thread):
                               'flavor': [],
                               'vm': [],
                               }
+        self.notify_data = None
 
     def run(self):
         try:
             self.term_pre()
             self.grant_resource()
             self.query_inst_resource()
+            self.query_notify_data()
             self.delete_resource()
-            self.lcm_notify()
+            self.lcm_notify()
             JobUtil.add_job_status(self.job_id, 100, "Terminate Vnf success.")
         except NFLCMException as e:
             self.vnf_term_failed_handle(e.message)
@@ -168,6 +170,68 @@ class TermVnf(Thread):
             self.inst_resource['vm'].append(vm_info)
         logger.info('[query_vm_resource]:ret_vms=%s' % self.inst_resource['vm'])
 
+    def query_notify_data(self):
+        logger.info('[NF instantiation] send notify request to nfvo start')
+        affected_vnfc = []
+        vnfcs = VNFCInstModel.objects.filter(instid=self.nf_inst_id)
+        for vnfc in vnfcs:
+            vmResource = {}
+            if vnfc.vmid:
+                vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
+                if vm:
+                    vmResource = {'vimId': vm[0].vimid, 'resourceId': vm[0].resouceid,
+                                  'resourceName': vm[0].vmname, 'resourceType': 'vm'}
+            affected_vnfc.append(
+                {'vnfcInstanceId': vnfc.vnfcinstanceid,
+                 'vduId': vnfc.vduid,
+                 'changeType': 'removed',
+                 'computeResource': vmResource})
+        affected_vl = []
+        networks = NetworkInstModel.objects.filter(instid=self.nf_inst_id)
+        for network in networks:
+            network_resource = {'vimId': network.vimid, 'resourceId': network.resouceid,
+                                'resourceName': network.name, 'resourceType': 'network'}
+            affected_vl.append(
+                {'vlInstanceId': network.networkid,
+                 'vldid': network.nodeId,
+                 'changeType': 'removed',
+                 'networkResource': network_resource})
+        affected_cp = []
+        ports = PortInstModel.objects.filter(instid=self.nf_inst_id)
+        for port in ports:
+            affected_cp.append(
+                {'vsInstanceId': port.portid,
+                 'cpdid': port.nodeId,
+                 'changeType': 'removed',
+                 'storageResource': {'vimId': port.vimid, 'resourceId': port.resouceid,
+                                     'resourceName': port.name, 'resourceType': 'port'}})
+        affected_vs = []
+        vss = StorageInstModel.objects.filter(instid=self.nf_inst_id)
+        for vs in vss:
+            affected_vs.append(
+                {'vsInstanceId': vs.storageid,
+                 'vsdId': vs.nodeId,
+                 'changeType': 'removed',
+                 'storageResource': {'vimId': vs.vimid, 'resourceId': vs.resouceid,
+                                     'resourceName': vs.name, 'resourceType': 'volume'}})
+        self.notify_data = {
+            "status": 'result',
+            "nfInstanceId": self.nf_inst_id,
+            "operation": 'Terminal',
+            "jobId": self.job_id,
+            'affectedVnfc': affected_vnfc,
+            'affectedVirtualLink': affected_vl,
+            'affectedVirtualStorage': affected_vs,
+            # "vnfdmodule": allocate_data,
+            # "additionalParam": addition_param,
+            # "nfvoInstanceId": self.nfvo_inst_id,
+            # "vnfmInstanceId": self.vnfm_inst_id,
+            # 'affectedcapacity': affectedcapacity,
+            # 'affectedService': [],
+            'affectedCp': affected_cp
+            }
+        logger.info('content_args=%s' % self.notify_data)
+
     def delete_resource(self):
         logger.error('rollback resource begin')
         adaptor.delete_vim_res(self.inst_resource, self.do_notify_delete)
@@ -189,7 +253,10 @@ class TermVnf(Thread):
             StorageInstModel.objects.filter(instid=self.nf_inst_id, resouceid=res_id).delete()
 
     def lcm_notify(self):
-        pass
+        logger.info('[NF termination] send notify request to nfvo end')
+        resp = notify_lcm_to_nfvo(self.notify_data, self.nf_inst_id)
+        logger.info('[NF termination] get lcm response %s' % resp)
+        logger.info('[NF termination] send notify request to nfvo end')
 
     def vnf_term_failed_handle(self, error_msg):
         logger.error('VNF termination failed, detail message: %s' % error_msg)
index 9760381..685decf 100644 (file)
@@ -210,9 +210,6 @@ class InstVnf(Thread):
         # call rest api
         resp = notify_lcm_to_nfvo(content_args, self.nf_inst_id)
         logger.info('[NF instantiation] get lcm response %s' % resp)
-        if resp[0] != 0:
-            logger.error("notify lifecycle to nfvo failed.[%s]" % resp[1])
-            raise NFLCMException("send notify request to nfvo failed")
         logger.info('[NF instantiation] send notify request to nfvo end')
 
     # def load_nfvo_config(self):
index b2d7440..dc31045 100644 (file)
@@ -39,7 +39,10 @@ def apply_grant_to_nfvo(data):
 #call gvnfm driver
 def notify_lcm_to_nfvo(data, nf_inst_id):
     ret = req_by_msb("openoapi/nslcm/v1/vnfs/%s/Notify"%nf_inst_id, "POST", data)
-    return ret
+    if ret[0] != 0:
+        logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
+        raise NFLCMException("Nf lcm notify exception")
+    return json.JSONDecoder().decode(ret[1])
 
 #call gvnfm driver
 def apply_res_to_nfvo(data):