Fix vfc-vnflcm create resource issue
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / 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 traceback
17 import uuid
18
19 from lcm.pub.database.models import NfInstModel
20 from lcm.pub.exceptions import NFLCMException
21 from lcm.pub.msapi.sdc_run_catalog import query_vnfpackage_by_id
22 from lcm.pub.utils.timeutil import now_time
23 from lcm.pub.utils.values import ignore_case_get
24
25 logger = logging.getLogger(__name__)
26
27
28 class CreateVnf:
29     def __init__(self, data):
30         self.data = data
31         self.vnfd_id = ignore_case_get(self.data, "vnfdId")
32         self.vnf_instance_mame = ignore_case_get(self.data, "vnfInstanceName")
33         self.description = ignore_case_get(self.data, "vnfInstanceDescription")
34         self.vnfd = None
35         self.csar_id = self.vnfd_id
36
37     def do_biz(self):
38         self.nf_inst_id = str(uuid.uuid4())
39         try:
40             self.check_valid()
41             self.save_db()
42         except NFLCMException as e:
43             logger.debug('Create VNF instance[%s]: %s', self.nf_inst_id, e.message)
44             raise NFLCMException(e.message)
45         except Exception as e:
46             logger.error(e.message)
47             logger.error(traceback.format_exc())
48             NfInstModel.objects.create(nfinstid=self.nf_inst_id,
49                                        nf_name=self.vnf_instance_mame,
50                                        package_id='',
51                                        version='',
52                                        vendor='',
53                                        netype='',
54                                        vnfd_model='',
55                                        status='NOT_INSTANTIATED',
56                                        nf_desc=self.description,
57                                        vnfdid=self.vnfd_id,
58                                        vnfSoftwareVersion='',
59                                        create_time=now_time())
60
61         vnf_inst = NfInstModel.objects.get(nfinstid=self.nf_inst_id)
62         logger.debug('id is [%s],name is [%s],vnfd_id is [%s],vnfd_model is [%s],'
63                      'description is [%s],create_time is [%s]' %
64                      (vnf_inst.nfinstid, vnf_inst.nf_name, vnf_inst.vnfdid,
65                       vnf_inst.vnfd_model, vnf_inst.nf_desc, vnf_inst.create_time))
66         resp = {
67             'id': vnf_inst.nfinstid,
68             'vnfInstanceName': vnf_inst.nf_name,
69             'vnfInstanceDescription': 'Human-readable description of the VNF instance.',
70             'vnfdId': vnf_inst.vnfdid,
71             'vnfProvider': vnf_inst.vendor,
72             'vnfProductName': vnf_inst.nf_name,
73             'vnfSoftwareVersion': vnf_inst.vnfSoftwareVersion,
74             'vnfdVersion': vnf_inst.version,
75             'vnfPkgId': vnf_inst.package_id,
76             'vnfConfigurableProperties': {}
77         }
78         return resp
79
80     def check_valid(self):
81         logger.debug("CreateVnf--check_valid::> %s" % self.data)
82         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
83         logger.debug("check_valid::is_exist=%s" % is_exist)
84         if is_exist:
85             raise NFLCMException('VNF is already exist.')
86         vnf_package_info = query_vnfpackage_by_id(self.csar_id)
87         self.vnfd_info = json.loads(ignore_case_get(ignore_case_get(vnf_package_info, "packageInfo"), "vnfdModel"))
88
89     def save_db(self):
90         metadata = ignore_case_get(self.vnfd_info, "metadata")
91         version = ignore_case_get(metadata, "vnfdVersion")
92         vendor = ignore_case_get(metadata, "vendor")
93         netype = ignore_case_get(metadata, "type")
94         vnfsoftwareversion = ignore_case_get(metadata, "version")
95         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
96                                    nf_name=self.vnf_instance_mame,
97                                    package_id=self.csar_id,
98                                    version=version,
99                                    vendor=vendor,
100                                    netype=netype,
101                                    vnfd_model=self.vnfd_info,
102                                    status='NOT_INSTANTIATED',
103                                    nf_desc=self.description,
104                                    vnfdid=self.vnfd_id,
105                                    vnfSoftwareVersion=vnfsoftwareversion,
106                                    create_time=now_time())