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.
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
25 logger = logging.getLogger(__name__)
28 class DeleteVls(object):
29 def __init__(self, vl_inst_id):
30 self.vl_inst_id = vl_inst_id
35 vl_inst_info = VLInstModel.objects.filter(vlinstanceid=self.vl_inst_id)
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()
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)
55 def exception_handle(self, e):
56 detail = "vl delete failed, detail message: %s" % e.message
58 return {"result": 0, "detail": detail}
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)
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)
74 def delete_vl_from_resmgr(self):
75 resmgr.delete_vl(self.vl_inst_id)
77 def delete_vl_inst_id_in_vnffg(self):
78 for vnffg_info in VNFFGInstModel.objects.filter(nsinstid=self.ns_inst_id):
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)
86 def delete_network_and_subnet_in_aai(self):
87 logger.debug("DeleteVls::delete_network_in_aai[%s] in aai.", self.vl_inst_id)
89 # query network in aai, get resource_version
90 customer_info = query_network_aai(self.vl_inst_id)
91 resource_version = customer_info.get("resource-version")
93 # delete network from aai
94 resp_data, resp_status = delete_network_aai(self.vl_inst_id, resource_version)
95 logger.debug("Delete network[%s] from aai successfully, status: %s", self.vl_inst_id, resp_status)
96 except NSLCMException as e:
97 logger.debug("Fail to delete network[%s] from aai: %s", self.vl_inst_id, e.message)
98 except Exception as e:
99 logger.error("Exception occurs when delete network[%s] from aai: %s", self.vl_inst_id, e.message)
100 logger.error(traceback.format_exc())
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()