Add default value of nf inst
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / create_vnf.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.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.csar_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
34     def do_biz(self):
35         self.nf_inst_id = str(uuid.uuid4())
36         self.check_valid()
37         self.save_db()
38         vnf_inst = NfInstModel.objects.get(nfinstid=self.nf_inst_id)
39         return vnf_inst
40
41     def check_valid(self):
42         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
43         if is_exist:
44             raise NFLCMException('VNF is already exist.')
45         vnf_package_info = query_vnfpackage_by_id(self.csar_id)
46         self.vnfd_info = json.loads(ignore_case_get(ignore_case_get(vnf_package_info, "packageInfo"), "vnfdModel"))
47
48     def save_db(self):
49         metadata = ignore_case_get(self.vnfd_info, "metadata")
50         version = ignore_case_get(metadata, "csarVersion", "undefined")
51         provider = ignore_case_get(metadata, "csarProvider", "undefined")
52         netype = ignore_case_get(metadata, "type", "undefined")
53         vnfsoftwareversion = ignore_case_get(metadata, "version", "undefined")
54         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
55                                    nf_name=self.vnf_instance_mame,
56                                    package_id=self.csar_id,
57                                    version=version,
58                                    vendor=provider,
59                                    netype=netype,
60                                    vnfd_model=json.dumps(self.vnfd_info),
61                                    status='NOT_INSTANTIATED',
62                                    nf_desc=self.description,
63                                    vnfdid=self.csar_id,
64                                    vnfSoftwareVersion=vnfsoftwareversion,
65                                    create_time=now_time())
66         logger.debug('Create VNF instance[%s] success', self.nf_inst_id)