summaryrefslogtreecommitdiffstats
path: root/lcm/ns/ns_create.py
blob: 216d6386e670616affe8166d0d4a5c72e2b05d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright 2016 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import traceback
import uuid

from lcm.pub.config.config import REPORT_TO_AAI
from lcm.pub.database.models import NSInstModel
from lcm.pub.exceptions import NSLCMException
from lcm.pub.msapi.aai import create_ns_aai
from lcm.pub.msapi.sdc_run_catalog import query_nspackage_by_id
from lcm.pub.utils.timeutil import now_time
from lcm.pub.utils.values import ignore_case_get
from lcm.ns.const import SERVICE_ROLE, SERVICE_TYPE

logger = logging.getLogger(__name__)


class CreateNSService(object):
    def __init__(self, csar_id, ns_name, description, context):
        self.csar_id = csar_id
        self.ns_name = ns_name
        self.description = description
        self.global_customer_id = ignore_case_get(context, 'globalCustomerId')
        self.service_type = ignore_case_get(context, 'serviceType')
        self.ns_inst_id = ''
        self.ns_package_id = ''

    def do_biz(self):
        self.check_nsd_valid()
        self.check_ns_inst_name_exist()
        self.create_ns_inst()
        if REPORT_TO_AAI:
            self.create_ns_in_aai()
        logger.debug("CreateNSService::do_biz::ns_inst_id=%s" % self.ns_inst_id)
        return self.ns_inst_id

    def check_nsd_valid(self):
        logger.debug("CreateNSService::check_nsd_valid::csar_id=%s" % self.csar_id)
        ns_package_info = query_nspackage_by_id(self.csar_id)
        if not ns_package_info:
            raise NSLCMException("nsd(%s) not exists." % self.csar_id)
        packageInfo = ns_package_info["packageInfo"]
        self.ns_package_id = ignore_case_get(packageInfo, "nsPackageId")
        self.nsd_id = ignore_case_get(packageInfo, "nsdId")
        logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s,nsd_id=%s", self.ns_package_id, self.nsd_id)

    def check_ns_inst_name_exist(self):
        is_exist = NSInstModel.objects.filter(name=self.ns_name).exists()
        logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist)
        if is_exist:
            raise NSLCMException("ns(%s) already existed." % self.ns_name)

    def create_ns_inst(self):
        self.ns_inst_id = str(uuid.uuid4())
        logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
        NSInstModel(id=self.ns_inst_id,
                    name=self.ns_name,
                    nspackage_id=self.ns_package_id,
                    nsd_id=self.nsd_id,
                    description=self.description,
                    status='empty',
                    lastuptime=now_time(),
                    global_customer_id=self.global_customer_id,
                    service_type=self.service_type).save()

    def create_ns_in_aai(self):
        logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
        try:
            # global_customer_id = "global-customer-id-" + self.ns_inst_id
            # data = {
            #     "global-customer-id": "global-customer-id-" + self.ns_inst_id,
            #     "subscriber-name": "subscriber-name-" + self.ns_inst_id,
            #     "subscriber-type": "subscriber-type-" + self.ns_inst_id,
            #     "service-subscriptions": {
            #         "service-subscription": [
            #             {
            #                 "service-type": "Network",
            #                 "service-instances": {
            #                     "service-instance": [
            #                         {
            #                             "service-instance-id": self.ns_inst_id,
            #                             "service-instance-name": self.ns_name,
            #                             "service-type": "Network",
            #                             "service-role": "service-role-" + self.ns_inst_id
            #                         }
            #                     ]
            #                 }
            #             }
            #         ]
            #     }
            # }
            # resp_data, resp_status = create_customer_aai(global_customer_id, data)
            data = {
                "service-instance-id": self.ns_inst_id,
                "service-instance-name": self.ns_name,
                "service-type": SERVICE_TYPE,
                "service-role": SERVICE_ROLE
            }
            resp_data, resp_status = create_ns_aai(self.global_customer_id, self.service_type, self.ns_inst_id, data)
            if resp_data:
                logger.debug("Fail to create ns[%s] to aai: [%s].", self.ns_inst_id, resp_status)
            else:
                logger.debug("Success to create ns[%s] to aai: [%s].", self.ns_inst_id, resp_status)
        except NSLCMException as e:
            logger.debug("Fail to createns[%s] to aai, detail message: %s" % (self.ns_inst_id, e.message))
        except:
            logger.error(traceback.format_exc())