Modify vfc-lcm variable name to hump
[vfc/nfvo/lcm.git] / lcm / ns / 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     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')
37         self.ns_inst_id = ''
38         self.ns_package_id = ''
39
40     def do_biz(self):
41         self.check_nsd_valid()
42         self.check_ns_inst_name_exist()
43         self.create_ns_inst()
44         if REPORT_TO_AAI:
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
48
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)
58
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)
62         if is_exist:
63             raise NSLCMException("ns(%s) already existed." % self.ns_name)
64
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,
69                     name=self.ns_name,
70                     nspackage_id=self.ns_package_id,
71                     nsd_id=self.nsd_id,
72                     description=self.description,
73                     status='empty',
74                     lastuptime=now_time(),
75                     global_customer_id=self.global_customer_id,
76                     service_type=self.service_type).save()
77
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)
80         try:
81             # global_customer_id = "global-customer-id-" + self.ns_inst_id
82             # data = {
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": [
88             #             {
89             #                 "service-type": "Network",
90             #                 "service-instances": {
91             #                     "service-instance": [
92             #                         {
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
97             #                         }
98             #                     ]
99             #                 }
100             #             }
101             #         ]
102             #     }
103             # }
104             # resp_data, resp_status = create_customer_aai(global_customer_id, data)
105             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
110             }
111             resp_data, resp_status = create_ns_aai(self.global_customer_id, SERVICE_TYPE, self.ns_inst_id, data)
112             if resp_data:
113                 logger.debug("Fail to create ns[%s] to aai: [%s].", self.ns_inst_id, resp_status)
114             else:
115                 logger.debug("Success to create ns[%s] to aai: [%s].", self.ns_inst_id, resp_status)
116         except NSLCMException as e:
117             logger.debug("Fail to createns[%s] to aai, detail message: %s" % (self.ns_inst_id, e.message))
118         except:
119             logger.error(traceback.format_exc())