refactor job url const
[vfc/nfvo/lcm.git] / lcm / ns / biz / ns_terminate.py
index 9baabf9..53973d3 100644 (file)
@@ -17,32 +17,35 @@ import threading
 import time
 import traceback
 
+from lcm.jobs.const import JOB_INSTANCE_RESPONSE_ID_URI
 from lcm.pub.database.models import NSInstModel, VLInstModel, FPInstModel, NfInstModel
 from lcm.pub.exceptions import NSLCMException
 from lcm.pub.msapi.nslcm import call_from_ns_cancel_resource
 from lcm.pub.utils.jobutil import JobUtil
 from lcm.pub.utils.values import ignore_case_get
 from lcm.pub.utils import restcall
-from lcm.ns.const import OWNER_TYPE
+from lcm.ns.enum import OWNER_TYPE
 from lcm.pub.database.models import PNFInstModel
-
-JOB_ERROR = 255
+from lcm.ns.biz.ns_lcm_op_occ import NsLcmOpOcc
+from lcm.jobs.enum import JOB_PROGRESS
 
 logger = logging.getLogger(__name__)
 
 
 class TerminateNsService(threading.Thread):
-    def __init__(self, ns_inst_id, terminate_type, terminate_timeout, job_id):
+    def __init__(self, ns_inst_id, job_id, request_data):
         threading.Thread.__init__(self)
-        self.ns_inst_id = ns_inst_id
-        self.terminate_type = terminate_type
-        self.terminate_timeout = terminate_timeout
+        self.terminate_type = request_data.get('terminationType', 'GRACEFUL')
+        self.terminate_timeout = request_data.get('gracefulTerminationTimeout', 600)
         self.job_id = job_id
+        self.ns_inst_id = ns_inst_id
+        self.occ_id = NsLcmOpOcc.create(ns_inst_id, "TERMINATE", "PROCESSING", False, request_data)
 
     def run(self):
         try:
             if not NSInstModel.objects.filter(id=self.ns_inst_id):
-                JobUtil.add_job_status(self.job_id, 100, "Need not terminate.", '')
+                JobUtil.add_job_status(self.job_id, JOB_PROGRESS.FINISHED, "Need not terminate.", '')
+                NsLcmOpOcc.update(self.occ_id, "COMPLETED")
                 return
             JobUtil.add_job_status(self.job_id, 10, "Starting terminate...", '')
 
@@ -52,13 +55,16 @@ class TerminateNsService(threading.Thread):
             self.cancel_pnf_list()
 
             NSInstModel.objects.filter(id=self.ns_inst_id).update(status='null')
-            JobUtil.add_job_status(self.job_id, 100, "ns terminate ends.", '')
+            JobUtil.add_job_status(self.job_id, JOB_PROGRESS.FINISHED, "ns terminate ends.", '')
+            NsLcmOpOcc.update(self.occ_id, "COMPLETED")
         except NSLCMException as e:
-            JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
-        except Exception as ex:
-            logger.error(ex.message)
+            JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, e.args[0])
+            NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.args[0])
+        except Exception as e:
+            logger.error(e.args[0])
             logger.error(traceback.format_exc())
-            JobUtil.add_job_status(self.job_id, JOB_ERROR, "ns terminate fail.")
+            JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, "ns terminate fail.")
+            NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.args[0])
 
     def cancel_vl_list(self):
         array_vlinst = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=self.ns_inst_id)
@@ -77,7 +83,7 @@ class TerminateNsService(threading.Thread):
                     if str(result) == '0':
                         delete_result = "success"
             except Exception as e:
-                logger.error("[cancel_vl_list] error[%s]!" % e.message)
+                logger.error("[cancel_vl_list] error[%s]!" % e.args[0])
                 logger.error(traceback.format_exc())
             job_msg = "Delete vlinst:[%s] %s." % (vlinst.vlinstanceid, delete_result)
             JobUtil.add_job_status(self.job_id, cur_progress, job_msg)
@@ -99,7 +105,7 @@ class TerminateNsService(threading.Thread):
                     if str(result) == '0':
                         delete_result = "success"
             except Exception as e:
-                logger.error("[cancel_sfc_list] error[%s]!" % e.message)
+                logger.error("[cancel_sfc_list] error[%s]!" % e.args[0])
                 logger.error(traceback.format_exc())
             job_msg = "Delete sfcinst:[%s] %s." % (sfcinst.sfcid, delete_result)
             JobUtil.add_job_status(self.job_id, cur_progress, job_msg)
@@ -121,7 +127,7 @@ class TerminateNsService(threading.Thread):
                 if vnf_job_id:
                     delete_result = "deleting"
             except Exception as e:
-                logger.error("[cancel_vnf_list] error[%s]!" % e.message)
+                logger.error("[cancel_vnf_list] error[%s]!" % e.args[0])
                 logger.error(traceback.format_exc())
             job_msg = "Delete vnfinst:[%s] %s." % (vnfinst.nfinstid, delete_result)
             JobUtil.add_job_status(self.job_id, cur_progress, job_msg)
@@ -164,7 +170,7 @@ class TerminateNsService(threading.Thread):
         while count < retry_count:
             count = count + 1
             time.sleep(interval_second)
-            uri = "/api/nslcm/v1/jobs/%s?responseId=%s" % (vnf_job_id, response_id)
+            uri = JOB_INSTANCE_RESPONSE_ID_URI % (vnf_job_id, response_id)
             ret = restcall.req_by_msb(uri, "GET")
             if ret[0] != 0:
                 logger.error("Failed to query job: %s:%s", ret[2], ret[1])
@@ -180,11 +186,11 @@ class TerminateNsService(threading.Thread):
                 logger.debug("%s:%s:%s", progress, new_response_id, job_desc)
                 response_id = new_response_id
                 count = 0
-            if progress == JOB_ERROR:
+            if progress == JOB_PROGRESS.ERROR:
                 job_timeout = False
                 logger.error("Job(%s) failed: %s", vnf_job_id, job_desc)
                 break
-            elif progress == 100:
+            elif progress == JOB_PROGRESS.FINISHED:
                 job_end_normal, job_timeout = True, False
                 logger.info("Job(%s) ended normally", vnf_job_id)
                 break
@@ -202,9 +208,9 @@ class TerminateNsService(threading.Thread):
                 try:
                     ret = call_from_ns_cancel_resource('pnf', pnfinst.pnfId)
                     if ret[0] == 0:
-                            delete_result = "success"
+                        delete_result = "success"
                 except Exception as e:
-                    logger.error("[cancel_pnf_list] error[%s]!" % e.message)
+                    logger.error("[cancel_pnf_list] error[%s]!" % e.args[0])
                     logger.error(traceback.format_exc())
                 job_msg = "Delete pnfinst:[%s] %s" % (pnfinst.pnfId, delete_result)
                 cur_progress += step_progress