Don't write information to resmanagement
[vfc/nfvo/lcm.git] / lcm / ns_sfcs / biz / delete_sfcs.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.database.models import FPInstModel, VNFFGInstModel
20 from lcm.pub.exceptions import NSLCMException
21 from lcm.pub.msapi import extsys, resmgr, sdncdriver
22
23 logger = logging.getLogger(__name__)
24
25
26 class DeleteSfcs(object):
27     def __init__(self, sfc_inst_id):
28         self.sfc_inst_id = sfc_inst_id
29         self.ns_inst_id = ""
30
31     def do(self):
32         try:
33             sfc_inst_info = FPInstModel.objects.filter(fpinstid=self.sfc_inst_id)
34             if not sfc_inst_info:
35                 logger.warn("sfc inst id(%s) is not exist or has been already deleted" % self.sfc_inst_id)
36                 return {"result": 0, "detail": "sfc is not exist or has been already deleted"}
37             self.ns_inst_id = sfc_inst_info[0].nsinstid
38             self.delete_sfc_from_driver(sfc_inst_info[0])
39             # self.delete_sfc_from_resmgr()
40             self.delete_sfc_from_db(sfc_inst_info)
41             return {"result": 0, "detail": "delete sfc success"}
42         except NSLCMException as e:
43             return self.exception_handle(e)
44         except Exception as e:
45             logger.error(traceback.format_exc())
46             return self.exception_handle(e)
47
48     def exception_handle(self, e):
49         detail = 'sfc delete failed, detail message: %s' % e.args[0]
50         logger.error(detail)
51         return {"result": 1, "detail": detail}
52
53     def delete_sfc_from_driver(self, sfc_inst_info):
54         sdn_controller_id = sfc_inst_info.sdncontrollerid
55         sdn_controller_url = extsys.get_sdn_controller_by_id(sdn_controller_id)["url"]
56         sfc_id = sfc_inst_info.sfcid
57         flow_classifiers = sfc_inst_info.flowclassifiers
58         port_pair_groups = sfc_inst_info.portpairgroups
59         if sfc_id:
60             req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": sfc_id}
61             sdncdriver.delete_port_chain(req_param)
62         if flow_classifiers:
63             for flow_id in flow_classifiers.split(","):
64                 req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": flow_id}
65                 sdncdriver.delete_flow_classifier(req_param)
66         if port_pair_groups:
67             for group in json.JSONDecoder().decode(port_pair_groups):
68                 group_id = group["groupid"]
69                 req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": group_id}
70                 sdncdriver.delete_port_pair_group(req_param)
71                 port_pair = group["portpair"]
72                 for port_pair_id in port_pair:
73                     req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": port_pair_id}
74                     sdncdriver.delete_port_pair(req_param)
75
76     def delete_sfc_from_db(self, sfc_inst_info):
77         # do_biz_with_share_lock("delete-sfclist-in-vnffg-%s" % self.ns_inst_id, self.delete_sfc_inst_id_in_vnffg)
78         self.delete_sfc_inst_id_in_vnffg()
79         sfc_inst_info.delete()
80
81     def delete_sfc_from_resmgr(self):
82         resmgr.delete_sfc(self.sfc_inst_id)
83
84     def delete_sfc_inst_id_in_vnffg(self):
85         for vnffg_info in VNFFGInstModel.objects.filter(nsinstid=self.ns_inst_id):
86             new_sfc_id_list = ""
87             for old_sfc_id in vnffg_info.fplist.split(","):
88                 if old_sfc_id != self.sfc_inst_id:
89                     new_sfc_id_list += old_sfc_id + ","
90             new_sfc_id_list = new_sfc_id_list[:-1]
91             VNFFGInstModel.objects.filter(vnffginstid=vnffg_info.vnffginstid).update(fplist=new_sfc_id_list)