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