aboutsummaryrefslogtreecommitdiffstats
path: root/catalog/packages/biz/ns_descriptor.py
blob: 8b337dca8abcfd1474acd1df9043de632bf13691 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Copyright 2018 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 json
import logging
import os
import uuid

from catalog.packages.biz.common import parse_file_range, read, save
from catalog.pub.config.config import CATALOG_ROOT_PATH
from catalog.pub.database.models import NSPackageModel, PnfPackageModel, VnfPackageModel
from catalog.pub.exceptions import CatalogException, ResourceNotFoundException
from catalog.pub.utils import fileutil, toscaparser
from catalog.pub.utils.values import ignore_case_get
from catalog.packages.biz.notificationsutil import NsdNotifications
from catalog.packages import const

logger = logging.getLogger(__name__)

METADATA = "metadata"


class NsDescriptor(object):

    def __init__(self):
        pass

    def create(self, data, id=None):
        logger.info('Start to create a NSD...')
        user_defined_data = ignore_case_get(data, 'userDefinedData', {})
        data = {
            'id': id if id else str(uuid.uuid4()),
            'nsdOnboardingState': const.PKG_STATUS.CREATED,
            'nsdOperationalState': const.PKG_STATUS.DISABLED,
            'nsdUsageState': const.PKG_STATUS.NOT_IN_USE,
            'userDefinedData': user_defined_data,
            '_links': None  # TODO
        }
        NSPackageModel.objects.create(
            nsPackageId=data['id'],
            onboardingState=data['nsdOnboardingState'],
            operationalState=data['nsdOperationalState'],
            usageState=data['nsdUsageState'],
            userDefinedData=json.dumps(user_defined_data)
        )
        logger.info('A NSD(%s) has been created.' % data['id'])
        return data

    def query_multiple(self, nsdId=None):
        if nsdId:
            ns_pkgs = NSPackageModel.objects.filter(nsdId=nsdId)
        else:
            ns_pkgs = NSPackageModel.objects.all()
        response_data = []
        for ns_pkg in ns_pkgs:
            data = self.fill_resp_data(ns_pkg)
            response_data.append(data)
        return response_data

    def query_single(self, nsd_info_id):
        ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
        if not ns_pkgs.exists():
            logger.error('NSD(%s) does not exist.' % nsd_info_id)
            raise ResourceNotFoundException('NSD(%s) does not exist.' % nsd_info_id)
        return self.fill_resp_data(ns_pkgs[0])

    def delete_single(self, nsd_info_id):
        logger.info('Start to delete NSD(%s)...' % nsd_info_id)
        ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
        if not ns_pkgs.exists():
            logger.info('NSD(%s) has been deleted.' % nsd_info_id)
            return
        '''
        if ns_pkgs[0].operationalState != PKG_STATUS.DISABLED:
            logger.error('NSD(%s) shall be DISABLED.' % nsd_info_id)
            raise CatalogException('NSD(%s) shall be DISABLED.' % nsd_info_id)
        if ns_pkgs[0].usageState != PKG_STATUS.NOT_IN_USE:
            logger.error('NSD(%s) shall be NOT_IN_USE.' % nsd_info_id)
            raise CatalogException('NSD(%s) shall be NOT_IN_USE.' % nsd_info_id)
        '''
        ns_pkgs.delete()
        ns_pkg_path = os.path.join(CATALOG_ROOT_PATH, nsd_info_id)
        fileutil.delete_dirs(ns_pkg_path)
        send_notification(const.NSD_NOTIFICATION_TYPE.NSD_DELETION, nsd_info_id)
        logger.info('NSD(%s) has been deleted.' % nsd_info_id)

    def upload(self, nsd_info_id, remote_file):
        logger.info('Start to upload NSD(%s)...' % nsd_info_id)
        ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
        if not ns_pkgs.exists():
            logger.error('NSD(%s) does not exist.' % nsd_info_id)
            raise CatalogException('NSD(%s) does not exist.' % nsd_info_id)
        ns_pkgs.update(onboardingState=const.PKG_STATUS.UPLOADING)

        local_file_name = save(remote_file, nsd_info_id)
        logger.info('NSD(%s) content has been uploaded.' % nsd_info_id)
        return local_file_name

    def download(self, nsd_info_id, file_range):
        logger.info('Start to download NSD(%s)...' % nsd_info_id)
        ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
        if not ns_pkgs.exists():
            logger.error('NSD(%s) does not exist.' % nsd_info_id)
            raise ResourceNotFoundException('NSD(%s) does not exist.' % nsd_info_id)
        if ns_pkgs[0].onboardingState != const.PKG_STATUS.ONBOARDED:
            logger.error('NSD(%s) is not ONBOARDED.' % nsd_info_id)
            raise CatalogException('NSD(%s) is not ONBOARDED.' % nsd_info_id)

        local_file_path = ns_pkgs[0].localFilePath
        start, end = parse_file_range(local_file_path, file_range)
        logger.info('NSD(%s) has been downloaded.' % nsd_info_id)
        return read(local_file_path, start, end)

    def parse_nsd_and_save(self, nsd_info_id, local_file_name):
        logger.info('Start to process NSD(%s)...' % nsd_info_id)
        ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
        ns_pkgs.update(onboardingState=const.PKG_STATUS.PROCESSING)

        nsd_json = toscaparser.parse_nsd(local_file_name)
        logger.debug("%s", nsd_json)
        nsd = json.JSONDecoder().decode(nsd_json)

        nsd_id = nsd.get("ns", {}).get("properties", {}).get("descriptor_id", "")
        nsd_name = nsd.get("ns", {}).get("properties", {}).get("name", "")
        nsd_version = nsd.get("ns", {}).get("properties", {}).get("version", "")
        nsd_designer = nsd.get("ns", {}).get("properties", {}).get("designer", "")
        invariant_id = nsd.get("ns", {}).get("properties", {}).get("invariant_id", "")
        if nsd_id == "":
            raise CatalogException("nsd_id(%s) does not exist in metadata." % nsd_id)
        other_nspkg = NSPackageModel.objects.filter(nsdId=nsd_id)
        if other_nspkg and other_nspkg[0].nsPackageId != nsd_info_id:
            failure_details = "NSD(%s,%s) already exists.", nsd_id, other_nspkg[0].nsPackageId
            logger.warn(failure_details)
            send_notification(const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING_FAILURE, nsd_info_id, nsd_id,
                              failure_details=failure_details)
            raise CatalogException("NSD(%s) already exists." % nsd_id)

        for vnf in nsd["vnfs"]:
            vnfd_id = vnf["properties"].get("descriptor_id", "undefined")
            if vnfd_id == "undefined":
                vnfd_id = vnf["properties"].get("id", "undefined")
            pkg = VnfPackageModel.objects.filter(vnfdId=vnfd_id)
            if not pkg:
                pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfd_id)
            if not pkg:
                vnfd_name = vnf.get("vnf_id", "undefined")
                logger.error("[%s] is not distributed.", vnfd_name)
                raise CatalogException("VNF package(%s) is not distributed." % vnfd_id)

        for pnf in nsd["pnfs"]:
            pnfd_id = pnf["properties"].get("descriptor_id", "undefined")
            if pnfd_id == "undefined":
                pnfd_id = pnf["properties"].get("id", "undefined")
            pkg = PnfPackageModel.objects.filter(pnfdId=pnfd_id)
            if not pkg:
                pkg = PnfPackageModel.objects.filter(pnfPackageId=pnfd_id)
            if not pkg:
                pnfd_name = pnf.get("pnf_id", "undefined")
                logger.error("[%s] is not distributed.", pnfd_name)
                raise CatalogException("PNF package(%s) is not distributed." % pnfd_name)

        ns_pkgs.update(
            nsdId=nsd_id,
            nsdName=nsd_name,
            nsdDesginer=nsd_designer,
            nsdDescription=nsd.get("description", ""),
            nsdVersion=nsd_version,
            invariantId=invariant_id,
            onboardingState=const.PKG_STATUS.ONBOARDED,
            operationalState=const.PKG_STATUS.ENABLED,
            usageState=const.PKG_STATUS.NOT_IN_USE,
            nsPackageUri=local_file_name,
            sdcCsarId=nsd_info_id,
            localFilePath=local_file_name,
            nsdModel=nsd_json
        )
        send_notification(const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING, nsd_info_id, nsd_id)
        logger.info('NSD(%s) has been processed.' % nsd_info_id)

    def fill_resp_data(self, ns_pkg):
        data = {
            'id': ns_pkg.nsPackageId,
            'nsdId': ns_pkg.nsdId,
            'nsdName': ns_pkg.nsdName,
            'nsdVersion': ns_pkg.nsdVersion,
            'nsdDesigner': ns_pkg.nsdDesginer,
            'nsdInvariantId': ns_pkg.invariantId,
            'vnfPkgIds': [],
            'pnfdInfoIds': [],  # TODO
            'nestedNsdInfoIds': [],  # TODO
            'nsdOnboardingState': ns_pkg.onboardingState,
            'onboardingFailureDetails': None,  # TODO
            'nsdOperationalState': ns_pkg.operationalState,
            'nsdUsageState': ns_pkg.usageState,
            'userDefinedData': {},
            '_links': None  # TODO
        }

        if ns_pkg.nsdModel:
            nsd_model = json.JSONDecoder().decode(ns_pkg.nsdModel)
            vnf_pkg_ids = []
            for vnf in nsd_model['vnfs']:
                vnfd_id = vnf["properties"].get("descriptor_id", "undefined")
                if vnfd_id == "undefined":
                    vnfd_id = vnf["properties"].get("id", "undefined")
                pkgs = VnfPackageModel.objects.filter(vnfdId=vnfd_id)
                if not pkgs:
                    pkgs = VnfPackageModel.objects.filter(vnfPackageId=vnfd_id)
                for pkg in pkgs:
                    vnf_pkg_ids.append(pkg.vnfPackageId)
            data['vnfPkgIds'] = vnf_pkg_ids

            pnf_info_ids = []
            for pnf in nsd_model['pnfs']:
                pnfd_id = pnf["properties"].get("descriptor_id", "undefined")
                if pnfd_id == "undefined":
                    pnfd_id = pnf["properties"].get("id", "undefined")
                pkgs = PnfPackageModel.objects.filter(pnfdId=pnfd_id)
                if not pkgs:
                    pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_id)
                for pkg in pkgs:
                    pnf_info_ids.append(pkg.pnfPackageId)
            data['pnfInfoIds'] = pnf_info_ids  # TODO: need reconfirming

        if ns_pkg.userDefinedData:
            user_defined_data = json.JSONDecoder().decode(ns_pkg.userDefinedData)
            data['userDefinedData'] = user_defined_data

        return data

    def handle_upload_failed(self, nsd_info_id):
        ns_pkg = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
        ns_pkg.update(onboardingState=const.PKG_STATUS.CREATED)


def send_notification(type, nsd_info_id, nsd_id=None, failure_details=None, operational_state=None):
    notify = NsdNotifications(type, nsd_info_id, nsd_id,
                              failure_details=failure_details,
                              operational_state=operational_state)
    notify.send_notification()