update version of lcm
[vfc/nfvo/lcm.git] / lcm / ns / sfcs / sfc_instance.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
16 import logging
17
18 from lcm.pub.database.models import VNFFGInstModel, FPInstModel
19 from lcm.pub.exceptions import NSLCMException
20 from lcm.pub.utils.share_lock import do_biz_with_share_lock
21
22 logger = logging.getLogger(__name__)
23
24
25 class SfcInstance(object):
26     def __init__(self, data):
27         self.ns_inst_id = data["nsinstid"]
28         self.ns_model_data = data["ns_model_data"]
29         self.fp_id = data["fpindex"]
30         self.fp_inst_id = data["fpinstid"]
31         self.sdnControllerId = data["sdncontrollerid"]
32
33     def do_biz(self):
34         self.init_data()
35         return self.save()
36
37     def init_data(self):
38         self.fp_model = self.get_fp_model_by_fp_id()
39         logger.info("fp_model.properties:%s, fp_id:%s" % (self.fp_model["properties"], self.fp_id))
40         if not self.fp_model:
41             return
42         logger.info("sfc_inst_symmetric %s" % self.fp_model["properties"].get("symmetric"))
43         self.symmetric = self.fp_model["properties"].get("symmetric")
44         logger.info("sfc_inst_symmetric %s" % self.symmetric)
45         self.policyinfo = self.fp_model["properties"].get("policy")
46         self.status = "processing"
47         vnffg_database_info = VNFFGInstModel.objects.filter(vnffgdid=self.get_vnffgdid_by_fp_id(),
48                                                             nsinstid=self.ns_inst_id).get()
49         self.vnffg_inst_id = vnffg_database_info.vnffginstid
50
51     def get_fp_model_by_fp_id(self):
52         fps_model = self.ns_model_data["fps"]
53         for fp_model in fps_model:
54             if fp_model["fp_id"] == self.fp_id:
55                 return fp_model
56         return None
57
58     def get_vnffgdid_by_fp_id(self):
59         vnffgs_model = self.ns_model_data["vnffgs"]
60         for vnffg_model in vnffgs_model:
61             fp_ids = vnffg_model["members"]
62             for fp_id in fp_ids:
63                 if fp_id == self.fp_id:
64                     return vnffg_model["vnffg_id"]
65
66     def save(self):
67         try:
68             logger.info("Sfc Instanciate save2db start : ")
69             FPInstModel(fpid=self.fp_id,
70                         fpinstid=self.fp_inst_id,
71                         nsinstid=self.ns_inst_id,
72                         vnffginstid=self.vnffg_inst_id,
73                         symmetric=1 if self.symmetric else 0,
74                         policyinfo=self.policyinfo,
75                         status=self.status,
76                         sdncontrollerid=self.sdnControllerId
77                         ).save()
78
79             do_biz_with_share_lock("update-sfclist-in-vnffg-%s" % self.ns_inst_id, self.update_vnfffg_info)
80             logger.info("Sfc Instanciate save2db end : ")
81
82         except:
83             logger.error('SFC instantiation failed')
84             raise NSLCMException('SFC instantiation failed.')
85         return {
86             "fpinstid": self.fp_inst_id
87         }
88
89     def update_vnfffg_info(self):
90         vnffg_database_info = VNFFGInstModel.objects.filter(vnffginstid=self.vnffg_inst_id).get()
91         fp_inst_list = vnffg_database_info.fplist
92         fp_inst_list = fp_inst_list + ',' + self.fp_inst_id if fp_inst_list else self.fp_inst_id
93         VNFFGInstModel.objects.filter(vnffginstid=self.vnffg_inst_id).update(fplist=fp_inst_list)