fix vserver deletion in AAI
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / terminate_nfs.py
index c88e844..7db2d7c 100644 (file)
@@ -11,6 +11,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+
 import logging
 import traceback
 import json
@@ -55,10 +56,10 @@ class TerminateVnfs(threading.Thread):
                 self.delete_vnf_in_aai()
             self.delete_data_from_db()
         except NSLCMException as e:
-            self.exception(e.message)
-        except Exception:
+            self.set_job_err(e.message)
+        except Exception as ex:
             logger.error(traceback.format_exc())
-            self.exception('unexpected exception')
+            self.set_job_err(ex.message)
 
     def set_vnf_status(self, vnf_inst_info):
         vnf_status = vnf_inst_info.status
@@ -69,10 +70,10 @@ class TerminateVnfs(threading.Thread):
             vnf_inst_info.status = VNF_STATUS.TERMINATING
             vnf_inst_info.save()
 
-    def check_vnf_is_exist(self):
+    def get_vnf_inst(self):
         vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id)
         if not vnf_inst.exists():
-            logger.warning('[VNF terminate] Vnf terminate [%s] is not exist.' % self.vnf_inst_id)
+            logger.warning('[VNF terminate] Vnf terminate [%s] does not exist.' % self.vnf_inst_id)
             return None
         return vnf_inst[0]
 
@@ -80,7 +81,7 @@ class TerminateVnfs(threading.Thread):
         JobUtil.add_job_status(self.job_id, progress, status_decs, error_code)
 
     def initdata(self):
-        vnf_inst_info = self.check_vnf_is_exist()
+        vnf_inst_info = self.get_vnf_inst()
         if not vnf_inst_info:
             self.add_progress(100, "TERM_VNF_NOT_EXIST_SUCCESS", "finished")
         self.add_progress(2, "GET_VNF_INST_SUCCESS")
@@ -99,7 +100,7 @@ class TerminateVnfs(threading.Thread):
             raise NSLCMException('[VNF terminate] Vnf instance is not exist.')
         self.set_vnf_status(vnf_inst[0])
 
-    def exception(self, error_msg):
+    def set_job_err(self, error_msg):
         logger.error('VNF Terminate failed, detail message: %s' % error_msg)
         NfInstModel.objects.filter(nfinstid=self.vnf_inst_id).update(status=VNF_STATUS.FAILED)
         JobUtil.add_job_status(self.job_id, 255, 'VNF Terminate failed, detail message: %s' % error_msg, 0)
@@ -134,44 +135,44 @@ class TerminateVnfs(threading.Thread):
 
     def delete_vnf_in_aai(self):
         logger.debug("TerminateVnfs::delete_vnf_in_aai::delete vnf instance[%s] in aai." % self.vnf_inst_id)
+        try:
+            # query vnf instance in aai, get resource_version
+            customer_info = query_vnf_aai(self.vnf_inst_id)
+            resource_version = customer_info["resource-version"]
 
-        # query vnf instance in aai, get resource_version
-        customer_info = query_vnf_aai(self.vnf_inst_id)
-        resource_version = customer_info["resource-version"]
-
-        # delete vnf instance from aai
-        resp_data, resp_status = delete_vnf_aai(self.vnf_inst_id, resource_version)
-        if resp_data:
-            logger.debug("Fail to delete vnf instance[%s] from aai, resp_status: [%s]." % (self.vnf_inst_id, resp_status))
-        else:
+            # delete vnf instance from aai
+            resp_data, resp_status = delete_vnf_aai(self.vnf_inst_id, resource_version)
             logger.debug(
-                "Success to delete vnf instance[%s] from aai, resp_status: [%s]." % (self.vnf_inst_id, resp_status))
+                "Success to delete vnf[%s] from aai, resp_status: [%s]." % (self.vnf_inst_id, resp_status))
+        except NSLCMException as e:
+            logger.debug("Fail to delete vnf from aai[%s], detail message: %s" % (self.vnf_inst_id, e.message))
+        except:
+            logger.error(traceback.format_exc())
 
     def delete_vserver_in_aai(self):
         logger.debug("delete_vserver_in_aai start!")
-
-        vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.vnf_inst_id)
-        for vm_inst_info in vm_inst_infos:
-            vserver_id = vm_inst_info.resouceid
-            vim_id = vm_inst_info.vimid
-            cloud_owner, cloud_region_id = split_vim_to_owner_region(vim_id)
-            # query vim_info from aai, get tenant
-            vim_info = get_vim_by_id(vim_id)
-            tenant_id = vim_info["tenant"]
-
-            # query vserver instance in aai, get resource_version
-            vserver_info = query_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id)
-            resource_version = vserver_info["resource-version"]
-
-            # delete vserver instance from aai
-            resp_data, resp_status = delete_vserver_aai(cloud_owner, cloud_region_id,
-                                                        tenant_id, vserver_id, resource_version)
-            if resp_data:
-                logger.debug("Fail to delete vserver instance[%s] from aai, resp_status: [%s]." %
-                             (vserver_id, resp_status))
-            else:
+        try:
+            vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.vnf_inst_id)
+            for vm_inst_info in vm_inst_infos:
+                vserver_id = vm_inst_info.resouceid
+                vim_id = vm_inst_info.vimid
+                cloud_owner, cloud_region_id = split_vim_to_owner_region(vim_id)
+                # query vim_info from aai, get tenant
+                vim_info = get_vim_by_id(vim_id)
+                tenant_id = vim_info["tenantId"]
+
+                # query vserver instance in aai, get resource_version
+                vserver_info = query_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id)
+                resource_version = vserver_info["resource-version"]
+
+                # delete vserver instance from aai
+                resp_data, resp_status = delete_vserver_aai(cloud_owner, cloud_region_id,
+                                                            tenant_id, vserver_id, resource_version)
                 logger.debug(
                     "Success to delete vserver instance[%s] from aai, resp_status: [%s]." %
                     (vserver_id, resp_status))
-
-        logger.debug("delete_vserver_in_aai end!")
+            logger.debug("delete_vserver_in_aai end!")
+        except NSLCMException as e:
+            logger.debug("Fail to delete vserver from aai, detail message: %s" % e.message)
+        except:
+            logger.error(traceback.format_exc())