7d51df461d433789e57d60a6ee835239cae46836
[vfc/nfvo/lcm.git] / lcm / ns / ns_delete.py
1 # Copyright 2016 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15
16 import logging
17 import traceback
18
19 from lcm.pub.database.models import DefPkgMappingModel, InputParamMappingModel, ServiceBaseInfoModel
20 from lcm.pub.database.models import NSInstModel
21 from lcm.pub.msapi.aai import get_customer_aai, delete_customer_aai
22
23
24 logger = logging.getLogger(__name__)
25
26
27 class DeleteNsService(object):
28     def __init__(self, ns_inst_id):
29         self.ns_inst_id = ns_inst_id
30
31     def do_biz(self):
32         try:
33             self.delete_ns()
34             self.delete_ns_in_aai()
35         except:
36             logger.error(traceback.format_exc())
37
38     def delete_ns(self):
39         logger.debug("delele NSInstModel(%s)", self.ns_inst_id)
40         NSInstModel.objects.filter(id=self.ns_inst_id).delete()
41
42         logger.debug("delele InputParamMappingModel(%s)", self.ns_inst_id)
43         InputParamMappingModel.objects.filter(service_id=self.ns_inst_id).delete()
44
45         logger.debug("delele DefPkgMappingModel(%s)", self.ns_inst_id)
46         DefPkgMappingModel.objects.filter(service_id=self.ns_inst_id).delete()
47
48         logger.debug("delele ServiceBaseInfoModel(%s)", self.ns_inst_id)
49         ServiceBaseInfoModel.objects.filter(service_id=self.ns_inst_id).delete()
50
51     def delete_ns_in_aai(self):
52         logger.debug("DeleteNsService::delete_ns_in_aai::delete ns instance[%s] in aai." % self.ns_inst_id)
53         global_customer_id = "global-customer-id-" + self.ns_inst_id
54
55         # query ns instance in aai, get resource_version
56         customer_info = get_customer_aai(global_customer_id)
57         resource_version = customer_info["resource-version"]
58
59         # delete ns instance from aai
60         resp_data, resp_status = delete_customer_aai(global_customer_id, resource_version)
61         if resp_data:
62             logger.debug("Fail to delete ns instance[%s] from aai, resp_status: [%s]." % (self.ns_inst_id, resp_status))
63         else:
64             logger.debug(
65                 "Success to delete ns instance[%s] from aai, resp_status: [%s]." % (self.ns_inst_id, resp_status))