Change in terminate 23/60823/1
authorbiancunkang <bian.cunkang@zte.com.cn>
Thu, 16 Aug 2018 02:26:29 +0000 (10:26 +0800)
committerbiancunkang <bian.cunkang@zte.com.cn>
Thu, 16 Aug 2018 02:27:58 +0000 (10:27 +0800)
Modify grant_vnf.py

Change-Id: Ia7314d00b286489b221e22a6e3de791f2c7fa77b
Issue-ID: VFC-1015
Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
lcm/lcm/nf/biz/grant_vnf.py
lcm/lcm/nf/biz/terminate_vnf.py

index cbfa88b..268e8e4 100644 (file)
 import json
 import logging
 
-from lcm.pub.database.models import NfInstModel, VmInstModel
+from lcm.pub.database.models import NfInstModel
 from lcm.pub.msapi.gvnfmdriver import apply_grant_to_nfvo
+from lcm.pub.utils.values import ignore_case_get
 
 logger = logging.getLogger(__name__)
 
 
-def grant_resource(nf_inst_id, job_id):
+def grant_resource(data, nf_inst_id, job_id, grant_type, vdus):
     logger.info("Grant resource begin")
+    if grant_type == "Terminate":
+        lifecycleOperration = "Terminate"
+    elif grant_type == "instantiate":
+        lifecycleOperration = "Instantiate"
+
     content_args = {
         'vnfInstanceId': nf_inst_id,
         'vnfDescriptorId': '',
-        'lifecycleOperation': 'Terminate',
+        'lifecycleOperation': lifecycleOperration,
         'vnfLcmOpOccId': job_id,
-        'addResource': [],
-        'removeResource': [],
+        'addResources': [],
+        'removeResources': [],
         'placementConstraint': [],
         'additionalParam': {}
     }
 
-    vdus = VmInstModel.objects.filter(instid=nf_inst_id, is_predefined=1)
-    res_index = 1
-    for vdu in vdus:
-        res_def = {
-            'type': 'VDU',
-            'resDefId': str(res_index),
-            'resDesId': vdu.resouceid}
-        content_args['removeResource'].append(res_def)
-        res_index += 1
+    if grant_type == "Terminate":
+        res_index = 1
+        for vdu in vdus:
+            res_def = {
+                'type': 'VDU',
+                'resDefId': str(res_index),
+                'resDesId': vdu.resouceid}
+            content_args['removeResources'].append(res_def)
+            res_index += 1
+        content_args['additionalParam']['vimid'] = vdus[0].vimid
+    elif grant_type == "Instantiate":
+        vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
+        res_index = 1
+        for vdu in vdus:
+            res_def = {
+                'type': 'VDU',
+                'resDefId': str(res_index),
+                'resDesId': ignore_case_get(vdu, "vdu_id")
+            }
+            content_args['addResources'].append(res_def)
+            res_index += 1
+        content_args['additionalParam']['vimid'] = vim_id
 
     vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
     content_args['additionalParam']['vnfmid'] = vnfInsts[0].vnfminstid
-    content_args['additionalParam']['vimid'] = vdus[0].vimid
     logger.info('Grant request data=%s' % content_args)
     apply_result = apply_grant_to_nfvo(json.dumps(content_args))
-    logger.info("Grant resource end, response: %s" % apply_result)
+    return apply_result
index c0caf51..271fc3f 100644 (file)
@@ -40,11 +40,15 @@ class TerminateVnf(Thread):
         self.terminationType = ignore_case_get(self.data, "terminationType")
         self.gracefulTerminationTimeout = ignore_case_get(self.data, "gracefulTerminationTimeout")
         self.inst_resource = {'volumn': [], 'network': [], 'subnet': [], 'port': [], 'flavor': [], 'vm': []}
+        self.grant_type = "Terminate"
 
     def run(self):
         try:
             if self.term_pre():
-                grant_resource(nf_inst_id=self.nf_inst_id, job_id=self.job_id)
+                vdus = VmInstModel.objects.filter(instid=self.nf_inst_id, is_predefined=1)
+                apply_result = grant_resource(data=self.data, nf_inst_id=self.nf_inst_id, job_id=self.job_id,
+                                              grant_type=self.grant_type, vdus=vdus)
+                logger.info("Grant resource end, response: %s" % apply_result)
                 JobUtil.add_job_status(self.job_id, 20, 'Nf terminating grant_resource finish')
                 self.query_inst_resource()
                 self.query_notify_data()