683b3baaa043e68036d74684915e88c9ec8d4649
[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 json
16 import logging
17 import uuid
18
19 from lcm.pub.database.models import NfInstModel
20 from lcm.pub.exceptions import NFLCMException
21 from lcm.pub.msapi.aai import create_vnf_aai
22 from lcm.pub.msapi.catalog import query_rawdata_from_catalog
23 from lcm.pub.msapi.gvnfmdriver import get_packageinfo_by_vnfdid
24 from lcm.pub.utils import toscautil
25 from lcm.pub.utils.timeutil import now_time
26 from lcm.pub.utils.values import ignore_case_get
27
28 logger = logging.getLogger(__name__)
29
30
31 class CreateVnf:
32     def __init__(self, data):
33         self.data = data
34         self.vnfd_id = ignore_case_get(self.data, "vnfdId")
35         self.vnf_instance_mame = ignore_case_get(self.data, "vnfInstanceName")
36         self.description = ignore_case_get(self.data, "vnfInstanceDescription")
37         self.vnfd = None
38         self.package_info = ''
39         self.package_id = ''
40         self.csar_id = ''
41
42     def do_biz(self):
43         self.nf_inst_id = str(uuid.uuid4())
44         try:
45             self.check_vnf_name_valid()
46             self.get_vnfd_info()
47             self.save_info_to_db()
48             self.create_vnf_in_aai()
49         except NFLCMException as e:
50             logger.debug('Create VNF instance[%s] to AAI failed' % self.nf_inst_id)
51         except:
52             NfInstModel.objects.create(nfinstid=self.nf_inst_id, nf_name=self.vnf_instance_mame, package_id='',
53                                        version='', vendor='', netype='', vnfd_model='',
54                                        status='NOT_INSTANTIATED', nf_desc=self.description, vnfdid=self.vnfd_id,
55                                        vnfSoftwareVersion='', 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_vnf_name_valid(self):
65         logger.debug("CreateVnfIdentifier--CreateVnf::> %s" % self.data)
66         is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
67         logger.debug("check_inst_name_exist::is_exist=%s" % is_exist)
68         if is_exist:
69             raise NFLCMException('VNF is already exist.')
70
71     def get_vnfd_info(self):
72         self.nf_inst_id = str(uuid.uuid4())
73         self.package_info = get_packageinfo_by_vnfdid(self.vnfd_id)
74         for val in ignore_case_get(self.package_info, "csars"):
75             if self.vnfd_id == ignore_case_get(val, "vnfdId"):
76                 self.package_id = ignore_case_get(val, "csarId")
77                 break
78
79         raw_data = query_rawdata_from_catalog(self.package_id)
80         self.vnfd = toscautil.convert_vnfd_model(raw_data["rawData"])  # convert to inner json
81         self.vnfd = json.JSONDecoder().decode(self.vnfd)
82
83     def save_info_to_db(self):
84         metadata = ignore_case_get(self.vnfd, "metadata")
85         version = ignore_case_get(metadata, "vnfd_version")
86         vendor = ignore_case_get(metadata, "vendor")
87         netype = ignore_case_get(metadata, "vnf_type")
88         vnfsoftwareversion = ignore_case_get(metadata, "version")
89         vnfd_model = self.vnfd
90         NfInstModel.objects.create(nfinstid=self.nf_inst_id, nf_name=self.vnf_instance_mame, package_id=self.package_id,
91                                    version=version, vendor=vendor, netype=netype, vnfd_model=vnfd_model,
92                                    status='NOT_INSTANTIATED', nf_desc=self.description, vnfdid=self.vnfd_id,
93                                    vnfSoftwareVersion=vnfsoftwareversion, create_time=now_time())
94
95     def create_vnf_in_aai(self):
96         logger.debug("CreateVnf::create_vnf_in_aai::report vnf instance[%s] to aai." % self.nf_inst_id)
97         data = {
98             "vnf-id": self.nf_inst_id,
99             "vnf-name": self.vnf_instance_mame,
100             "vnf-type": "INFRA",
101             "in-maint": True,
102             "is-closed-loop-disabled": False
103         }
104         resp_data, resp_status = create_vnf_aai(self.nf_inst_id, data)
105         if resp_data:
106             logger.debug("Fail to create vnf instance[%s] to aai, resp_status: [%s]." % (self.nf_inst_id, resp_status))
107         else:
108             logger.debug("Success to create vnf instance[%s] to aai, resp_status: [%s]." % (self.nf_inst_id, resp_status))