1 # Copyright 2017 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 NfInstModel
20 from lcm.pub.exceptions import NFLCMException
21 from lcm.pub.msapi.catalog import query_rawdata_from_catalog
22 from lcm.pub.msapi.gvnfmdriver import get_packageinfo_by_vnfdid
23 from lcm.pub.utils import toscautil
24 from lcm.pub.utils.timeutil import now_time
25 from lcm.pub.utils.values import ignore_case_get
27 logger = logging.getLogger(__name__)
31 def __init__(self, data):
33 self.vnfd_id = ignore_case_get(self.data, "vnfdId")
34 self.vnf_instance_mame = ignore_case_get(self.data, "vnfInstanceName")
35 self.description = ignore_case_get(self.data, "vnfInstanceDescription")
37 self.package_info = ''
42 self.nf_inst_id = str(uuid.uuid4())
44 self.check_vnf_name_valid()
46 self.save_info_to_db()
47 except NFLCMException as e:
48 logger.debug('Create VNF instance[%s] to AAI failed: %s', self.nf_inst_id, e.message)
50 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
51 nf_name=self.vnf_instance_mame,
57 status='NOT_INSTANTIATED',
58 nf_desc=self.description,
60 vnfSoftwareVersion='',
61 create_time=now_time())
63 vnf_inst = NfInstModel.objects.get(nfinstid=self.nf_inst_id)
64 logger.debug('id is [%s],name is [%s],vnfd_id is [%s],vnfd_model is [%s],'
65 'description is [%s],create_time is [%s]' %
66 (vnf_inst.nfinstid, vnf_inst.nf_name, vnf_inst.vnfdid,
67 vnf_inst.vnfd_model, vnf_inst.nf_desc, vnf_inst.create_time))
68 return self.nf_inst_id
70 def check_vnf_name_valid(self):
71 logger.debug("CreateVnfIdentifier--CreateVnf::> %s" % self.data)
72 is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
73 logger.debug("check_inst_name_exist::is_exist=%s" % is_exist)
75 raise NFLCMException('VNF is already exist.')
77 def get_vnfd_info(self):
78 self.nf_inst_id = str(uuid.uuid4())
79 self.package_info = get_packageinfo_by_vnfdid(self.vnfd_id)
80 for val in ignore_case_get(self.package_info, "csars"):
81 if self.vnfd_id == ignore_case_get(val, "vnfdId"):
82 self.package_id = ignore_case_get(val, "csarId")
85 raw_data = query_rawdata_from_catalog(self.package_id)
86 self.vnfd = toscautil.convert_vnfd_model(raw_data["rawData"]) # convert to inner json
87 self.vnfd = json.JSONDecoder().decode(self.vnfd)
89 def save_info_to_db(self):
90 metadata = ignore_case_get(self.vnfd, "metadata")
91 version = ignore_case_get(metadata, "vnfd_version")
92 vendor = ignore_case_get(metadata, "vendor")
93 netype = ignore_case_get(metadata, "vnf_type")
94 vnfsoftwareversion = ignore_case_get(metadata, "version")
95 vnfd_model = self.vnfd
96 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
97 nf_name=self.vnf_instance_mame,
98 package_id=self.package_id,
102 vnfd_model=vnfd_model,
103 status='NOT_INSTANTIATED',
104 nf_desc=self.description,
106 vnfSoftwareVersion=vnfsoftwareversion,
107 create_time=now_time())