9d6e2cdf1527d11a9c0f15818a090f2dd65f4838
[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
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.sdc_run_catalog import query_vnfpackage_by_id
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
26
27 class CreateVnf:
28     def __init__(self, data):
29         self.data = data
30         self.vnfd_id = ignore_case_get(self.data, "vnfdId")
31         self.vnf_instance_mame = ignore_case_get(self.data, "vnfInstanceName")
32         self.description = ignore_case_get(self.data, "vnfInstanceDescription")
33         self.vnfd = None
34         self.csar_id = self.vnfd_id
35
36     def do_biz(self):
37         self.nf_inst_id = str(uuid.uuid4())
38         try:
39             self.check_valid()
40             self.save_db()
41         except NFLCMException as e:
42             logger.debug('Create VNF instance[%s]: %s', self.nf_inst_id, e.message)
43         except:
44             NfInstModel.objects.create(nfinstid=self.nf_inst_id,
45                                        nf_name=self.vnf_instance_mame,
46                                        package_id='',
47                                        version='',
48                                        vendor='',
49                                        netype='',
50                                        vnfd_model='',
51                                        status='NOT_INSTANTIATED',
52                                        nf_desc=self.description,
53                                        vnfdid=self.vnfd_id,
54                                        vnfSoftwareVersion='',
55                                        create_time=now_time())
56
57         vnf_inst = NfInstModel.objects.get(nfinstid=self.nf_inst_id)
58         logger.debug('id is [%s],name is [%s],vnfd_id is [%s],vnfd_model is [%s],'
59                      'description is [%s],create_time is [%s]' %
60                      (vnf_inst.nfinstid, vnf_inst.nf_name, vnf_inst.vnfdid,
61                       vnf_inst.vnfd_model, vnf_inst.nf_desc, vnf_inst.create_time))
62         return self.nf_inst_id
63
64     def check_valid(self):
65         logger.debug("CreateVnf--check_valid::> %s" % self.data)
66         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
67         logger.debug("check_valid::is_exist=%s" % is_exist)
68         if is_exist:
69             raise NFLCMException('VNF is already exist.')
70         self.vnfdModel = query_vnfpackage_by_id(self.csar_id)
71
72     def save_db(self):
73         metadata = ignore_case_get(self.vnfdModel, "metadata")
74         version = ignore_case_get(metadata, "vnfdVersion")
75         vendor = ignore_case_get(metadata, "vendor")
76         netype = ignore_case_get(metadata, "type")
77         vnfsoftwareversion = ignore_case_get(metadata, "version")
78         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
79                                    nf_name=self.vnf_instance_mame,
80                                    package_id=self.csar_id,
81                                    version=version,
82                                    vendor=vendor,
83                                    netype=netype,
84                                    vnfd_model=self.vnfdModel,
85                                    status='NOT_INSTANTIATED',
86                                    nf_desc=self.description,
87                                    vnfdid=self.vnfd_id,
88                                    vnfSoftwareVersion=vnfsoftwareversion,
89                                    create_time=now_time())