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):
32 This class for NS instance Model create
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')
42 self.ns_package_id = ''
46 Create NS instance model
49 self.check_nsd_valid()
50 self.check_ns_inst_name_exist()
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
57 def check_nsd_valid(self):
59 Check the validation of NSD
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)
72 def check_ns_inst_name_exist(self):
74 Check if the ns instance with same name exists
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)
80 raise NSLCMException("ns(%s) already existed." % self.ns_name)
82 def create_ns_inst(self):
84 Create NS instance Model
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,
91 nspackage_id=self.ns_package_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()
100 def create_ns_in_aai(self):
102 Create NS instance record in AAI
105 logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
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
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]))
118 logger.error(traceback.format_exc())