a80bf8d596aeedf2f5d3e05f6b8ae61c778b6519
[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 uuid
16
17 from lcm.pub.database.models import NSDModel, NSInstModel
18 from lcm.pub.exceptions import NSLCMException
19 from lcm.pub.msapi.aai import create_customer_aai
20 from lcm.pub.utils.timeutil import now_time
21
22 logger = logging.getLogger(__name__)
23
24
25 class CreateNSService(object):
26     def __init__(self, nsd_id, ns_name, description):
27         self.nsd_id = nsd_id
28         self.ns_name = ns_name
29         self.description = description
30         self.ns_inst_id = ''
31         self.ns_package_id = ''
32
33     def do_biz(self):
34         self.check_nsd_valid()
35         self.check_ns_inst_name_exist()
36         self.create_ns_inst()
37         self.create_ns_in_aai()
38         logger.debug("CreateNSService::do_biz::ns_inst_id=%s" % self.ns_inst_id)
39         return self.ns_inst_id
40
41     def check_nsd_valid(self):
42         logger.debug("CreateNSService::check_nsd_valid::nsd_id=%s" % self.nsd_id)
43         ns_package_info = NSDModel.objects.filter(nsd_id=self.nsd_id)
44         if not ns_package_info:
45             raise NSLCMException("nsd(%s) not exists." % self.nsd_id)
46         self.ns_package_id = ns_package_info[0].id
47         logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s" % self.ns_package_id)
48
49     def check_ns_inst_name_exist(self):
50         is_exist = NSInstModel.objects.filter(name=self.ns_name).exists()
51         logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist)
52         if is_exist:
53             raise NSLCMException("ns(%s) already existed." % self.ns_name)
54
55     def create_ns_inst(self):
56         self.ns_inst_id = str(uuid.uuid4())
57         logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
58         NSInstModel(id=self.ns_inst_id, name=self.ns_name, nspackage_id=self.ns_package_id,
59                     nsd_id=self.nsd_id, description=self.description, status='empty',
60                     lastuptime=now_time()).save()
61
62     def create_ns_in_aai(self):
63         logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
64         global_customer_id = "global-customer-id-" + self.ns_inst_id
65         data = {
66             "global-customer-id": "global-customer-id-" + self.ns_inst_id,
67             "subscriber-name": "subscriber-name-" + self.ns_inst_id,
68             "subscriber-type": "subscriber-type-" + self.ns_inst_id,
69             "service-subscriptions": {
70                 "service-subscription": [
71                     {
72                         "service-type": "service-type-" + self.ns_inst_id,
73                         "service-instances": {
74                             "service-instance": [
75                                 {
76                                     "service-instance-id": self.ns_inst_id,
77                                     "service-instance-name": self.ns_name,
78                                     "service-type": "service-type-" + self.ns_inst_id,
79                                     "service-role": "service-role-" + self.ns_inst_id
80                                 }
81                             ]
82                         }
83                     }
84                 ]
85             }
86         }
87         resp_data, resp_status = create_customer_aai(global_customer_id, data)
88         if resp_data:
89             logger.debug("Fail to create ns instance[%s] to aai, resp_status: [%s]." % (self.ns_inst_id, resp_status) )
90         else:
91             logger.debug("Success to create ns instance[%s] to aai, resp_status: [%s]." % (self.ns_inst_id, resp_status) )