improve code coverage rate (change ext conn) after vfclcm upgraded from python2...
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / change_ext_conn.py
index 3cde0cf..4cc63a1 100644 (file)
@@ -20,12 +20,14 @@ from lcm.nf.biz.common import port_save
 from lcm.nf.biz.grant_vnf import grant_resource
 from lcm.nf.const import RESOURCE_MAP, GRANT_TYPE, OPERATION_STATE_TYPE
 from lcm.nf.const import VNF_STATUS, OPERATION_TASK, OPERATION_TYPE
+from lcm.nf.const import SUB_OPERATION_TASK
 from lcm.pub.database.models import VmInstModel, NfInstModel, PortInstModel
 from lcm.pub.utils.notificationsutil import NotificationsUtil, prepare_notification
 from lcm.pub.utils.values import ignore_case_get
 from lcm.pub.utils.timeutil import now_time
 from lcm.pub.utils.jobutil import JobUtil
 from lcm.pub.exceptions import NFLCMException
+from lcm.pub.exceptions import NFLCMExceptionConflict
 from lcm.pub.vimapi import adaptor
 from .operate_vnf_lcm_op_occ import VnfLcmOpOcc
 
@@ -48,6 +50,7 @@ class ChangeExtConn(Thread):
             operation=OPERATION_TYPE.CHANGE_EXT_CONN,
             task=OPERATION_TASK.CHANGE_EXT_CONN
         )
+        self.pre_deal()
 
     def run(self):
         try:
@@ -64,6 +67,10 @@ class ChangeExtConn(Thread):
                 50,
                 "Start to change ext conn."
             )
+            self.lcm_op_occ.upd(
+                sub_operation=SUB_OPERATION_TASK.GRANTED,
+                operation_state=OPERATION_STATE_TYPE.PROCESSING
+            )
             self.do_operation()
             self.vnf_insts.update(
                 status='INSTANTIATED',
@@ -75,13 +82,27 @@ class ChangeExtConn(Thread):
                 100,
                 "Change ext conn success."
             )
+            self.lcm_op_occ.upd(
+                sub_operation=SUB_OPERATION_TASK.SUCCESS,
+                operation_state=OPERATION_STATE_TYPE.COMPLETED
+            )
         except NFLCMException as e:
-            logger.error(e.message)
-            self.change_ext_conn_failed_handle(e.message)
+            logger.error(e.args[0])
+            self.change_ext_conn_failed_handle(e.args[0])
         except Exception as e:
-            logger.error(e.message)
+            logger.error(e.args[0])
             logger.error(traceback.format_exc())
-            self.change_ext_conn_failed_handle(e.message)
+            self.change_ext_conn_failed_handle(e.args[0])
+
+    def pre_deal(self):
+        logger.debug("Start pre deal for VNF change_ext_conn task")
+
+        vnf_is_in_processing, vnf_op = self.lcm_op_occ.is_in_processing()
+        if vnf_is_in_processing:
+            raise NFLCMExceptionConflict('VNF(%s) %s in processing.' % (
+                self.nf_inst_id, vnf_op
+            ))
+        self.lcm_op_occ.add()
 
     def apply_grant(self):
         vdus = VmInstModel.objects.filter(instid=self.nf_inst_id)
@@ -146,7 +167,21 @@ class ChangeExtConn(Thread):
                         "location_info": {
                             "vimid": vim_id,
                             "tenant": tenant
-                        }
+                        },
+                        # TODO need confirm
+                        "protocol_data": [
+                            {
+                                "address_data": {
+                                    "l3_address_data": {
+                                        "fixed_ip_address":
+                                            {"ip_address_assignment": True,
+                                             "floating_ip_activated": True,
+                                             "ip_address_type": "IPV4",
+                                             "number_of_ip_address": 1}
+                                    },
+                                },
+                            },
+                        ],
                     },
                     "vl_id": network_id,
                     "vdu_id": vdu_id,
@@ -211,7 +246,7 @@ class ChangeExtConn(Thread):
 
     def query_inst_resource(self, inst_resource):
         logger.debug('Query resource begin')
-        for resource_type in RESOURCE_MAP.keys():
+        for resource_type in list(RESOURCE_MAP.keys()):
             resource_table = globals().get(resource_type + 'InstModel')
             resource_insts = resource_table.objects.filter(
                 instid=self.nf_inst_id
@@ -233,7 +268,7 @@ class ChangeExtConn(Thread):
 
     def do_create_port_notify(self, res_type, ret):
         self.port_id = ignore_case_get(ret, "id")
-        port_save("", self.nf_inst_id, ret)
+        port_save(self.job_id, self.nf_inst_id, ret)
 
     def do_notify_op(self, operation_type, status, resid):
         if operation_type == "delete":
@@ -287,3 +322,11 @@ class ChangeExtConn(Thread):
         )
         self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
         JobUtil.add_job_status(self.job_id, 255, error_msg)
+        self.lcm_op_occ.upd(
+            sub_operation=SUB_OPERATION_TASK.ERROR,
+            operation_state=OPERATION_STATE_TYPE.FAILED,
+            error={
+                "status": 500,
+                "detail": error_msg
+            }
+        )