1 # Copyright 2016 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
27 logger = logging.getLogger(__name__)
30 class CreateNSService(object):
31 def __init__(self, csar_id, ns_name, description, context):
32 self.csar_id = csar_id
33 self.ns_name = ns_name
34 self.description = description
35 self.global_customer_id = ignore_case_get(context, 'globalCustomerId')
36 self.service_type = ignore_case_get(context, 'serviceType')
38 self.ns_package_id = ''
41 self.check_nsd_valid()
42 self.check_ns_inst_name_exist()
45 self.create_ns_in_aai()
46 logger.debug("CreateNSService::do_biz::ns_inst_id=%s" % self.ns_inst_id)
47 return self.ns_inst_id
49 def check_nsd_valid(self):
50 logger.debug("CreateNSService::check_nsd_valid::csar_id=%s" % self.csar_id)
51 ns_package_info = query_nspackage_by_id(self.csar_id)
52 if not ns_package_info:
53 raise NSLCMException("nsd(%s) not exists." % self.csar_id)
54 packageInfo = ns_package_info["packageInfo"]
55 self.ns_package_id = ignore_case_get(packageInfo, "nsPackageId")
56 self.nsd_id = ignore_case_get(packageInfo, "nsdId")
57 self.nsd_invariant_id = ignore_case_get(packageInfo, "nsdInvariantId")
58 logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s,nsd_id=%s", self.ns_package_id, self.nsd_id)
60 def check_ns_inst_name_exist(self):
61 is_exist = NSInstModel.objects.filter(name=self.ns_name).exists()
62 logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist)
64 raise NSLCMException("ns(%s) already existed." % self.ns_name)
66 def create_ns_inst(self):
67 self.ns_inst_id = str(uuid.uuid4())
68 logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
69 NSInstModel(id=self.ns_inst_id,
71 nspackage_id=self.ns_package_id,
73 nsd_invariant_id=self.nsd_invariant_id,
74 description=self.description,
75 status='NOT_INSTANTIATED', # 'empty',
76 lastuptime=now_time(),
77 global_customer_id=self.global_customer_id,
78 service_type=self.service_type).save()
80 def create_ns_in_aai(self):
81 logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
84 "service-instance-id": self.ns_inst_id,
85 "service-instance-name": self.ns_name,
86 "service-type": SERVICE_TYPE,
87 "service-role": SERVICE_ROLE
89 resp_data, resp_status = create_ns_aai(self.global_customer_id, self.service_type, self.ns_inst_id, data)
90 logger.debug("Success to create ns[%s] to aai:[%s],[%s].", self.ns_inst_id, resp_data, resp_status)
91 except NSLCMException as e:
92 logger.debug("Fail to createns[%s] to aai, detail message: %s" % (self.ns_inst_id, e.message))
94 logger.error(traceback.format_exc())