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.database.models import FPInstModel, VNFFGInstModel
20 from lcm.pub.exceptions import NSLCMException
21 from lcm.pub.msapi import extsys, sdncdriver
23 logger = logging.getLogger(__name__)
26 class DeleteSfcs(object):
27 def __init__(self, sfc_inst_id):
28 self.sfc_inst_id = sfc_inst_id
33 sfc_inst_info = FPInstModel.objects.filter(fpinstid=self.sfc_inst_id)
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_db(sfc_inst_info)
40 return {"result": 0, "detail": "delete sfc success"}
41 except NSLCMException as e:
42 return self.exception_handle(e)
43 except Exception as e:
44 logger.error(traceback.format_exc())
45 return self.exception_handle(e)
47 def exception_handle(self, e):
48 detail = 'sfc delete failed, detail message: %s' % e.args[0]
50 return {"result": 1, "detail": detail}
52 def delete_sfc_from_driver(self, sfc_inst_info):
53 sdn_controller_id = sfc_inst_info.sdncontrollerid
54 sdn_controller_url = extsys.get_sdn_controller_by_id(sdn_controller_id)["url"]
55 sfc_id = sfc_inst_info.sfcid
56 flow_classifiers = sfc_inst_info.flowclassifiers
57 port_pair_groups = sfc_inst_info.portpairgroups
59 req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": sfc_id}
60 sdncdriver.delete_port_chain(req_param)
62 for flow_id in flow_classifiers.split(","):
63 req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": flow_id}
64 sdncdriver.delete_flow_classifier(req_param)
66 for group in json.JSONDecoder().decode(port_pair_groups):
67 group_id = group["groupid"]
68 req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": group_id}
69 sdncdriver.delete_port_pair_group(req_param)
70 port_pair = group["portpair"]
71 for port_pair_id in port_pair:
72 req_param = {"sdnControllerId": sdn_controller_id, "url": sdn_controller_url, "id": port_pair_id}
73 sdncdriver.delete_port_pair(req_param)
75 def delete_sfc_from_db(self, sfc_inst_info):
76 # do_biz_with_share_lock("delete-sfclist-in-vnffg-%s" % self.ns_inst_id, self.delete_sfc_inst_id_in_vnffg)
77 self.delete_sfc_inst_id_in_vnffg()
78 sfc_inst_info.delete()
80 def delete_sfc_inst_id_in_vnffg(self):
81 for vnffg_info in VNFFGInstModel.objects.filter(nsinstid=self.ns_inst_id):
83 for old_sfc_id in vnffg_info.fplist.split(","):
84 if old_sfc_id != self.sfc_inst_id:
85 new_sfc_id_list += old_sfc_id + ","
86 new_sfc_id_list = new_sfc_id_list[:-1]
87 VNFFGInstModel.objects.filter(vnffginstid=vnffg_info.vnffginstid).update(fplist=new_sfc_id_list)