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 VLInstModel, VNFFGInstModel
21 from lcm.pub.exceptions import NSLCMException
22 from lcm.pub.msapi import resmgr, extsys
23 from lcm.pub.msapi.aai import query_network_aai, delete_network_aai
24 from lcm.pub.nfvi.vim import vimadaptor
26 logger = logging.getLogger(__name__)
29 class DeleteVls(object):
30 def __init__(self, vl_inst_id):
31 self.vl_inst_id = vl_inst_id
36 vl_inst_info = VLInstModel.objects.filter(vlinstanceid=self.vl_inst_id)
38 logger.info("vl inst id(%s) is not exist or has been already deleted" % self.vl_inst_id)
39 return {"result": 0, "detail": "vl is not exist or has been already deleted"}
40 self.ns_inst_id = vl_inst_info[0].ownerid
41 # vim_id = vl_inst_info[0].vimid
42 vim_id = json.JSONDecoder().decode(vl_inst_info[0].vimid) if isinstance(vl_inst_info[0].vimid, str) \
43 else vl_inst_info[0].vimid
44 subnetwork_id_list = vl_inst_info[0].relatedsubnetworkid.split(",")
45 network_id = vl_inst_info[0].relatednetworkid
46 self.delete_vl_from_vim(vim_id, subnetwork_id_list, network_id)
47 self.delete_vl_from_resmgr()
49 self.delete_network_and_subnet_in_aai()
50 self.delete_vl_from_db(vl_inst_info)
51 return {"result": 0, "detail": "delete vl success"}
52 except NSLCMException as e:
53 return self.exception_handle(e)
54 except Exception as e:
55 logger.error(traceback.format_exc())
56 return self.exception_handle(e)
58 def exception_handle(self, e):
59 detail = "vl delete failed, detail message: %s" % e.args[0]
61 return {"result": 0, "detail": detail}
63 def delete_vl_from_vim(self, vim_id, subnetwork_id_list, network_id):
64 vim_resp_body = extsys.get_vim_by_id(vim_id)
67 "vimtype": vim_resp_body["type"],
68 "url": vim_resp_body["url"],
69 "user": vim_resp_body["userName"],
70 "passwd": vim_resp_body["password"],
71 "tenant": vim_resp_body["tenant"]}
72 vim_api = vimadaptor.VimAdaptor(data)
73 for subnetwork_id in subnetwork_id_list:
74 vim_api.delete_subnet(subnet_id=subnetwork_id)
75 vim_api.delete_network(network_id=network_id)
77 def delete_vl_from_resmgr(self):
78 resmgr.delete_vl(self.vl_inst_id)
80 def delete_vl_inst_id_in_vnffg(self):
81 for vnffg_info in VNFFGInstModel.objects.filter(nsinstid=self.ns_inst_id):
83 for old_vl_id in vnffg_info.vllist.split(","):
84 if old_vl_id != self.vl_inst_id:
85 new_vl_id_list += old_vl_id + ","
86 new_vl_id_list = new_vl_id_list[:-1]
87 VNFFGInstModel.objects.filter(vnffginstid=vnffg_info.vnffginstid).update(vllist=new_vl_id_list)
89 def delete_network_and_subnet_in_aai(self):
90 logger.debug("DeleteVls::delete_network_in_aai[%s] in aai.", self.vl_inst_id)
92 # query network in aai, get resource_version
93 customer_info = query_network_aai(self.vl_inst_id)
94 resource_version = customer_info.get("resource-version")
96 # delete network from aai
97 resp_data, resp_status = delete_network_aai(self.vl_inst_id, resource_version)
98 logger.debug("Delete network[%s] from aai successfully, status: %s", self.vl_inst_id, resp_status)
99 except NSLCMException as e:
100 logger.debug("Fail to delete network[%s] from aai: %s", self.vl_inst_id, e.message)
101 except Exception as e:
102 logger.error("Exception occurs when delete network[%s] from aai: %s", self.vl_inst_id, e.message)
103 logger.error(traceback.format_exc())
105 def delete_vl_from_db(self, vl_inst_info):
106 # do_biz_with_share_lock("delete-vllist-in-vnffg-%s" % self.ns_inst_id, self.delete_vl_inst_id_in_vnffg)
107 self.delete_vl_inst_id_in_vnffg()
108 vl_inst_info.delete()