| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| tierno | d125caf | 2018-11-22 16:05:54 +0000 | [diff] [blame] | 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 |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 16 | import tarfile |
| 17 | import yaml |
| 18 | import json |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 19 | import copy |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 20 | import os |
| 21 | import shutil |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 22 | import functools |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 23 | import re |
| garciadeblas | dcfa3d6 | 2025-06-25 16:44:25 +0200 | [diff] [blame] | 24 | import base64 |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 25 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 26 | # import logging |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 27 | from deepdiff import DeepDiff |
| garciadeblas | dcfa3d6 | 2025-06-25 16:44:25 +0200 | [diff] [blame] | 28 | from hashlib import md5, sha384 |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 29 | from osm_common.dbbase import DbException, deep_update_rfc7396 |
| 30 | from http import HTTPStatus |
| delacruzramo | 26301bb | 2019-11-15 14:45:32 +0100 | [diff] [blame] | 31 | from time import time |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 32 | from uuid import uuid4 |
| 33 | from re import fullmatch |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 34 | from zipfile import ZipFile |
| Gabriel Cuba | 646773d | 2023-11-20 01:43:05 -0500 | [diff] [blame] | 35 | from urllib.parse import urlparse |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 36 | from osm_nbi.validation import ( |
| 37 | ValidationError, |
| 38 | pdu_new_schema, |
| 39 | pdu_edit_schema, |
| 40 | validate_input, |
| 41 | vnfpkgop_new_schema, |
| kayal2001 | f71c2e8 | 2024-06-25 15:26:24 +0530 | [diff] [blame] | 42 | ns_config_template, |
| 43 | vnf_schema, |
| 44 | vld_schema, |
| 45 | additional_params_for_vnf, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 46 | ) |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 47 | from osm_nbi.base_topic import ( |
| 48 | BaseTopic, |
| 49 | EngineException, |
| 50 | get_iterable, |
| 51 | detect_descriptor_usage, |
| 52 | ) |
| sousaedu | 317b9fd | 2021-07-29 17:40:16 +0200 | [diff] [blame] | 53 | from osm_im import etsi_nfv_vnfd, etsi_nfv_nsd |
| Adurti | 014a630 | 2024-05-08 05:03:01 +0000 | [diff] [blame^] | 54 | from osm_im.validation import Validation as validation_im |
| gcalvino | 70434c1 | 2018-11-27 15:17:04 +0100 | [diff] [blame] | 55 | from osm_im.nst import nst as nst_im |
| gcalvino | 46e4cb8 | 2018-10-26 13:10:22 +0200 | [diff] [blame] | 56 | from pyangbind.lib.serialise import pybindJSONDecoder |
| 57 | import pyangbind.lib.pybindJSON as pybindJSON |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 58 | from osm_nbi import utils |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 59 | |
| 60 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 61 | |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 62 | valid_helm_chart_re = re.compile( |
| 63 | r"^[a-z0-9]([-a-z0-9]*[a-z0-9]/)?([a-z0-9]([-a-z0-9]*[a-z0-9])?)*$" |
| 64 | ) |
| 65 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 66 | |
| 67 | class DescriptorTopic(BaseTopic): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 68 | def __init__(self, db, fs, msg, auth): |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 69 | super().__init__(db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 70 | |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 71 | def _validate_input_new(self, indata, storage_params, force=False): |
| 72 | return indata |
| 73 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 74 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 75 | final_content = super().check_conflict_on_edit( |
| 76 | session, final_content, edit_content, _id |
| 77 | ) |
| K Sai Kiran | 45bd94c | 2019-11-25 17:30:37 +0530 | [diff] [blame] | 78 | |
| 79 | def _check_unique_id_name(descriptor, position=""): |
| 80 | for desc_key, desc_item in descriptor.items(): |
| 81 | if isinstance(desc_item, list) and desc_item: |
| 82 | used_ids = [] |
| 83 | desc_item_id = None |
| 84 | for index, list_item in enumerate(desc_item): |
| 85 | if isinstance(list_item, dict): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 86 | _check_unique_id_name( |
| 87 | list_item, "{}.{}[{}]".format(position, desc_key, index) |
| 88 | ) |
| K Sai Kiran | 45bd94c | 2019-11-25 17:30:37 +0530 | [diff] [blame] | 89 | # Base case |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 90 | if index == 0 and ( |
| 91 | list_item.get("id") or list_item.get("name") |
| 92 | ): |
| K Sai Kiran | 45bd94c | 2019-11-25 17:30:37 +0530 | [diff] [blame] | 93 | desc_item_id = "id" if list_item.get("id") else "name" |
| 94 | if desc_item_id and list_item.get(desc_item_id): |
| 95 | if list_item[desc_item_id] in used_ids: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 96 | position = "{}.{}[{}]".format( |
| 97 | position, desc_key, index |
| 98 | ) |
| 99 | raise EngineException( |
| 100 | "Error: identifier {} '{}' is not unique and repeats at '{}'".format( |
| 101 | desc_item_id, |
| 102 | list_item[desc_item_id], |
| 103 | position, |
| 104 | ), |
| 105 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 106 | ) |
| K Sai Kiran | 45bd94c | 2019-11-25 17:30:37 +0530 | [diff] [blame] | 107 | used_ids.append(list_item[desc_item_id]) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 108 | |
| K Sai Kiran | 45bd94c | 2019-11-25 17:30:37 +0530 | [diff] [blame] | 109 | _check_unique_id_name(final_content) |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 110 | # 1. validate again with pyangbind |
| 111 | # 1.1. remove internal keys |
| 112 | internal_keys = {} |
| 113 | for k in ("_id", "_admin"): |
| 114 | if k in final_content: |
| 115 | internal_keys[k] = final_content.pop(k) |
| gcalvino | a6fe000 | 2019-01-09 13:27:11 +0100 | [diff] [blame] | 116 | storage_params = internal_keys["_admin"].get("storage") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 117 | serialized = self._validate_input_new( |
| 118 | final_content, storage_params, session["force"] |
| 119 | ) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 120 | |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 121 | # 1.2. modify final_content with a serialized version |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 122 | final_content = copy.deepcopy(serialized) |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 123 | # 1.3. restore internal keys |
| 124 | for k, v in internal_keys.items(): |
| 125 | final_content[k] = v |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 126 | if session["force"]: |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 127 | return final_content |
| 128 | |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 129 | # 2. check that this id is not present |
| 130 | if "id" in edit_content: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 131 | _filter = self._get_project_filter(session) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 132 | |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 133 | _filter["id"] = final_content["id"] |
| 134 | _filter["_id.neq"] = _id |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 135 | |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 136 | if self.db.get_one(self.topic, _filter, fail_on_empty=False): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 137 | raise EngineException( |
| 138 | "{} with id '{}' already exists for this project".format( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 139 | (str(self.topic))[:-1], final_content["id"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 140 | ), |
| 141 | HTTPStatus.CONFLICT, |
| 142 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 143 | |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 144 | return final_content |
| 145 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 146 | @staticmethod |
| 147 | def format_on_new(content, project_id=None, make_public=False): |
| 148 | BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public) |
| 149 | content["_admin"]["onboardingState"] = "CREATED" |
| 150 | content["_admin"]["operationalState"] = "DISABLED" |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 151 | content["_admin"]["usageState"] = "NOT_IN_USE" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 152 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 153 | def delete_extra(self, session, _id, db_content, not_send_msg=None): |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 154 | """ |
| 155 | Deletes file system storage associated with the descriptor |
| 156 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 157 | :param _id: server internal id |
| 158 | :param db_content: The database content of the descriptor |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 159 | :param not_send_msg: To not send message (False) or store content (list) instead |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 160 | :return: None if ok or raises EngineException with the problem |
| 161 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 162 | self.fs.file_delete(_id, ignore_non_exist=True) |
| tierno | f717cbe | 2018-12-03 16:35:42 +0000 | [diff] [blame] | 163 | self.fs.file_delete(_id + "_", ignore_non_exist=True) # remove temp folder |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 164 | # Remove file revisions |
| 165 | if "revision" in db_content["_admin"]: |
| 166 | revision = db_content["_admin"]["revision"] |
| 167 | while revision > 0: |
| 168 | self.fs.file_delete(_id + ":" + str(revision), ignore_non_exist=True) |
| 169 | revision = revision - 1 |
| 170 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 171 | @staticmethod |
| 172 | def get_one_by_id(db, session, topic, id): |
| 173 | # find owned by this project |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 174 | _filter = BaseTopic._get_project_filter(session) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 175 | _filter["id"] = id |
| 176 | desc_list = db.get_list(topic, _filter) |
| 177 | if len(desc_list) == 1: |
| 178 | return desc_list[0] |
| 179 | elif len(desc_list) > 1: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 180 | raise DbException( |
| 181 | "Found more than one {} with id='{}' belonging to this project".format( |
| 182 | topic[:-1], id |
| 183 | ), |
| 184 | HTTPStatus.CONFLICT, |
| 185 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 186 | |
| 187 | # not found any: try to find public |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 188 | _filter = BaseTopic._get_project_filter(session) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 189 | _filter["id"] = id |
| 190 | desc_list = db.get_list(topic, _filter) |
| 191 | if not desc_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 192 | raise DbException( |
| 193 | "Not found any {} with id='{}'".format(topic[:-1], id), |
| 194 | HTTPStatus.NOT_FOUND, |
| 195 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 196 | elif len(desc_list) == 1: |
| 197 | return desc_list[0] |
| 198 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 199 | raise DbException( |
| 200 | "Found more than one public {} with id='{}'; and no one belonging to this project".format( |
| 201 | topic[:-1], id |
| 202 | ), |
| 203 | HTTPStatus.CONFLICT, |
| 204 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 205 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 206 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 207 | """ |
| 208 | Creates a new almost empty DISABLED entry into database. Due to SOL005, it does not follow normal procedure. |
| 209 | Creating a VNFD or NSD is done in two steps: 1. Creates an empty descriptor (this step) and 2) upload content |
| 210 | (self.upload_content) |
| 211 | :param rollback: list to append created items at database in case a rollback may to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 212 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 213 | :param indata: data to be inserted |
| 214 | :param kwargs: used to override the indata descriptor |
| 215 | :param headers: http request headers |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 216 | :return: _id, None: identity of the inserted data; and None as there is not any operation |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 217 | """ |
| 218 | |
| tierno | d774958 | 2020-05-28 10:41:10 +0000 | [diff] [blame] | 219 | # No needed to capture exceptions |
| 220 | # Check Quota |
| 221 | self.check_quota(session) |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 222 | |
| tierno | d774958 | 2020-05-28 10:41:10 +0000 | [diff] [blame] | 223 | # _remove_envelop |
| 224 | if indata: |
| 225 | if "userDefinedData" in indata: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 226 | indata = indata["userDefinedData"] |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 227 | |
| tierno | d774958 | 2020-05-28 10:41:10 +0000 | [diff] [blame] | 228 | # Override descriptor with query string kwargs |
| 229 | self._update_input_with_kwargs(indata, kwargs) |
| 230 | # uncomment when this method is implemented. |
| 231 | # Avoid override in this case as the target is userDefinedData, but not vnfd,nsd descriptors |
| 232 | # indata = DescriptorTopic._validate_input_new(self, indata, project_id=session["force"]) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 233 | |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 234 | content = {"_admin": {"userDefinedData": indata, "revision": 0}} |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 235 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 236 | self.format_on_new( |
| 237 | content, session["project_id"], make_public=session["public"] |
| 238 | ) |
| tierno | d774958 | 2020-05-28 10:41:10 +0000 | [diff] [blame] | 239 | _id = self.db.create(self.topic, content) |
| 240 | rollback.append({"topic": self.topic, "_id": _id}) |
| 241 | self._send_msg("created", {"_id": _id}) |
| 242 | return _id, None |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 243 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 244 | def upload_content(self, session, _id, indata, kwargs, headers): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 245 | """ |
| 246 | Used for receiving content by chunks (with a transaction_id header and/or gzip file. It will store and extract) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 247 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 248 | :param _id : the nsd,vnfd is already created, this is the id |
| 249 | :param indata: http body request |
| 250 | :param kwargs: user query string to override parameters. NOT USED |
| 251 | :param headers: http request headers |
| tierno | 5a5c218 | 2018-11-20 12:27:42 +0000 | [diff] [blame] | 252 | :return: True if package is completely uploaded or False if partial content has been uploded |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 253 | Raise exception on error |
| 254 | """ |
| 255 | # Check that _id exists and it is valid |
| 256 | current_desc = self.show(session, _id) |
| 257 | |
| 258 | content_range_text = headers.get("Content-Range") |
| 259 | expected_md5 = headers.get("Content-File-MD5") |
| garciadeblas | dcfa3d6 | 2025-06-25 16:44:25 +0200 | [diff] [blame] | 260 | digest_header = headers.get("Digest") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 261 | compressed = None |
| 262 | content_type = headers.get("Content-Type") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 263 | if ( |
| 264 | content_type |
| 265 | and "application/gzip" in content_type |
| 266 | or "application/x-gzip" in content_type |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 267 | ): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 268 | compressed = "gzip" |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 269 | if content_type and "application/zip" in content_type: |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 270 | compressed = "zip" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 271 | filename = headers.get("Content-Filename") |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 272 | if not filename and compressed: |
| 273 | filename = "package.tar.gz" if compressed == "gzip" else "package.zip" |
| 274 | elif not filename: |
| 275 | filename = "package" |
| 276 | |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 277 | revision = 1 |
| 278 | if "revision" in current_desc["_admin"]: |
| 279 | revision = current_desc["_admin"]["revision"] + 1 |
| 280 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 281 | # TODO change to Content-Disposition filename https://tools.ietf.org/html/rfc6266 |
| 282 | file_pkg = None |
| 283 | error_text = "" |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 284 | fs_rollback = [] |
| 285 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 286 | try: |
| 287 | if content_range_text: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 288 | content_range = ( |
| 289 | content_range_text.replace("-", " ").replace("/", " ").split() |
| 290 | ) |
| 291 | if ( |
| 292 | content_range[0] != "bytes" |
| 293 | ): # TODO check x<y not negative < total.... |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 294 | raise IndexError() |
| 295 | start = int(content_range[1]) |
| 296 | end = int(content_range[2]) + 1 |
| 297 | total = int(content_range[3]) |
| 298 | else: |
| 299 | start = 0 |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 300 | # Rather than using a temp folder, we will store the package in a folder based on |
| 301 | # the current revision. |
| 302 | proposed_revision_path = ( |
| 303 | _id + ":" + str(revision) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 304 | ) # all the content is upload here and if ok, it is rename from id_ to is folder |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 305 | |
| 306 | if start: |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 307 | if not self.fs.file_exists(proposed_revision_path, "dir"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 308 | raise EngineException( |
| 309 | "invalid Transaction-Id header", HTTPStatus.NOT_FOUND |
| 310 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 311 | else: |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 312 | self.fs.file_delete(proposed_revision_path, ignore_non_exist=True) |
| 313 | self.fs.mkdir(proposed_revision_path) |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 314 | fs_rollback.append(proposed_revision_path) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 315 | |
| 316 | storage = self.fs.get_params() |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 317 | storage["folder"] = proposed_revision_path |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 318 | |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 319 | file_path = (proposed_revision_path, filename) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 320 | if self.fs.file_exists(file_path, "file"): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 321 | file_size = self.fs.file_size(file_path) |
| 322 | else: |
| 323 | file_size = 0 |
| 324 | if file_size != start: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 325 | raise EngineException( |
| 326 | "invalid Content-Range start sequence, expected '{}' but received '{}'".format( |
| 327 | file_size, start |
| 328 | ), |
| 329 | HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE, |
| 330 | ) |
| 331 | file_pkg = self.fs.file_open(file_path, "a+b") |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 332 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 333 | if isinstance(indata, dict): |
| 334 | indata_text = yaml.safe_dump(indata, indent=4, default_flow_style=False) |
| 335 | file_pkg.write(indata_text.encode(encoding="utf-8")) |
| 336 | else: |
| 337 | indata_len = 0 |
| 338 | while True: |
| 339 | indata_text = indata.read(4096) |
| 340 | indata_len += len(indata_text) |
| 341 | if not indata_text: |
| 342 | break |
| 343 | file_pkg.write(indata_text) |
| 344 | if content_range_text: |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 345 | if indata_len != end - start: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 346 | raise EngineException( |
| 347 | "Mismatch between Content-Range header {}-{} and body length of {}".format( |
| 348 | start, end - 1, indata_len |
| 349 | ), |
| 350 | HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE, |
| 351 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 352 | if end != total: |
| 353 | # TODO update to UPLOADING |
| 354 | return False |
| 355 | |
| 356 | # PACKAGE UPLOADED |
| 357 | if expected_md5: |
| 358 | file_pkg.seek(0, 0) |
| 359 | file_md5 = md5() |
| 360 | chunk_data = file_pkg.read(1024) |
| 361 | while chunk_data: |
| 362 | file_md5.update(chunk_data) |
| 363 | chunk_data = file_pkg.read(1024) |
| 364 | if expected_md5 != file_md5.hexdigest(): |
| 365 | raise EngineException("Error, MD5 mismatch", HTTPStatus.CONFLICT) |
| garciadeblas | dcfa3d6 | 2025-06-25 16:44:25 +0200 | [diff] [blame] | 366 | if digest_header: |
| 367 | alg, b64_digest = digest_header.split("=", 1) |
| 368 | if alg.strip().lower() != "sha-384": |
| 369 | raise ValueError(f"Unsupported digest algorithm: {alg}") |
| 370 | expected_digest = base64.b64decode(b64_digest) |
| 371 | # Get real digest |
| 372 | file_pkg.seek(0, 0) |
| 373 | file_sha384 = sha384() |
| 374 | chunk_data = file_pkg.read(1024) |
| 375 | while chunk_data: |
| 376 | file_sha384.update(chunk_data) |
| 377 | chunk_data = file_pkg.read(1024) |
| 378 | if expected_digest != file_sha384.digest(): |
| 379 | raise EngineException("Error, SHA384 mismatch", HTTPStatus.CONFLICT) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 380 | file_pkg.seek(0, 0) |
| 381 | if compressed == "gzip": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 382 | tar = tarfile.open(mode="r", fileobj=file_pkg) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 383 | descriptor_file_name = None |
| 384 | for tarinfo in tar: |
| 385 | tarname = tarinfo.name |
| 386 | tarname_path = tarname.split("/") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 387 | if ( |
| 388 | not tarname_path[0] or ".." in tarname_path |
| 389 | ): # if start with "/" means absolute path |
| 390 | raise EngineException( |
| 391 | "Absolute path or '..' are not allowed for package descriptor tar.gz" |
| 392 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 393 | if len(tarname_path) == 1 and not tarinfo.isdir(): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 394 | raise EngineException( |
| 395 | "All files must be inside a dir for package descriptor tar.gz" |
| 396 | ) |
| 397 | if ( |
| 398 | tarname.endswith(".yaml") |
| 399 | or tarname.endswith(".json") |
| 400 | or tarname.endswith(".yml") |
| 401 | ): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 402 | storage["pkg-dir"] = tarname_path[0] |
| 403 | if len(tarname_path) == 2: |
| 404 | if descriptor_file_name: |
| 405 | raise EngineException( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 406 | "Found more than one descriptor file at package descriptor tar.gz" |
| 407 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 408 | descriptor_file_name = tarname |
| 409 | if not descriptor_file_name: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 410 | raise EngineException( |
| 411 | "Not found any descriptor file at package descriptor tar.gz" |
| 412 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 413 | storage["descriptor"] = descriptor_file_name |
| 414 | storage["zipfile"] = filename |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 415 | self.fs.file_extract(tar, proposed_revision_path) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 416 | with self.fs.file_open( |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 417 | (proposed_revision_path, descriptor_file_name), "r" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 418 | ) as descriptor_file: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 419 | content = descriptor_file.read() |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 420 | elif compressed == "zip": |
| 421 | zipfile = ZipFile(file_pkg) |
| 422 | descriptor_file_name = None |
| 423 | for package_file in zipfile.infolist(): |
| 424 | zipfilename = package_file.filename |
| 425 | file_path = zipfilename.split("/") |
| 426 | if ( |
| 427 | not file_path[0] or ".." in zipfilename |
| 428 | ): # if start with "/" means absolute path |
| 429 | raise EngineException( |
| 430 | "Absolute path or '..' are not allowed for package descriptor zip" |
| 431 | ) |
| 432 | |
| 433 | if ( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 434 | zipfilename.endswith(".yaml") |
| 435 | or zipfilename.endswith(".json") |
| 436 | or zipfilename.endswith(".yml") |
| 437 | ) and ( |
| 438 | zipfilename.find("/") < 0 |
| 439 | or zipfilename.find("Definitions") >= 0 |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 440 | ): |
| 441 | storage["pkg-dir"] = "" |
| 442 | if descriptor_file_name: |
| 443 | raise EngineException( |
| 444 | "Found more than one descriptor file at package descriptor zip" |
| 445 | ) |
| 446 | descriptor_file_name = zipfilename |
| 447 | if not descriptor_file_name: |
| 448 | raise EngineException( |
| 449 | "Not found any descriptor file at package descriptor zip" |
| 450 | ) |
| 451 | storage["descriptor"] = descriptor_file_name |
| 452 | storage["zipfile"] = filename |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 453 | self.fs.file_extract(zipfile, proposed_revision_path) |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 454 | |
| 455 | with self.fs.file_open( |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 456 | (proposed_revision_path, descriptor_file_name), "r" |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 457 | ) as descriptor_file: |
| 458 | content = descriptor_file.read() |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 459 | else: |
| 460 | content = file_pkg.read() |
| 461 | storage["descriptor"] = descriptor_file_name = filename |
| 462 | |
| 463 | if descriptor_file_name.endswith(".json"): |
| 464 | error_text = "Invalid json format " |
| 465 | indata = json.load(content) |
| 466 | else: |
| 467 | error_text = "Invalid yaml format " |
| garciadeblas | 4cd875d | 2023-02-14 19:05:34 +0100 | [diff] [blame] | 468 | indata = yaml.safe_load(content) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 469 | |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 470 | # Need to close the file package here so it can be copied from the |
| 471 | # revision to the current, unrevisioned record |
| 472 | if file_pkg: |
| 473 | file_pkg.close() |
| 474 | file_pkg = None |
| 475 | |
| 476 | # Fetch both the incoming, proposed revision and the original revision so we |
| 477 | # can call a validate method to compare them |
| 478 | current_revision_path = _id + "/" |
| 479 | self.fs.sync(from_path=current_revision_path) |
| 480 | self.fs.sync(from_path=proposed_revision_path) |
| 481 | |
| 482 | if revision > 1: |
| 483 | try: |
| 484 | self._validate_descriptor_changes( |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 485 | _id, |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 486 | descriptor_file_name, |
| 487 | current_revision_path, |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 488 | proposed_revision_path, |
| 489 | ) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 490 | except Exception as e: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 491 | shutil.rmtree( |
| 492 | self.fs.path + current_revision_path, ignore_errors=True |
| 493 | ) |
| 494 | shutil.rmtree( |
| 495 | self.fs.path + proposed_revision_path, ignore_errors=True |
| 496 | ) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 497 | # Only delete the new revision. We need to keep the original version in place |
| 498 | # as it has not been changed. |
| 499 | self.fs.file_delete(proposed_revision_path, ignore_non_exist=True) |
| 500 | raise e |
| 501 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 502 | indata = self._remove_envelop(indata) |
| 503 | |
| 504 | # Override descriptor with query string kwargs |
| 505 | if kwargs: |
| 506 | self._update_input_with_kwargs(indata, kwargs) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 507 | |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 508 | current_desc["_admin"]["storage"] = storage |
| 509 | current_desc["_admin"]["onboardingState"] = "ONBOARDED" |
| 510 | current_desc["_admin"]["operationalState"] = "ENABLED" |
| 511 | current_desc["_admin"]["modified"] = time() |
| 512 | current_desc["_admin"]["revision"] = revision |
| 513 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 514 | deep_update_rfc7396(current_desc, indata) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 515 | current_desc = self.check_conflict_on_edit( |
| 516 | session, current_desc, indata, _id=_id |
| 517 | ) |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 518 | |
| 519 | # Copy the revision to the active package name by its original id |
| 520 | shutil.rmtree(self.fs.path + current_revision_path, ignore_errors=True) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 521 | os.rename( |
| 522 | self.fs.path + proposed_revision_path, |
| 523 | self.fs.path + current_revision_path, |
| 524 | ) |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 525 | self.fs.file_delete(current_revision_path, ignore_non_exist=True) |
| 526 | self.fs.mkdir(current_revision_path) |
| 527 | self.fs.reverse_sync(from_path=current_revision_path) |
| 528 | |
| 529 | shutil.rmtree(self.fs.path + _id) |
| 530 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 531 | self.db.replace(self.topic, _id, current_desc) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 532 | |
| 533 | # Store a copy of the package as a point in time revision |
| 534 | revision_desc = dict(current_desc) |
| 535 | revision_desc["_id"] = _id + ":" + str(revision_desc["_admin"]["revision"]) |
| 536 | self.db.create(self.topic + "_revisions", revision_desc) |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 537 | fs_rollback = [] |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 538 | |
| 539 | indata["_id"] = _id |
| K Sai Kiran | c96fd69 | 2019-10-16 17:50:53 +0530 | [diff] [blame] | 540 | self._send_msg("edited", indata) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 541 | |
| 542 | # TODO if descriptor has changed because kwargs update content and remove cached zip |
| 543 | # TODO if zip is not present creates one |
| 544 | return True |
| 545 | |
| 546 | except EngineException: |
| 547 | raise |
| 548 | except IndexError: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 549 | raise EngineException( |
| 550 | "invalid Content-Range header format. Expected 'bytes start-end/total'", |
| 551 | HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE, |
| 552 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 553 | except IOError as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 554 | raise EngineException( |
| 555 | "invalid upload transaction sequence: '{}'".format(e), |
| 556 | HTTPStatus.BAD_REQUEST, |
| 557 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 558 | except tarfile.ReadError as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 559 | raise EngineException( |
| 560 | "invalid file content {}".format(e), HTTPStatus.BAD_REQUEST |
| 561 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 562 | except (ValueError, yaml.YAMLError) as e: |
| 563 | raise EngineException(error_text + str(e)) |
| 564 | except ValidationError as e: |
| 565 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 566 | finally: |
| 567 | if file_pkg: |
| 568 | file_pkg.close() |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 569 | for file in fs_rollback: |
| 570 | self.fs.file_delete(file, ignore_non_exist=True) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 571 | |
| 572 | def get_file(self, session, _id, path=None, accept_header=None): |
| 573 | """ |
| 574 | Return the file content of a vnfd or nsd |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 575 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 576 | :param _id: Identity of the vnfd, nsd |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 577 | :param path: artifact path or "$DESCRIPTOR" or None |
| 578 | :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 579 | :return: opened file plus Accept format or raises an exception |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 580 | """ |
| 581 | accept_text = accept_zip = False |
| 582 | if accept_header: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 583 | if "text/plain" in accept_header or "*/*" in accept_header: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 584 | accept_text = True |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 585 | if "application/zip" in accept_header or "*/*" in accept_header: |
| 586 | accept_zip = "application/zip" |
| 587 | elif "application/gzip" in accept_header: |
| 588 | accept_zip = "application/gzip" |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 589 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 590 | if not accept_text and not accept_zip: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 591 | raise EngineException( |
| 592 | "provide request header 'Accept' with 'application/zip' or 'text/plain'", |
| 593 | http_code=HTTPStatus.NOT_ACCEPTABLE, |
| 594 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 595 | |
| 596 | content = self.show(session, _id) |
| 597 | if content["_admin"]["onboardingState"] != "ONBOARDED": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 598 | raise EngineException( |
| 599 | "Cannot get content because this resource is not at 'ONBOARDED' state. " |
| 600 | "onboardingState is {}".format(content["_admin"]["onboardingState"]), |
| 601 | http_code=HTTPStatus.CONFLICT, |
| 602 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 603 | storage = content["_admin"]["storage"] |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 604 | if path is not None and path != "$DESCRIPTOR": # artifacts |
| selvi.j | 5be838c | 2022-08-25 06:24:49 +0000 | [diff] [blame] | 605 | if not storage.get("pkg-dir") and not storage.get("folder"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 606 | raise EngineException( |
| 607 | "Packages does not contains artifacts", |
| 608 | http_code=HTTPStatus.BAD_REQUEST, |
| 609 | ) |
| 610 | if self.fs.file_exists( |
| 611 | (storage["folder"], storage["pkg-dir"], *path), "dir" |
| 612 | ): |
| 613 | folder_content = self.fs.dir_ls( |
| 614 | (storage["folder"], storage["pkg-dir"], *path) |
| 615 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 616 | return folder_content, "text/plain" |
| 617 | # TODO manage folders in http |
| 618 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 619 | return ( |
| 620 | self.fs.file_open( |
| 621 | (storage["folder"], storage["pkg-dir"], *path), "rb" |
| 622 | ), |
| 623 | "application/octet-stream", |
| 624 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 625 | |
| 626 | # pkgtype accept ZIP TEXT -> result |
| 627 | # manyfiles yes X -> zip |
| 628 | # no yes -> error |
| 629 | # onefile yes no -> zip |
| 630 | # X yes -> text |
| tierno | ee00275 | 2020-08-04 14:14:16 +0000 | [diff] [blame] | 631 | contain_many_files = False |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 632 | if storage.get("pkg-dir"): |
| tierno | ee00275 | 2020-08-04 14:14:16 +0000 | [diff] [blame] | 633 | # check if there are more than one file in the package, ignoring checksums.txt. |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 634 | pkg_files = self.fs.dir_ls((storage["folder"], storage["pkg-dir"])) |
| 635 | if len(pkg_files) >= 3 or ( |
| 636 | len(pkg_files) == 2 and "checksums.txt" not in pkg_files |
| 637 | ): |
| tierno | ee00275 | 2020-08-04 14:14:16 +0000 | [diff] [blame] | 638 | contain_many_files = True |
| 639 | if accept_text and (not contain_many_files or path == "$DESCRIPTOR"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 640 | return ( |
| 641 | self.fs.file_open((storage["folder"], storage["descriptor"]), "r"), |
| 642 | "text/plain", |
| 643 | ) |
| tierno | ee00275 | 2020-08-04 14:14:16 +0000 | [diff] [blame] | 644 | elif contain_many_files and not accept_zip: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 645 | raise EngineException( |
| 646 | "Packages that contains several files need to be retrieved with 'application/zip'" |
| 647 | "Accept header", |
| 648 | http_code=HTTPStatus.NOT_ACCEPTABLE, |
| 649 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 650 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 651 | if not storage.get("zipfile"): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 652 | # TODO generate zipfile if not present |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 653 | raise EngineException( |
| 654 | "Only allowed 'text/plain' Accept header for this descriptor. To be solved in " |
| 655 | "future versions", |
| 656 | http_code=HTTPStatus.NOT_ACCEPTABLE, |
| 657 | ) |
| 658 | return ( |
| 659 | self.fs.file_open((storage["folder"], storage["zipfile"]), "rb"), |
| 660 | accept_zip, |
| 661 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 662 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 663 | def _remove_yang_prefixes_from_descriptor(self, descriptor): |
| 664 | new_descriptor = {} |
| 665 | for k, v in descriptor.items(): |
| 666 | new_v = v |
| 667 | if isinstance(v, dict): |
| 668 | new_v = self._remove_yang_prefixes_from_descriptor(v) |
| 669 | elif isinstance(v, list): |
| 670 | new_v = list() |
| 671 | for x in v: |
| 672 | if isinstance(x, dict): |
| 673 | new_v.append(self._remove_yang_prefixes_from_descriptor(x)) |
| 674 | else: |
| 675 | new_v.append(x) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 676 | new_descriptor[k.split(":")[-1]] = new_v |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 677 | return new_descriptor |
| 678 | |
| gcalvino | 46e4cb8 | 2018-10-26 13:10:22 +0200 | [diff] [blame] | 679 | def pyangbind_validation(self, item, data, force=False): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 680 | raise EngineException( |
| 681 | "Not possible to validate '{}' item".format(item), |
| 682 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 683 | ) |
| gcalvino | 46e4cb8 | 2018-10-26 13:10:22 +0200 | [diff] [blame] | 684 | |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 685 | def _validate_input_edit(self, indata, content, force=False): |
| 686 | # not needed to validate with pyangbind becuase it will be validated at check_conflict_on_edit |
| 687 | if "_id" in indata: |
| 688 | indata.pop("_id") |
| 689 | if "_admin" not in indata: |
| 690 | indata["_admin"] = {} |
| 691 | |
| 692 | if "operationalState" in indata: |
| 693 | if indata["operationalState"] in ("ENABLED", "DISABLED"): |
| 694 | indata["_admin"]["operationalState"] = indata.pop("operationalState") |
| 695 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 696 | raise EngineException( |
| 697 | "State '{}' is not a valid operational state".format( |
| 698 | indata["operationalState"] |
| 699 | ), |
| 700 | http_code=HTTPStatus.BAD_REQUEST, |
| 701 | ) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 702 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 703 | # In the case of user defined data, we need to put the data in the root of the object |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 704 | # to preserve current expected behaviour |
| 705 | if "userDefinedData" in indata: |
| 706 | data = indata.pop("userDefinedData") |
| gatici | d7debb9 | 2023-07-31 14:37:32 +0300 | [diff] [blame] | 707 | if isinstance(data, dict): |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 708 | indata["_admin"]["userDefinedData"] = data |
| 709 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 710 | raise EngineException( |
| 711 | "userDefinedData should be an object, but is '{}' instead".format( |
| 712 | type(data) |
| 713 | ), |
| 714 | http_code=HTTPStatus.BAD_REQUEST, |
| 715 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 716 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 717 | if ( |
| 718 | "operationalState" in indata["_admin"] |
| 719 | and content["_admin"]["operationalState"] |
| 720 | == indata["_admin"]["operationalState"] |
| 721 | ): |
| 722 | raise EngineException( |
| 723 | "operationalState already {}".format( |
| 724 | content["_admin"]["operationalState"] |
| 725 | ), |
| 726 | http_code=HTTPStatus.CONFLICT, |
| 727 | ) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 728 | |
| 729 | return indata |
| 730 | |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 731 | def _validate_descriptor_changes( |
| 732 | self, |
| 733 | descriptor_id, |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 734 | descriptor_file_name, |
| 735 | old_descriptor_directory, |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 736 | new_descriptor_directory, |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 737 | ): |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 738 | # Example: |
| 739 | # raise EngineException( |
| 740 | # "Error in validating new descriptor: <NODE> cannot be modified", |
| 741 | # http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 742 | # ) |
| 743 | pass |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 744 | |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 745 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 746 | class VnfdTopic(DescriptorTopic): |
| 747 | topic = "vnfds" |
| 748 | topic_msg = "vnfd" |
| 749 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 750 | def __init__(self, db, fs, msg, auth): |
| 751 | DescriptorTopic.__init__(self, db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 752 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 753 | def pyangbind_validation(self, item, data, force=False): |
| garciaale | df718ae | 2020-12-03 19:17:28 -0300 | [diff] [blame] | 754 | if self._descriptor_data_is_in_old_format(data): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 755 | raise EngineException( |
| 756 | "ERROR: Unsupported descriptor format. Please, use an ETSI SOL006 descriptor.", |
| 757 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 758 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 759 | try: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 760 | myvnfd = etsi_nfv_vnfd.etsi_nfv_vnfd() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 761 | pybindJSONDecoder.load_ietf_json( |
| 762 | {"etsi-nfv-vnfd:vnfd": data}, |
| 763 | None, |
| 764 | None, |
| 765 | obj=myvnfd, |
| 766 | path_helper=True, |
| 767 | skip_unknown=force, |
| 768 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 769 | out = pybindJSON.dumps(myvnfd, mode="ietf") |
| 770 | desc_out = self._remove_envelop(yaml.safe_load(out)) |
| 771 | desc_out = self._remove_yang_prefixes_from_descriptor(desc_out) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 772 | return utils.deep_update_dict(data, desc_out) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 773 | except Exception as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 774 | raise EngineException( |
| 775 | "Error in pyangbind validation: {}".format(str(e)), |
| 776 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 777 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 778 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 779 | @staticmethod |
| garciaale | df718ae | 2020-12-03 19:17:28 -0300 | [diff] [blame] | 780 | def _descriptor_data_is_in_old_format(data): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 781 | return ("vnfd-catalog" in data) or ("vnfd:vnfd-catalog" in data) |
| garciaale | df718ae | 2020-12-03 19:17:28 -0300 | [diff] [blame] | 782 | |
| 783 | @staticmethod |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 784 | def _remove_envelop(indata=None): |
| 785 | if not indata: |
| 786 | return {} |
| 787 | clean_indata = indata |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 788 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 789 | if clean_indata.get("etsi-nfv-vnfd:vnfd"): |
| 790 | if not isinstance(clean_indata["etsi-nfv-vnfd:vnfd"], dict): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 791 | raise EngineException("'etsi-nfv-vnfd:vnfd' must be a dict") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 792 | clean_indata = clean_indata["etsi-nfv-vnfd:vnfd"] |
| 793 | elif clean_indata.get("vnfd"): |
| 794 | if not isinstance(clean_indata["vnfd"], dict): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 795 | raise EngineException("'vnfd' must be dict") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 796 | clean_indata = clean_indata["vnfd"] |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 797 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 798 | return clean_indata |
| 799 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 800 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 801 | final_content = super().check_conflict_on_edit( |
| 802 | session, final_content, edit_content, _id |
| 803 | ) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 804 | |
| 805 | # set type of vnfd |
| 806 | contains_pdu = False |
| 807 | contains_vdu = False |
| 808 | for vdu in get_iterable(final_content.get("vdu")): |
| 809 | if vdu.get("pdu-type"): |
| 810 | contains_pdu = True |
| 811 | else: |
| 812 | contains_vdu = True |
| 813 | if contains_pdu: |
| 814 | final_content["_admin"]["type"] = "hnfd" if contains_vdu else "pnfd" |
| 815 | elif contains_vdu: |
| 816 | final_content["_admin"]["type"] = "vnfd" |
| 817 | # if neither vud nor pdu do not fill type |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 818 | return final_content |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 819 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 820 | def check_conflict_on_del(self, session, _id, db_content): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 821 | """ |
| 822 | Check that there is not any NSD that uses this VNFD. Only NSDs belonging to this project are considered. Note |
| 823 | that VNFD can be public and be used by NSD of other projects. Also check there are not deployments, or vnfr |
| 824 | that uses this vnfd |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 825 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 826 | :param _id: vnfd internal id |
| 827 | :param db_content: The database content of the _id. |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 828 | :return: None or raises EngineException with the conflict |
| 829 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 830 | if session["force"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 831 | return |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 832 | descriptor = db_content |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 833 | descriptor_id = descriptor.get("id") |
| 834 | if not descriptor_id: # empty vnfd not uploaded |
| 835 | return |
| 836 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 837 | _filter = self._get_project_filter(session) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 838 | # check vnfrs using this vnfd |
| 839 | _filter["vnfd-id"] = _id |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 840 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 841 | if self.db.get_list("vnfrs", _filter): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 842 | raise EngineException( |
| 843 | "There is at least one VNF instance using this descriptor", |
| 844 | http_code=HTTPStatus.CONFLICT, |
| 845 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 846 | |
| 847 | # check NSD referencing this VNFD |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 848 | del _filter["vnfd-id"] |
| garciadeblas | f576eb9 | 2021-04-18 20:54:13 +0000 | [diff] [blame] | 849 | _filter["vnfd-id"] = descriptor_id |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 850 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 851 | if self.db.get_list("nsds", _filter): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 852 | raise EngineException( |
| 853 | "There is at least one NS package referencing this descriptor", |
| 854 | http_code=HTTPStatus.CONFLICT, |
| 855 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 856 | |
| gcalvino | a6fe000 | 2019-01-09 13:27:11 +0100 | [diff] [blame] | 857 | def _validate_input_new(self, indata, storage_params, force=False): |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 858 | indata.pop("onboardingState", None) |
| 859 | indata.pop("operationalState", None) |
| 860 | indata.pop("usageState", None) |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 861 | indata.pop("links", None) |
| 862 | |
| Adurti | 014a630 | 2024-05-08 05:03:01 +0000 | [diff] [blame^] | 863 | validation = validation_im() |
| gcalvino | 46e4cb8 | 2018-10-26 13:10:22 +0200 | [diff] [blame] | 864 | indata = self.pyangbind_validation("vnfds", indata, force) |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 865 | # Cross references validation in the descriptor |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 866 | |
| 867 | self.validate_mgmt_interface_connection_point(indata) |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 868 | |
| 869 | for vdu in get_iterable(indata.get("vdu")): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 870 | self.validate_vdu_internal_connection_points(vdu) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 871 | self._validate_vdu_cloud_init_in_package(storage_params, vdu, indata) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 872 | self._validate_vdu_charms_in_package(storage_params, indata) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 873 | |
| 874 | self._validate_vnf_charms_in_package(storage_params, indata) |
| 875 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 876 | self.validate_external_connection_points(indata) |
| 877 | self.validate_internal_virtual_links(indata) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 878 | self.validate_monitoring_params(indata) |
| 879 | self.validate_scaling_group_descriptor(indata) |
| Adurti | 10f814e | 2023-11-08 06:26:04 +0000 | [diff] [blame] | 880 | self.validate_healing_group_descriptor(indata) |
| 881 | self.validate_alarm_group_descriptor(indata) |
| 882 | self.validate_storage_compute_descriptor(indata) |
| Adurti | 014a630 | 2024-05-08 05:03:01 +0000 | [diff] [blame^] | 883 | validation.validate_vdu_profile_in_descriptor(indata) |
| 884 | validation.validate_instantiation_level_descriptor(indata) |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 885 | self.validate_helm_chart(indata) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 886 | |
| 887 | return indata |
| 888 | |
| 889 | @staticmethod |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 890 | def validate_helm_chart(indata): |
| Gabriel Cuba | 646773d | 2023-11-20 01:43:05 -0500 | [diff] [blame] | 891 | def is_url(url): |
| 892 | result = urlparse(url) |
| 893 | return all([result.scheme, result.netloc]) |
| 894 | |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 895 | kdus = indata.get("kdu", []) |
| 896 | for kdu in kdus: |
| 897 | helm_chart_value = kdu.get("helm-chart") |
| 898 | if not helm_chart_value: |
| 899 | continue |
| Gabriel Cuba | 646773d | 2023-11-20 01:43:05 -0500 | [diff] [blame] | 900 | if not ( |
| 901 | valid_helm_chart_re.match(helm_chart_value) or is_url(helm_chart_value) |
| 902 | ): |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 903 | raise EngineException( |
| 904 | "helm-chart '{}' is not valid".format(helm_chart_value), |
| 905 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 906 | ) |
| 907 | |
| 908 | @staticmethod |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 909 | def validate_mgmt_interface_connection_point(indata): |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 910 | if not indata.get("vdu"): |
| 911 | return |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 912 | if not indata.get("mgmt-cp"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 913 | raise EngineException( |
| 914 | "'mgmt-cp' is a mandatory field and it is not defined", |
| 915 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 916 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 917 | |
| 918 | for cp in get_iterable(indata.get("ext-cpd")): |
| 919 | if cp["id"] == indata["mgmt-cp"]: |
| 920 | break |
| 921 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 922 | raise EngineException( |
| 923 | "mgmt-cp='{}' must match an existing ext-cpd".format(indata["mgmt-cp"]), |
| 924 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 925 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 926 | |
| 927 | @staticmethod |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 928 | def validate_vdu_internal_connection_points(vdu): |
| 929 | int_cpds = set() |
| 930 | for cpd in get_iterable(vdu.get("int-cpd")): |
| 931 | cpd_id = cpd.get("id") |
| 932 | if cpd_id and cpd_id in int_cpds: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 933 | raise EngineException( |
| 934 | "vdu[id='{}']:int-cpd[id='{}'] is already used by other int-cpd".format( |
| 935 | vdu["id"], cpd_id |
| 936 | ), |
| 937 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 938 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 939 | int_cpds.add(cpd_id) |
| 940 | |
| 941 | @staticmethod |
| 942 | def validate_external_connection_points(indata): |
| 943 | all_vdus_int_cpds = set() |
| 944 | for vdu in get_iterable(indata.get("vdu")): |
| 945 | for int_cpd in get_iterable(vdu.get("int-cpd")): |
| 946 | all_vdus_int_cpds.add((vdu.get("id"), int_cpd.get("id"))) |
| 947 | |
| 948 | ext_cpds = set() |
| 949 | for cpd in get_iterable(indata.get("ext-cpd")): |
| 950 | cpd_id = cpd.get("id") |
| 951 | if cpd_id and cpd_id in ext_cpds: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 952 | raise EngineException( |
| 953 | "ext-cpd[id='{}'] is already used by other ext-cpd".format(cpd_id), |
| 954 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 955 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 956 | ext_cpds.add(cpd_id) |
| 957 | |
| 958 | int_cpd = cpd.get("int-cpd") |
| 959 | if int_cpd: |
| 960 | if (int_cpd.get("vdu-id"), int_cpd.get("cpd")) not in all_vdus_int_cpds: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 961 | raise EngineException( |
| 962 | "ext-cpd[id='{}']:int-cpd must match an existing vdu int-cpd".format( |
| 963 | cpd_id |
| 964 | ), |
| 965 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 966 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 967 | # TODO: Validate k8s-cluster-net points to a valid k8s-cluster:nets ? |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 968 | |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 969 | def _validate_vdu_charms_in_package(self, storage_params, indata): |
| 970 | for df in indata["df"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 971 | if ( |
| 972 | "lcm-operations-configuration" in df |
| 973 | and "operate-vnf-op-config" in df["lcm-operations-configuration"] |
| 974 | ): |
| 975 | configs = df["lcm-operations-configuration"][ |
| 976 | "operate-vnf-op-config" |
| 977 | ].get("day1-2", []) |
| garciaale | 2c4f9ec | 2021-03-01 11:04:50 -0300 | [diff] [blame] | 978 | vdus = df.get("vdu-profile", []) |
| bravof | 2325828 | 2021-02-22 18:04:40 -0300 | [diff] [blame] | 979 | for vdu in vdus: |
| 980 | for config in configs: |
| 981 | if config["id"] == vdu["id"] and utils.find_in_list( |
| 982 | config.get("execution-environment-list", []), |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 983 | lambda ee: "juju" in ee, |
| bravof | 2325828 | 2021-02-22 18:04:40 -0300 | [diff] [blame] | 984 | ): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 985 | if not self._validate_package_folders( |
| 986 | storage_params, "charms" |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 987 | ) and not self._validate_package_folders( |
| 988 | storage_params, "Scripts/charms" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 989 | ): |
| 990 | raise EngineException( |
| 991 | "Charm defined in vnf[id={}] but not present in " |
| 992 | "package".format(indata["id"]) |
| 993 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 994 | |
| 995 | def _validate_vdu_cloud_init_in_package(self, storage_params, vdu, indata): |
| 996 | if not vdu.get("cloud-init-file"): |
| 997 | return |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 998 | if not self._validate_package_folders( |
| 999 | storage_params, "cloud_init", vdu["cloud-init-file"] |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 1000 | ) and not self._validate_package_folders( |
| 1001 | storage_params, "Scripts/cloud_init", vdu["cloud-init-file"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1002 | ): |
| 1003 | raise EngineException( |
| 1004 | "Cloud-init defined in vnf[id={}]:vdu[id={}] but not present in " |
| 1005 | "package".format(indata["id"], vdu["id"]) |
| 1006 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1007 | |
| 1008 | def _validate_vnf_charms_in_package(self, storage_params, indata): |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1009 | # Get VNF configuration through new container |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1010 | for deployment_flavor in indata.get("df", []): |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1011 | if "lcm-operations-configuration" not in deployment_flavor: |
| 1012 | return |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1013 | if ( |
| 1014 | "operate-vnf-op-config" |
| 1015 | not in deployment_flavor["lcm-operations-configuration"] |
| 1016 | ): |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1017 | return |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1018 | for day_1_2_config in deployment_flavor["lcm-operations-configuration"][ |
| 1019 | "operate-vnf-op-config" |
| 1020 | ]["day1-2"]: |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1021 | if day_1_2_config["id"] == indata["id"]: |
| bravof | 2325828 | 2021-02-22 18:04:40 -0300 | [diff] [blame] | 1022 | if utils.find_in_list( |
| 1023 | day_1_2_config.get("execution-environment-list", []), |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1024 | lambda ee: "juju" in ee, |
| bravof | 2325828 | 2021-02-22 18:04:40 -0300 | [diff] [blame] | 1025 | ): |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 1026 | if not self._validate_package_folders( |
| 1027 | storage_params, "charms" |
| 1028 | ) and not self._validate_package_folders( |
| 1029 | storage_params, "Scripts/charms" |
| 1030 | ): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1031 | raise EngineException( |
| 1032 | "Charm defined in vnf[id={}] but not present in " |
| 1033 | "package".format(indata["id"]) |
| 1034 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1035 | |
| 1036 | def _validate_package_folders(self, storage_params, folder, file=None): |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 1037 | if not storage_params: |
| 1038 | return False |
| 1039 | elif not storage_params.get("pkg-dir"): |
| 1040 | if self.fs.file_exists("{}_".format(storage_params["folder"]), "dir"): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1041 | f = "{}_/{}".format(storage_params["folder"], folder) |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 1042 | else: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1043 | f = "{}/{}".format(storage_params["folder"], folder) |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 1044 | if file: |
| 1045 | return self.fs.file_exists("{}/{}".format(f, file), "file") |
| 1046 | else: |
| bravof | c26740a | 2021-11-08 09:44:54 -0300 | [diff] [blame] | 1047 | if self.fs.file_exists(f, "dir"): |
| 1048 | if self.fs.dir_ls(f): |
| 1049 | return True |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1050 | return False |
| 1051 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1052 | if self.fs.file_exists("{}_".format(storage_params["folder"]), "dir"): |
| 1053 | f = "{}_/{}/{}".format( |
| 1054 | storage_params["folder"], storage_params["pkg-dir"], folder |
| 1055 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1056 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1057 | f = "{}/{}/{}".format( |
| 1058 | storage_params["folder"], storage_params["pkg-dir"], folder |
| 1059 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1060 | if file: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1061 | return self.fs.file_exists("{}/{}".format(f, file), "file") |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1062 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1063 | if self.fs.file_exists(f, "dir"): |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1064 | if self.fs.dir_ls(f): |
| 1065 | return True |
| 1066 | return False |
| 1067 | |
| 1068 | @staticmethod |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1069 | def validate_internal_virtual_links(indata): |
| 1070 | all_ivld_ids = set() |
| 1071 | for ivld in get_iterable(indata.get("int-virtual-link-desc")): |
| 1072 | ivld_id = ivld.get("id") |
| 1073 | if ivld_id and ivld_id in all_ivld_ids: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1074 | raise EngineException( |
| 1075 | "Duplicated VLD id in int-virtual-link-desc[id={}]".format(ivld_id), |
| 1076 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1077 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1078 | else: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1079 | all_ivld_ids.add(ivld_id) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1080 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1081 | for vdu in get_iterable(indata.get("vdu")): |
| 1082 | for int_cpd in get_iterable(vdu.get("int-cpd")): |
| 1083 | int_cpd_ivld_id = int_cpd.get("int-virtual-link-desc") |
| 1084 | if int_cpd_ivld_id and int_cpd_ivld_id not in all_ivld_ids: |
| 1085 | raise EngineException( |
| 1086 | "vdu[id='{}']:int-cpd[id='{}']:int-virtual-link-desc='{}' must match an existing " |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1087 | "int-virtual-link-desc".format( |
| 1088 | vdu["id"], int_cpd["id"], int_cpd_ivld_id |
| 1089 | ), |
| 1090 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1091 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1092 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1093 | for df in get_iterable(indata.get("df")): |
| 1094 | for vlp in get_iterable(df.get("virtual-link-profile")): |
| 1095 | vlp_ivld_id = vlp.get("id") |
| 1096 | if vlp_ivld_id and vlp_ivld_id not in all_ivld_ids: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1097 | raise EngineException( |
| 1098 | "df[id='{}']:virtual-link-profile='{}' must match an existing " |
| 1099 | "int-virtual-link-desc".format(df["id"], vlp_ivld_id), |
| 1100 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1101 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1102 | |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1103 | @staticmethod |
| 1104 | def validate_monitoring_params(indata): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1105 | all_monitoring_params = set() |
| 1106 | for ivld in get_iterable(indata.get("int-virtual-link-desc")): |
| 1107 | for mp in get_iterable(ivld.get("monitoring-parameters")): |
| 1108 | mp_id = mp.get("id") |
| 1109 | if mp_id and mp_id in all_monitoring_params: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1110 | raise EngineException( |
| 1111 | "Duplicated monitoring-parameter id in " |
| 1112 | "int-virtual-link-desc[id='{}']:monitoring-parameters[id='{}']".format( |
| 1113 | ivld["id"], mp_id |
| 1114 | ), |
| 1115 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1116 | ) |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 1117 | else: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1118 | all_monitoring_params.add(mp_id) |
| 1119 | |
| 1120 | for vdu in get_iterable(indata.get("vdu")): |
| 1121 | for mp in get_iterable(vdu.get("monitoring-parameter")): |
| 1122 | mp_id = mp.get("id") |
| 1123 | if mp_id and mp_id in all_monitoring_params: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1124 | raise EngineException( |
| 1125 | "Duplicated monitoring-parameter id in " |
| 1126 | "vdu[id='{}']:monitoring-parameter[id='{}']".format( |
| 1127 | vdu["id"], mp_id |
| 1128 | ), |
| 1129 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1130 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1131 | else: |
| 1132 | all_monitoring_params.add(mp_id) |
| 1133 | |
| 1134 | for df in get_iterable(indata.get("df")): |
| 1135 | for mp in get_iterable(df.get("monitoring-parameter")): |
| 1136 | mp_id = mp.get("id") |
| 1137 | if mp_id and mp_id in all_monitoring_params: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1138 | raise EngineException( |
| 1139 | "Duplicated monitoring-parameter id in " |
| 1140 | "df[id='{}']:monitoring-parameter[id='{}']".format( |
| 1141 | df["id"], mp_id |
| 1142 | ), |
| 1143 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1144 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1145 | else: |
| 1146 | all_monitoring_params.add(mp_id) |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 1147 | |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1148 | @staticmethod |
| 1149 | def validate_scaling_group_descriptor(indata): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1150 | all_monitoring_params = set() |
| Adurti | 10f814e | 2023-11-08 06:26:04 +0000 | [diff] [blame] | 1151 | all_vdu_ids = set() |
| 1152 | for df in get_iterable(indata.get("df")): |
| 1153 | for il in get_iterable(df.get("instantiation-level")): |
| 1154 | for vl in get_iterable(il.get("vdu-level")): |
| 1155 | all_vdu_ids.add(vl.get("vdu-id")) |
| 1156 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1157 | for ivld in get_iterable(indata.get("int-virtual-link-desc")): |
| 1158 | for mp in get_iterable(ivld.get("monitoring-parameters")): |
| 1159 | all_monitoring_params.add(mp.get("id")) |
| 1160 | |
| 1161 | for vdu in get_iterable(indata.get("vdu")): |
| 1162 | for mp in get_iterable(vdu.get("monitoring-parameter")): |
| 1163 | all_monitoring_params.add(mp.get("id")) |
| 1164 | |
| 1165 | for df in get_iterable(indata.get("df")): |
| 1166 | for mp in get_iterable(df.get("monitoring-parameter")): |
| 1167 | all_monitoring_params.add(mp.get("id")) |
| 1168 | |
| 1169 | for df in get_iterable(indata.get("df")): |
| 1170 | for sa in get_iterable(df.get("scaling-aspect")): |
| Adurti | 10f814e | 2023-11-08 06:26:04 +0000 | [diff] [blame] | 1171 | for deltas in get_iterable( |
| 1172 | sa.get("aspect-delta-details").get("deltas") |
| 1173 | ): |
| 1174 | for vds in get_iterable(deltas.get("vdu-delta")): |
| 1175 | sa_vdu_id = vds.get("id") |
| 1176 | if sa_vdu_id and sa_vdu_id not in all_vdu_ids: |
| 1177 | raise EngineException( |
| 1178 | "df[id='{}']:scaling-aspect[id='{}']:aspect-delta-details" |
| 1179 | "[delta='{}']: " |
| 1180 | "vdu-id='{}' not defined in vdu".format( |
| 1181 | df["id"], |
| 1182 | sa["id"], |
| 1183 | deltas["id"], |
| 1184 | sa_vdu_id, |
| 1185 | ), |
| 1186 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1187 | ) |
| 1188 | |
| 1189 | for df in get_iterable(indata.get("df")): |
| 1190 | for sa in get_iterable(df.get("scaling-aspect")): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1191 | for sp in get_iterable(sa.get("scaling-policy")): |
| 1192 | for sc in get_iterable(sp.get("scaling-criteria")): |
| 1193 | sc_monitoring_param = sc.get("vnf-monitoring-param-ref") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1194 | if ( |
| 1195 | sc_monitoring_param |
| 1196 | and sc_monitoring_param not in all_monitoring_params |
| 1197 | ): |
| 1198 | raise EngineException( |
| 1199 | "df[id='{}']:scaling-aspect[id='{}']:scaling-policy" |
| 1200 | "[name='{}']:scaling-criteria[name='{}']: " |
| 1201 | "vnf-monitoring-param-ref='{}' not defined in any monitoring-param".format( |
| 1202 | df["id"], |
| 1203 | sa["id"], |
| 1204 | sp["name"], |
| 1205 | sc["name"], |
| 1206 | sc_monitoring_param, |
| 1207 | ), |
| 1208 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1209 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1210 | |
| 1211 | for sca in get_iterable(sa.get("scaling-config-action")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1212 | if ( |
| 1213 | "lcm-operations-configuration" not in df |
| 1214 | or "operate-vnf-op-config" |
| 1215 | not in df["lcm-operations-configuration"] |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1216 | or not utils.find_in_list( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1217 | df["lcm-operations-configuration"][ |
| 1218 | "operate-vnf-op-config" |
| 1219 | ].get("day1-2", []), |
| 1220 | lambda config: config["id"] == indata["id"], |
| 1221 | ) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1222 | ): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1223 | raise EngineException( |
| 1224 | "'day1-2 configuration' not defined in the descriptor but it is " |
| 1225 | "referenced by df[id='{}']:scaling-aspect[id='{}']:scaling-config-action".format( |
| 1226 | df["id"], sa["id"] |
| 1227 | ), |
| 1228 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1229 | ) |
| 1230 | for configuration in get_iterable( |
| 1231 | df["lcm-operations-configuration"]["operate-vnf-op-config"].get( |
| 1232 | "day1-2", [] |
| 1233 | ) |
| 1234 | ): |
| 1235 | for primitive in get_iterable( |
| 1236 | configuration.get("config-primitive") |
| 1237 | ): |
| 1238 | if ( |
| 1239 | primitive["name"] |
| 1240 | == sca["vnf-config-primitive-name-ref"] |
| 1241 | ): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1242 | break |
| 1243 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1244 | raise EngineException( |
| 1245 | "df[id='{}']:scaling-aspect[id='{}']:scaling-config-action:vnf-" |
| 1246 | "config-primitive-name-ref='{}' does not match any " |
| 1247 | "day1-2 configuration:config-primitive:name".format( |
| 1248 | df["id"], |
| 1249 | sa["id"], |
| 1250 | sca["vnf-config-primitive-name-ref"], |
| 1251 | ), |
| 1252 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1253 | ) |
| gcalvino | a6fe000 | 2019-01-09 13:27:11 +0100 | [diff] [blame] | 1254 | |
| Adurti | 10f814e | 2023-11-08 06:26:04 +0000 | [diff] [blame] | 1255 | @staticmethod |
| 1256 | def validate_healing_group_descriptor(indata): |
| 1257 | all_vdu_ids = set() |
| 1258 | for df in get_iterable(indata.get("df")): |
| 1259 | for il in get_iterable(df.get("instantiation-level")): |
| 1260 | for vl in get_iterable(il.get("vdu-level")): |
| 1261 | all_vdu_ids.add(vl.get("vdu-id")) |
| 1262 | |
| 1263 | for df in get_iterable(indata.get("df")): |
| 1264 | for ha in get_iterable(df.get("healing-aspect")): |
| 1265 | for hp in get_iterable(ha.get("healing-policy")): |
| 1266 | hp_monitoring_param = hp.get("vdu-id") |
| 1267 | if hp_monitoring_param and hp_monitoring_param not in all_vdu_ids: |
| 1268 | raise EngineException( |
| 1269 | "df[id='{}']:healing-aspect[id='{}']:healing-policy" |
| 1270 | "[name='{}']: " |
| 1271 | "vdu-id='{}' not defined in vdu".format( |
| 1272 | df["id"], |
| 1273 | ha["id"], |
| 1274 | hp["event-name"], |
| 1275 | hp_monitoring_param, |
| 1276 | ), |
| 1277 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1278 | ) |
| 1279 | |
| 1280 | @staticmethod |
| 1281 | def validate_alarm_group_descriptor(indata): |
| 1282 | all_monitoring_params = set() |
| 1283 | for ivld in get_iterable(indata.get("int-virtual-link-desc")): |
| 1284 | for mp in get_iterable(ivld.get("monitoring-parameters")): |
| 1285 | all_monitoring_params.add(mp.get("id")) |
| 1286 | |
| 1287 | for vdu in get_iterable(indata.get("vdu")): |
| 1288 | for mp in get_iterable(vdu.get("monitoring-parameter")): |
| 1289 | all_monitoring_params.add(mp.get("id")) |
| 1290 | |
| 1291 | for df in get_iterable(indata.get("df")): |
| 1292 | for mp in get_iterable(df.get("monitoring-parameter")): |
| 1293 | all_monitoring_params.add(mp.get("id")) |
| 1294 | |
| 1295 | for vdus in get_iterable(indata.get("vdu")): |
| 1296 | for alarms in get_iterable(vdus.get("alarm")): |
| 1297 | alarm_monitoring_param = alarms.get("vnf-monitoring-param-ref") |
| 1298 | if ( |
| 1299 | alarm_monitoring_param |
| 1300 | and alarm_monitoring_param not in all_monitoring_params |
| 1301 | ): |
| 1302 | raise EngineException( |
| 1303 | "vdu[id='{}']:alarm[id='{}']:" |
| 1304 | "vnf-monitoring-param-ref='{}' not defined in any monitoring-param".format( |
| 1305 | vdus["id"], |
| 1306 | alarms["alarm-id"], |
| 1307 | alarm_monitoring_param, |
| 1308 | ), |
| 1309 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1310 | ) |
| 1311 | |
| 1312 | @staticmethod |
| 1313 | def validate_storage_compute_descriptor(indata): |
| 1314 | all_vsd_ids = set() |
| 1315 | for vsd in get_iterable(indata.get("virtual-storage-desc")): |
| 1316 | all_vsd_ids.add(vsd.get("id")) |
| 1317 | |
| 1318 | all_vcd_ids = set() |
| 1319 | for vcd in get_iterable(indata.get("virtual-compute-desc")): |
| 1320 | all_vcd_ids.add(vcd.get("id")) |
| 1321 | |
| 1322 | for vdus in get_iterable(indata.get("vdu")): |
| 1323 | for vsd_ref in vdus.get("virtual-storage-desc"): |
| 1324 | if vsd_ref and vsd_ref not in all_vsd_ids: |
| 1325 | raise EngineException( |
| 1326 | "vdu[virtual-storage-desc='{}']" |
| 1327 | "not defined in vnfd".format( |
| 1328 | vsd_ref, |
| 1329 | ), |
| 1330 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1331 | ) |
| 1332 | |
| 1333 | for vdus in get_iterable(indata.get("vdu")): |
| 1334 | vcd_ref = vdus.get("virtual-compute-desc") |
| 1335 | if vcd_ref and vcd_ref not in all_vcd_ids: |
| 1336 | raise EngineException( |
| 1337 | "vdu[virtual-compute-desc='{}']" |
| 1338 | "not defined in vnfd".format( |
| 1339 | vdus["virtual-compute-desc"], |
| 1340 | ), |
| 1341 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1342 | ) |
| 1343 | |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 1344 | def delete_extra(self, session, _id, db_content, not_send_msg=None): |
| 1345 | """ |
| 1346 | Deletes associate file system storage (via super) |
| 1347 | Deletes associated vnfpkgops from database. |
| 1348 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1349 | :param _id: server internal id |
| 1350 | :param db_content: The database content of the descriptor |
| 1351 | :return: None |
| 1352 | :raises: FsException in case of error while deleting associated storage |
| 1353 | """ |
| 1354 | super().delete_extra(session, _id, db_content, not_send_msg) |
| 1355 | self.db.del_list("vnfpkgops", {"vnfPkgId": _id}) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1356 | self.db.del_list(self.topic + "_revisions", {"_id": {"$regex": _id}}) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1357 | |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 1358 | def sol005_projection(self, data): |
| 1359 | data["onboardingState"] = data["_admin"]["onboardingState"] |
| 1360 | data["operationalState"] = data["_admin"]["operationalState"] |
| 1361 | data["usageState"] = data["_admin"]["usageState"] |
| 1362 | |
| 1363 | links = {} |
| 1364 | links["self"] = {"href": "/vnfpkgm/v1/vnf_packages/{}".format(data["_id"])} |
| 1365 | links["vnfd"] = {"href": "/vnfpkgm/v1/vnf_packages/{}/vnfd".format(data["_id"])} |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1366 | links["packageContent"] = { |
| 1367 | "href": "/vnfpkgm/v1/vnf_packages/{}/package_content".format(data["_id"]) |
| 1368 | } |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 1369 | data["_links"] = links |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1370 | |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 1371 | return super().sol005_projection(data) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 1372 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1373 | @staticmethod |
| 1374 | def find_software_version(vnfd: dict) -> str: |
| 1375 | """Find the sotware version in the VNFD descriptors |
| 1376 | |
| 1377 | Args: |
| 1378 | vnfd (dict): Descriptor as a dictionary |
| 1379 | |
| 1380 | Returns: |
| 1381 | software-version (str) |
| 1382 | """ |
| 1383 | default_sw_version = "1.0" |
| 1384 | if vnfd.get("vnfd"): |
| 1385 | vnfd = vnfd["vnfd"] |
| 1386 | if vnfd.get("software-version"): |
| 1387 | return vnfd["software-version"] |
| 1388 | else: |
| 1389 | return default_sw_version |
| 1390 | |
| 1391 | @staticmethod |
| 1392 | def extract_policies(vnfd: dict) -> dict: |
| 1393 | """Removes the policies from the VNFD descriptors |
| 1394 | |
| 1395 | Args: |
| 1396 | vnfd (dict): Descriptor as a dictionary |
| 1397 | |
| 1398 | Returns: |
| 1399 | vnfd (dict): VNFD which does not include policies |
| 1400 | """ |
| elumalai | 3622f83 | 2022-07-08 12:06:27 +0530 | [diff] [blame] | 1401 | for df in vnfd.get("df", {}): |
| 1402 | for policy in ["scaling-aspect", "healing-aspect"]: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1403 | if df.get(policy, {}): |
| elumalai | 3622f83 | 2022-07-08 12:06:27 +0530 | [diff] [blame] | 1404 | df.pop(policy) |
| 1405 | for vdu in vnfd.get("vdu", {}): |
| 1406 | for alarm_policy in ["alarm", "monitoring-parameter"]: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1407 | if vdu.get(alarm_policy, {}): |
| elumalai | 3622f83 | 2022-07-08 12:06:27 +0530 | [diff] [blame] | 1408 | vdu.pop(alarm_policy) |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1409 | return vnfd |
| 1410 | |
| 1411 | @staticmethod |
| 1412 | def extract_day12_primitives(vnfd: dict) -> dict: |
| 1413 | """Removes the day12 primitives from the VNFD descriptors |
| 1414 | |
| 1415 | Args: |
| 1416 | vnfd (dict): Descriptor as a dictionary |
| 1417 | |
| 1418 | Returns: |
| 1419 | vnfd (dict) |
| 1420 | """ |
| 1421 | for df_id, df in enumerate(vnfd.get("df", {})): |
| 1422 | if ( |
| 1423 | df.get("lcm-operations-configuration", {}) |
| 1424 | .get("operate-vnf-op-config", {}) |
| 1425 | .get("day1-2") |
| 1426 | ): |
| 1427 | day12 = df["lcm-operations-configuration"]["operate-vnf-op-config"].get( |
| 1428 | "day1-2" |
| 1429 | ) |
| 1430 | for config_id, config in enumerate(day12): |
| 1431 | for key in [ |
| 1432 | "initial-config-primitive", |
| 1433 | "config-primitive", |
| 1434 | "terminate-config-primitive", |
| 1435 | ]: |
| 1436 | config.pop(key, None) |
| 1437 | day12[config_id] = config |
| 1438 | df["lcm-operations-configuration"]["operate-vnf-op-config"][ |
| 1439 | "day1-2" |
| 1440 | ] = day12 |
| 1441 | vnfd["df"][df_id] = df |
| 1442 | return vnfd |
| 1443 | |
| 1444 | def remove_modifiable_items(self, vnfd: dict) -> dict: |
| 1445 | """Removes the modifiable parts from the VNFD descriptors |
| 1446 | |
| 1447 | It calls different extract functions according to different update types |
| 1448 | to clear all the modifiable items from VNFD |
| 1449 | |
| 1450 | Args: |
| 1451 | vnfd (dict): Descriptor as a dictionary |
| 1452 | |
| 1453 | Returns: |
| 1454 | vnfd (dict): Descriptor which does not include modifiable contents |
| 1455 | """ |
| 1456 | if vnfd.get("vnfd"): |
| 1457 | vnfd = vnfd["vnfd"] |
| 1458 | vnfd.pop("_admin", None) |
| 1459 | # If the other extractions need to be done from VNFD, |
| 1460 | # the new extract methods could be appended to below list. |
| 1461 | for extract_function in [self.extract_day12_primitives, self.extract_policies]: |
| 1462 | vnfd_temp = extract_function(vnfd) |
| 1463 | vnfd = vnfd_temp |
| 1464 | return vnfd |
| 1465 | |
| 1466 | def _validate_descriptor_changes( |
| 1467 | self, |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1468 | descriptor_id: str, |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1469 | descriptor_file_name: str, |
| 1470 | old_descriptor_directory: str, |
| 1471 | new_descriptor_directory: str, |
| 1472 | ): |
| 1473 | """Compares the old and new VNFD descriptors and validates the new descriptor. |
| 1474 | |
| 1475 | Args: |
| 1476 | old_descriptor_directory (str): Directory of descriptor which is in-use |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1477 | new_descriptor_directory (str): Directory of descriptor which is proposed to update (new revision) |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1478 | |
| 1479 | Returns: |
| 1480 | None |
| 1481 | |
| 1482 | Raises: |
| 1483 | EngineException: In case of error when there are unallowed changes |
| 1484 | """ |
| 1485 | try: |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1486 | # If VNFD does not exist in DB or it is not in use by any NS, |
| 1487 | # validation is not required. |
| 1488 | vnfd = self.db.get_one("vnfds", {"_id": descriptor_id}) |
| 1489 | if not vnfd or not detect_descriptor_usage(vnfd, "vnfds", self.db): |
| 1490 | return |
| 1491 | |
| 1492 | # Get the old and new descriptor contents in order to compare them. |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1493 | with self.fs.file_open( |
| 1494 | (old_descriptor_directory.rstrip("/"), descriptor_file_name), "r" |
| 1495 | ) as old_descriptor_file: |
| 1496 | with self.fs.file_open( |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1497 | (new_descriptor_directory.rstrip("/"), descriptor_file_name), "r" |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1498 | ) as new_descriptor_file: |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1499 | old_content = yaml.safe_load(old_descriptor_file.read()) |
| 1500 | new_content = yaml.safe_load(new_descriptor_file.read()) |
| 1501 | |
| 1502 | # If software version has changed, we do not need to validate |
| 1503 | # the differences anymore. |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1504 | if old_content and new_content: |
| 1505 | if self.find_software_version( |
| 1506 | old_content |
| 1507 | ) != self.find_software_version(new_content): |
| 1508 | return |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1509 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1510 | disallowed_change = DeepDiff( |
| 1511 | self.remove_modifiable_items(old_content), |
| 1512 | self.remove_modifiable_items(new_content), |
| 1513 | ) |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1514 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1515 | if disallowed_change: |
| 1516 | changed_nodes = functools.reduce( |
| 1517 | lambda a, b: a + " , " + b, |
| 1518 | [ |
| 1519 | node.lstrip("root") |
| 1520 | for node in disallowed_change.get( |
| 1521 | "values_changed" |
| 1522 | ).keys() |
| 1523 | ], |
| 1524 | ) |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1525 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1526 | raise EngineException( |
| 1527 | f"Error in validating new descriptor: {changed_nodes} cannot be modified, " |
| 1528 | "there are disallowed changes in the vnf descriptor.", |
| 1529 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1530 | ) |
| 1531 | except ( |
| 1532 | DbException, |
| 1533 | AttributeError, |
| 1534 | IndexError, |
| 1535 | KeyError, |
| 1536 | ValueError, |
| 1537 | ) as e: |
| 1538 | raise type(e)( |
| 1539 | "VNF Descriptor could not be processed with error: {}.".format(e) |
| 1540 | ) |
| 1541 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1542 | |
| 1543 | class NsdTopic(DescriptorTopic): |
| 1544 | topic = "nsds" |
| 1545 | topic_msg = "nsd" |
| 1546 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1547 | def __init__(self, db, fs, msg, auth): |
| Daniel Arndt | 00f83aa | 2023-06-15 16:43:33 +0200 | [diff] [blame] | 1548 | super().__init__(db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1549 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1550 | def pyangbind_validation(self, item, data, force=False): |
| garciaale | df718ae | 2020-12-03 19:17:28 -0300 | [diff] [blame] | 1551 | if self._descriptor_data_is_in_old_format(data): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1552 | raise EngineException( |
| 1553 | "ERROR: Unsupported descriptor format. Please, use an ETSI SOL006 descriptor.", |
| 1554 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1555 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1556 | try: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1557 | nsd_vnf_profiles = data.get("df", [{}])[0].get("vnf-profile", []) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1558 | mynsd = etsi_nfv_nsd.etsi_nfv_nsd() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1559 | pybindJSONDecoder.load_ietf_json( |
| 1560 | {"nsd": {"nsd": [data]}}, |
| 1561 | None, |
| 1562 | None, |
| 1563 | obj=mynsd, |
| 1564 | path_helper=True, |
| 1565 | skip_unknown=force, |
| 1566 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1567 | out = pybindJSON.dumps(mynsd, mode="ietf") |
| 1568 | desc_out = self._remove_envelop(yaml.safe_load(out)) |
| 1569 | desc_out = self._remove_yang_prefixes_from_descriptor(desc_out) |
| garciaale | 341ac1b | 2020-12-11 20:04:11 -0300 | [diff] [blame] | 1570 | if nsd_vnf_profiles: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1571 | desc_out["df"][0]["vnf-profile"] = nsd_vnf_profiles |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1572 | return desc_out |
| 1573 | except Exception as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1574 | raise EngineException( |
| 1575 | "Error in pyangbind validation: {}".format(str(e)), |
| 1576 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1577 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1578 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1579 | @staticmethod |
| garciaale | df718ae | 2020-12-03 19:17:28 -0300 | [diff] [blame] | 1580 | def _descriptor_data_is_in_old_format(data): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1581 | return ("nsd-catalog" in data) or ("nsd:nsd-catalog" in data) |
| garciaale | df718ae | 2020-12-03 19:17:28 -0300 | [diff] [blame] | 1582 | |
| 1583 | @staticmethod |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1584 | def _remove_envelop(indata=None): |
| 1585 | if not indata: |
| 1586 | return {} |
| 1587 | clean_indata = indata |
| 1588 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1589 | if clean_indata.get("nsd"): |
| 1590 | clean_indata = clean_indata["nsd"] |
| 1591 | elif clean_indata.get("etsi-nfv-nsd:nsd"): |
| 1592 | clean_indata = clean_indata["etsi-nfv-nsd:nsd"] |
| 1593 | if clean_indata.get("nsd"): |
| 1594 | if ( |
| 1595 | not isinstance(clean_indata["nsd"], list) |
| 1596 | or len(clean_indata["nsd"]) != 1 |
| 1597 | ): |
| gcalvino | 46e4cb8 | 2018-10-26 13:10:22 +0200 | [diff] [blame] | 1598 | raise EngineException("'nsd' must be a list of only one element") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1599 | clean_indata = clean_indata["nsd"][0] |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1600 | return clean_indata |
| 1601 | |
| gcalvino | a6fe000 | 2019-01-09 13:27:11 +0100 | [diff] [blame] | 1602 | def _validate_input_new(self, indata, storage_params, force=False): |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 1603 | indata.pop("nsdOnboardingState", None) |
| 1604 | indata.pop("nsdOperationalState", None) |
| 1605 | indata.pop("nsdUsageState", None) |
| 1606 | |
| 1607 | indata.pop("links", None) |
| 1608 | |
| gcalvino | 46e4cb8 | 2018-10-26 13:10:22 +0200 | [diff] [blame] | 1609 | indata = self.pyangbind_validation("nsds", indata, force) |
| tierno | 5a5c218 | 2018-11-20 12:27:42 +0000 | [diff] [blame] | 1610 | # Cross references validation in the descriptor |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 1611 | # TODO validata that if contains cloud-init-file or charms, have artifacts _admin.storage."pkg-dir" is not none |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1612 | for vld in get_iterable(indata.get("virtual-link-desc")): |
| 1613 | self.validate_vld_mgmt_network_with_virtual_link_protocol_data(vld, indata) |
| selvi.j | 828f3f2 | 2023-05-16 05:43:48 +0000 | [diff] [blame] | 1614 | for fg in get_iterable(indata.get("vnffgd")): |
| 1615 | self.validate_vnffgd_data(fg, indata) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1616 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1617 | self.validate_vnf_profiles_vnfd_id(indata) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1618 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1619 | return indata |
| 1620 | |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1621 | @staticmethod |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1622 | def validate_vld_mgmt_network_with_virtual_link_protocol_data(vld, indata): |
| 1623 | if not vld.get("mgmt-network"): |
| 1624 | return |
| 1625 | vld_id = vld.get("id") |
| 1626 | for df in get_iterable(indata.get("df")): |
| 1627 | for vlp in get_iterable(df.get("virtual-link-profile")): |
| 1628 | if vld_id and vld_id == vlp.get("virtual-link-desc-id"): |
| 1629 | if vlp.get("virtual-link-protocol-data"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1630 | raise EngineException( |
| 1631 | "Error at df[id='{}']:virtual-link-profile[id='{}']:virtual-link-" |
| 1632 | "protocol-data You cannot set a virtual-link-protocol-data " |
| 1633 | "when mgmt-network is True".format(df["id"], vlp["id"]), |
| 1634 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1635 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1636 | |
| 1637 | @staticmethod |
| selvi.j | 828f3f2 | 2023-05-16 05:43:48 +0000 | [diff] [blame] | 1638 | def validate_vnffgd_data(fg, indata): |
| 1639 | position_list = [] |
| 1640 | all_vnf_ids = set(get_iterable(fg.get("vnf-profile-id"))) |
| 1641 | for fgposition in get_iterable(fg.get("nfp-position-element")): |
| 1642 | position_list.append(fgposition["id"]) |
| 1643 | |
| 1644 | for nfpd in get_iterable(fg.get("nfpd")): |
| 1645 | nfp_position = [] |
| 1646 | for position in get_iterable(nfpd.get("position-desc-id")): |
| 1647 | nfp_position = position.get("nfp-position-element-id") |
| 1648 | if position == "nfp-position-element-id": |
| 1649 | nfp_position = position.get("nfp-position-element-id") |
| 1650 | if nfp_position[0] not in position_list: |
| 1651 | raise EngineException( |
| 1652 | "Error at vnffgd nfpd[id='{}']:nfp-position-element-id='{}' " |
| 1653 | "does not match any nfp-position-element".format( |
| 1654 | nfpd["id"], nfp_position[0] |
| 1655 | ), |
| 1656 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1657 | ) |
| 1658 | |
| 1659 | for cp in get_iterable(position.get("cp-profile-id")): |
| 1660 | for cpe in get_iterable(cp.get("constituent-profile-elements")): |
| 1661 | constituent_base_element_id = cpe.get( |
| 1662 | "constituent-base-element-id" |
| 1663 | ) |
| 1664 | if ( |
| 1665 | constituent_base_element_id |
| 1666 | and constituent_base_element_id not in all_vnf_ids |
| 1667 | ): |
| 1668 | raise EngineException( |
| 1669 | "Error at vnffgd constituent_profile[id='{}']:vnfd-id='{}' " |
| 1670 | "does not match any constituent-base-element-id".format( |
| 1671 | cpe["id"], constituent_base_element_id |
| 1672 | ), |
| 1673 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1674 | ) |
| Adurti | 809a7cd | 2024-05-07 10:37:30 +0000 | [diff] [blame] | 1675 | vnf_ip_list = set() |
| 1676 | for ma in get_iterable(position.get("match-attributes")): |
| 1677 | ma_source_ip = ma.get("source-ip-address") |
| 1678 | ma_dest_ip = ma.get("destination-ip-address") |
| 1679 | ma_vp_id = "" |
| 1680 | if ma_source_ip and ma_dest_ip: |
| 1681 | if ma_source_ip == ma_dest_ip: |
| 1682 | raise EngineException( |
| 1683 | "Error at vnffgd match-attributes:source-ip-address and destination-ip-address should not match", |
| 1684 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1685 | ) |
| 1686 | if ma_source_ip: |
| 1687 | for df in get_iterable(indata.get("df")): |
| 1688 | for vp in get_iterable(df.get("vnf-profile")): |
| 1689 | for vlc in get_iterable( |
| 1690 | vp.get("virtual-link-connectivity") |
| 1691 | ): |
| 1692 | for cpd in get_iterable( |
| 1693 | vlc.get("constituent-cpd-id") |
| 1694 | ): |
| 1695 | vnf_ip_list.add(cpd.get("ip-address")) |
| 1696 | if ma_source_ip == cpd.get("ip-address"): |
| 1697 | ma_vp_id = vp.get("id") |
| 1698 | if ma_source_ip not in vnf_ip_list: |
| 1699 | raise EngineException( |
| 1700 | "Error at vnffgd match-attributes:source-ip-address='{}' " |
| 1701 | "does not match any ip-address".format(ma_source_ip), |
| 1702 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1703 | ) |
| 1704 | if ma_dest_ip not in vnf_ip_list: |
| 1705 | raise EngineException( |
| 1706 | "Error at vnffgd match-attributes:destination-ip-address='{}' " |
| 1707 | "does not match any ip-address".format(ma_dest_ip), |
| 1708 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1709 | ) |
| 1710 | constituent_base_element_id = ma.get("constituent-base-element-id") |
| 1711 | if ( |
| 1712 | constituent_base_element_id |
| 1713 | and constituent_base_element_id not in ma_vp_id |
| 1714 | ): |
| 1715 | raise EngineException( |
| 1716 | "Error at vnffgd match-attributes:vnfd-id='{}' " |
| 1717 | "does not match source constituent-base-element-id".format( |
| 1718 | constituent_base_element_id |
| 1719 | ), |
| 1720 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1721 | ) |
| selvi.j | 828f3f2 | 2023-05-16 05:43:48 +0000 | [diff] [blame] | 1722 | |
| 1723 | @staticmethod |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1724 | def validate_vnf_profiles_vnfd_id(indata): |
| 1725 | all_vnfd_ids = set(get_iterable(indata.get("vnfd-id"))) |
| 1726 | for df in get_iterable(indata.get("df")): |
| 1727 | for vnf_profile in get_iterable(df.get("vnf-profile")): |
| 1728 | vnfd_id = vnf_profile.get("vnfd-id") |
| 1729 | if vnfd_id and vnfd_id not in all_vnfd_ids: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1730 | raise EngineException( |
| 1731 | "Error at df[id='{}']:vnf_profile[id='{}']:vnfd-id='{}' " |
| 1732 | "does not match any vnfd-id".format( |
| 1733 | df["id"], vnf_profile["id"], vnfd_id |
| 1734 | ), |
| 1735 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1736 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1737 | |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1738 | def _validate_input_edit(self, indata, content, force=False): |
| tierno | aa1ca7b | 2018-11-08 19:00:20 +0100 | [diff] [blame] | 1739 | # not needed to validate with pyangbind becuase it will be validated at check_conflict_on_edit |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1740 | """ |
| 1741 | indata looks as follows: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1742 | - In the new case (conformant) |
| 1743 | {'nsdOperationalState': 'DISABLED', 'userDefinedData': {'id': 'string23', |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1744 | '_id': 'c6ddc544-cede-4b94-9ebe-be07b298a3c1', 'name': 'simon46'}} |
| 1745 | - In the old case (backwards-compatible) |
| 1746 | {'id': 'string23', '_id': 'c6ddc544-cede-4b94-9ebe-be07b298a3c1', 'name': 'simon46'} |
| 1747 | """ |
| 1748 | if "_admin" not in indata: |
| 1749 | indata["_admin"] = {} |
| 1750 | |
| 1751 | if "nsdOperationalState" in indata: |
| 1752 | if indata["nsdOperationalState"] in ("ENABLED", "DISABLED"): |
| 1753 | indata["_admin"]["operationalState"] = indata.pop("nsdOperationalState") |
| 1754 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1755 | raise EngineException( |
| 1756 | "State '{}' is not a valid operational state".format( |
| 1757 | indata["nsdOperationalState"] |
| 1758 | ), |
| 1759 | http_code=HTTPStatus.BAD_REQUEST, |
| 1760 | ) |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1761 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1762 | # In the case of user defined data, we need to put the data in the root of the object |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1763 | # to preserve current expected behaviour |
| 1764 | if "userDefinedData" in indata: |
| 1765 | data = indata.pop("userDefinedData") |
| gatici | d7debb9 | 2023-07-31 14:37:32 +0300 | [diff] [blame] | 1766 | if isinstance(data, dict): |
| Frank Bryden | deba68e | 2020-07-27 13:55:11 +0000 | [diff] [blame] | 1767 | indata["_admin"]["userDefinedData"] = data |
| 1768 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1769 | raise EngineException( |
| 1770 | "userDefinedData should be an object, but is '{}' instead".format( |
| 1771 | type(data) |
| 1772 | ), |
| 1773 | http_code=HTTPStatus.BAD_REQUEST, |
| 1774 | ) |
| 1775 | if ( |
| 1776 | "operationalState" in indata["_admin"] |
| 1777 | and content["_admin"]["operationalState"] |
| 1778 | == indata["_admin"]["operationalState"] |
| 1779 | ): |
| 1780 | raise EngineException( |
| 1781 | "nsdOperationalState already {}".format( |
| 1782 | content["_admin"]["operationalState"] |
| 1783 | ), |
| 1784 | http_code=HTTPStatus.CONFLICT, |
| 1785 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1786 | return indata |
| 1787 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1788 | def _check_descriptor_dependencies(self, session, descriptor): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1789 | """ |
| tierno | 5a5c218 | 2018-11-20 12:27:42 +0000 | [diff] [blame] | 1790 | Check that the dependent descriptors exist on a new descriptor or edition. Also checks references to vnfd |
| 1791 | connection points are ok |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1792 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1793 | :param descriptor: descriptor to be inserted or edit |
| 1794 | :return: None or raises exception |
| 1795 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1796 | if session["force"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1797 | return |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1798 | vnfds_index = self._get_descriptor_constituent_vnfds_index(session, descriptor) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1799 | |
| 1800 | # Cross references validation in the descriptor and vnfd connection point validation |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1801 | for df in get_iterable(descriptor.get("df")): |
| 1802 | self.validate_df_vnf_profiles_constituent_connection_points(df, vnfds_index) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1803 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1804 | def _get_descriptor_constituent_vnfds_index(self, session, descriptor): |
| 1805 | vnfds_index = {} |
| 1806 | if descriptor.get("vnfd-id") and not session["force"]: |
| 1807 | for vnfd_id in get_iterable(descriptor.get("vnfd-id")): |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1808 | query_filter = self._get_project_filter(session) |
| 1809 | query_filter["id"] = vnfd_id |
| 1810 | vnf_list = self.db.get_list("vnfds", query_filter) |
| tierno | 5a5c218 | 2018-11-20 12:27:42 +0000 | [diff] [blame] | 1811 | if not vnf_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1812 | raise EngineException( |
| 1813 | "Descriptor error at 'vnfd-id'='{}' references a non " |
| 1814 | "existing vnfd".format(vnfd_id), |
| 1815 | http_code=HTTPStatus.CONFLICT, |
| 1816 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1817 | vnfds_index[vnfd_id] = vnf_list[0] |
| 1818 | return vnfds_index |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1819 | |
| 1820 | @staticmethod |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1821 | def validate_df_vnf_profiles_constituent_connection_points(df, vnfds_index): |
| 1822 | for vnf_profile in get_iterable(df.get("vnf-profile")): |
| 1823 | vnfd = vnfds_index.get(vnf_profile["vnfd-id"]) |
| 1824 | all_vnfd_ext_cpds = set() |
| 1825 | for ext_cpd in get_iterable(vnfd.get("ext-cpd")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1826 | if ext_cpd.get("id"): |
| 1827 | all_vnfd_ext_cpds.add(ext_cpd.get("id")) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1828 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1829 | for virtual_link in get_iterable( |
| 1830 | vnf_profile.get("virtual-link-connectivity") |
| 1831 | ): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1832 | for vl_cpd in get_iterable(virtual_link.get("constituent-cpd-id")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1833 | vl_cpd_id = vl_cpd.get("constituent-cpd-id") |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1834 | if vl_cpd_id and vl_cpd_id not in all_vnfd_ext_cpds: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1835 | raise EngineException( |
| 1836 | "Error at df[id='{}']:vnf-profile[id='{}']:virtual-link-connectivity" |
| 1837 | "[virtual-link-profile-id='{}']:constituent-cpd-id='{}' references a " |
| 1838 | "non existing ext-cpd:id inside vnfd '{}'".format( |
| 1839 | df["id"], |
| 1840 | vnf_profile["id"], |
| 1841 | virtual_link["virtual-link-profile-id"], |
| 1842 | vl_cpd_id, |
| 1843 | vnfd["id"], |
| 1844 | ), |
| 1845 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1846 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1847 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1848 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1849 | final_content = super().check_conflict_on_edit( |
| 1850 | session, final_content, edit_content, _id |
| 1851 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1852 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1853 | self._check_descriptor_dependencies(session, final_content) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1854 | |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 1855 | return final_content |
| 1856 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1857 | def check_conflict_on_del(self, session, _id, db_content): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1858 | """ |
| 1859 | Check that there is not any NSR that uses this NSD. Only NSRs belonging to this project are considered. Note |
| 1860 | that NSD can be public and be used by other projects. |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1861 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1862 | :param _id: nsd internal id |
| 1863 | :param db_content: The database content of the _id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1864 | :return: None or raises EngineException with the conflict |
| 1865 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1866 | if session["force"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1867 | return |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1868 | descriptor = db_content |
| 1869 | descriptor_id = descriptor.get("id") |
| 1870 | if not descriptor_id: # empty nsd not uploaded |
| 1871 | return |
| 1872 | |
| 1873 | # check NSD used by NS |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1874 | _filter = self._get_project_filter(session) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1875 | _filter["nsd-id"] = _id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1876 | if self.db.get_list("nsrs", _filter): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1877 | raise EngineException( |
| 1878 | "There is at least one NS instance using this descriptor", |
| 1879 | http_code=HTTPStatus.CONFLICT, |
| 1880 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 1881 | |
| 1882 | # check NSD referenced by NST |
| 1883 | del _filter["nsd-id"] |
| 1884 | _filter["netslice-subnet.ANYINDEX.nsd-ref"] = descriptor_id |
| 1885 | if self.db.get_list("nsts", _filter): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1886 | raise EngineException( |
| 1887 | "There is at least one NetSlice Template referencing this descriptor", |
| 1888 | http_code=HTTPStatus.CONFLICT, |
| 1889 | ) |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 1890 | |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 1891 | def delete_extra(self, session, _id, db_content, not_send_msg=None): |
| 1892 | """ |
| 1893 | Deletes associate file system storage (via super) |
| 1894 | Deletes associated vnfpkgops from database. |
| 1895 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1896 | :param _id: server internal id |
| 1897 | :param db_content: The database content of the descriptor |
| 1898 | :return: None |
| 1899 | :raises: FsException in case of error while deleting associated storage |
| 1900 | """ |
| 1901 | super().delete_extra(session, _id, db_content, not_send_msg) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1902 | self.db.del_list(self.topic + "_revisions", {"_id": {"$regex": _id}}) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 1903 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1904 | @staticmethod |
| 1905 | def extract_day12_primitives(nsd: dict) -> dict: |
| 1906 | """Removes the day12 primitives from the NSD descriptors |
| 1907 | |
| 1908 | Args: |
| 1909 | nsd (dict): Descriptor as a dictionary |
| 1910 | |
| 1911 | Returns: |
| 1912 | nsd (dict): Cleared NSD |
| 1913 | """ |
| 1914 | if nsd.get("ns-configuration"): |
| 1915 | for key in [ |
| 1916 | "config-primitive", |
| 1917 | "initial-config-primitive", |
| 1918 | "terminate-config-primitive", |
| 1919 | ]: |
| 1920 | nsd["ns-configuration"].pop(key, None) |
| 1921 | return nsd |
| 1922 | |
| 1923 | def remove_modifiable_items(self, nsd: dict) -> dict: |
| 1924 | """Removes the modifiable parts from the VNFD descriptors |
| 1925 | |
| 1926 | It calls different extract functions according to different update types |
| 1927 | to clear all the modifiable items from NSD |
| 1928 | |
| 1929 | Args: |
| 1930 | nsd (dict): Descriptor as a dictionary |
| 1931 | |
| 1932 | Returns: |
| 1933 | nsd (dict): Descriptor which does not include modifiable contents |
| 1934 | """ |
| 1935 | while isinstance(nsd, dict) and nsd.get("nsd"): |
| 1936 | nsd = nsd["nsd"] |
| 1937 | if isinstance(nsd, list): |
| 1938 | nsd = nsd[0] |
| 1939 | nsd.pop("_admin", None) |
| 1940 | # If the more extractions need to be done from NSD, |
| 1941 | # the new extract methods could be appended to below list. |
| 1942 | for extract_function in [self.extract_day12_primitives]: |
| 1943 | nsd_temp = extract_function(nsd) |
| 1944 | nsd = nsd_temp |
| 1945 | return nsd |
| 1946 | |
| 1947 | def _validate_descriptor_changes( |
| 1948 | self, |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1949 | descriptor_id: str, |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1950 | descriptor_file_name: str, |
| 1951 | old_descriptor_directory: str, |
| 1952 | new_descriptor_directory: str, |
| 1953 | ): |
| 1954 | """Compares the old and new NSD descriptors and validates the new descriptor |
| 1955 | |
| 1956 | Args: |
| 1957 | old_descriptor_directory: Directory of descriptor which is in-use |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1958 | new_descriptor_directory: Directory of descriptor which is proposed to update (new revision) |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1959 | |
| 1960 | Returns: |
| 1961 | None |
| 1962 | |
| 1963 | Raises: |
| 1964 | EngineException: In case of error if the changes are not allowed |
| 1965 | """ |
| 1966 | |
| 1967 | try: |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1968 | # If NSD does not exist in DB, or it is not in use by any NS, |
| 1969 | # validation is not required. |
| 1970 | nsd = self.db.get_one("nsds", {"_id": descriptor_id}, fail_on_empty=False) |
| 1971 | if not nsd or not detect_descriptor_usage(nsd, "nsds", self.db): |
| 1972 | return |
| 1973 | |
| 1974 | # Get the old and new descriptor contents in order to compare them. |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1975 | with self.fs.file_open( |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1976 | (old_descriptor_directory.rstrip("/"), descriptor_file_name), "r" |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1977 | ) as old_descriptor_file: |
| 1978 | with self.fs.file_open( |
| 1979 | (new_descriptor_directory.rstrip("/"), descriptor_file_name), "r" |
| 1980 | ) as new_descriptor_file: |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1981 | old_content = yaml.safe_load(old_descriptor_file.read()) |
| 1982 | new_content = yaml.safe_load(new_descriptor_file.read()) |
| 1983 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1984 | if old_content and new_content: |
| 1985 | disallowed_change = DeepDiff( |
| 1986 | self.remove_modifiable_items(old_content), |
| 1987 | self.remove_modifiable_items(new_content), |
| 1988 | ) |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 1989 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 1990 | if disallowed_change: |
| 1991 | changed_nodes = functools.reduce( |
| 1992 | lambda a, b: a + ", " + b, |
| 1993 | [ |
| 1994 | node.lstrip("root") |
| 1995 | for node in disallowed_change.get( |
| 1996 | "values_changed" |
| 1997 | ).keys() |
| 1998 | ], |
| 1999 | ) |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 2000 | |
| aticig | 9cfa816 | 2022-04-07 11:57:18 +0300 | [diff] [blame] | 2001 | raise EngineException( |
| 2002 | f"Error in validating new descriptor: {changed_nodes} cannot be modified, " |
| 2003 | "there are disallowed changes in the ns descriptor. ", |
| 2004 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 2005 | ) |
| 2006 | except ( |
| 2007 | DbException, |
| 2008 | AttributeError, |
| 2009 | IndexError, |
| 2010 | KeyError, |
| 2011 | ValueError, |
| 2012 | ) as e: |
| 2013 | raise type(e)( |
| 2014 | "NS Descriptor could not be processed with error: {}.".format(e) |
| 2015 | ) |
| 2016 | |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 2017 | def sol005_projection(self, data): |
| 2018 | data["nsdOnboardingState"] = data["_admin"]["onboardingState"] |
| 2019 | data["nsdOperationalState"] = data["_admin"]["operationalState"] |
| 2020 | data["nsdUsageState"] = data["_admin"]["usageState"] |
| 2021 | |
| 2022 | links = {} |
| 2023 | links["self"] = {"href": "/nsd/v1/ns_descriptors/{}".format(data["_id"])} |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2024 | links["nsd_content"] = { |
| 2025 | "href": "/nsd/v1/ns_descriptors/{}/nsd_content".format(data["_id"]) |
| 2026 | } |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 2027 | data["_links"] = links |
| garciaale | 960531a | 2020-10-20 18:29:45 -0300 | [diff] [blame] | 2028 | |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 2029 | return super().sol005_projection(data) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2030 | |
| 2031 | |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2032 | class NstTopic(DescriptorTopic): |
| 2033 | topic = "nsts" |
| 2034 | topic_msg = "nst" |
| tierno | 6b02b05 | 2020-06-02 10:07:41 +0000 | [diff] [blame] | 2035 | quota_name = "slice_templates" |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2036 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 2037 | def __init__(self, db, fs, msg, auth): |
| 2038 | DescriptorTopic.__init__(self, db, fs, msg, auth) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2039 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 2040 | def pyangbind_validation(self, item, data, force=False): |
| 2041 | try: |
| 2042 | mynst = nst_im() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2043 | pybindJSONDecoder.load_ietf_json( |
| 2044 | {"nst": [data]}, |
| 2045 | None, |
| 2046 | None, |
| 2047 | obj=mynst, |
| 2048 | path_helper=True, |
| 2049 | skip_unknown=force, |
| 2050 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 2051 | out = pybindJSON.dumps(mynst, mode="ietf") |
| 2052 | desc_out = self._remove_envelop(yaml.safe_load(out)) |
| 2053 | return desc_out |
| 2054 | except Exception as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2055 | raise EngineException( |
| 2056 | "Error in pyangbind validation: {}".format(str(e)), |
| 2057 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 2058 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 2059 | |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2060 | @staticmethod |
| 2061 | def _remove_envelop(indata=None): |
| 2062 | if not indata: |
| 2063 | return {} |
| 2064 | clean_indata = indata |
| 2065 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2066 | if clean_indata.get("nst"): |
| 2067 | if ( |
| 2068 | not isinstance(clean_indata["nst"], list) |
| 2069 | or len(clean_indata["nst"]) != 1 |
| 2070 | ): |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2071 | raise EngineException("'nst' must be a list only one element") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2072 | clean_indata = clean_indata["nst"][0] |
| 2073 | elif clean_indata.get("nst:nst"): |
| 2074 | if ( |
| 2075 | not isinstance(clean_indata["nst:nst"], list) |
| 2076 | or len(clean_indata["nst:nst"]) != 1 |
| 2077 | ): |
| gcalvino | 70434c1 | 2018-11-27 15:17:04 +0100 | [diff] [blame] | 2078 | raise EngineException("'nst:nst' must be a list only one element") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2079 | clean_indata = clean_indata["nst:nst"][0] |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2080 | return clean_indata |
| 2081 | |
| gcalvino | a6fe000 | 2019-01-09 13:27:11 +0100 | [diff] [blame] | 2082 | def _validate_input_new(self, indata, storage_params, force=False): |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 2083 | indata.pop("onboardingState", None) |
| 2084 | indata.pop("operationalState", None) |
| 2085 | indata.pop("usageState", None) |
| gcalvino | 70434c1 | 2018-11-27 15:17:04 +0100 | [diff] [blame] | 2086 | indata = self.pyangbind_validation("nsts", indata, force) |
| Felipe Vicens | e36ab85 | 2018-11-23 14:12:09 +0100 | [diff] [blame] | 2087 | return indata.copy() |
| 2088 | |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2089 | def _check_descriptor_dependencies(self, session, descriptor): |
| 2090 | """ |
| 2091 | Check that the dependent descriptors exist on a new descriptor or edition |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2092 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2093 | :param descriptor: descriptor to be inserted or edit |
| 2094 | :return: None or raises exception |
| 2095 | """ |
| 2096 | if not descriptor.get("netslice-subnet"): |
| 2097 | return |
| 2098 | for nsd in descriptor["netslice-subnet"]: |
| 2099 | nsd_id = nsd["nsd-ref"] |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2100 | filter_q = self._get_project_filter(session) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2101 | filter_q["id"] = nsd_id |
| 2102 | if not self.db.get_list("nsds", filter_q): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2103 | raise EngineException( |
| 2104 | "Descriptor error at 'netslice-subnet':'nsd-ref'='{}' references a non " |
| 2105 | "existing nsd".format(nsd_id), |
| 2106 | http_code=HTTPStatus.CONFLICT, |
| 2107 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2108 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2109 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2110 | final_content = super().check_conflict_on_edit( |
| 2111 | session, final_content, edit_content, _id |
| 2112 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2113 | |
| 2114 | self._check_descriptor_dependencies(session, final_content) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 2115 | return final_content |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2116 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2117 | def check_conflict_on_del(self, session, _id, db_content): |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2118 | """ |
| 2119 | Check that there is not any NSIR that uses this NST. Only NSIRs belonging to this project are considered. Note |
| 2120 | that NST can be public and be used by other projects. |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2121 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2122 | :param _id: nst internal id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2123 | :param db_content: The database content of the _id. |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2124 | :return: None or raises EngineException with the conflict |
| 2125 | """ |
| 2126 | # TODO: Check this method |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2127 | if session["force"]: |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2128 | return |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2129 | # Get Network Slice Template from Database |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2130 | _filter = self._get_project_filter(session) |
| tierno | ea97c04 | 2019-09-13 09:44:42 +0000 | [diff] [blame] | 2131 | _filter["_admin.nst-id"] = _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2132 | if self.db.get_list("nsis", _filter): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2133 | raise EngineException( |
| 2134 | "there is at least one Netslice Instance using this descriptor", |
| 2135 | http_code=HTTPStatus.CONFLICT, |
| 2136 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2137 | |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 2138 | def sol005_projection(self, data): |
| 2139 | data["onboardingState"] = data["_admin"]["onboardingState"] |
| 2140 | data["operationalState"] = data["_admin"]["operationalState"] |
| 2141 | data["usageState"] = data["_admin"]["usageState"] |
| 2142 | |
| 2143 | links = {} |
| 2144 | links["self"] = {"href": "/nst/v1/netslice_templates/{}".format(data["_id"])} |
| 2145 | links["nst"] = {"href": "/nst/v1/netslice_templates/{}/nst".format(data["_id"])} |
| 2146 | data["_links"] = links |
| 2147 | |
| 2148 | return super().sol005_projection(data) |
| 2149 | |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2150 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2151 | class PduTopic(BaseTopic): |
| 2152 | topic = "pdus" |
| 2153 | topic_msg = "pdu" |
| tierno | 6b02b05 | 2020-06-02 10:07:41 +0000 | [diff] [blame] | 2154 | quota_name = "pduds" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2155 | schema_new = pdu_new_schema |
| 2156 | schema_edit = pdu_edit_schema |
| 2157 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 2158 | def __init__(self, db, fs, msg, auth): |
| 2159 | BaseTopic.__init__(self, db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2160 | |
| 2161 | @staticmethod |
| 2162 | def format_on_new(content, project_id=None, make_public=False): |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2163 | BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2164 | content["_admin"]["onboardingState"] = "CREATED" |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2165 | content["_admin"]["operationalState"] = "ENABLED" |
| 2166 | content["_admin"]["usageState"] = "NOT_IN_USE" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2167 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2168 | def check_conflict_on_del(self, session, _id, db_content): |
| 2169 | """ |
| 2170 | Check that there is not any vnfr that uses this PDU |
| 2171 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 2172 | :param _id: pdu internal id |
| 2173 | :param db_content: The database content of the _id. |
| 2174 | :return: None or raises EngineException with the conflict |
| 2175 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2176 | if session["force"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2177 | return |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2178 | |
| 2179 | _filter = self._get_project_filter(session) |
| 2180 | _filter["vdur.pdu-id"] = _id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2181 | if self.db.get_list("vnfrs", _filter): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2182 | raise EngineException( |
| 2183 | "There is at least one VNF instance using this PDU", |
| 2184 | http_code=HTTPStatus.CONFLICT, |
| 2185 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2186 | |
| 2187 | |
| 2188 | class VnfPkgOpTopic(BaseTopic): |
| 2189 | topic = "vnfpkgops" |
| 2190 | topic_msg = "vnfd" |
| 2191 | schema_new = vnfpkgop_new_schema |
| 2192 | schema_edit = None |
| 2193 | |
| 2194 | def __init__(self, db, fs, msg, auth): |
| 2195 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 2196 | |
| 2197 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2198 | raise EngineException( |
| 2199 | "Method 'edit' not allowed for topic '{}'".format(self.topic), |
| 2200 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 2201 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2202 | |
| 2203 | def delete(self, session, _id, dry_run=False): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2204 | raise EngineException( |
| 2205 | "Method 'delete' not allowed for topic '{}'".format(self.topic), |
| 2206 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 2207 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2208 | |
| 2209 | def delete_list(self, session, filter_q=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2210 | raise EngineException( |
| 2211 | "Method 'delete_list' not allowed for topic '{}'".format(self.topic), |
| 2212 | HTTPStatus.METHOD_NOT_ALLOWED, |
| 2213 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2214 | |
| 2215 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 2216 | """ |
| 2217 | Creates a new entry into database. |
| 2218 | :param rollback: list to append created items at database in case a rollback may to be done |
| 2219 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 2220 | :param indata: data to be inserted |
| 2221 | :param kwargs: used to override the indata descriptor |
| 2222 | :param headers: http request headers |
| 2223 | :return: _id, op_id: |
| 2224 | _id: identity of the inserted data. |
| 2225 | op_id: None |
| 2226 | """ |
| 2227 | self._update_input_with_kwargs(indata, kwargs) |
| 2228 | validate_input(indata, self.schema_new) |
| 2229 | vnfpkg_id = indata["vnfPkgId"] |
| 2230 | filter_q = BaseTopic._get_project_filter(session) |
| 2231 | filter_q["_id"] = vnfpkg_id |
| 2232 | vnfd = self.db.get_one("vnfds", filter_q) |
| 2233 | operation = indata["lcmOperationType"] |
| 2234 | kdu_name = indata["kdu_name"] |
| 2235 | for kdu in vnfd.get("kdu", []): |
| 2236 | if kdu["name"] == kdu_name: |
| 2237 | helm_chart = kdu.get("helm-chart") |
| 2238 | juju_bundle = kdu.get("juju-bundle") |
| 2239 | break |
| 2240 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2241 | raise EngineException( |
| 2242 | "Not found vnfd[id='{}']:kdu[name='{}']".format(vnfpkg_id, kdu_name) |
| 2243 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2244 | if helm_chart: |
| 2245 | indata["helm-chart"] = helm_chart |
| 2246 | match = fullmatch(r"([^/]*)/([^/]*)", helm_chart) |
| 2247 | repo_name = match.group(1) if match else None |
| 2248 | elif juju_bundle: |
| 2249 | indata["juju-bundle"] = juju_bundle |
| 2250 | match = fullmatch(r"([^/]*)/([^/]*)", juju_bundle) |
| 2251 | repo_name = match.group(1) if match else None |
| 2252 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2253 | raise EngineException( |
| 2254 | "Found neither 'helm-chart' nor 'juju-bundle' in vnfd[id='{}']:kdu[name='{}']".format( |
| 2255 | vnfpkg_id, kdu_name |
| 2256 | ) |
| 2257 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2258 | if repo_name: |
| 2259 | del filter_q["_id"] |
| 2260 | filter_q["name"] = repo_name |
| 2261 | repo = self.db.get_one("k8srepos", filter_q) |
| 2262 | k8srepo_id = repo.get("_id") |
| 2263 | k8srepo_url = repo.get("url") |
| 2264 | else: |
| 2265 | k8srepo_id = None |
| 2266 | k8srepo_url = None |
| 2267 | indata["k8srepoId"] = k8srepo_id |
| 2268 | indata["k8srepo_url"] = k8srepo_url |
| 2269 | vnfpkgop_id = str(uuid4()) |
| 2270 | vnfpkgop_desc = { |
| 2271 | "_id": vnfpkgop_id, |
| 2272 | "operationState": "PROCESSING", |
| 2273 | "vnfPkgId": vnfpkg_id, |
| 2274 | "lcmOperationType": operation, |
| 2275 | "isAutomaticInvocation": False, |
| 2276 | "isCancelPending": False, |
| 2277 | "operationParams": indata, |
| 2278 | "links": { |
| 2279 | "self": "/osm/vnfpkgm/v1/vnfpkg_op_occs/" + vnfpkgop_id, |
| 2280 | "vnfpkg": "/osm/vnfpkgm/v1/vnf_packages/" + vnfpkg_id, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2281 | }, |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2282 | } |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2283 | self.format_on_new( |
| 2284 | vnfpkgop_desc, session["project_id"], make_public=session["public"] |
| 2285 | ) |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 2286 | ctime = vnfpkgop_desc["_admin"]["created"] |
| 2287 | vnfpkgop_desc["statusEnteredTime"] = ctime |
| 2288 | vnfpkgop_desc["startTime"] = ctime |
| 2289 | self.db.create(self.topic, vnfpkgop_desc) |
| 2290 | rollback.append({"topic": self.topic, "_id": vnfpkgop_id}) |
| 2291 | self.msg.write(self.topic_msg, operation, vnfpkgop_desc) |
| 2292 | return vnfpkgop_id, None |
| kayal2001 | f71c2e8 | 2024-06-25 15:26:24 +0530 | [diff] [blame] | 2293 | |
| 2294 | |
| 2295 | class NsConfigTemplateTopic(DescriptorTopic): |
| 2296 | topic = "ns_config_template" |
| 2297 | topic_msg = "nsd" |
| 2298 | schema_new = ns_config_template |
| 2299 | instantiation_params = { |
| 2300 | "vnf": vnf_schema, |
| 2301 | "vld": vld_schema, |
| 2302 | "additionalParamsForVnf": additional_params_for_vnf, |
| 2303 | } |
| 2304 | |
| 2305 | def __init__(self, db, fs, msg, auth): |
| 2306 | super().__init__(db, fs, msg, auth) |
| 2307 | |
| 2308 | def check_conflict_on_del(self, session, _id, db_content): |
| 2309 | """ |
| 2310 | Check that there is not any NSR that uses this NS CONFIG TEMPLATE. Only NSRs belonging to this project are considered. |
| 2311 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 2312 | :param _id: ns config template internal id |
| 2313 | :param db_content: The database content of the _id |
| 2314 | :return: None or raises EngineException with the conflict |
| 2315 | """ |
| 2316 | if session["force"]: |
| 2317 | return |
| 2318 | descriptor = db_content |
| 2319 | descriptor_id = descriptor.get("nsdId") |
| 2320 | if not descriptor_id: # empty nsd not uploaded |
| 2321 | return |
| 2322 | |
| 2323 | # check NS CONFIG TEMPLATE used by NS |
| 2324 | ns_config_template_id = _id |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 2325 | |
| kayal2001 | f71c2e8 | 2024-06-25 15:26:24 +0530 | [diff] [blame] | 2326 | if self.db.get_list( |
| 2327 | "nsrs", {"instantiate_params.nsConfigTemplateId": ns_config_template_id} |
| 2328 | ): |
| 2329 | raise EngineException( |
| 2330 | "There is at least one NS instance using this template", |
| 2331 | http_code=HTTPStatus.CONFLICT, |
| 2332 | ) |
| 2333 | |
| 2334 | def check_unique_template_name(self, edit_content, _id, session): |
| 2335 | """ |
| 2336 | Check whether the name of the template is unique or not |
| 2337 | """ |
| 2338 | |
| 2339 | if edit_content.get("name"): |
| 2340 | name = edit_content.get("name") |
| 2341 | db_content = self.db.get_one( |
| 2342 | "ns_config_template", {"name": name}, fail_on_empty=False |
| 2343 | ) |
| 2344 | if db_content is not None: |
| 2345 | if db_content.get("_id") == _id: |
| 2346 | if db_content.get("name") == name: |
| 2347 | return |
| 2348 | elif db_content.get("_id") != _id: |
| 2349 | raise EngineException( |
| 2350 | "{} of the template already exist".format(name) |
| 2351 | ) |
| 2352 | else: |
| 2353 | return |
| 2354 | |
| 2355 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| 2356 | """ |
| 2357 | Check the input data format |
| 2358 | And the edit content data too. |
| 2359 | """ |
| 2360 | final_content = super().check_conflict_on_edit( |
| 2361 | session, final_content, edit_content, _id |
| 2362 | ) |
| 2363 | db_content_id = self.db.get_one( |
| 2364 | "ns_config_template", {"_id": _id}, fail_on_empty=False |
| 2365 | ) |
| 2366 | if not ( |
| 2367 | db_content_id.get("name") |
| 2368 | and db_content_id.get("nsdId") |
| 2369 | and db_content_id.get("config") |
| 2370 | ): |
| 2371 | validate_input(edit_content, self.schema_new) |
| 2372 | |
| 2373 | try: |
| 2374 | for key, value in edit_content.items(): |
| 2375 | if key == "name": |
| 2376 | self.check_unique_template_name(edit_content, _id, session) |
| 2377 | elif key == "nsdId": |
| 2378 | ns_config_template = self.db.get_one( |
| 2379 | "ns_config_template", {"_id": _id}, fail_on_empty=False |
| 2380 | ) |
| 2381 | if not ns_config_template.get("nsdId"): |
| 2382 | pass |
| 2383 | else: |
| 2384 | raise EngineException("Nsd id cannot be edited") |
| 2385 | elif key == "config": |
| 2386 | edit_content_param = edit_content.get("config") |
| 2387 | for key, value in edit_content_param.items(): |
| 2388 | param = key |
| 2389 | param_content = value |
| kayal2001 | b16cf25 | 2024-11-28 10:47:32 +0530 | [diff] [blame] | 2390 | if param == "vnf": |
| 2391 | for content in param_content: |
| 2392 | for vdu in content.get("vdu"): |
| 2393 | if vdu.get("vim-flavor-name") and vdu.get( |
| 2394 | "vim-flavor-id" |
| 2395 | ): |
| 2396 | raise EngineException( |
| 2397 | "Instantiation parameters vim-flavor-name and vim-flavor-id are mutually exclusive" |
| 2398 | ) |
| kayal2001 | f71c2e8 | 2024-06-25 15:26:24 +0530 | [diff] [blame] | 2399 | validate_input(param_content, self.instantiation_params[param]) |
| kayal2001 | 770c0af | 2024-11-04 22:18:55 +0530 | [diff] [blame] | 2400 | final_content.update({"config": edit_content_param}) |
| kayal2001 | f71c2e8 | 2024-06-25 15:26:24 +0530 | [diff] [blame] | 2401 | return final_content |
| 2402 | except Exception as e: |
| 2403 | raise EngineException( |
| 2404 | "Error in instantiation parameters validation: {}".format(str(e)), |
| 2405 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 2406 | ) |