c92071359f1bbcb8a43b621599fddd69d03b762e
[vfc/nfvo/lcm.git] / lcm / ns_pnfs / biz / create_pnf.py
1 # Copyright 2018 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 from lcm.pub.database.models import PNFInstModel
17 from lcm.pub.exceptions import NSLCMException
18 from lcm.pub.msapi.sdc_run_catalog import query_pnf_descriptor
19 from lcm.ns_pnfs.biz.get_pnf import GetPnf
20
21 logger = logging.getLogger(__name__)
22
23
24 class CreatePnf(object):
25     def __init__(self, data):
26         self.pnfId = data.get("pnfId"),
27         self.pnfName = data.get("pnfName"),
28         self.pnfdId = data.get("pnfdId"),
29         self.pnfdInfoId = data.get("pnfdInfoId"),
30         self.pnfProfileId = data.get("pnfProfileId"),
31         self.cpInfo = data.get("cpInfo", "")
32         self.emsId = data.get("emsId", "")
33         self.nsInstances = data.get("nsInstances")
34
35     def do_biz(self):
36         self.check_pnfd_valid()
37         self.build_cpInfo()
38         self.build_emsId()
39         self.create_pnf_inst()
40         logger.debug("CreatePnf::do_biz::pnfId=%s" % self.pnfId)
41         return GetPnf({"pnfId": self.pnfId}, True).do_biz()
42
43     def check_pnfd_valid(self):
44         pnf_package_info = query_pnf_descriptor({"pnfId": self.pnfdInfoId})
45         if not pnf_package_info:
46             raise NSLCMException("Pnfd(%s) does not exist." % self.pnfdInfoId)
47
48     def build_cpInfo(self):
49         # todo
50         pass
51
52     def build_emsId(self):
53         # todo
54         pass
55
56     def create_pnf_inst(self):
57         pnfInstances = PNFInstModel.objects.filter(pnfId=self.pnfId)
58         if pnfInstances:
59             if not pnfInstances.filter(nsInstances__contains=self.nsInstances):
60                 for pnfInstance in pnfInstances:
61                     new_nsInstances = pnfInstance.nsInstances + "," + self.nsInstances
62                     pnfInstance.update(nsInstances=new_nsInstances)
63         else:
64             PNFInstModel(pnfId=self.pnfId,
65                          pnfName=self.pnfName,
66                          pnfdId=self.pnfdId,
67                          pnfdInfoId=self.pnfdInfoId,
68                          pnfProfileId=self.pnfProfileId,
69                          cpInfo=self.cpInfo,
70                          emsId=self.emsId,
71                          nsInstances=self.nsInstances).save()