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 logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s,nsd_id=%s", self.ns_package_id, self.nsd_id)
59 def check_ns_inst_name_exist(self):
60 is_exist = NSInstModel.objects.filter(name=self.ns_name).exists()
61 logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist)
63 raise NSLCMException("ns(%s) already existed." % self.ns_name)
65 def create_ns_inst(self):
66 self.ns_inst_id = str(uuid.uuid4())
67 logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
68 NSInstModel(id=self.ns_inst_id,
70 nspackage_id=self.ns_package_id,
72 description=self.description,
74 lastuptime=now_time(),
75 global_customer_id=self.global_customer_id,
76 service_type=self.service_type).save()
78 def create_ns_in_aai(self):
79 logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
81 # global_customer_id = "global-customer-id-" + self.ns_inst_id
83 # "global-customer-id": "global-customer-id-" + self.ns_inst_id,
84 # "subscriber-name": "subscriber-name-" + self.ns_inst_id,
85 # "subscriber-type": "subscriber-type-" + self.ns_inst_id,
86 # "service-subscriptions": {
87 # "service-subscription": [
89 # "service-type": "Network",
90 # "service-instances": {
91 # "service-instance": [
93 # "service-instance-id": self.ns_inst_id,
94 # "service-instance-name": self.ns_name,
95 # "service-type": "Network",
96 # "service-role": "service-role-" + self.ns_inst_id
104 # resp_data, resp_status = create_customer_aai(global_customer_id, data)
106 "service-instance-id": self.ns_inst_id,
107 "service-instance-name": self.ns_name,
108 "service-type": SERVICE_TYPE,
109 "service-role": SERVICE_ROLE
111 resp_data, resp_status = create_ns_aai(self.global_customer_id, self.service_type, self.ns_inst_id, data)
112 logger.debug("Success to create ns[%s] to aai:[%s],[%s].", self.ns_inst_id, resp_data, resp_status)
113 except NSLCMException as e:
114 logger.debug("Fail to createns[%s] to aai, detail message: %s" % (self.ns_inst_id, e.message))
116 logger.error(traceback.format_exc())