Modify vfc-vnflcm vnf creation
[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.csar_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
35     def do_biz(self):
36         self.nf_inst_id = str(uuid.uuid4())
37         try:
38             self.check_valid()
39             self.save_db()
40         except NFLCMException as e:
41             logger.debug('Create VNF instance[%s]: %s', self.nf_inst_id, e.message)
42             raise NFLCMException(e.message)
43         except Exception as e:
44             logger.error(e.message)
45             logger.error(traceback.format_exc())
46             NfInstModel.objects.create(nfinstid=self.nf_inst_id,
47                                        nf_name=self.vnf_instance_mame,
48                                        package_id='',
49                                        version='',
50                                        vendor='',
51                                        netype='',
52                                        vnfd_model='',
53                                        status='NOT_INSTANTIATED',
54                                        nf_desc=self.description,
55                                        vnfdid=self.csar_id,
56                                        vnfSoftwareVersion='',
57                                        create_time=now_time())
58         vnf_inst = NfInstModel.objects.get(nfinstid=self.nf_inst_id)
59         resp = {
60             'id': vnf_inst.nfinstid,
61             'vnfInstanceName': vnf_inst.nf_name,
62             'vnfInstanceDescription': 'Human-readable description of the VNF instance.',
63             'vnfdId': vnf_inst.vnfdid,
64             'vnfProvider': vnf_inst.vendor,
65             'vnfProductName': vnf_inst.nf_name,
66             'vnfSoftwareVersion': vnf_inst.vnfSoftwareVersion,
67             'vnfdVersion': vnf_inst.version,
68             'vnfPkgId': vnf_inst.package_id,
69             'vnfConfigurableProperties': {}
70         }
71         return resp
72
73     def check_valid(self):
74         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
75         if is_exist:
76             raise NFLCMException('VNF is already exist.')
77         vnf_package_info = query_vnfpackage_by_id(self.csar_id)
78         self.vnfd_info = json.loads(ignore_case_get(ignore_case_get(vnf_package_info, "packageInfo"), "vnfdModel"))
79
80     def save_db(self):
81         metadata = ignore_case_get(self.vnfd_info, "metadata")
82         version = ignore_case_get(metadata, "vnfdVersion")
83         vendor = ignore_case_get(metadata, "vendor")
84         netype = ignore_case_get(metadata, "type")
85         vnfsoftwareversion = ignore_case_get(metadata, "version")
86         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
87                                    nf_name=self.vnf_instance_mame,
88                                    package_id=self.csar_id,
89                                    version=version,
90                                    vendor=vendor,
91                                    netype=netype,
92                                    vnfd_model=self.vnfd_info,
93                                    status='NOT_INSTANTIATED',
94                                    nf_desc=self.description,
95                                    vnfdid=self.csar_id,
96                                    vnfSoftwareVersion=vnfsoftwareversion,
97                                    create_time=now_time())
98         logger.debug('Create VNF instance[%s] success', self.nf_inst_id)