Change gvnfm-vnflcm aai config
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / vnf_cancel / delete_vnf_identifier.py
1 # Copyright 2017 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 import logging
16
17 from lcm.pub.config.config import REPORT_TO_AAI
18 from lcm.pub.database.models import NfInstModel, NfvoRegInfoModel
19 from lcm.pub.exceptions import NFLCMException
20 from lcm.pub.msapi.aai import query_vnf_aai, delete_vnf_aai
21
22 logger = logging.getLogger(__name__)
23
24
25 class DeleteVnf:
26     def __init__(self, data, instanceid):
27         self.data = data
28         self.nf_inst_id = instanceid
29
30     def do_biz(self):
31         try:
32             self.check_parameter()
33             self.delete_info_from_db()
34             if REPORT_TO_AAI:
35                 self.delete_vnf_in_aai()
36         except NFLCMException as e:
37             logger.debug('Delete VNF instance[%s] from AAI failed' % self.nf_inst_id)
38         except:
39             logger.debug('Delete VNF instance[%s] failed' % self.nf_inst_id)
40
41     def check_parameter(self):
42         vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
43         if not vnf_insts.exists():
44             logger.warn('VnfInst(%s) does not exist' % self.nf_inst_id)
45             return
46             # sel_vnf = vnf_insts[0]
47             # if sel_vnf.status != 'NOT_INSTANTIATED':
48             #    raise NFLCMException("Don't allow to delete vnf(status:[%s])" % sel_vnf.status)
49
50     def delete_info_from_db(self):
51         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
52         NfvoRegInfoModel.objects.filter(nfvoid=self.nf_inst_id).delete()
53
54     def delete_vnf_in_aai(self):
55         logger.debug("DeleteVnf::delete_vnf_in_aai::delete vnf instance[%s] in aai." % self.nf_inst_id)
56
57         # query vnf instance in aai, get resource_version
58         customer_info = query_vnf_aai(self.nf_inst_id)
59         resource_version = customer_info["resource-version"]
60
61         # delete vnf instance from aai
62         resp_data, resp_status = delete_vnf_aai(self.nf_inst_id, resource_version)
63         if resp_data:
64             logger.debug("Fail to delete vnf instance[%s] from aai, resp_status: [%s]." % (self.nf_inst_id, resp_status))
65         else:
66             logger.debug(
67                 "Success to delete vnf instance[%s] from aai, resp_status: [%s]." % (self.nf_inst_id, resp_status))