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