add comments
[vfc/nfvo/lcm.git] / lcm / ns / biz / ns_create.py
1 # Copyright 2016 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 logging
15 import traceback
16 import uuid
17
18 from lcm.pub.config.config import REPORT_TO_AAI
19 from lcm.pub.database.models import NSInstModel
20 from lcm.pub.exceptions import NSLCMException
21 from lcm.pub.msapi.aai import create_ns_aai
22 from lcm.pub.msapi.sdc_run_catalog import query_nspackage_by_id
23 from lcm.pub.utils.timeutil import now_time
24 from lcm.pub.utils.values import ignore_case_get
25 from lcm.ns.const import SERVICE_ROLE, SERVICE_TYPE
26
27 logger = logging.getLogger(__name__)
28
29
30 class CreateNSService(object):
31     """
32     This class for NS instance Model create
33     """
34
35     def __init__(self, csar_id, ns_name, description, context):
36         self.csar_id = csar_id
37         self.ns_name = ns_name
38         self.description = description
39         self.global_customer_id = ignore_case_get(context, 'globalCustomerId')
40         self.service_type = ignore_case_get(context, 'serviceType')
41         self.ns_inst_id = ''
42         self.ns_package_id = ''
43
44     def do_biz(self):
45         """
46         Create NS instance model
47         :return:
48         """
49         self.check_nsd_valid()
50         self.check_ns_inst_name_exist()
51         self.create_ns_inst()
52         if REPORT_TO_AAI:
53             self.create_ns_in_aai()
54         logger.debug("CreateNSService::do_biz::ns_inst_id=%s" % self.ns_inst_id)
55         return self.ns_inst_id
56
57     def check_nsd_valid(self):
58         """
59         Check the validation of NSD
60         :return:
61         """
62         logger.debug("CreateNSService::check_nsd_valid::csar_id=%s" % self.csar_id)
63         ns_package_info = query_nspackage_by_id(self.csar_id)
64         if not ns_package_info:
65             raise NSLCMException("nsd(%s) not exists." % self.csar_id)
66         packageInfo = ns_package_info["packageInfo"]
67         self.ns_package_id = ignore_case_get(packageInfo, "nsPackageId")
68         self.nsd_id = ignore_case_get(packageInfo, "nsdId")
69         self.nsd_invariant_id = ignore_case_get(packageInfo, "nsdInvariantId")
70         logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s,nsd_id=%s", self.ns_package_id, self.nsd_id)
71
72     def check_ns_inst_name_exist(self):
73         """
74         Check if the ns instance with same name exists
75         :return:
76         """
77         is_exist = NSInstModel.objects.filter(name=self.ns_name).exclude(status='null').exists()
78         logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist)
79         if is_exist:
80             raise NSLCMException("ns(%s) already existed." % self.ns_name)
81
82     def create_ns_inst(self):
83         """
84         Create NS instance Model
85         :return:
86         """
87         self.ns_inst_id = str(uuid.uuid4())
88         logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
89         NSInstModel(id=self.ns_inst_id,
90                     name=self.ns_name,
91                     nspackage_id=self.ns_package_id,
92                     nsd_id=self.nsd_id,
93                     nsd_invariant_id=self.nsd_invariant_id,
94                     description=self.description,
95                     status='NOT_INSTANTIATED',  # 'empty',
96                     lastuptime=now_time(),
97                     global_customer_id=self.global_customer_id,
98                     service_type=self.service_type).save()
99
100     def create_ns_in_aai(self):
101         """
102         Create NS instance record in AAI
103         :return:
104         """
105         logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
106         try:
107             data = {
108                 "service-instance-id": self.ns_inst_id,
109                 "service-instance-name": self.ns_name,
110                 "service-type": SERVICE_TYPE,
111                 "service-role": SERVICE_ROLE
112             }
113             resp_data, resp_status = create_ns_aai(self.global_customer_id, self.service_type, self.ns_inst_id, data)
114             logger.debug("Success to create ns[%s] to aai:[%s],[%s].", self.ns_inst_id, resp_data, resp_status)
115         except NSLCMException as e:
116             logger.debug("Fail to createns[%s] to aai, detail message: %s" % (self.ns_inst_id, e.args[0]))
117         except:
118             logger.error(traceback.format_exc())