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.
17 from lcm.pub.config.config import REPORT_TO_AAI
18 from lcm.pub.database.models import NSInstModel
19 from lcm.pub.exceptions import NSLCMException
20 from lcm.pub.msapi.aai import create_customer_aai
21 from lcm.pub.msapi.sdc_run_catalog import query_nspackage_by_id
22 from lcm.pub.utils.timeutil import now_time
23 from lcm.pub.utils.values import ignore_case_get
25 logger = logging.getLogger(__name__)
28 class CreateNSService(object):
29 def __init__(self, csar_id, ns_name, description):
30 self.csar_id = csar_id
31 self.ns_name = ns_name
32 self.description = description
34 self.ns_package_id = ''
37 self.check_nsd_valid()
38 self.check_ns_inst_name_exist()
41 self.create_ns_in_aai()
42 logger.debug("CreateNSService::do_biz::ns_inst_id=%s" % self.ns_inst_id)
43 return self.ns_inst_id
45 def check_nsd_valid(self):
46 logger.debug("CreateNSService::check_nsd_valid::csar_id=%s" % self.csar_id)
47 ns_package_info = query_nspackage_by_id(self.csar_id)
48 if not ns_package_info:
49 raise NSLCMException("nsd(%s) not exists." % self.csar_id)
50 packageInfo = ns_package_info["packageInfo"]
51 self.ns_package_id = ignore_case_get(packageInfo, "nsPackageId")
52 self.nsd_id = ignore_case_get(packageInfo, "nsdId")
53 logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s,nsd_id=%s", self.ns_package_id, self.nsd_id)
55 def check_ns_inst_name_exist(self):
56 is_exist = NSInstModel.objects.filter(name=self.ns_name).exists()
57 logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist)
59 raise NSLCMException("ns(%s) already existed." % self.ns_name)
61 def create_ns_inst(self):
62 self.ns_inst_id = str(uuid.uuid4())
63 logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
64 NSInstModel(id=self.ns_inst_id, name=self.ns_name, nspackage_id=self.ns_package_id,
65 nsd_id=self.nsd_id, description=self.description, status='empty',
66 lastuptime=now_time()).save()
68 def create_ns_in_aai(self):
69 logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
70 global_customer_id = "global-customer-id-" + self.ns_inst_id
72 "global-customer-id": "global-customer-id-" + self.ns_inst_id,
73 "subscriber-name": "subscriber-name-" + self.ns_inst_id,
74 "subscriber-type": "subscriber-type-" + self.ns_inst_id,
75 "service-subscriptions": {
76 "service-subscription": [
78 "service-type": "Network",
79 "service-instances": {
82 "service-instance-id": self.ns_inst_id,
83 "service-instance-name": self.ns_name,
84 "service-type": "Network",
85 "service-role": "service-role-" + self.ns_inst_id
93 resp_data, resp_status = create_customer_aai(global_customer_id, data)
95 logger.debug("Fail to create ns[%s] to aai: [%s].", self.ns_inst_id, resp_status)
97 logger.debug("Success to create ns[%s] to aai: [%s].", self.ns_inst_id, resp_status)