Modify code of apply_grant
[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.msapi.catalog import query_rawdata_from_catalog
21 from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get, get_packageinfo_by_vnfdid
22 from lcm.pub.utils import toscautil
23 from lcm.pub.utils.timeutil import now_time
24 from lcm.pub.utils.values import ignore_case_get
25
26 logger = logging.getLogger(__name__)
27
28
29 class CreateVnf:
30     def __init__(self, data):
31         self.data = data
32         self.vnfd_id = ignore_case_get(self.data, "vnfdId")
33         self.vnf_instance_mame = ignore_case_get(self.data, "vnfInstanceName")
34         self.description = ignore_case_get(self.data, "vnfInstanceDescription")
35         self.package_info = ''
36         self.package_id = ''
37         self.csar_id = ''
38
39     def do_biz(self):
40         logger.debug("CreateVnfIdentifier--CreateVnf::> %s" % self.data)
41         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
42         logger.debug("check_inst_name_exist::is_exist=%s" % is_exist)
43         if is_exist:
44             raise NFLCMException('VNF is already exist.')
45
46         # get package_info from nslcm by vnfd_id
47         self.package_info = get_packageinfo_by_vnfdid(self.vnfd_id)
48         self.package_id = ignore_case_get(self.package_info, "package_id")
49         self.csar_id = ignore_case_get(self.package_info, "csar_id")
50
51         # get rawdata from catalog by csar_id
52         raw_data = query_rawdata_from_catalog(self.csar_id, self.data)
53         self.vnfd = toscautil.convert_vnfd_model(raw_data["rawData"])  # convert to inner json
54         self.vnfd = json.JSONDecoder().decode(self.vnfd)
55
56         metadata = ignore_case_get(self.vnfd, "metadata")
57         version = ignore_case_get(metadata, "vnfd_version")
58         vendor = ignore_case_get(metadata, "vendor")
59         netype = ignore_case_get(metadata, "vnf_type")
60         vnfsoftwareversion = ignore_case_get(metadata, "version")
61         vnfd_model = self.vnfd
62
63         nf_inst_id = str(uuid.uuid4())
64         NfInstModel.objects.create(nfinstid=nf_inst_id, nf_name=self.vnf_instance_mame, package_id=self.package_id,
65                                    version=version, vendor=vendor, netype=netype, vnfd_model=vnfd_model,
66                                    status='NOT_INSTANTIATED', nf_desc=self.description, vnfdid=self.vnfd_id,
67                                    vnfSoftwareVersion=vnfsoftwareversion, create_time=now_time())
68         vnf_inst = NfInstModel.objects.get(nfinstid=nf_inst_id)
69         logger.debug('id is [%s],name is [%s],vnfd_id is [%s],description is [%s],create_time is [%s]' %
70                      (vnf_inst.nfinstid, vnf_inst.nf_name, vnf_inst.vnfdid, vnf_inst.nf_desc, vnf_inst.create_time))
71         return nf_inst_id