Refactor the notification process code
[modeling/etsicatalog.git] / catalog / packages / biz / ns_descriptor.py
1 # Copyright 2018 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
15
16 import json
17 import logging
18 import os
19 import uuid
20
21 from catalog.packages.biz.common import parse_file_range, read, save
22 from catalog.pub.config.config import CATALOG_ROOT_PATH
23 from catalog.pub.database.models import NSPackageModel, PnfPackageModel, VnfPackageModel
24 from catalog.pub.exceptions import CatalogException, ResourceNotFoundException
25 from catalog.pub.utils import fileutil, toscaparser
26 from catalog.pub.utils.values import ignore_case_get
27 from catalog.packages.biz.notificationsutil import NsdNotifications
28 from catalog.packages import const
29
30 logger = logging.getLogger(__name__)
31
32 METADATA = "metadata"
33
34
35 class NsDescriptor(object):
36
37     def __init__(self):
38         pass
39
40     def create(self, data, id=None):
41         logger.info('Start to create a NSD...')
42         user_defined_data = ignore_case_get(data, 'userDefinedData', {})
43         data = {
44             'id': id if id else str(uuid.uuid4()),
45             'nsdOnboardingState': const.PKG_STATUS.CREATED,
46             'nsdOperationalState': const.PKG_STATUS.DISABLED,
47             'nsdUsageState': const.PKG_STATUS.NOT_IN_USE,
48             'userDefinedData': user_defined_data,
49             '_links': None  # TODO
50         }
51         NSPackageModel.objects.create(
52             nsPackageId=data['id'],
53             onboardingState=data['nsdOnboardingState'],
54             operationalState=data['nsdOperationalState'],
55             usageState=data['nsdUsageState'],
56             userDefinedData=json.dumps(user_defined_data)
57         )
58         logger.info('A NSD(%s) has been created.' % data['id'])
59         return data
60
61     def query_multiple(self, nsdId=None):
62         if nsdId:
63             ns_pkgs = NSPackageModel.objects.filter(nsdId=nsdId)
64         else:
65             ns_pkgs = NSPackageModel.objects.all()
66         response_data = []
67         for ns_pkg in ns_pkgs:
68             data = self.fill_resp_data(ns_pkg)
69             response_data.append(data)
70         return response_data
71
72     def query_single(self, nsd_info_id):
73         ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
74         if not ns_pkgs.exists():
75             logger.error('NSD(%s) does not exist.' % nsd_info_id)
76             raise ResourceNotFoundException('NSD(%s) does not exist.' % nsd_info_id)
77         return self.fill_resp_data(ns_pkgs[0])
78
79     def delete_single(self, nsd_info_id):
80         logger.info('Start to delete NSD(%s)...' % nsd_info_id)
81         ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
82         if not ns_pkgs.exists():
83             logger.info('NSD(%s) has been deleted.' % nsd_info_id)
84             return
85         '''
86         if ns_pkgs[0].operationalState != PKG_STATUS.DISABLED:
87             logger.error('NSD(%s) shall be DISABLED.' % nsd_info_id)
88             raise CatalogException('NSD(%s) shall be DISABLED.' % nsd_info_id)
89         if ns_pkgs[0].usageState != PKG_STATUS.NOT_IN_USE:
90             logger.error('NSD(%s) shall be NOT_IN_USE.' % nsd_info_id)
91             raise CatalogException('NSD(%s) shall be NOT_IN_USE.' % nsd_info_id)
92         '''
93         ns_pkgs.delete()
94         ns_pkg_path = os.path.join(CATALOG_ROOT_PATH, nsd_info_id)
95         fileutil.delete_dirs(ns_pkg_path)
96         send_notification(const.NSD_NOTIFICATION_TYPE.NSD_DELETION, nsd_info_id)
97         logger.info('NSD(%s) has been deleted.' % nsd_info_id)
98
99     def upload(self, nsd_info_id, remote_file):
100         logger.info('Start to upload NSD(%s)...' % nsd_info_id)
101         ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
102         if not ns_pkgs.exists():
103             logger.error('NSD(%s) does not exist.' % nsd_info_id)
104             raise CatalogException('NSD(%s) does not exist.' % nsd_info_id)
105         ns_pkgs.update(onboardingState=const.PKG_STATUS.UPLOADING)
106
107         local_file_name = save(remote_file, nsd_info_id)
108         logger.info('NSD(%s) content has been uploaded.' % nsd_info_id)
109         return local_file_name
110
111     def download(self, nsd_info_id, file_range):
112         logger.info('Start to download NSD(%s)...' % nsd_info_id)
113         ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
114         if not ns_pkgs.exists():
115             logger.error('NSD(%s) does not exist.' % nsd_info_id)
116             raise ResourceNotFoundException('NSD(%s) does not exist.' % nsd_info_id)
117         if ns_pkgs[0].onboardingState != const.PKG_STATUS.ONBOARDED:
118             logger.error('NSD(%s) is not ONBOARDED.' % nsd_info_id)
119             raise CatalogException('NSD(%s) is not ONBOARDED.' % nsd_info_id)
120
121         local_file_path = ns_pkgs[0].localFilePath
122         start, end = parse_file_range(local_file_path, file_range)
123         logger.info('NSD(%s) has been downloaded.' % nsd_info_id)
124         return read(local_file_path, start, end)
125
126     def parse_nsd_and_save(self, nsd_info_id, local_file_name):
127         logger.info('Start to process NSD(%s)...' % nsd_info_id)
128         ns_pkgs = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
129         ns_pkgs.update(onboardingState=const.PKG_STATUS.PROCESSING)
130
131         nsd_json = toscaparser.parse_nsd(local_file_name)
132         logger.debug("%s", nsd_json)
133         nsd = json.JSONDecoder().decode(nsd_json)
134
135         nsd_id = nsd.get("ns", {}).get("properties", {}).get("descriptor_id", "")
136         nsd_name = nsd.get("ns", {}).get("properties", {}).get("name", "")
137         nsd_version = nsd.get("ns", {}).get("properties", {}).get("version", "")
138         nsd_designer = nsd.get("ns", {}).get("properties", {}).get("designer", "")
139         invariant_id = nsd.get("ns", {}).get("properties", {}).get("invariant_id", "")
140         if nsd_id == "":
141             raise CatalogException("nsd_id(%s) does not exist in metadata." % nsd_id)
142         other_nspkg = NSPackageModel.objects.filter(nsdId=nsd_id)
143         if other_nspkg and other_nspkg[0].nsPackageId != nsd_info_id:
144             failure_details = "NSD(%s,%s) already exists.", nsd_id, other_nspkg[0].nsPackageId
145             logger.warn(failure_details)
146             send_notification(const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING_FAILURE, nsd_info_id, nsd_id,
147                               failure_details=failure_details)
148             raise CatalogException("NSD(%s) already exists." % nsd_id)
149
150         for vnf in nsd["vnfs"]:
151             vnfd_id = vnf["properties"].get("descriptor_id", "undefined")
152             if vnfd_id == "undefined":
153                 vnfd_id = vnf["properties"].get("id", "undefined")
154             pkg = VnfPackageModel.objects.filter(vnfdId=vnfd_id)
155             if not pkg:
156                 pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfd_id)
157             if not pkg:
158                 vnfd_name = vnf.get("vnf_id", "undefined")
159                 logger.error("[%s] is not distributed.", vnfd_name)
160                 raise CatalogException("VNF package(%s) is not distributed." % vnfd_id)
161
162         for pnf in nsd["pnfs"]:
163             pnfd_id = pnf["properties"].get("descriptor_id", "undefined")
164             if pnfd_id == "undefined":
165                 pnfd_id = pnf["properties"].get("id", "undefined")
166             pkg = PnfPackageModel.objects.filter(pnfdId=pnfd_id)
167             if not pkg:
168                 pkg = PnfPackageModel.objects.filter(pnfPackageId=pnfd_id)
169             if not pkg:
170                 pnfd_name = pnf.get("pnf_id", "undefined")
171                 logger.error("[%s] is not distributed.", pnfd_name)
172                 raise CatalogException("PNF package(%s) is not distributed." % pnfd_name)
173
174         ns_pkgs.update(
175             nsdId=nsd_id,
176             nsdName=nsd_name,
177             nsdDesginer=nsd_designer,
178             nsdDescription=nsd.get("description", ""),
179             nsdVersion=nsd_version,
180             invariantId=invariant_id,
181             onboardingState=const.PKG_STATUS.ONBOARDED,
182             operationalState=const.PKG_STATUS.ENABLED,
183             usageState=const.PKG_STATUS.NOT_IN_USE,
184             nsPackageUri=local_file_name,
185             sdcCsarId=nsd_info_id,
186             localFilePath=local_file_name,
187             nsdModel=nsd_json
188         )
189         send_notification(const.NSD_NOTIFICATION_TYPE.NSD_ONBOARDING, nsd_info_id, nsd_id)
190         logger.info('NSD(%s) has been processed.' % nsd_info_id)
191
192     def fill_resp_data(self, ns_pkg):
193         data = {
194             'id': ns_pkg.nsPackageId,
195             'nsdId': ns_pkg.nsdId,
196             'nsdName': ns_pkg.nsdName,
197             'nsdVersion': ns_pkg.nsdVersion,
198             'nsdDesigner': ns_pkg.nsdDesginer,
199             'nsdInvariantId': ns_pkg.invariantId,
200             'vnfPkgIds': [],
201             'pnfdInfoIds': [],  # TODO
202             'nestedNsdInfoIds': [],  # TODO
203             'nsdOnboardingState': ns_pkg.onboardingState,
204             'onboardingFailureDetails': None,  # TODO
205             'nsdOperationalState': ns_pkg.operationalState,
206             'nsdUsageState': ns_pkg.usageState,
207             'userDefinedData': {},
208             '_links': None  # TODO
209         }
210
211         if ns_pkg.nsdModel:
212             nsd_model = json.JSONDecoder().decode(ns_pkg.nsdModel)
213             vnf_pkg_ids = []
214             for vnf in nsd_model['vnfs']:
215                 vnfd_id = vnf["properties"].get("descriptor_id", "undefined")
216                 if vnfd_id == "undefined":
217                     vnfd_id = vnf["properties"].get("id", "undefined")
218                 pkgs = VnfPackageModel.objects.filter(vnfdId=vnfd_id)
219                 if not pkgs:
220                     pkgs = VnfPackageModel.objects.filter(vnfPackageId=vnfd_id)
221                 for pkg in pkgs:
222                     vnf_pkg_ids.append(pkg.vnfPackageId)
223             data['vnfPkgIds'] = vnf_pkg_ids
224
225             pnf_info_ids = []
226             for pnf in nsd_model['pnfs']:
227                 pnfd_id = pnf["properties"].get("descriptor_id", "undefined")
228                 if pnfd_id == "undefined":
229                     pnfd_id = pnf["properties"].get("id", "undefined")
230                 pkgs = PnfPackageModel.objects.filter(pnfdId=pnfd_id)
231                 if not pkgs:
232                     pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_id)
233                 for pkg in pkgs:
234                     pnf_info_ids.append(pkg.pnfPackageId)
235             data['pnfInfoIds'] = pnf_info_ids  # TODO: need reconfirming
236
237         if ns_pkg.userDefinedData:
238             user_defined_data = json.JSONDecoder().decode(ns_pkg.userDefinedData)
239             data['userDefinedData'] = user_defined_data
240
241         return data
242
243     def handle_upload_failed(self, nsd_info_id):
244         ns_pkg = NSPackageModel.objects.filter(nsPackageId=nsd_info_id)
245         ns_pkg.update(onboardingState=const.PKG_STATUS.CREATED)
246
247
248 def send_notification(type, nsd_info_id, nsd_id=None, failure_details=None, operational_state=None):
249     notify = NsdNotifications(type, nsd_info_id, nsd_id,
250                               failure_details=failure_details,
251                               operational_state=operational_state)
252     notify.send_notification()