Rm resmgr.grant releated contents
[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 json
16 import logging
17 import traceback
18
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 extsys
23 from lcm.pub.msapi.aai import query_network_aai, delete_network_aai
24 from lcm.pub.nfvi.vim import vimadaptor
25
26 logger = logging.getLogger(__name__)
27
28
29 class DeleteVls(object):
30     def __init__(self, vl_inst_id):
31         self.vl_inst_id = vl_inst_id
32         self.ns_inst_id = ""
33
34     def do(self):
35         try:
36             vl_inst_info = VLInstModel.objects.filter(vlinstanceid=self.vl_inst_id)
37             if not vl_inst_info:
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             if REPORT_TO_AAI:
48                 self.delete_network_and_subnet_in_aai()
49             self.delete_vl_from_db(vl_inst_info)
50             return {"result": 0, "detail": "delete vl success"}
51         except NSLCMException as e:
52             return self.exception_handle(e)
53         except Exception as e:
54             logger.error(traceback.format_exc())
55             return self.exception_handle(e)
56
57     def exception_handle(self, e):
58         detail = "vl delete failed, detail message: %s" % e.args[0]
59         logger.error(detail)
60         return {"result": 0, "detail": detail}
61
62     def delete_vl_from_vim(self, vim_id, subnetwork_id_list, network_id):
63         vim_resp_body = extsys.get_vim_by_id(vim_id)
64         data = {
65             "vimid": vim_id,
66             "vimtype": vim_resp_body["type"],
67             "url": vim_resp_body["url"],
68             "user": vim_resp_body["userName"],
69             "passwd": vim_resp_body["password"],
70             "tenant": vim_resp_body["tenant"]}
71         vim_api = vimadaptor.VimAdaptor(data)
72         for subnetwork_id in subnetwork_id_list:
73             vim_api.delete_subnet(subnet_id=subnetwork_id)
74         vim_api.delete_network(network_id=network_id)
75
76     def delete_vl_inst_id_in_vnffg(self):
77         for vnffg_info in VNFFGInstModel.objects.filter(nsinstid=self.ns_inst_id):
78             new_vl_id_list = ""
79             for old_vl_id in vnffg_info.vllist.split(","):
80                 if old_vl_id != self.vl_inst_id:
81                     new_vl_id_list += old_vl_id + ","
82             new_vl_id_list = new_vl_id_list[:-1]
83             VNFFGInstModel.objects.filter(vnffginstid=vnffg_info.vnffginstid).update(vllist=new_vl_id_list)
84
85     def delete_network_and_subnet_in_aai(self):
86         logger.debug("DeleteVls::delete_network_in_aai[%s] in aai.", self.vl_inst_id)
87         try:
88             # query network in aai, get resource_version
89             customer_info = query_network_aai(self.vl_inst_id)
90             resource_version = customer_info.get("resource-version")
91
92             # delete network from aai
93             resp_data, resp_status = delete_network_aai(self.vl_inst_id, resource_version)
94             logger.debug("Delete network[%s] from aai successfully, status: %s", self.vl_inst_id, resp_status)
95         except NSLCMException as e:
96             logger.debug("Fail to delete network[%s] from aai: %s", self.vl_inst_id, e.args[0])
97         except Exception as e:
98             logger.error("Exception occurs when delete network[%s] from aai: %s", self.vl_inst_id, e.args[0])
99             logger.error(traceback.format_exc())
100
101     def delete_vl_from_db(self, vl_inst_info):
102         # do_biz_with_share_lock("delete-vllist-in-vnffg-%s" % self.ns_inst_id, self.delete_vl_inst_id_in_vnffg)
103         self.delete_vl_inst_id_in_vnffg()
104         vl_inst_info.delete()