1 # Copyright 2016 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from lcm.pub.config.config import REPORT_TO_AAI
20 from lcm.pub.database.models import DefPkgMappingModel, InputParamMappingModel, ServiceBaseInfoModel
21 from lcm.pub.database.models import NSInstModel
22 from lcm.pub.exceptions import NSLCMException
23 from lcm.pub.msapi.aai import query_ns_aai, delete_ns_aai
25 logger = logging.getLogger(__name__)
28 class DeleteNsService(object):
29 def __init__(self, ns_inst_id):
30 self.ns_inst_id = ns_inst_id
36 self.delete_ns_in_aai()
38 logger.error(traceback.format_exc())
41 logger.debug("delele NSInstModel(%s)", self.ns_inst_id)
42 NSInstModel.objects.filter(id=self.ns_inst_id).delete()
44 logger.debug("delele InputParamMappingModel(%s)", self.ns_inst_id)
45 InputParamMappingModel.objects.filter(service_id=self.ns_inst_id).delete()
47 logger.debug("delele DefPkgMappingModel(%s)", self.ns_inst_id)
48 DefPkgMappingModel.objects.filter(service_id=self.ns_inst_id).delete()
50 logger.debug("delele ServiceBaseInfoModel(%s)", self.ns_inst_id)
51 ServiceBaseInfoModel.objects.filter(service_id=self.ns_inst_id).delete()
53 def delete_ns_in_aai(self):
54 logger.debug("DeleteNsService::delete_ns_in_aai::delete ns instance[%s] in aai." % self.ns_inst_id)
56 # global_customer_id = "global-customer-id-" + self.ns_inst_id
58 # # query ns instance in aai, get resource_version
59 # customer_info = query_customer_aai(global_customer_id)
60 # resource_version = customer_info["resource-version"]
62 # # delete ns instance from aai
63 # resp_data, resp_status = delete_customer_aai(global_customer_id, resource_version)
65 ns_insts = NSInstModel.objects.filter(id=self.ns_inst_id)
66 self.global_customer_id = ns_insts[0].global_customer_id
67 self.service_type = ns_insts[0].service_type
68 # query ns instance in aai, get resource_version
69 logger.debug("self.global_customer_id=[%s], self.service_type=[%s], self.ns_inst_id=[%s]"
70 % (self.global_customer_id, self.service_type, self.ns_inst_id))
71 ns_info = query_ns_aai(self.global_customer_id, self.service_type, self.ns_inst_id)
72 resource_version = ns_info["resource-version"]
74 # delete ns instance from aai
75 logger.debug("ns instance resource_version=[%s]" % resource_version)
76 resp_data, resp_status = delete_ns_aai(self.global_customer_id, self.service_type,
77 self.ns_inst_id, resource_version)
79 logger.debug("Fail to delete ns[%s] from aai, resp_status: [%s]." % (self.ns_inst_id, resp_status))
81 logger.debug("Success to delete ns[%s] from aai, resp_status: [%s]."
82 % (self.ns_inst_id, resp_status))
83 except NSLCMException as e:
84 logger.debug("Fail to delete ns[%s] from aai, detail message: %s" % (self.ns_inst_id, e.message))
86 logger.error(traceback.format_exc())