Decompose the VNF instance
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / vnf_create / create_vnf_identifier.py
1 # Copyright 2017 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 import json
15 import logging
16 import uuid
17
18 from lcm.pub.database.models import NfInstModel
19 from lcm.pub.exceptions import NFLCMException
20 from lcm.pub.utils.restcall import req_by_msb
21 from lcm.pub.utils.timeutil import now_time
22 from lcm.pub.utils.values import ignore_case_get
23
24 logger = logging.getLogger(__name__)
25 # Query vnfd_rawdata by vnfdid
26 def vnfd_rawdata_get(vnfdid):
27     ret = req_by_msb("openoapi/nslcm/v1/vnfpackage/%s" % vnfdid, "GET")
28     return ret
29
30 class CreateVnf:
31     def __init__(self, data):
32         self.data = data
33
34     def do_biz(self):
35         logger.debug("CreateVnfIdentifier--CreateVnf::> %s" % self.data)
36         self.vnfd_id = ignore_case_get(self.data, "vnfdId")
37         self.vnf_instance_mame = ignore_case_get(self.data, "vnfInstanceName")
38         self.description = ignore_case_get(self.data, "vnfInstanceDescription")
39         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
40         logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
41         if is_exist:
42             raise NFLCMException('VNF is already exist.')
43
44         #get rawdata by vnfd_id
45         ret = vnfd_rawdata_get(self.vnfd_id)
46         if ret[0] != 0:
47             raise NFLCMException('Get vnfd_raw_data failed.')
48         dst_plan = json.JSONDecoder().decode(ret[1])
49         self.vnfd_version = dst_plan['metadata']['vnfd_version']
50         self.vendor = dst_plan['metadata']['vendor']
51         self.producttype = dst_plan['metadata']['domain_type']
52         self.netype = dst_plan['metadata']['vnf_type']
53         self.vnfd_model = dst_plan
54         self.vnfSoftwareVersion = dst_plan['metadata']['version']
55
56         self.nf_inst_id = str(uuid.uuid4())
57         NfInstModel.objects.create(nfinstid=self.nf_inst_id, mnfinstid=self.nf_inst_id, nf_name=self.vnf_instance_mame,
58                                    package_id='todo', vnfm_inst_id='todo', version=self.vnfd_version, vendor=self.vendor,
59                                    producttype=self.producttype,netype=self.netype, vnfd_model=self.vnfd_model,
60                                    instantiationState='NOT_INSTANTIATED', nf_desc=self.description, vnfdid=self.vnfd_id,
61                                    vnfSoftwareVersion=self.vnfSoftwareVersion, vnfConfigurableProperties='todo',
62                                    localizationLanguage='EN_US',create_time=now_time())
63         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
64         logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
65         return self.nf_inst_id