update version of lcm
[vfc/nfvo/lcm.git] / lcm / ns_vls / biz / delete_vls.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 import logging
16 import traceback
17
18 from lcm.pub.config.config import REPORT_TO_AAI
19 from lcm.pub.database.models import VLInstModel, VNFFGInstModel
20 from lcm.pub.exceptions import NSLCMException
21 from lcm.pub.msapi import resmgr, extsys
22 from lcm.pub.msapi.aai import query_network_aai, delete_network_aai
23 from lcm.pub.nfvi.vim import vimadaptor
24
25 logger = logging.getLogger(__name__)
26
27
28 class DeleteVls(object):
29     def __init__(self, vl_inst_id):
30         self.vl_inst_id = vl_inst_id
31         self.ns_inst_id = ""
32
33     def do(self):
34         try:
35             vl_inst_info = VLInstModel.objects.filter(vlinstanceid=self.vl_inst_id)
36             if not vl_inst_info:
37                 logger.info("vl inst id(%s) is not exist or has been already deleted" % self.vl_inst_id)
38                 return {"result": 0, "detail": "vl is not exist or has been already deleted"}
39             self.ns_inst_id = vl_inst_info[0].ownerid
40             vim_id = vl_inst_info[0].vimid
41             subnetwork_id_list = vl_inst_info[0].relatedsubnetworkid.split(",")
42             network_id = vl_inst_info[0].relatednetworkid
43             self.delete_vl_from_vim(vim_id, subnetwork_id_list, network_id)
44             self.delete_vl_from_resmgr()
45             if REPORT_TO_AAI:
46                 self.delete_network_and_subnet_in_aai()
47             self.delete_vl_from_db(vl_inst_info)
48             return {"result": 0, "detail": "delete vl success"}
49         except NSLCMException as e:
50             return self.exception_handle(e)
51         except Exception as e:
52             logger.error(traceback.format_exc())
53             return self.exception_handle(e)
54
55     def exception_handle(self, e):
56         detail = "vl delete failed, detail message: %s" % e.message
57         logger.error(detail)
58         return {"result": 0, "detail": detail}
59
60     def delete_vl_from_vim(self, vim_id, subnetwork_id_list, network_id):
61         vim_resp_body = extsys.get_vim_by_id(vim_id)
62         data = {
63             "vimid": vim_id,
64             "vimtype": vim_resp_body["type"],
65             "url": vim_resp_body["url"],
66             "user": vim_resp_body["userName"],
67             "passwd": vim_resp_body["password"],
68             "tenant": vim_resp_body["tenant"]}
69         vim_api = vimadaptor.VimAdaptor(data)
70         for subnetwork_id in subnetwork_id_list:
71             vim_api.delete_subnet(subnet_id=subnetwork_id)
72         vim_api.delete_network(network_id=network_id)
73
74     def delete_vl_from_resmgr(self):
75         resmgr.delete_vl(self.vl_inst_id)
76
77     def delete_vl_inst_id_in_vnffg(self):
78         for vnffg_info in VNFFGInstModel.objects.filter(nsinstid=self.ns_inst_id):
79             new_vl_id_list = ""
80             for old_vl_id in vnffg_info.vllist.split(","):
81                 if old_vl_id != self.vl_inst_id:
82                     new_vl_id_list += old_vl_id + ","
83             new_vl_id_list = new_vl_id_list[:-1]
84             VNFFGInstModel.objects.filter(vnffginstid=vnffg_info.vnffginstid).update(vllist=new_vl_id_list)
85
86     def delete_network_and_subnet_in_aai(self):
87         logger.debug("DeleteVls::delete_network_in_aai::delete network[%s] in aai." % self.vl_inst_id)
88         try:
89             # query network in aai, get resource_version
90             customer_info = query_network_aai(self.vl_inst_id)
91             resource_version = customer_info["resource-version"]
92
93             # delete network from aai
94             resp_data, resp_status = delete_network_aai(self.vl_inst_id, resource_version)
95             logger.debug("Success to delete network[%s] from aai, resp_status: [%s]."
96                          % (self.vl_inst_id, resp_status))
97         except NSLCMException as e:
98             logger.debug("Fail to delete network[%s] to aai, detail message: %s" % (self.vl_inst_id, e.message))
99         except:
100             logger.error(traceback.format_exc())
101
102     def delete_vl_from_db(self, vl_inst_info):
103         # do_biz_with_share_lock("delete-vllist-in-vnffg-%s" % self.ns_inst_id, self.delete_vl_inst_id_in_vnffg)
104         self.delete_vl_inst_id_in_vnffg()
105         vl_inst_info.delete()