| 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 logging |
| Guillermo Calvino | 7fcbd4f | 2022-01-26 17:37:56 +0100 | [diff] [blame] | 17 | import json |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 18 | from uuid import uuid4 |
| 19 | from http import HTTPStatus |
| 20 | from time import time |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 21 | from copy import copy, deepcopy |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 22 | from osm_nbi.validation import ( |
| 23 | validate_input, |
| 24 | ValidationError, |
| 25 | ns_instantiate, |
| 26 | ns_terminate, |
| 27 | ns_action, |
| 28 | ns_scale, |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 29 | ns_update, |
| garciadeblas | 0964edf | 2022-02-11 00:43:44 +0100 | [diff] [blame] | 30 | ns_heal, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 31 | nsi_instantiate, |
| elumalai | 8e3806c | 2022-04-28 17:26:24 +0530 | [diff] [blame] | 32 | ns_migrate, |
| govindarajul | 519da48 | 2022-04-29 19:05:22 +0530 | [diff] [blame] | 33 | ns_verticalscale, |
| Gabriel Cuba | 84a60df | 2023-10-30 14:01:54 -0500 | [diff] [blame] | 34 | nslcmop_cancel, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 35 | ) |
| 36 | from osm_nbi.base_topic import ( |
| 37 | BaseTopic, |
| 38 | EngineException, |
| 39 | get_iterable, |
| 40 | deep_get, |
| 41 | increment_ip_mac, |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 42 | update_descriptor_usage_state, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 43 | ) |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 44 | from yaml import safe_dump |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 45 | from osm_common.dbbase import DbException |
| tierno | 1bfe4e2 | 2019-09-02 16:03:25 +0000 | [diff] [blame] | 46 | from osm_common.msgbase import MsgException |
| 47 | from osm_common.fsbase import FsException |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 48 | from osm_nbi import utils |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 49 | from re import ( |
| 50 | match, |
| 51 | ) # For checking that additional parameter names are valid Jinja2 identifiers |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 52 | |
| 53 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 54 | |
| 55 | |
| 56 | class NsrTopic(BaseTopic): |
| 57 | topic = "nsrs" |
| 58 | topic_msg = "ns" |
| tierno | 6b02b05 | 2020-06-02 10:07:41 +0000 | [diff] [blame] | 59 | quota_name = "ns_instances" |
| tierno | d77ba6f | 2019-06-27 14:31:10 +0000 | [diff] [blame] | 60 | schema_new = ns_instantiate |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 61 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 62 | def __init__(self, db, fs, msg, auth): |
| 63 | BaseTopic.__init__(self, db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 64 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 65 | @staticmethod |
| 66 | def format_on_new(content, project_id=None, make_public=False): |
| 67 | BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public) |
| 68 | content["_admin"]["nsState"] = "NOT_INSTANTIATED" |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 69 | return None |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 70 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 71 | def check_conflict_on_del(self, session, _id, db_content): |
| 72 | """ |
| 73 | Check that NSR is not instantiated |
| 74 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 75 | :param _id: nsr internal id |
| 76 | :param db_content: The database content of the nsr |
| 77 | :return: None or raises EngineException with the conflict |
| 78 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 79 | if session["force"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 80 | return |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 81 | nsr = db_content |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 82 | if nsr["_admin"].get("nsState") == "INSTANTIATED": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 83 | raise EngineException( |
| 84 | "nsr '{}' cannot be deleted because it is in 'INSTANTIATED' state. " |
| 85 | "Launch 'terminate' operation first; or force deletion".format(_id), |
| 86 | http_code=HTTPStatus.CONFLICT, |
| 87 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 88 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 89 | def delete_extra(self, session, _id, db_content, not_send_msg=None): |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 90 | """ |
| 91 | Deletes associated nslcmops and vnfrs from database. Deletes associated filesystem. |
| 92 | Set usageState of pdu, vnfd, nsd |
| 93 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 94 | :param _id: server internal id |
| 95 | :param db_content: The database content of the descriptor |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 96 | :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] | 97 | :return: None if ok or raises EngineException with the problem |
| 98 | """ |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 99 | self.fs.file_delete(_id, ignore_non_exist=True) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 100 | self.db.del_list("nslcmops", {"nsInstanceId": _id}) |
| 101 | self.db.del_list("vnfrs", {"nsr-id-ref": _id}) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 102 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 103 | # set all used pdus as free |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 104 | self.db.set_list( |
| 105 | "pdus", |
| 106 | {"_admin.usage.nsr_id": _id}, |
| 107 | {"_admin.usageState": "NOT_IN_USE", "_admin.usage": None}, |
| 108 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 109 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 110 | # Set NSD usageState |
| 111 | nsr = db_content |
| 112 | used_nsd_id = nsr.get("nsd-id") |
| 113 | if used_nsd_id: |
| 114 | # check if used by another NSR |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 115 | nsrs_list = self.db.get_one( |
| 116 | "nsrs", {"nsd-id": used_nsd_id}, fail_on_empty=False, fail_on_more=False |
| 117 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 118 | if not nsrs_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 119 | self.db.set_one( |
| 120 | "nsds", {"_id": used_nsd_id}, {"_admin.usageState": "NOT_IN_USE"} |
| 121 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 122 | |
| 123 | # Set VNFD usageState |
| 124 | used_vnfd_id_list = nsr.get("vnfd-id") |
| 125 | if used_vnfd_id_list: |
| 126 | for used_vnfd_id in used_vnfd_id_list: |
| 127 | # check if used by another NSR |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 128 | nsrs_list = self.db.get_one( |
| 129 | "nsrs", |
| 130 | {"vnfd-id": used_vnfd_id}, |
| 131 | fail_on_empty=False, |
| 132 | fail_on_more=False, |
| 133 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 134 | if not nsrs_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 135 | self.db.set_one( |
| 136 | "vnfds", |
| 137 | {"_id": used_vnfd_id}, |
| 138 | {"_admin.usageState": "NOT_IN_USE"}, |
| 139 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 140 | |
| tierno | f0441ea | 2020-05-26 15:39:18 +0000 | [diff] [blame] | 141 | # delete extra ro_nsrs used for internal RO module |
| 142 | self.db.del_one("ro_nsrs", q_filter={"_id": _id}, fail_on_empty=False) |
| 143 | |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 144 | @staticmethod |
| 145 | def _format_ns_request(ns_request): |
| 146 | formated_request = copy(ns_request) |
| 147 | formated_request.pop("additionalParamsForNs", None) |
| 148 | formated_request.pop("additionalParamsForVnf", None) |
| 149 | return formated_request |
| 150 | |
| 151 | @staticmethod |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 152 | def _format_additional_params( |
| 153 | ns_request, member_vnf_index=None, vdu_id=None, kdu_name=None, descriptor=None |
| 154 | ): |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 155 | """ |
| Pedro Escaleira | dadeccd | 2022-05-20 15:29:20 +0100 | [diff] [blame] | 156 | Get and format user additional params for NS or VNF. |
| 157 | The vdu_id and kdu_name params are mutually exclusive! If none of them are given, then the method will |
| 158 | exclusively search for the VNF/NS LCM additional params. |
| 159 | |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 160 | :param ns_request: User instantiation additional parameters |
| 161 | :param member_vnf_index: None for extract NS params, or member_vnf_index to extract VNF params |
| Pedro Escaleira | dadeccd | 2022-05-20 15:29:20 +0100 | [diff] [blame] | 162 | :vdu_id: VDU's ID against which we want to format the additional params |
| 163 | :kdu_name: KDU's name against which we want to format the additional params |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 164 | :param descriptor: If not None it check that needed parameters of descriptor are supplied |
| tierno | 54db2e4 | 2020-04-06 15:29:42 +0000 | [diff] [blame] | 165 | :return: tuple with a formatted copy of additional params or None if not supplied, plus other parameters |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 166 | """ |
| 167 | additional_params = None |
| tierno | 54db2e4 | 2020-04-06 15:29:42 +0000 | [diff] [blame] | 168 | other_params = None |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 169 | if not member_vnf_index: |
| 170 | additional_params = copy(ns_request.get("additionalParamsForNs")) |
| 171 | where_ = "additionalParamsForNs" |
| 172 | elif ns_request.get("additionalParamsForVnf"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 173 | where_ = "additionalParamsForVnf[member-vnf-index={}]".format( |
| 174 | member_vnf_index |
| 175 | ) |
| 176 | item = next( |
| 177 | ( |
| 178 | x |
| 179 | for x in ns_request["additionalParamsForVnf"] |
| 180 | if x["member-vnf-index"] == member_vnf_index |
| 181 | ), |
| 182 | None, |
| 183 | ) |
| tierno | 714954e | 2019-11-29 13:43:26 +0000 | [diff] [blame] | 184 | if item: |
| tierno | 54db2e4 | 2020-04-06 15:29:42 +0000 | [diff] [blame] | 185 | if not vdu_id and not kdu_name: |
| 186 | other_params = item |
| tierno | 714954e | 2019-11-29 13:43:26 +0000 | [diff] [blame] | 187 | additional_params = copy(item.get("additionalParams")) or {} |
| 188 | if vdu_id and item.get("additionalParamsForVdu"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 189 | item_vdu = next( |
| 190 | ( |
| 191 | x |
| 192 | for x in item["additionalParamsForVdu"] |
| 193 | if x["vdu_id"] == vdu_id |
| 194 | ), |
| 195 | None, |
| 196 | ) |
| tierno | bce98f0 | 2020-04-17 11:27:47 +0000 | [diff] [blame] | 197 | other_params = item_vdu |
| tierno | 714954e | 2019-11-29 13:43:26 +0000 | [diff] [blame] | 198 | if item_vdu and item_vdu.get("additionalParams"): |
| 199 | where_ += ".additionalParamsForVdu[vdu_id={}]".format(vdu_id) |
| tierno | b091dc1 | 2019-12-02 15:53:25 +0000 | [diff] [blame] | 200 | additional_params = item_vdu["additionalParams"] |
| 201 | if kdu_name: |
| 202 | additional_params = {} |
| 203 | if item.get("additionalParamsForKdu"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 204 | item_kdu = next( |
| 205 | ( |
| 206 | x |
| 207 | for x in item["additionalParamsForKdu"] |
| 208 | if x["kdu_name"] == kdu_name |
| 209 | ), |
| 210 | None, |
| 211 | ) |
| tierno | bce98f0 | 2020-04-17 11:27:47 +0000 | [diff] [blame] | 212 | other_params = item_kdu |
| tierno | b091dc1 | 2019-12-02 15:53:25 +0000 | [diff] [blame] | 213 | if item_kdu and item_kdu.get("additionalParams"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 214 | where_ += ".additionalParamsForKdu[kdu_name={}]".format( |
| 215 | kdu_name |
| 216 | ) |
| tierno | b091dc1 | 2019-12-02 15:53:25 +0000 | [diff] [blame] | 217 | additional_params = item_kdu["additionalParams"] |
| tierno | 714954e | 2019-11-29 13:43:26 +0000 | [diff] [blame] | 218 | |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 219 | if additional_params: |
| 220 | for k, v in additional_params.items(): |
| tierno | 714954e | 2019-11-29 13:43:26 +0000 | [diff] [blame] | 221 | # BEGIN Check that additional parameter names are valid Jinja2 identifiers if target is not Kdu |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 222 | if not kdu_name and not match("^[a-zA-Z_][a-zA-Z0-9_]*$", k): |
| 223 | raise EngineException( |
| 224 | "Invalid param name at {}:{}. Must contain only alphanumeric characters " |
| 225 | "and underscores, and cannot start with a digit".format( |
| 226 | where_, k |
| 227 | ) |
| 228 | ) |
| delacruzramo | 36ffe55 | 2019-05-03 14:52:37 +0200 | [diff] [blame] | 229 | # END Check that additional parameter names are valid Jinja2 identifiers |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 230 | if not isinstance(k, str): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 231 | raise EngineException( |
| 232 | "Invalid param at {}:{}. Only string keys are allowed".format( |
| 233 | where_, k |
| 234 | ) |
| 235 | ) |
| Guillermo Calvino | 7fcbd4f | 2022-01-26 17:37:56 +0100 | [diff] [blame] | 236 | if "$" in k: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 237 | raise EngineException( |
| Guillermo Calvino | 7fcbd4f | 2022-01-26 17:37:56 +0100 | [diff] [blame] | 238 | "Invalid param at {}:{}. Keys must not contain $ symbol".format( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 239 | where_, k |
| 240 | ) |
| 241 | ) |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 242 | if isinstance(v, (dict, tuple, list)): |
| 243 | additional_params[k] = "!!yaml " + safe_dump(v) |
| Guillermo Calvino | 7fcbd4f | 2022-01-26 17:37:56 +0100 | [diff] [blame] | 244 | if kdu_name: |
| 245 | additional_params = json.dumps(additional_params) |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 246 | |
| Pedro Escaleira | dadeccd | 2022-05-20 15:29:20 +0100 | [diff] [blame] | 247 | # Select the VDU ID, KDU name or NS/VNF ID, depending on the method's call intent |
| 248 | selector = vdu_id if vdu_id else kdu_name if kdu_name else descriptor.get("id") |
| 249 | |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 250 | if descriptor: |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 251 | for df in descriptor.get("df", []): |
| 252 | # check that enough parameters are supplied for the initial-config-primitive |
| 253 | # TODO: check for cloud-init |
| 254 | if member_vnf_index: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 255 | initial_primitives = [] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 256 | if ( |
| 257 | "lcm-operations-configuration" in df |
| 258 | and "operate-vnf-op-config" |
| 259 | in df["lcm-operations-configuration"] |
| 260 | ): |
| 261 | for config in df["lcm-operations-configuration"][ |
| 262 | "operate-vnf-op-config" |
| 263 | ].get("day1-2", []): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 264 | # Verify the target object (VNF|NS|VDU|KDU) where we need to populate |
| Pedro Escaleira | dadeccd | 2022-05-20 15:29:20 +0100 | [diff] [blame] | 265 | # the params with the additional ones given by the user |
| 266 | if config.get("id") == selector: |
| 267 | for primitive in get_iterable( |
| 268 | config.get("initial-config-primitive") |
| 269 | ): |
| 270 | initial_primitives.append(primitive) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 271 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 272 | initial_primitives = deep_get( |
| 273 | descriptor, ("ns-configuration", "initial-config-primitive") |
| 274 | ) |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 275 | |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 276 | for initial_primitive in get_iterable(initial_primitives): |
| 277 | for param in get_iterable(initial_primitive.get("parameter")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 278 | if param["value"].startswith("<") and param["value"].endswith( |
| 279 | ">" |
| 280 | ): |
| 281 | if param["value"] in ( |
| 282 | "<rw_mgmt_ip>", |
| 283 | "<VDU_SCALE_INFO>", |
| 284 | "<ns_config_info>", |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 285 | "<OSM>", |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 286 | ): |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 287 | continue |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 288 | if ( |
| 289 | not additional_params |
| 290 | or param["value"][1:-1] not in additional_params |
| 291 | ): |
| 292 | raise EngineException( |
| 293 | "Parameter '{}' needed for vnfd[id={}]:day1-2 configuration:" |
| 294 | "initial-config-primitive[name={}] not supplied".format( |
| 295 | param["value"], |
| 296 | descriptor["id"], |
| 297 | initial_primitive["name"], |
| 298 | ) |
| 299 | ) |
| tierno | 714954e | 2019-11-29 13:43:26 +0000 | [diff] [blame] | 300 | |
| tierno | 54db2e4 | 2020-04-06 15:29:42 +0000 | [diff] [blame] | 301 | return additional_params or None, other_params or None |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 302 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 303 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 304 | """ |
| 305 | Creates a new nsr into database. It also creates needed vnfrs |
| 306 | :param rollback: list to append the created items at database in case a rollback must be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 307 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 308 | :param indata: params to be used for the nsr |
| 309 | :param kwargs: used to override the indata descriptor |
| 310 | :param headers: http request headers |
| tierno | 1bfe4e2 | 2019-09-02 16:03:25 +0000 | [diff] [blame] | 311 | :return: the _id of nsr descriptor created at database. Or an exception of type |
| 312 | EngineException, ValidationError, DbException, FsException, MsgException. |
| 313 | Note: Exceptions are not captured on purpose. They should be captured at called |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 314 | """ |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 315 | step = "checking quotas" # first step must be defined outside try |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 316 | try: |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 317 | self.check_quota(session) |
| 318 | |
| tierno | 99d4b17 | 2019-07-02 09:28:40 +0000 | [diff] [blame] | 319 | step = "validating input parameters" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 320 | ns_request = self._remove_envelop(indata) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 321 | self._update_input_with_kwargs(ns_request, kwargs) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 322 | ns_request = self._validate_input_new(ns_request, session["force"]) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 323 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 324 | step = "getting nsd id='{}' from database".format(ns_request.get("nsdId")) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 325 | nsd = self._get_nsd_from_db(ns_request["nsdId"], session) |
| 326 | ns_k8s_namespace = self._get_ns_k8s_namespace(nsd, ns_request, session) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 327 | |
| Frank Bryden | 3c64ab6 | 2020-07-21 14:25:32 +0000 | [diff] [blame] | 328 | step = "checking nsdOperationalState" |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 329 | self._check_nsd_operational_state(nsd, ns_request) |
| Frank Bryden | 3c64ab6 | 2020-07-21 14:25:32 +0000 | [diff] [blame] | 330 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 331 | step = "filling nsr from input data" |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 332 | nsr_id = str(uuid4()) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 333 | nsr_descriptor = self._create_nsr_descriptor_from_nsd( |
| 334 | nsd, ns_request, nsr_id, session |
| 335 | ) |
| tierno | 54db2e4 | 2020-04-06 15:29:42 +0000 | [diff] [blame] | 336 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 337 | # Create VNFRs |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 338 | needed_vnfds = {} |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 339 | # TODO: Change for multiple df support |
| K Sai Kiran | bb00602 | 2021-05-20 11:09:49 +0530 | [diff] [blame] | 340 | vnf_profiles = nsd.get("df", [{}])[0].get("vnf-profile", ()) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 341 | for vnfp in vnf_profiles: |
| 342 | vnfd_id = vnfp.get("vnfd-id") |
| 343 | vnf_index = vnfp.get("id") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 344 | step = ( |
| 345 | "getting vnfd id='{}' constituent-vnfd='{}' from database".format( |
| 346 | vnfd_id, vnf_index |
| 347 | ) |
| 348 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 349 | if vnfd_id not in needed_vnfds: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 350 | vnfd = self._get_vnfd_from_db(vnfd_id, session) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 351 | if "revision" in vnfd["_admin"]: |
| 352 | vnfd["revision"] = vnfd["_admin"]["revision"] |
| 353 | vnfd.pop("_admin") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 354 | needed_vnfds[vnfd_id] = vnfd |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 355 | nsr_descriptor["vnfd-id"].append(vnfd["_id"]) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 356 | else: |
| 357 | vnfd = needed_vnfds[vnfd_id] |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 358 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 359 | step = "filling vnfr vnfd-id='{}' constituent-vnfd='{}'".format( |
| 360 | vnfd_id, vnf_index |
| 361 | ) |
| 362 | vnfr_descriptor = self._create_vnfr_descriptor_from_vnfd( |
| 363 | nsd, |
| 364 | vnfd, |
| 365 | vnfd_id, |
| 366 | vnf_index, |
| 367 | nsr_descriptor, |
| 368 | ns_request, |
| 369 | ns_k8s_namespace, |
| 370 | ) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 371 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 372 | step = "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format( |
| 373 | vnfd_id, vnf_index |
| 374 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 375 | self._add_vnfr_to_db(vnfr_descriptor, rollback, session) |
| 376 | nsr_descriptor["constituent-vnfr-ref"].append(vnfr_descriptor["id"]) |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 377 | step = "Updating VNFD usageState" |
| 378 | update_descriptor_usage_state(vnfd, "vnfds", self.db) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 379 | |
| 380 | step = "creating nsr at database" |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 381 | self._add_nsr_to_db(nsr_descriptor, rollback, session) |
| aticig | 2b5e123 | 2022-08-10 17:30:12 +0300 | [diff] [blame] | 382 | step = "Updating NSD usageState" |
| 383 | update_descriptor_usage_state(nsd, "nsds", self.db) |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 384 | |
| 385 | step = "creating nsr temporal folder" |
| 386 | self.fs.mkdir(nsr_id) |
| 387 | |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 388 | return nsr_id, None |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 389 | except ( |
| 390 | ValidationError, |
| 391 | EngineException, |
| 392 | DbException, |
| 393 | MsgException, |
| 394 | FsException, |
| 395 | ) as e: |
| Frank Bryden | 3c64ab6 | 2020-07-21 14:25:32 +0000 | [diff] [blame] | 396 | raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 397 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 398 | def _get_nsd_from_db(self, nsd_id, session): |
| 399 | _filter = self._get_project_filter(session) |
| 400 | _filter["_id"] = nsd_id |
| 401 | return self.db.get_one("nsds", _filter) |
| 402 | |
| 403 | def _get_vnfd_from_db(self, vnfd_id, session): |
| 404 | _filter = self._get_project_filter(session) |
| 405 | _filter["id"] = vnfd_id |
| 406 | vnfd = self.db.get_one("vnfds", _filter, fail_on_empty=True, fail_on_more=True) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 407 | return vnfd |
| 408 | |
| 409 | def _add_nsr_to_db(self, nsr_descriptor, rollback, session): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 410 | self.format_on_new( |
| 411 | nsr_descriptor, session["project_id"], make_public=session["public"] |
| 412 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 413 | self.db.create("nsrs", nsr_descriptor) |
| 414 | rollback.append({"topic": "nsrs", "_id": nsr_descriptor["id"]}) |
| 415 | |
| 416 | def _add_vnfr_to_db(self, vnfr_descriptor, rollback, session): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 417 | self.format_on_new( |
| 418 | vnfr_descriptor, session["project_id"], make_public=session["public"] |
| 419 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 420 | self.db.create("vnfrs", vnfr_descriptor) |
| 421 | rollback.append({"topic": "vnfrs", "_id": vnfr_descriptor["id"]}) |
| 422 | |
| 423 | def _check_nsd_operational_state(self, nsd, ns_request): |
| 424 | if nsd["_admin"]["operationalState"] == "DISABLED": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 425 | raise EngineException( |
| 426 | "nsd with id '{}' is DISABLED, and thus cannot be used to create " |
| 427 | "a network service".format(ns_request["nsdId"]), |
| 428 | http_code=HTTPStatus.CONFLICT, |
| 429 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 430 | |
| 431 | def _get_ns_k8s_namespace(self, nsd, ns_request, session): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 432 | additional_params, _ = self._format_additional_params( |
| 433 | ns_request, descriptor=nsd |
| 434 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 435 | # use for k8s-namespace from ns_request or additionalParamsForNs. By default, the project_id |
| 436 | ns_k8s_namespace = session["project_id"][0] if session["project_id"] else None |
| 437 | if ns_request and ns_request.get("k8s-namespace"): |
| 438 | ns_k8s_namespace = ns_request["k8s-namespace"] |
| 439 | if additional_params and additional_params.get("k8s-namespace"): |
| 440 | ns_k8s_namespace = additional_params["k8s-namespace"] |
| 441 | |
| 442 | return ns_k8s_namespace |
| 443 | |
| vegall | 18101ea | 2023-03-06 13:49:21 +0000 | [diff] [blame] | 444 | def _add_shared_volumes_to_nsr( |
| 445 | self, vdu, vnfd, nsr_descriptor, member_vnf_index, revision=None |
| 446 | ): |
| 447 | svsd = [] |
| 448 | for vsd in vnfd.get("virtual-storage-desc", ()): |
| 449 | if vsd.get("vdu-storage-requirements"): |
| 450 | if ( |
| 451 | vsd.get("vdu-storage-requirements")[0].get("key") == "multiattach" |
| 452 | and vsd.get("vdu-storage-requirements")[0].get("value") == "True" |
| 453 | ): |
| vegall | f976a3a | 2023-06-02 21:25:32 +0000 | [diff] [blame] | 454 | # Avoid setting the volume name multiple times |
| 455 | if not match(f"shared-.*-{vnfd['id']}", vsd["id"]): |
| vegall | 18101ea | 2023-03-06 13:49:21 +0000 | [diff] [blame] | 456 | vsd["id"] = f"shared-{vsd['id']}-{vnfd['id']}" |
| 457 | svsd.append(vsd) |
| 458 | if svsd: |
| 459 | nsr_descriptor["shared-volumes"] = svsd |
| 460 | |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 461 | def _add_flavor_to_nsr( |
| 462 | self, vdu, vnfd, nsr_descriptor, member_vnf_index, revision=None |
| 463 | ): |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 464 | flavor_data = {} |
| 465 | guest_epa = {} |
| 466 | # Find this vdu compute and storage descriptors |
| 467 | vdu_virtual_compute = {} |
| 468 | vdu_virtual_storage = {} |
| 469 | for vcd in vnfd.get("virtual-compute-desc", ()): |
| 470 | if vcd.get("id") == vdu.get("virtual-compute-desc"): |
| 471 | vdu_virtual_compute = vcd |
| 472 | for vsd in vnfd.get("virtual-storage-desc", ()): |
| 473 | if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]: |
| 474 | vdu_virtual_storage = vsd |
| 475 | # Get this vdu vcpus, memory and storage info for flavor_data |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 476 | if vdu_virtual_compute.get("virtual-cpu", {}).get("num-virtual-cpu"): |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 477 | flavor_data["vcpu-count"] = vdu_virtual_compute["virtual-cpu"][ |
| 478 | "num-virtual-cpu" |
| 479 | ] |
| 480 | if vdu_virtual_compute.get("virtual-memory", {}).get("size"): |
| 481 | flavor_data["memory-mb"] = ( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 482 | float(vdu_virtual_compute["virtual-memory"]["size"]) * 1024.0 |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 483 | ) |
| 484 | if vdu_virtual_storage.get("size-of-storage"): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 485 | flavor_data["storage-gb"] = vdu_virtual_storage["size-of-storage"] |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 486 | # Get this vdu EPA info for guest_epa |
| 487 | if vdu_virtual_compute.get("virtual-cpu", {}).get("cpu-quota"): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 488 | guest_epa["cpu-quota"] = vdu_virtual_compute["virtual-cpu"]["cpu-quota"] |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 489 | if vdu_virtual_compute.get("virtual-cpu", {}).get("pinning"): |
| 490 | vcpu_pinning = vdu_virtual_compute["virtual-cpu"]["pinning"] |
| 491 | if vcpu_pinning.get("thread-policy"): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 492 | guest_epa["cpu-thread-pinning-policy"] = vcpu_pinning["thread-policy"] |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 493 | if vcpu_pinning.get("policy"): |
| 494 | cpu_policy = ( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 495 | "SHARED" if vcpu_pinning["policy"] == "dynamic" else "DEDICATED" |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 496 | ) |
| 497 | guest_epa["cpu-pinning-policy"] = cpu_policy |
| 498 | if vdu_virtual_compute.get("virtual-memory", {}).get("mem-quota"): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 499 | guest_epa["mem-quota"] = vdu_virtual_compute["virtual-memory"]["mem-quota"] |
| 500 | if vdu_virtual_compute.get("virtual-memory", {}).get("mempage-size"): |
| 501 | guest_epa["mempage-size"] = vdu_virtual_compute["virtual-memory"][ |
| 502 | "mempage-size" |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 503 | ] |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 504 | if vdu_virtual_compute.get("virtual-memory", {}).get("numa-node-policy"): |
| 505 | guest_epa["numa-node-policy"] = vdu_virtual_compute["virtual-memory"][ |
| 506 | "numa-node-policy" |
| 507 | ] |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 508 | if vdu_virtual_storage.get("disk-io-quota"): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 509 | guest_epa["disk-io-quota"] = vdu_virtual_storage["disk-io-quota"] |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 510 | |
| 511 | if guest_epa: |
| 512 | flavor_data["guest-epa"] = guest_epa |
| 513 | |
| elumalai | 99078a9 | 2022-07-05 17:53:59 +0530 | [diff] [blame] | 514 | revision = revision if revision is not None else 1 |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 515 | flavor_data["name"] = ( |
| 516 | vdu["id"][:56] + "-" + member_vnf_index + "-" + str(revision) + "-flv" |
| 517 | ) |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 518 | flavor_data["id"] = str(len(nsr_descriptor["flavor"])) |
| 519 | nsr_descriptor["flavor"].append(flavor_data) |
| 520 | |
| bravof | e76b882 | 2021-02-26 16:57:52 -0300 | [diff] [blame] | 521 | def _create_nsr_descriptor_from_nsd(self, nsd, ns_request, nsr_id, session): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 522 | now = time() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 523 | additional_params, _ = self._format_additional_params( |
| 524 | ns_request, descriptor=nsd |
| 525 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 526 | |
| 527 | nsr_descriptor = { |
| 528 | "name": ns_request["nsName"], |
| 529 | "name-ref": ns_request["nsName"], |
| 530 | "short-name": ns_request["nsName"], |
| 531 | "admin-status": "ENABLED", |
| 532 | "nsState": "NOT_INSTANTIATED", |
| 533 | "currentOperation": "IDLE", |
| 534 | "currentOperationID": None, |
| 535 | "errorDescription": None, |
| 536 | "errorDetail": None, |
| 537 | "deploymentStatus": None, |
| 538 | "configurationStatus": None, |
| 539 | "vcaStatus": None, |
| 540 | "nsd": {k: v for k, v in nsd.items()}, |
| 541 | "datacenter": ns_request["vimAccountId"], |
| 542 | "resource-orchestrator": "osmopenmano", |
| 543 | "description": ns_request.get("nsDescription", ""), |
| 544 | "constituent-vnfr-ref": [], |
| 545 | "operational-status": "init", # typedef ns-operational- |
| 546 | "config-status": "init", # typedef config-states |
| 547 | "detailed-status": "scheduled", |
| 548 | "orchestration-progress": {}, |
| 549 | "create-time": now, |
| 550 | "nsd-name-ref": nsd["name"], |
| 551 | "operational-events": [], # "id", "timestamp", "description", "event", |
| 552 | "nsd-ref": nsd["id"], |
| 553 | "nsd-id": nsd["_id"], |
| 554 | "vnfd-id": [], |
| 555 | "instantiate_params": self._format_ns_request(ns_request), |
| 556 | "additionalParamsForNs": additional_params, |
| 557 | "ns-instance-config-ref": nsr_id, |
| 558 | "id": nsr_id, |
| 559 | "_id": nsr_id, |
| 560 | "ssh-authorized-key": ns_request.get("ssh_keys"), # TODO remove |
| 561 | "flavor": [], |
| 562 | "image": [], |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 563 | "affinity-or-anti-affinity-group": [], |
| vegall | 18101ea | 2023-03-06 13:49:21 +0000 | [diff] [blame] | 564 | "shared-volumes": [], |
| selvi.j | 828f3f2 | 2023-05-16 05:43:48 +0000 | [diff] [blame] | 565 | "vnffgd": [], |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 566 | } |
| beierlm | bc5a524 | 2022-05-17 21:25:29 -0400 | [diff] [blame] | 567 | if "revision" in nsd["_admin"]: |
| 568 | nsr_descriptor["revision"] = nsd["_admin"]["revision"] |
| 569 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 570 | ns_request["nsr_id"] = nsr_id |
| 571 | if ns_request and ns_request.get("config-units"): |
| 572 | nsr_descriptor["config-units"] = ns_request["config-units"] |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 573 | # Create vld |
| 574 | if nsd.get("virtual-link-desc"): |
| 575 | nsr_vld = deepcopy(nsd.get("virtual-link-desc", [])) |
| 576 | # Fill each vld with vnfd-connection-point-ref data |
| 577 | # TODO: Change for multiple df support |
| 578 | all_vld_connection_point_data = {vld.get("id"): [] for vld in nsr_vld} |
| 579 | vnf_profiles = nsd.get("df", [[]])[0].get("vnf-profile", ()) |
| 580 | for vnf_profile in vnf_profiles: |
| 581 | for vlc in vnf_profile.get("virtual-link-connectivity", ()): |
| 582 | for cpd in vlc.get("constituent-cpd-id", ()): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 583 | all_vld_connection_point_data[ |
| 584 | vlc.get("virtual-link-profile-id") |
| 585 | ].append( |
| 586 | { |
| 587 | "member-vnf-index-ref": cpd.get( |
| 588 | "constituent-base-element-id" |
| 589 | ), |
| 590 | "vnfd-connection-point-ref": cpd.get( |
| 591 | "constituent-cpd-id" |
| 592 | ), |
| 593 | "vnfd-id-ref": vnf_profile.get("vnfd-id"), |
| 594 | } |
| 595 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 596 | |
| bravof | e76b882 | 2021-02-26 16:57:52 -0300 | [diff] [blame] | 597 | vnfd = self._get_vnfd_from_db(vnf_profile.get("vnfd-id"), session) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 598 | vnfd.pop("_admin") |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 599 | |
| 600 | for vdu in vnfd.get("vdu", ()): |
| elumalai | 99078a9 | 2022-07-05 17:53:59 +0530 | [diff] [blame] | 601 | member_vnf_index = vnf_profile.get("id") |
| 602 | self._add_flavor_to_nsr(vdu, vnfd, nsr_descriptor, member_vnf_index) |
| vegall | 18101ea | 2023-03-06 13:49:21 +0000 | [diff] [blame] | 603 | self._add_shared_volumes_to_nsr( |
| 604 | vdu, vnfd, nsr_descriptor, member_vnf_index |
| 605 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 606 | sw_image_id = vdu.get("sw-image-desc") |
| 607 | if sw_image_id: |
| lloretgalleg | 28c13b6 | 2021-02-08 11:48:48 +0000 | [diff] [blame] | 608 | image_data = self._get_image_data_from_vnfd(vnfd, sw_image_id) |
| 609 | self._add_image_to_nsr(nsr_descriptor, image_data) |
| 610 | |
| 611 | # also add alternative images to the list of images |
| 612 | for alt_image in vdu.get("alternative-sw-image-desc", ()): |
| 613 | image_data = self._get_image_data_from_vnfd(vnfd, alt_image) |
| 614 | self._add_image_to_nsr(nsr_descriptor, image_data) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 615 | |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 616 | # Add Affinity or Anti-affinity group information to NSR |
| 617 | vdu_profiles = vnfd.get("df", [[]])[0].get("vdu-profile", ()) |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 618 | affinity_group_prefix_name = "{}-{}".format( |
| 619 | nsr_descriptor["name"][:16], vnf_profile.get("id")[:16] |
| 620 | ) |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 621 | |
| 622 | for vdu_profile in vdu_profiles: |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 623 | affinity_group_data = {} |
| 624 | for affinity_group in vdu_profile.get( |
| 625 | "affinity-or-anti-affinity-group", () |
| 626 | ): |
| 627 | affinity_group_data = ( |
| 628 | self._get_affinity_or_anti_affinity_group_data_from_vnfd( |
| 629 | vnfd, affinity_group["id"] |
| 630 | ) |
| 631 | ) |
| 632 | affinity_group_data["member-vnf-index"] = vnf_profile.get("id") |
| 633 | self._add_affinity_or_anti_affinity_group_to_nsr( |
| 634 | nsr_descriptor, |
| 635 | affinity_group_data, |
| 636 | affinity_group_prefix_name, |
| 637 | ) |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 638 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 639 | for vld in nsr_vld: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 640 | vld["vnfd-connection-point-ref"] = all_vld_connection_point_data.get( |
| 641 | vld.get("id"), [] |
| 642 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 643 | vld["name"] = vld["id"] |
| 644 | nsr_descriptor["vld"] = nsr_vld |
| selvi.j | 828f3f2 | 2023-05-16 05:43:48 +0000 | [diff] [blame] | 645 | if nsd.get("vnffgd"): |
| 646 | vnffgd = nsd.get("vnffgd") |
| 647 | for vnffg in vnffgd: |
| 648 | info = {} |
| 649 | for k, v in vnffg.items(): |
| 650 | if k == "id": |
| 651 | info.update({k: v}) |
| 652 | if k == "nfpd": |
| 653 | info.update({k: v}) |
| 654 | nsr_descriptor["vnffgd"].append(info) |
| 655 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 656 | return nsr_descriptor |
| 657 | |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 658 | def _get_affinity_or_anti_affinity_group_data_from_vnfd( |
| 659 | self, vnfd, affinity_group_id |
| 660 | ): |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 661 | """ |
| 662 | Gets affinity-or-anti-affinity-group info from df and returns the desired affinity group |
| 663 | """ |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 664 | affinity_group = utils.find_in_list( |
| 665 | vnfd.get("df", [[]])[0].get("affinity-or-anti-affinity-group", ()), |
| 666 | lambda ag: ag["id"] == affinity_group_id, |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 667 | ) |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 668 | affinity_group_data = {} |
| 669 | if affinity_group: |
| 670 | if affinity_group.get("id"): |
| 671 | affinity_group_data["ag-id"] = affinity_group["id"] |
| 672 | if affinity_group.get("type"): |
| 673 | affinity_group_data["type"] = affinity_group["type"] |
| 674 | if affinity_group.get("scope"): |
| 675 | affinity_group_data["scope"] = affinity_group["scope"] |
| 676 | return affinity_group_data |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 677 | |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 678 | def _add_affinity_or_anti_affinity_group_to_nsr( |
| 679 | self, nsr_descriptor, affinity_group_data, affinity_group_prefix_name |
| 680 | ): |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 681 | """ |
| 682 | Adds affinity-or-anti-affinity-group to nsr checking first it is not already added |
| 683 | """ |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 684 | affinity_group = next( |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 685 | ( |
| 686 | f |
| 687 | for f in nsr_descriptor["affinity-or-anti-affinity-group"] |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 688 | if all(f.get(k) == affinity_group_data[k] for k in affinity_group_data) |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 689 | ), |
| 690 | None, |
| 691 | ) |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 692 | if not affinity_group: |
| 693 | affinity_group_data["id"] = str( |
| 694 | len(nsr_descriptor["affinity-or-anti-affinity-group"]) |
| 695 | ) |
| 696 | affinity_group_data["name"] = "{}-{}".format( |
| 697 | affinity_group_prefix_name, affinity_group_data["ag-id"][:32] |
| 698 | ) |
| 699 | nsr_descriptor["affinity-or-anti-affinity-group"].append( |
| 700 | affinity_group_data |
| 701 | ) |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 702 | |
| lloretgalleg | 28c13b6 | 2021-02-08 11:48:48 +0000 | [diff] [blame] | 703 | def _get_image_data_from_vnfd(self, vnfd, sw_image_id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 704 | sw_image_desc = utils.find_in_list( |
| 705 | vnfd.get("sw-image-desc", ()), lambda sw: sw["id"] == sw_image_id |
| 706 | ) |
| lloretgalleg | 28c13b6 | 2021-02-08 11:48:48 +0000 | [diff] [blame] | 707 | image_data = {} |
| 708 | if sw_image_desc.get("image"): |
| 709 | image_data["image"] = sw_image_desc["image"] |
| 710 | if sw_image_desc.get("checksum"): |
| 711 | image_data["image_checksum"] = sw_image_desc["checksum"]["hash"] |
| 712 | if sw_image_desc.get("vim-type"): |
| 713 | image_data["vim-type"] = sw_image_desc["vim-type"] |
| 714 | return image_data |
| 715 | |
| 716 | def _add_image_to_nsr(self, nsr_descriptor, image_data): |
| 717 | """ |
| 718 | Adds image to nsr checking first it is not already added |
| 719 | """ |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 720 | img = next( |
| 721 | ( |
| 722 | f |
| 723 | for f in nsr_descriptor["image"] |
| 724 | if all(f.get(k) == image_data[k] for k in image_data) |
| 725 | ), |
| 726 | None, |
| 727 | ) |
| lloretgalleg | 28c13b6 | 2021-02-08 11:48:48 +0000 | [diff] [blame] | 728 | if not img: |
| 729 | image_data["id"] = str(len(nsr_descriptor["image"])) |
| 730 | nsr_descriptor["image"].append(image_data) |
| 731 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 732 | def _create_vnfr_descriptor_from_vnfd( |
| 733 | self, |
| 734 | nsd, |
| 735 | vnfd, |
| 736 | vnfd_id, |
| 737 | vnf_index, |
| 738 | nsr_descriptor, |
| 739 | ns_request, |
| 740 | ns_k8s_namespace, |
| elumalai | 99078a9 | 2022-07-05 17:53:59 +0530 | [diff] [blame] | 741 | revision=None, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 742 | ): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 743 | vnfr_id = str(uuid4()) |
| 744 | nsr_id = nsr_descriptor["id"] |
| 745 | now = time() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 746 | additional_params, vnf_params = self._format_additional_params( |
| 747 | ns_request, vnf_index, descriptor=vnfd |
| 748 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 749 | |
| 750 | vnfr_descriptor = { |
| 751 | "id": vnfr_id, |
| 752 | "_id": vnfr_id, |
| 753 | "nsr-id-ref": nsr_id, |
| 754 | "member-vnf-index-ref": vnf_index, |
| 755 | "additionalParamsForVnf": additional_params, |
| 756 | "created-time": now, |
| 757 | # "vnfd": vnfd, # at OSM model.but removed to avoid data duplication TODO: revise |
| 758 | "vnfd-ref": vnfd_id, |
| 759 | "vnfd-id": vnfd["_id"], # not at OSM model, but useful |
| 760 | "vim-account-id": None, |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 761 | "vca-id": None, |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 762 | "vdur": [], |
| 763 | "connection-point": [], |
| 764 | "ip-address": None, # mgmt-interface filled by LCM |
| 765 | } |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 766 | |
| 767 | # Revision backwards compatility. Only specify the revision in the record if |
| 768 | # the original VNFD has a revision. |
| 769 | if "revision" in vnfd: |
| 770 | vnfr_descriptor["revision"] = vnfd["revision"] |
| 771 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 772 | vnf_k8s_namespace = ns_k8s_namespace |
| 773 | if vnf_params: |
| 774 | if vnf_params.get("k8s-namespace"): |
| 775 | vnf_k8s_namespace = vnf_params["k8s-namespace"] |
| 776 | if vnf_params.get("config-units"): |
| 777 | vnfr_descriptor["config-units"] = vnf_params["config-units"] |
| 778 | |
| 779 | # Create vld |
| 780 | if vnfd.get("int-virtual-link-desc"): |
| 781 | vnfr_descriptor["vld"] = [] |
| 782 | for vnfd_vld in vnfd.get("int-virtual-link-desc"): |
| 783 | vnfr_descriptor["vld"].append({key: vnfd_vld[key] for key in vnfd_vld}) |
| 784 | |
| 785 | for cp in vnfd.get("ext-cpd", ()): |
| 786 | vnf_cp = { |
| 787 | "name": cp.get("id"), |
| David Garcia | 1409c27 | 2020-12-02 15:47:46 +0100 | [diff] [blame] | 788 | "connection-point-id": cp.get("int-cpd", {}).get("cpd"), |
| 789 | "connection-point-vdu-id": cp.get("int-cpd", {}).get("vdu-id"), |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 790 | "id": cp.get("id"), |
| 791 | # "ip-address", "mac-address" # filled by LCM |
| 792 | # vim-id # TODO it would be nice having a vim port id |
| 793 | } |
| 794 | vnfr_descriptor["connection-point"].append(vnf_cp) |
| 795 | |
| 796 | # Create k8s-cluster information |
| 797 | # TODO: Validate if a k8s-cluster net can have more than one ext-cpd ? |
| 798 | if vnfd.get("k8s-cluster"): |
| 799 | vnfr_descriptor["k8s-cluster"] = vnfd["k8s-cluster"] |
| 800 | all_k8s_cluster_nets_cpds = {} |
| 801 | for cpd in get_iterable(vnfd.get("ext-cpd")): |
| 802 | if cpd.get("k8s-cluster-net"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 803 | all_k8s_cluster_nets_cpds[cpd.get("k8s-cluster-net")] = cpd.get( |
| 804 | "id" |
| 805 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 806 | for net in get_iterable(vnfr_descriptor["k8s-cluster"].get("nets")): |
| 807 | if net.get("id") in all_k8s_cluster_nets_cpds: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 808 | net["external-connection-point-ref"] = all_k8s_cluster_nets_cpds[ |
| 809 | net.get("id") |
| 810 | ] |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 811 | |
| 812 | # update kdus |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 813 | for kdu in get_iterable(vnfd.get("kdu")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 814 | additional_params, kdu_params = self._format_additional_params( |
| 815 | ns_request, vnf_index, kdu_name=kdu["name"], descriptor=vnfd |
| 816 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 817 | kdu_k8s_namespace = vnf_k8s_namespace |
| 818 | kdu_model = kdu_params.get("kdu_model") if kdu_params else None |
| 819 | if kdu_params and kdu_params.get("k8s-namespace"): |
| 820 | kdu_k8s_namespace = kdu_params["k8s-namespace"] |
| 821 | |
| romeromonser | bfebfc0 | 2021-05-28 10:51:35 +0200 | [diff] [blame] | 822 | kdu_deployment_name = "" |
| 823 | if kdu_params and kdu_params.get("kdu-deployment-name"): |
| 824 | kdu_deployment_name = kdu_params.get("kdu-deployment-name") |
| 825 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 826 | kdur = { |
| 827 | "additionalParams": additional_params, |
| 828 | "k8s-namespace": kdu_k8s_namespace, |
| romeromonser | bfebfc0 | 2021-05-28 10:51:35 +0200 | [diff] [blame] | 829 | "kdu-deployment-name": kdu_deployment_name, |
| garciadeblas | 61e0c52 | 2020-12-15 10:33:40 +0000 | [diff] [blame] | 830 | "kdu-name": kdu["name"], |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 831 | # TODO "name": "" Name of the VDU in the VIM |
| 832 | "ip-address": None, # mgmt-interface filled by LCM |
| 833 | "k8s-cluster": {}, |
| 834 | } |
| 835 | if kdu_params and kdu_params.get("config-units"): |
| 836 | kdur["config-units"] = kdu_params["config-units"] |
| garciadeblas | 61e0c52 | 2020-12-15 10:33:40 +0000 | [diff] [blame] | 837 | if kdu.get("helm-version"): |
| 838 | kdur["helm-version"] = kdu["helm-version"] |
| 839 | for k8s_type in ("helm-chart", "juju-bundle"): |
| 840 | if kdu.get(k8s_type): |
| 841 | kdur[k8s_type] = kdu_model or kdu[k8s_type] |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 842 | if not vnfr_descriptor.get("kdur"): |
| 843 | vnfr_descriptor["kdur"] = [] |
| 844 | vnfr_descriptor["kdur"].append(kdur) |
| 845 | |
| 846 | vnfd_mgmt_cp = vnfd.get("mgmt-cp") |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 847 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 848 | for vdu in vnfd.get("vdu", ()): |
| bravof | f3c3955 | 2021-02-24 17:22:24 -0300 | [diff] [blame] | 849 | vdu_mgmt_cp = [] |
| 850 | try: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 851 | configs = vnfd.get("df")[0]["lcm-operations-configuration"][ |
| 852 | "operate-vnf-op-config" |
| 853 | ]["day1-2"] |
| 854 | vdu_config = utils.find_in_list( |
| 855 | configs, lambda config: config["id"] == vdu["id"] |
| 856 | ) |
| bravof | f3c3955 | 2021-02-24 17:22:24 -0300 | [diff] [blame] | 857 | except Exception: |
| 858 | vdu_config = None |
| bravof | 4ca5152 | 2021-04-22 10:03:02 -0400 | [diff] [blame] | 859 | |
| 860 | try: |
| 861 | vdu_instantiation_level = utils.find_in_list( |
| 862 | vnfd.get("df")[0]["instantiation-level"][0]["vdu-level"], |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 863 | lambda a_vdu_profile: a_vdu_profile["vdu-id"] == vdu["id"], |
| bravof | 4ca5152 | 2021-04-22 10:03:02 -0400 | [diff] [blame] | 864 | ) |
| 865 | except Exception: |
| 866 | vdu_instantiation_level = None |
| 867 | |
| bravof | f3c3955 | 2021-02-24 17:22:24 -0300 | [diff] [blame] | 868 | if vdu_config: |
| 869 | external_connection_ee = utils.filter_in_list( |
| 870 | vdu_config.get("execution-environment-list", []), |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 871 | lambda ee: "external-connection-point-ref" in ee, |
| bravof | f3c3955 | 2021-02-24 17:22:24 -0300 | [diff] [blame] | 872 | ) |
| 873 | for ee in external_connection_ee: |
| 874 | vdu_mgmt_cp.append(ee["external-connection-point-ref"]) |
| 875 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 876 | additional_params, vdu_params = self._format_additional_params( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 877 | ns_request, vnf_index, vdu_id=vdu["id"], descriptor=vnfd |
| 878 | ) |
| bravof | 65e22e5 | 2021-11-10 17:58:58 -0300 | [diff] [blame] | 879 | |
| 880 | try: |
| 881 | vdu_virtual_storage_descriptors = utils.filter_in_list( |
| 882 | vnfd.get("virtual-storage-desc", []), |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 883 | lambda stg_desc: stg_desc["id"] in vdu["virtual-storage-desc"], |
| bravof | 65e22e5 | 2021-11-10 17:58:58 -0300 | [diff] [blame] | 884 | ) |
| 885 | except Exception: |
| 886 | vdu_virtual_storage_descriptors = [] |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 887 | vdur = { |
| 888 | "vdu-id-ref": vdu["id"], |
| 889 | # TODO "name": "" Name of the VDU in the VIM |
| 890 | "ip-address": None, # mgmt-interface filled by LCM |
| 891 | # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM |
| 892 | "internal-connection-point": [], |
| 893 | "interfaces": [], |
| 894 | "additionalParams": additional_params, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 895 | "vdu-name": vdu["name"], |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 896 | "virtual-storages": vdu_virtual_storage_descriptors, |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 897 | } |
| 898 | if vdu_params and vdu_params.get("config-units"): |
| 899 | vdur["config-units"] = vdu_params["config-units"] |
| 900 | if deep_get(vdu, ("supplemental-boot-data", "boot-data-drive")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 901 | vdur["boot-data-drive"] = vdu["supplemental-boot-data"][ |
| 902 | "boot-data-drive" |
| 903 | ] |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 904 | if vdu.get("pdu-type"): |
| 905 | vdur["pdu-type"] = vdu["pdu-type"] |
| 906 | vdur["name"] = vdu["pdu-type"] |
| 907 | # TODO volumes: name, volume-id |
| 908 | for icp in vdu.get("int-cpd", ()): |
| 909 | vdu_icp = { |
| 910 | "id": icp["id"], |
| 911 | "connection-point-id": icp["id"], |
| 912 | "name": icp.get("id"), |
| 913 | } |
| bravof | 3576644 | 2021-02-04 14:58:04 -0300 | [diff] [blame] | 914 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 915 | vdur["internal-connection-point"].append(vdu_icp) |
| 916 | |
| 917 | for iface in icp.get("virtual-network-interface-requirement", ()): |
| aticig | c9c0339 | 2022-06-16 01:39:44 +0300 | [diff] [blame] | 918 | # Name, mac-address and interface position is taken from VNFD |
| 919 | # and included into VNFR. By this way RO can process this information |
| 920 | # while creating the VDU. |
| Gulsum Atici | 9af2a47 | 2023-03-28 17:50:48 +0300 | [diff] [blame] | 921 | iface_fields = ("name", "mac-address", "position", "ip-address") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 922 | vdu_iface = { |
| 923 | x: iface[x] for x in iface_fields if iface.get(x) is not None |
| 924 | } |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 925 | |
| 926 | vdu_iface["internal-connection-point-ref"] = vdu_icp["id"] |
| sousaedu | 003844e | 2021-03-02 00:19:15 +0100 | [diff] [blame] | 927 | if "port-security-enabled" in icp: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 928 | vdu_iface["port-security-enabled"] = icp[ |
| 929 | "port-security-enabled" |
| 930 | ] |
| sousaedu | 003844e | 2021-03-02 00:19:15 +0100 | [diff] [blame] | 931 | |
| 932 | if "port-security-disable-strategy" in icp: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 933 | vdu_iface["port-security-disable-strategy"] = icp[ |
| 934 | "port-security-disable-strategy" |
| 935 | ] |
| sousaedu | 003844e | 2021-03-02 00:19:15 +0100 | [diff] [blame] | 936 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 937 | for ext_cp in vnfd.get("ext-cpd", ()): |
| 938 | if not ext_cp.get("int-cpd"): |
| 939 | continue |
| 940 | if ext_cp["int-cpd"].get("vdu-id") != vdu["id"]: |
| 941 | continue |
| 942 | if icp["id"] == ext_cp["int-cpd"].get("cpd"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 943 | vdu_iface["external-connection-point-ref"] = ext_cp.get( |
| 944 | "id" |
| 945 | ) |
| sousaedu | 003844e | 2021-03-02 00:19:15 +0100 | [diff] [blame] | 946 | |
| 947 | if "port-security-enabled" in ext_cp: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 948 | vdu_iface["port-security-enabled"] = ext_cp[ |
| 949 | "port-security-enabled" |
| 950 | ] |
| sousaedu | 003844e | 2021-03-02 00:19:15 +0100 | [diff] [blame] | 951 | |
| 952 | if "port-security-disable-strategy" in ext_cp: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 953 | vdu_iface["port-security-disable-strategy"] = ext_cp[ |
| 954 | "port-security-disable-strategy" |
| 955 | ] |
| sousaedu | 003844e | 2021-03-02 00:19:15 +0100 | [diff] [blame] | 956 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 957 | break |
| 958 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 959 | if ( |
| 960 | vnfd_mgmt_cp |
| 961 | and vdu_iface.get("external-connection-point-ref") |
| 962 | == vnfd_mgmt_cp |
| 963 | ): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 964 | vdu_iface["mgmt-vnf"] = True |
| bravof | f3c3955 | 2021-02-24 17:22:24 -0300 | [diff] [blame] | 965 | vdu_iface["mgmt-interface"] = True |
| 966 | |
| 967 | for ecp in vdu_mgmt_cp: |
| 968 | if vdu_iface.get("external-connection-point-ref") == ecp: |
| 969 | vdu_iface["mgmt-interface"] = True |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 970 | |
| 971 | if iface.get("virtual-interface"): |
| 972 | vdu_iface.update(deepcopy(iface["virtual-interface"])) |
| 973 | |
| 974 | # look for network where this interface is connected |
| 975 | iface_ext_cp = vdu_iface.get("external-connection-point-ref") |
| 976 | if iface_ext_cp: |
| 977 | # TODO: Change for multiple df support |
| 978 | for df in get_iterable(nsd.get("df")): |
| 979 | for vnf_profile in get_iterable(df.get("vnf-profile")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 980 | for vlc_index, vlc in enumerate( |
| 981 | get_iterable( |
| 982 | vnf_profile.get("virtual-link-connectivity") |
| 983 | ) |
| 984 | ): |
| 985 | for cpd in get_iterable( |
| 986 | vlc.get("constituent-cpd-id") |
| 987 | ): |
| 988 | if ( |
| 989 | cpd.get("constituent-cpd-id") |
| 990 | == iface_ext_cp |
| Pedro Escaleira | 4606e4a | 2023-05-31 14:32:17 +0100 | [diff] [blame] | 991 | ) and vnf_profile.get("id") == vnf_index: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 992 | vdu_iface["ns-vld-id"] = vlc.get( |
| 993 | "virtual-link-profile-id" |
| 994 | ) |
| garciadeblas | 61c9591 | 2021-02-12 11:23:50 +0000 | [diff] [blame] | 995 | # if iface type is SRIOV or PASSTHROUGH, set pci-interfaces flag to True |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 996 | if vdu_iface.get("type") in ( |
| 997 | "SR-IOV", |
| 998 | "PCI-PASSTHROUGH", |
| 999 | ): |
| 1000 | nsr_descriptor["vld"][vlc_index][ |
| 1001 | "pci-interfaces" |
| 1002 | ] = True |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1003 | break |
| 1004 | elif vdu_iface.get("internal-connection-point-ref"): |
| 1005 | vdu_iface["vnf-vld-id"] = icp.get("int-virtual-link-desc") |
| garciadeblas | 61c9591 | 2021-02-12 11:23:50 +0000 | [diff] [blame] | 1006 | # TODO: store fixed IP address in the record (if it exists in the ICP) |
| 1007 | # if iface type is SRIOV or PASSTHROUGH, set pci-interfaces flag to True |
| 1008 | if vdu_iface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1009 | ivld_index = utils.find_index_in_list( |
| 1010 | vnfd.get("int-virtual-link-desc", ()), |
| 1011 | lambda ivld: ivld["id"] |
| 1012 | == icp.get("int-virtual-link-desc"), |
| 1013 | ) |
| garciadeblas | 61c9591 | 2021-02-12 11:23:50 +0000 | [diff] [blame] | 1014 | vnfr_descriptor["vld"][ivld_index]["pci-interfaces"] = True |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1015 | |
| 1016 | vdur["interfaces"].append(vdu_iface) |
| 1017 | |
| 1018 | if vdu.get("sw-image-desc"): |
| 1019 | sw_image = utils.find_in_list( |
| 1020 | vnfd.get("sw-image-desc", ()), |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1021 | lambda image: image["id"] == vdu.get("sw-image-desc"), |
| 1022 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1023 | nsr_sw_image_data = utils.find_in_list( |
| 1024 | nsr_descriptor["image"], |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1025 | lambda nsr_image: (nsr_image.get("image") == sw_image.get("image")), |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1026 | ) |
| 1027 | vdur["ns-image-id"] = nsr_sw_image_data["id"] |
| 1028 | |
| lloretgalleg | 28c13b6 | 2021-02-08 11:48:48 +0000 | [diff] [blame] | 1029 | if vdu.get("alternative-sw-image-desc"): |
| 1030 | alt_image_ids = [] |
| 1031 | for alt_image_id in vdu.get("alternative-sw-image-desc", ()): |
| 1032 | sw_image = utils.find_in_list( |
| 1033 | vnfd.get("sw-image-desc", ()), |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1034 | lambda image: image["id"] == alt_image_id, |
| 1035 | ) |
| lloretgalleg | 28c13b6 | 2021-02-08 11:48:48 +0000 | [diff] [blame] | 1036 | nsr_sw_image_data = utils.find_in_list( |
| 1037 | nsr_descriptor["image"], |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1038 | lambda nsr_image: ( |
| 1039 | nsr_image.get("image") == sw_image.get("image") |
| 1040 | ), |
| lloretgalleg | 28c13b6 | 2021-02-08 11:48:48 +0000 | [diff] [blame] | 1041 | ) |
| 1042 | alt_image_ids.append(nsr_sw_image_data["id"]) |
| 1043 | vdur["alt-image-ids"] = alt_image_ids |
| 1044 | |
| elumalai | 99078a9 | 2022-07-05 17:53:59 +0530 | [diff] [blame] | 1045 | revision = revision if revision is not None else 1 |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1046 | flavor_data_name = ( |
| 1047 | vdu["id"][:56] + "-" + vnf_index + "-" + str(revision) + "-flv" |
| 1048 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1049 | nsr_flavor_desc = utils.find_in_list( |
| 1050 | nsr_descriptor["flavor"], |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1051 | lambda flavor: flavor["name"] == flavor_data_name, |
| 1052 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1053 | |
| 1054 | if nsr_flavor_desc: |
| 1055 | vdur["ns-flavor-id"] = nsr_flavor_desc["id"] |
| 1056 | |
| vegall | 18101ea | 2023-03-06 13:49:21 +0000 | [diff] [blame] | 1057 | # Adding Shared Volume information to vdur |
| 1058 | if vdur.get("virtual-storages"): |
| 1059 | nsr_sv = [] |
| 1060 | for vsd in vdur["virtual-storages"]: |
| 1061 | if vsd.get("vdu-storage-requirements"): |
| 1062 | if ( |
| 1063 | vsd["vdu-storage-requirements"][0].get("key") |
| 1064 | == "multiattach" |
| 1065 | and vsd["vdu-storage-requirements"][0].get("value") |
| 1066 | == "True" |
| 1067 | ): |
| 1068 | nsr_sv.append(vsd["id"]) |
| 1069 | if nsr_sv: |
| 1070 | vdur["shared-volumes-id"] = nsr_sv |
| 1071 | |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 1072 | # Adding Affinity groups information to vdur |
| 1073 | try: |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 1074 | vdu_profile_affinity_group = utils.find_in_list( |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 1075 | vnfd.get("df")[0]["vdu-profile"], |
| 1076 | lambda a_vdu: a_vdu["id"] == vdu["id"], |
| 1077 | ) |
| 1078 | except Exception: |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 1079 | vdu_profile_affinity_group = None |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 1080 | |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 1081 | if vdu_profile_affinity_group: |
| 1082 | affinity_group_ids = [] |
| 1083 | for affinity_group in vdu_profile_affinity_group.get( |
| 1084 | "affinity-or-anti-affinity-group", () |
| 1085 | ): |
| 1086 | vdu_affinity_group = utils.find_in_list( |
| 1087 | vdu_profile_affinity_group.get( |
| 1088 | "affinity-or-anti-affinity-group", () |
| 1089 | ), |
| 1090 | lambda ag_fp: ag_fp["id"] == affinity_group["id"], |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 1091 | ) |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 1092 | nsr_affinity_group = utils.find_in_list( |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 1093 | nsr_descriptor["affinity-or-anti-affinity-group"], |
| 1094 | lambda nsr_ag: ( |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 1095 | nsr_ag.get("ag-id") == vdu_affinity_group.get("id") |
| 1096 | and nsr_ag.get("member-vnf-index") |
| 1097 | == vnfr_descriptor.get("member-vnf-index-ref") |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 1098 | ), |
| 1099 | ) |
| Alexis Romero | ee31f53 | 2022-04-26 19:10:21 +0200 | [diff] [blame] | 1100 | # Update Affinity Group VIM name if VDU instantiation parameter is present |
| 1101 | if vnf_params and vnf_params.get("affinity-or-anti-affinity-group"): |
| 1102 | vnf_params_affinity_group = utils.find_in_list( |
| 1103 | vnf_params["affinity-or-anti-affinity-group"], |
| 1104 | lambda vnfp_ag: ( |
| 1105 | vnfp_ag.get("id") == vdu_affinity_group.get("id") |
| 1106 | ), |
| 1107 | ) |
| 1108 | if vnf_params_affinity_group.get("vim-affinity-group-id"): |
| 1109 | nsr_affinity_group[ |
| 1110 | "vim-affinity-group-id" |
| 1111 | ] = vnf_params_affinity_group["vim-affinity-group-id"] |
| 1112 | affinity_group_ids.append(nsr_affinity_group["id"]) |
| 1113 | vdur["affinity-or-anti-affinity-group-id"] = affinity_group_ids |
| Alexis Romero | 03fb584 | 2022-03-11 15:53:40 +0100 | [diff] [blame] | 1114 | |
| bravof | 4ca5152 | 2021-04-22 10:03:02 -0400 | [diff] [blame] | 1115 | if vdu_instantiation_level: |
| 1116 | count = vdu_instantiation_level.get("number-of-instances") |
| 1117 | else: |
| 1118 | count = 1 |
| 1119 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1120 | for index in range(0, count): |
| 1121 | vdur = deepcopy(vdur) |
| 1122 | for iface in vdur["interfaces"]: |
| bravof | b7cdee1 | 2021-07-01 09:32:30 -0400 | [diff] [blame] | 1123 | if iface.get("ip-address") and index != 0: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1124 | iface["ip-address"] = increment_ip_mac(iface["ip-address"]) |
| bravof | b7cdee1 | 2021-07-01 09:32:30 -0400 | [diff] [blame] | 1125 | if iface.get("mac-address") and index != 0: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1126 | iface["mac-address"] = increment_ip_mac(iface["mac-address"]) |
| 1127 | |
| 1128 | vdur["_id"] = str(uuid4()) |
| 1129 | vdur["id"] = vdur["_id"] |
| 1130 | vdur["count-index"] = index |
| 1131 | vnfr_descriptor["vdur"].append(vdur) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1132 | return vnfr_descriptor |
| 1133 | |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1134 | def vca_status_refresh(self, session, ns_instance_content, filter_q): |
| 1135 | """ |
| 1136 | vcaStatus in ns_instance_content maybe stale, check if it is stale and create lcm op |
| 1137 | to refresh vca status by sending message to LCM when it is stale. Ignore otherwise. |
| 1138 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1139 | :param ns_instance_content: ns instance content |
| 1140 | :param filter_q: dict: query parameter containing vcaStatus-refresh as true or false |
| 1141 | :return: None |
| 1142 | """ |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1143 | time_now, time_delta = ( |
| 1144 | time(), |
| 1145 | time() - ns_instance_content["_admin"]["modified"], |
| 1146 | ) |
| 1147 | force_refresh = ( |
| 1148 | isinstance(filter_q, dict) and filter_q.get("vcaStatusRefresh") == "true" |
| 1149 | ) |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1150 | threshold_reached = time_delta > 120 |
| 1151 | if force_refresh or threshold_reached: |
| 1152 | operation, _id = "vca_status_refresh", ns_instance_content["_id"] |
| 1153 | ns_instance_content["_admin"]["modified"] = time_now |
| 1154 | self.db.set_one(self.topic, {"_id": _id}, ns_instance_content) |
| 1155 | nslcmop_desc = NsLcmOpTopic._create_nslcmop(_id, operation, None) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1156 | self.format_on_new( |
| 1157 | nslcmop_desc, session["project_id"], make_public=session["public"] |
| 1158 | ) |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 1159 | nslcmop_desc["_admin"].pop("nsState") |
| 1160 | self.msg.write("ns", operation, nslcmop_desc) |
| 1161 | return |
| 1162 | |
| 1163 | def show(self, session, _id, filter_q=None, api_req=False): |
| 1164 | """ |
| 1165 | Get complete information on an ns instance. |
| 1166 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1167 | :param _id: string, ns instance id |
| 1168 | :param filter_q: dict: query parameter containing vcaStatusRefresh as true or false |
| 1169 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| 1170 | :return: dictionary, raise exception if not found. |
| 1171 | """ |
| 1172 | ns_instance_content = super().show(session, _id, api_req) |
| 1173 | self.vca_status_refresh(session, ns_instance_content, filter_q) |
| 1174 | return ns_instance_content |
| 1175 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1176 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1177 | raise EngineException( |
| 1178 | "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 1179 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1180 | |
| 1181 | |
| 1182 | class VnfrTopic(BaseTopic): |
| 1183 | topic = "vnfrs" |
| 1184 | topic_msg = None |
| 1185 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1186 | def __init__(self, db, fs, msg, auth): |
| 1187 | BaseTopic.__init__(self, db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1188 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 1189 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1190 | raise EngineException( |
| 1191 | "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 1192 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1193 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1194 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1195 | raise EngineException( |
| 1196 | "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 1197 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1198 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1199 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1200 | # Not used because vnfrs are created and deleted by NsrTopic class directly |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1201 | raise EngineException( |
| 1202 | "Method new called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 1203 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1204 | |
| 1205 | |
| 1206 | class NsLcmOpTopic(BaseTopic): |
| 1207 | topic = "nslcmops" |
| 1208 | topic_msg = "ns" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1209 | operation_schema = { # mapping between operation and jsonschema to validate |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1210 | "instantiate": ns_instantiate, |
| 1211 | "action": ns_action, |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 1212 | "update": ns_update, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1213 | "scale": ns_scale, |
| garciadeblas | 0964edf | 2022-02-11 00:43:44 +0100 | [diff] [blame] | 1214 | "heal": ns_heal, |
| tierno | 1c38f2f | 2020-03-24 11:51:39 +0000 | [diff] [blame] | 1215 | "terminate": ns_terminate, |
| elumalai | 8e3806c | 2022-04-28 17:26:24 +0530 | [diff] [blame] | 1216 | "migrate": ns_migrate, |
| govindarajul | 519da48 | 2022-04-29 19:05:22 +0530 | [diff] [blame] | 1217 | "verticalscale": ns_verticalscale, |
| Gabriel Cuba | 84a60df | 2023-10-30 14:01:54 -0500 | [diff] [blame] | 1218 | "cancel": nslcmop_cancel, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 1221 | def __init__(self, db, fs, msg, auth): |
| 1222 | BaseTopic.__init__(self, db, fs, msg, auth) |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 1223 | self.nsrtopic = NsrTopic(db, fs, msg, auth) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1224 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1225 | def _check_ns_operation(self, session, nsr, operation, indata): |
| 1226 | """ |
| 1227 | Check that user has enter right parameters for the operation |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1228 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| garciadeblas | 0964edf | 2022-02-11 00:43:44 +0100 | [diff] [blame] | 1229 | :param operation: it can be: instantiate, terminate, action, update, heal |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1230 | :param indata: descriptor with the parameters of the operation |
| 1231 | :return: None |
| 1232 | """ |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1233 | if operation == "action": |
| 1234 | self._check_action_ns_operation(indata, nsr) |
| 1235 | elif operation == "scale": |
| 1236 | self._check_scale_ns_operation(indata, nsr) |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 1237 | elif operation == "update": |
| 1238 | self._check_update_ns_operation(indata, nsr) |
| garciadeblas | 0964edf | 2022-02-11 00:43:44 +0100 | [diff] [blame] | 1239 | elif operation == "heal": |
| 1240 | self._check_heal_ns_operation(indata, nsr) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1241 | elif operation == "instantiate": |
| 1242 | self._check_instantiate_ns_operation(indata, nsr, session) |
| 1243 | |
| 1244 | def _check_action_ns_operation(self, indata, nsr): |
| 1245 | nsd = nsr["nsd"] |
| 1246 | # check vnf_member_index |
| 1247 | if indata.get("vnf_member_index"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1248 | indata["member_vnf_index"] = indata.pop( |
| 1249 | "vnf_member_index" |
| 1250 | ) # for backward compatibility |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1251 | if indata.get("member_vnf_index"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1252 | vnfd = self._get_vnfd_from_vnf_member_index( |
| 1253 | indata["member_vnf_index"], nsr["_id"] |
| 1254 | ) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1255 | try: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1256 | configs = vnfd.get("df")[0]["lcm-operations-configuration"][ |
| 1257 | "operate-vnf-op-config" |
| 1258 | ]["day1-2"] |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1259 | except Exception: |
| 1260 | configs = [] |
| 1261 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1262 | if indata.get("vdu_id"): |
| 1263 | self._check_valid_vdu(vnfd, indata["vdu_id"]) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1264 | descriptor_configuration = utils.find_in_list( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1265 | configs, lambda config: config["id"] == indata["vdu_id"] |
| limon | 9b33fa8 | 2021-03-17 13:24:00 +0100 | [diff] [blame] | 1266 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1267 | elif indata.get("kdu_name"): |
| 1268 | self._check_valid_kdu(vnfd, indata["kdu_name"]) |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1269 | descriptor_configuration = utils.find_in_list( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1270 | configs, lambda config: config["id"] == indata.get("kdu_name") |
| limon | 9b33fa8 | 2021-03-17 13:24:00 +0100 | [diff] [blame] | 1271 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1272 | else: |
| bravof | 41a5205 | 2021-02-17 18:08:01 -0300 | [diff] [blame] | 1273 | descriptor_configuration = utils.find_in_list( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1274 | configs, lambda config: config["id"] == vnfd["id"] |
| limon | 9b33fa8 | 2021-03-17 13:24:00 +0100 | [diff] [blame] | 1275 | ) |
| 1276 | if descriptor_configuration is not None: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1277 | descriptor_configuration = descriptor_configuration.get( |
| 1278 | "config-primitive" |
| 1279 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1280 | else: # use a NSD |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1281 | descriptor_configuration = nsd.get("ns-configuration", {}).get( |
| 1282 | "config-primitive" |
| 1283 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1284 | |
| 1285 | # For k8s allows default primitives without validating the parameters |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1286 | if indata.get("kdu_name") and indata["primitive"] in ( |
| 1287 | "upgrade", |
| 1288 | "rollback", |
| 1289 | "status", |
| 1290 | "inspect", |
| 1291 | "readme", |
| 1292 | ): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1293 | # TODO should be checked that rollback only can contains revsision_numbe???? |
| 1294 | if not indata.get("member_vnf_index"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1295 | raise EngineException( |
| 1296 | "Missing action parameter 'member_vnf_index' for default KDU primitive '{}'".format( |
| 1297 | indata["primitive"] |
| 1298 | ) |
| 1299 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1300 | return |
| 1301 | # if not, check primitive |
| 1302 | for config_primitive in get_iterable(descriptor_configuration): |
| 1303 | if indata["primitive"] == config_primitive["name"]: |
| 1304 | # check needed primitive_params are provided |
| 1305 | if indata.get("primitive_params"): |
| 1306 | in_primitive_params_copy = copy(indata["primitive_params"]) |
| 1307 | else: |
| 1308 | in_primitive_params_copy = {} |
| 1309 | for paramd in get_iterable(config_primitive.get("parameter")): |
| 1310 | if paramd["name"] in in_primitive_params_copy: |
| 1311 | del in_primitive_params_copy[paramd["name"]] |
| 1312 | elif not paramd.get("default-value"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1313 | raise EngineException( |
| 1314 | "Needed parameter {} not provided for primitive '{}'".format( |
| 1315 | paramd["name"], indata["primitive"] |
| 1316 | ) |
| 1317 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1318 | # check no extra primitive params are provided |
| 1319 | if in_primitive_params_copy: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1320 | raise EngineException( |
| 1321 | "parameter/s '{}' not present at vnfd /nsd for primitive '{}'".format( |
| 1322 | list(in_primitive_params_copy.keys()), indata["primitive"] |
| 1323 | ) |
| 1324 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1325 | break |
| 1326 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1327 | raise EngineException( |
| 1328 | "Invalid primitive '{}' is not present at vnfd/nsd".format( |
| 1329 | indata["primitive"] |
| 1330 | ) |
| 1331 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1332 | |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 1333 | def _check_update_ns_operation(self, indata, nsr) -> None: |
| 1334 | """Validates the ns-update request according to updateType |
| 1335 | |
| 1336 | If updateType is CHANGE_VNFPKG: |
| 1337 | - it checks the vnfInstanceId, whether it's available under ns instance |
| 1338 | - it checks the vnfdId whether it matches with the vnfd-id in the vnf-record of specified VNF. |
| 1339 | Otherwise exception will be raised. |
| elumalai | 6380e7c | 2022-04-28 00:15:59 +0530 | [diff] [blame] | 1340 | If updateType is REMOVE_VNF: |
| 1341 | - it checks if the vnfInstanceId is available in the ns instance |
| 1342 | - Otherwise exception will be raised. |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 1343 | |
| 1344 | Args: |
| 1345 | indata: includes updateType such as CHANGE_VNFPKG, |
| 1346 | nsr: network service record |
| 1347 | |
| 1348 | Raises: |
| 1349 | EngineException: |
| 1350 | a meaningful error if given update parameters are not proper such as |
| 1351 | "Error in validating ns-update request: <ID> does not match |
| 1352 | with the vnfd-id of vnfinstance |
| 1353 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY" |
| 1354 | |
| 1355 | """ |
| 1356 | try: |
| 1357 | if indata["updateType"] == "CHANGE_VNFPKG": |
| 1358 | # vnfInstanceId, nsInstanceId, vnfdId are mandatory |
| 1359 | vnf_instance_id = indata["changeVnfPackageData"]["vnfInstanceId"] |
| 1360 | ns_instance_id = indata["nsInstanceId"] |
| 1361 | vnfd_id_2update = indata["changeVnfPackageData"]["vnfdId"] |
| 1362 | |
| 1363 | if vnf_instance_id not in nsr["constituent-vnfr-ref"]: |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 1364 | raise EngineException( |
| 1365 | f"Error in validating ns-update request: vnf {vnf_instance_id} does not " |
| 1366 | f"belong to NS {ns_instance_id}", |
| 1367 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1368 | ) |
| 1369 | |
| 1370 | # Getting vnfrs through the ns_instance_id |
| 1371 | vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_instance_id}) |
| 1372 | constituent_vnfd_id = next( |
| 1373 | ( |
| 1374 | vnfr["vnfd-id"] |
| 1375 | for vnfr in vnfrs |
| 1376 | if vnfr["id"] == vnf_instance_id |
| 1377 | ), |
| 1378 | None, |
| 1379 | ) |
| 1380 | |
| 1381 | # Check the given vnfd-id belongs to given vnf instance |
| 1382 | if constituent_vnfd_id and (vnfd_id_2update != constituent_vnfd_id): |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 1383 | raise EngineException( |
| 1384 | f"Error in validating ns-update request: vnfd-id {vnfd_id_2update} does not " |
| 1385 | f"match with the vnfd-id: {constituent_vnfd_id} of VNF instance: {vnf_instance_id}", |
| 1386 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1387 | ) |
| 1388 | |
| 1389 | # Validating the ns update timeout |
| 1390 | if ( |
| 1391 | indata.get("timeout_ns_update") |
| 1392 | and indata["timeout_ns_update"] < 300 |
| 1393 | ): |
| 1394 | raise EngineException( |
| 1395 | "Error in validating ns-update request: {} second is not enough " |
| 1396 | "to upgrade the VNF instance: {}".format( |
| 1397 | indata["timeout_ns_update"], vnf_instance_id |
| 1398 | ), |
| 1399 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1400 | ) |
| elumalai | 6380e7c | 2022-04-28 00:15:59 +0530 | [diff] [blame] | 1401 | elif indata["updateType"] == "REMOVE_VNF": |
| 1402 | vnf_instance_id = indata["removeVnfInstanceId"] |
| 1403 | ns_instance_id = indata["nsInstanceId"] |
| 1404 | if vnf_instance_id not in nsr["constituent-vnfr-ref"]: |
| 1405 | raise EngineException( |
| 1406 | "Invalid VNF Instance Id. '{}' is not " |
| 1407 | "present in the NS '{}'".format(vnf_instance_id, ns_instance_id) |
| 1408 | ) |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 1409 | |
| 1410 | except ( |
| 1411 | DbException, |
| 1412 | AttributeError, |
| 1413 | IndexError, |
| 1414 | KeyError, |
| 1415 | ValueError, |
| 1416 | ) as e: |
| 1417 | raise type(e)( |
| 1418 | "Ns update request could not be processed with error: {}.".format(e) |
| 1419 | ) |
| 1420 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1421 | def _check_scale_ns_operation(self, indata, nsr): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1422 | vnfd = self._get_vnfd_from_vnf_member_index( |
| 1423 | indata["scaleVnfData"]["scaleByStepData"]["member-vnf-index"], nsr["_id"] |
| 1424 | ) |
| lloretgalleg | df9fd61 | 2020-12-01 12:51:52 +0000 | [diff] [blame] | 1425 | for scaling_aspect in get_iterable(vnfd.get("df", ())[0]["scaling-aspect"]): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1426 | if ( |
| 1427 | indata["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"] |
| 1428 | == scaling_aspect["id"] |
| 1429 | ): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1430 | break |
| 1431 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1432 | raise EngineException( |
| 1433 | "Invalid scaleVnfData:scaleByStepData:scaling-group-descriptor '{}' is not " |
| 1434 | "present at vnfd:scaling-aspect".format( |
| 1435 | indata["scaleVnfData"]["scaleByStepData"][ |
| 1436 | "scaling-group-descriptor" |
| 1437 | ] |
| 1438 | ) |
| 1439 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1440 | |
| garciadeblas | 0964edf | 2022-02-11 00:43:44 +0100 | [diff] [blame] | 1441 | def _check_heal_ns_operation(self, indata, nsr): |
| 1442 | return |
| 1443 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1444 | def _check_instantiate_ns_operation(self, indata, nsr, session): |
| tierno | 982da4e | 2019-09-03 11:51:55 +0000 | [diff] [blame] | 1445 | vnf_member_index_to_vnfd = {} # map between vnf_member_index to vnf descriptor. |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1446 | vim_accounts = [] |
| tierno | 4f9d4ae | 2019-03-20 17:24:11 +0000 | [diff] [blame] | 1447 | wim_accounts = [] |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1448 | nsd = nsr["nsd"] |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1449 | self._check_valid_vim_account(indata["vimAccountId"], vim_accounts, session) |
| 1450 | self._check_valid_wim_account(indata.get("wimAccountId"), wim_accounts, session) |
| 1451 | for in_vnf in get_iterable(indata.get("vnf")): |
| 1452 | member_vnf_index = in_vnf["member-vnf-index"] |
| tierno | 982da4e | 2019-09-03 11:51:55 +0000 | [diff] [blame] | 1453 | if vnf_member_index_to_vnfd.get(member_vnf_index): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1454 | vnfd = vnf_member_index_to_vnfd[member_vnf_index] |
| tierno | 260dd6f | 2019-09-02 10:48:56 +0000 | [diff] [blame] | 1455 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1456 | vnfd = self._get_vnfd_from_vnf_member_index( |
| 1457 | member_vnf_index, nsr["_id"] |
| 1458 | ) |
| 1459 | vnf_member_index_to_vnfd[ |
| 1460 | member_vnf_index |
| 1461 | ] = vnfd # add to cache, avoiding a later look for |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1462 | self._check_vnf_instantiation_params(in_vnf, vnfd) |
| 1463 | if in_vnf.get("vimAccountId"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1464 | self._check_valid_vim_account( |
| 1465 | in_vnf["vimAccountId"], vim_accounts, session |
| 1466 | ) |
| tierno | 260dd6f | 2019-09-02 10:48:56 +0000 | [diff] [blame] | 1467 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1468 | for in_vld in get_iterable(indata.get("vld")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1469 | self._check_valid_wim_account( |
| 1470 | in_vld.get("wimAccountId"), wim_accounts, session |
| 1471 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1472 | for vldd in get_iterable(nsd.get("virtual-link-desc")): |
| 1473 | if in_vld["name"] == vldd["id"]: |
| 1474 | break |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1475 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1476 | raise EngineException( |
| 1477 | "Invalid parameter vld:name='{}' is not present at nsd:vld".format( |
| 1478 | in_vld["name"] |
| 1479 | ) |
| 1480 | ) |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1481 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1482 | def _get_vnfd_from_vnf_member_index(self, member_vnf_index, nsr_id): |
| 1483 | # Obtain vnf descriptor. The vnfr is used to get the vnfd._id used for this member_vnf_index |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1484 | vnfr = self.db.get_one( |
| 1485 | "vnfrs", |
| 1486 | {"nsr-id-ref": nsr_id, "member-vnf-index-ref": member_vnf_index}, |
| 1487 | fail_on_empty=False, |
| 1488 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1489 | if not vnfr: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1490 | raise EngineException( |
| 1491 | "Invalid parameter member_vnf_index='{}' is not one of the " |
| 1492 | "nsd:constituent-vnfd".format(member_vnf_index) |
| 1493 | ) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 1494 | |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1495 | # Backwards compatibility: if there is no revision, get it from the one and only VNFD entry |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 1496 | if "revision" in vnfr: |
| 1497 | vnfd_revision = vnfr["vnfd-id"] + ":" + str(vnfr["revision"]) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1498 | vnfd = self.db.get_one( |
| 1499 | "vnfds_revisions", {"_id": vnfd_revision}, fail_on_empty=False |
| 1500 | ) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 1501 | else: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1502 | vnfd = self.db.get_one( |
| 1503 | "vnfds", {"_id": vnfr["vnfd-id"]}, fail_on_empty=False |
| 1504 | ) |
| beierlm | cee2ebf | 2022-03-29 17:42:48 -0400 | [diff] [blame] | 1505 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1506 | if not vnfd: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1507 | raise EngineException( |
| 1508 | "vnfd id={} has been deleted!. Operation cannot be performed".format( |
| 1509 | vnfr["vnfd-id"] |
| 1510 | ) |
| 1511 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1512 | return vnfd |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 1513 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1514 | def _check_valid_vdu(self, vnfd, vdu_id): |
| 1515 | for vdud in get_iterable(vnfd.get("vdu")): |
| 1516 | if vdud["id"] == vdu_id: |
| 1517 | return vdud |
| 1518 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1519 | raise EngineException( |
| 1520 | "Invalid parameter vdu_id='{}' not present at vnfd:vdu:id".format( |
| 1521 | vdu_id |
| 1522 | ) |
| 1523 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1524 | |
| 1525 | def _check_valid_kdu(self, vnfd, kdu_name): |
| 1526 | for kdud in get_iterable(vnfd.get("kdu")): |
| 1527 | if kdud["name"] == kdu_name: |
| 1528 | return kdud |
| 1529 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1530 | raise EngineException( |
| 1531 | "Invalid parameter kdu_name='{}' not present at vnfd:kdu:name".format( |
| 1532 | kdu_name |
| 1533 | ) |
| 1534 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1535 | |
| 1536 | def _check_vnf_instantiation_params(self, in_vnf, vnfd): |
| 1537 | for in_vdu in get_iterable(in_vnf.get("vdu")): |
| 1538 | for vdu in get_iterable(vnfd.get("vdu")): |
| 1539 | if in_vdu["id"] == vdu["id"]: |
| 1540 | for volume in get_iterable(in_vdu.get("volume")): |
| 1541 | for volumed in get_iterable(vdu.get("virtual-storage-desc")): |
| aticig | d7753fc | 2022-05-18 18:55:23 +0300 | [diff] [blame] | 1542 | if volumed == volume["name"]: |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1543 | break |
| 1544 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1545 | raise EngineException( |
| 1546 | "Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:" |
| 1547 | "volume:name='{}' is not present at " |
| 1548 | "vnfd:vdu:virtual-storage-desc list".format( |
| 1549 | in_vnf["member-vnf-index"], |
| 1550 | in_vdu["id"], |
| 1551 | volume["id"], |
| 1552 | ) |
| 1553 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1554 | |
| 1555 | vdu_if_names = set() |
| 1556 | for cpd in get_iterable(vdu.get("int-cpd")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1557 | for iface in get_iterable( |
| 1558 | cpd.get("virtual-network-interface-requirement") |
| 1559 | ): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1560 | vdu_if_names.add(iface.get("name")) |
| 1561 | |
| aticig | d7753fc | 2022-05-18 18:55:23 +0300 | [diff] [blame] | 1562 | for in_iface in get_iterable(in_vdu.get("interface")): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1563 | if in_iface["name"] in vdu_if_names: |
| 1564 | break |
| 1565 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1566 | raise EngineException( |
| 1567 | "Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:" |
| 1568 | "int-cpd[id='{}'] is not present at vnfd:vdu:int-cpd".format( |
| 1569 | in_vnf["member-vnf-index"], |
| 1570 | in_vdu["id"], |
| 1571 | in_iface["name"], |
| 1572 | ) |
| 1573 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1574 | break |
| 1575 | |
| 1576 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1577 | raise EngineException( |
| 1578 | "Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}'] is not present " |
| 1579 | "at vnfd:vdu".format(in_vnf["member-vnf-index"], in_vdu["id"]) |
| 1580 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1581 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1582 | vnfd_ivlds_cpds = { |
| 1583 | ivld.get("id"): set() |
| 1584 | for ivld in get_iterable(vnfd.get("int-virtual-link-desc")) |
| 1585 | } |
| Gulsum Atici | 9af2a47 | 2023-03-28 17:50:48 +0300 | [diff] [blame] | 1586 | for vdu in vnfd.get("vdu", {}): |
| 1587 | for cpd in vdu.get("int-cpd", {}): |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1588 | if cpd.get("int-virtual-link-desc"): |
| 1589 | vnfd_ivlds_cpds[cpd.get("int-virtual-link-desc")] = cpd.get("id") |
| 1590 | |
| 1591 | for in_ivld in get_iterable(in_vnf.get("internal-vld")): |
| 1592 | if in_ivld.get("name") in vnfd_ivlds_cpds: |
| 1593 | for in_icp in get_iterable(in_ivld.get("internal-connection-point")): |
| 1594 | if in_icp["id-ref"] in vnfd_ivlds_cpds[in_ivld.get("name")]: |
| tierno | 40fbcad | 2018-10-26 10:58:15 +0200 | [diff] [blame] | 1595 | break |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1596 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1597 | raise EngineException( |
| 1598 | "Invalid parameter vnf[member-vnf-index='{}']:internal-vld[name" |
| 1599 | "='{}']:internal-connection-point[id-ref:'{}'] is not present at " |
| 1600 | "vnfd:internal-vld:name/id:internal-connection-point".format( |
| 1601 | in_vnf["member-vnf-index"], |
| 1602 | in_ivld["name"], |
| 1603 | in_icp["id-ref"], |
| 1604 | ) |
| 1605 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1606 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1607 | raise EngineException( |
| 1608 | "Invalid parameter vnf[member-vnf-index='{}']:internal-vld:name='{}'" |
| 1609 | " is not present at vnfd '{}'".format( |
| 1610 | in_vnf["member-vnf-index"], in_ivld["name"], vnfd["id"] |
| 1611 | ) |
| 1612 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1613 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1614 | def _check_valid_vim_account(self, vim_account, vim_accounts, session): |
| 1615 | if vim_account in vim_accounts: |
| 1616 | return |
| 1617 | try: |
| 1618 | db_filter = self._get_project_filter(session) |
| 1619 | db_filter["_id"] = vim_account |
| 1620 | self.db.get_one("vim_accounts", db_filter) |
| 1621 | except Exception: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1622 | raise EngineException( |
| 1623 | "Invalid vimAccountId='{}' not present for the project".format( |
| 1624 | vim_account |
| 1625 | ) |
| 1626 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1627 | vim_accounts.append(vim_account) |
| 1628 | |
| David Garcia | 98de298 | 2021-10-13 17:14:01 +0200 | [diff] [blame] | 1629 | def _get_vim_account(self, vim_id: str, session): |
| 1630 | try: |
| 1631 | db_filter = self._get_project_filter(session) |
| 1632 | db_filter["_id"] = vim_id |
| 1633 | return self.db.get_one("vim_accounts", db_filter) |
| 1634 | except Exception: |
| 1635 | raise EngineException( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1636 | "Invalid vimAccountId='{}' not present for the project".format(vim_id) |
| David Garcia | 98de298 | 2021-10-13 17:14:01 +0200 | [diff] [blame] | 1637 | ) |
| 1638 | |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1639 | def _check_valid_wim_account(self, wim_account, wim_accounts, session): |
| 1640 | if not isinstance(wim_account, str): |
| 1641 | return |
| 1642 | if wim_account in wim_accounts: |
| 1643 | return |
| 1644 | try: |
| gifrerenom | 44f5ec1 | 2022-03-07 16:57:25 +0000 | [diff] [blame] | 1645 | db_filter = self._get_project_filter(session) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1646 | db_filter["_id"] = wim_account |
| 1647 | self.db.get_one("wim_accounts", db_filter) |
| 1648 | except Exception: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1649 | raise EngineException( |
| 1650 | "Invalid wimAccountId='{}' not present for the project".format( |
| 1651 | wim_account |
| 1652 | ) |
| 1653 | ) |
| garciaale | 7cbd03c | 2020-11-27 10:38:35 -0300 | [diff] [blame] | 1654 | wim_accounts.append(wim_account) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1655 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1656 | def _look_for_pdu( |
| 1657 | self, session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback |
| 1658 | ): |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1659 | """ |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1660 | Look for a free PDU in the catalog matching vdur type and interfaces. Fills vnfr.vdur with the interface |
| 1661 | (ip_address, ...) information. |
| 1662 | Modifies PDU _admin.usageState to 'IN_USE' |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1663 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1664 | :param rollback: list with the database modifications to rollback if needed |
| 1665 | :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found |
| 1666 | :param vim_account: vim_account where this vnfr should be deployed |
| 1667 | :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr |
| 1668 | :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback |
| 1669 | of the changed vnfr is needed |
| 1670 | |
| 1671 | :return: List of PDU interfaces that are connected to an existing VIM network. Each item contains: |
| 1672 | "vim-network-name": used at VIM |
| 1673 | "name": interface name |
| 1674 | "vnf-vld-id": internal VNFD vld where this interface is connected, or |
| 1675 | "ns-vld-id": NSD vld where this interface is connected. |
| 1676 | NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1677 | """ |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1678 | |
| 1679 | ifaces_forcing_vim_network = [] |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1680 | for vdur_index, vdur in enumerate(get_iterable(vnfr.get("vdur"))): |
| 1681 | if not vdur.get("pdu-type"): |
| 1682 | continue |
| 1683 | pdu_type = vdur.get("pdu-type") |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 1684 | pdu_filter = self._get_project_filter(session) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1685 | pdu_filter["vim_accounts"] = vim_account |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1686 | pdu_filter["type"] = pdu_type |
| 1687 | pdu_filter["_admin.operationalState"] = "ENABLED" |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1688 | pdu_filter["_admin.usageState"] = "NOT_IN_USE" |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1689 | # TODO feature 1417: "shared": True, |
| 1690 | |
| 1691 | available_pdus = self.db.get_list("pdus", pdu_filter) |
| 1692 | for pdu in available_pdus: |
| 1693 | # step 1 check if this pdu contains needed interfaces: |
| 1694 | match_interfaces = True |
| 1695 | for vdur_interface in vdur["interfaces"]: |
| 1696 | for pdu_interface in pdu["interfaces"]: |
| 1697 | if pdu_interface["name"] == vdur_interface["name"]: |
| 1698 | # TODO feature 1417: match per mgmt type |
| 1699 | break |
| 1700 | else: # no interface found for name |
| 1701 | match_interfaces = False |
| 1702 | break |
| 1703 | if match_interfaces: |
| 1704 | break |
| 1705 | else: |
| 1706 | raise EngineException( |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1707 | "No PDU of type={} at vim_account={} found for member_vnf_index={}, vdu={} matching interface " |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1708 | "names".format( |
| 1709 | pdu_type, |
| 1710 | vim_account, |
| 1711 | vnfr["member-vnf-index-ref"], |
| 1712 | vdur["vdu-id-ref"], |
| 1713 | ) |
| 1714 | ) |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1715 | |
| 1716 | # step 2. Update pdu |
| 1717 | rollback_pdu = { |
| 1718 | "_admin.usageState": pdu["_admin"]["usageState"], |
| 1719 | "_admin.usage.vnfr_id": None, |
| 1720 | "_admin.usage.nsr_id": None, |
| 1721 | "_admin.usage.vdur": None, |
| 1722 | } |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1723 | self.db.set_one( |
| 1724 | "pdus", |
| 1725 | {"_id": pdu["_id"]}, |
| 1726 | { |
| 1727 | "_admin.usageState": "IN_USE", |
| 1728 | "_admin.usage": { |
| 1729 | "vnfr_id": vnfr["_id"], |
| 1730 | "nsr_id": vnfr["nsr-id-ref"], |
| 1731 | "vdur": vdur["vdu-id-ref"], |
| 1732 | }, |
| 1733 | }, |
| 1734 | ) |
| 1735 | rollback.append( |
| 1736 | { |
| 1737 | "topic": "pdus", |
| 1738 | "_id": pdu["_id"], |
| 1739 | "operation": "set", |
| 1740 | "content": rollback_pdu, |
| 1741 | } |
| 1742 | ) |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1743 | |
| 1744 | # step 3. Fill vnfr info by filling vdur |
| 1745 | vdu_text = "vdur.{}".format(vdur_index) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1746 | vnfr_update_rollback[vdu_text + ".pdu-id"] = None |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1747 | vnfr_update[vdu_text + ".pdu-id"] = pdu["_id"] |
| 1748 | for iface_index, vdur_interface in enumerate(vdur["interfaces"]): |
| 1749 | for pdu_interface in pdu["interfaces"]: |
| 1750 | if pdu_interface["name"] == vdur_interface["name"]: |
| 1751 | iface_text = vdu_text + ".interfaces.{}".format(iface_index) |
| 1752 | for k, v in pdu_interface.items(): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1753 | if k in ( |
| 1754 | "ip-address", |
| 1755 | "mac-address", |
| 1756 | ): # TODO: switch-xxxxx must be inserted |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1757 | vnfr_update[iface_text + ".{}".format(k)] = v |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1758 | vnfr_update_rollback[ |
| 1759 | iface_text + ".{}".format(k) |
| 1760 | ] = vdur_interface.get(v) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1761 | if pdu_interface.get("ip-address"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1762 | if vdur_interface.get( |
| 1763 | "mgmt-interface" |
| 1764 | ) or vdur_interface.get("mgmt-vnf"): |
| 1765 | vnfr_update_rollback[ |
| 1766 | vdu_text + ".ip-address" |
| 1767 | ] = vdur.get("ip-address") |
| 1768 | vnfr_update[vdu_text + ".ip-address"] = pdu_interface[ |
| 1769 | "ip-address" |
| 1770 | ] |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1771 | if vdur_interface.get("mgmt-vnf"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1772 | vnfr_update_rollback["ip-address"] = vnfr.get( |
| 1773 | "ip-address" |
| 1774 | ) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1775 | vnfr_update["ip-address"] = pdu_interface["ip-address"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1776 | vnfr_update[vdu_text + ".ip-address"] = pdu_interface[ |
| 1777 | "ip-address" |
| 1778 | ] |
| 1779 | if pdu_interface.get("vim-network-name") or pdu_interface.get( |
| 1780 | "vim-network-id" |
| 1781 | ): |
| 1782 | ifaces_forcing_vim_network.append( |
| 1783 | { |
| 1784 | "name": vdur_interface.get("vnf-vld-id") |
| 1785 | or vdur_interface.get("ns-vld-id"), |
| 1786 | "vnf-vld-id": vdur_interface.get("vnf-vld-id"), |
| 1787 | "ns-vld-id": vdur_interface.get("ns-vld-id"), |
| 1788 | } |
| 1789 | ) |
| gcalvino | 17d5b73 | 2018-12-17 16:26:21 +0100 | [diff] [blame] | 1790 | if pdu_interface.get("vim-network-id"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1791 | ifaces_forcing_vim_network[-1][ |
| 1792 | "vim-network-id" |
| 1793 | ] = pdu_interface["vim-network-id"] |
| gcalvino | 17d5b73 | 2018-12-17 16:26:21 +0100 | [diff] [blame] | 1794 | if pdu_interface.get("vim-network-name"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1795 | ifaces_forcing_vim_network[-1][ |
| 1796 | "vim-network-name" |
| 1797 | ] = pdu_interface["vim-network-name"] |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1798 | break |
| 1799 | |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 1800 | return ifaces_forcing_vim_network |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1801 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1802 | def _look_for_k8scluster( |
| 1803 | self, session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback |
| 1804 | ): |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1805 | """ |
| 1806 | Look for an available k8scluster for all the kuds in the vnfd matching version and cni requirements. |
| 1807 | Fills vnfr.kdur with the selected k8scluster |
| 1808 | |
| 1809 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 1810 | :param rollback: list with the database modifications to rollback if needed |
| 1811 | :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found |
| 1812 | :param vim_account: vim_account where this vnfr should be deployed |
| 1813 | :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr |
| 1814 | :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback |
| 1815 | of the changed vnfr is needed |
| 1816 | |
| 1817 | :return: List of KDU interfaces that are connected to an existing VIM network. Each item contains: |
| 1818 | "vim-network-name": used at VIM |
| 1819 | "name": interface name |
| 1820 | "vnf-vld-id": internal VNFD vld where this interface is connected, or |
| 1821 | "ns-vld-id": NSD vld where this interface is connected. |
| 1822 | NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None |
| 1823 | """ |
| 1824 | |
| 1825 | ifaces_forcing_vim_network = [] |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1826 | if not vnfr.get("kdur"): |
| 1827 | return ifaces_forcing_vim_network |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1828 | |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1829 | kdu_filter = self._get_project_filter(session) |
| 1830 | kdu_filter["vim_account"] = vim_account |
| 1831 | # TODO kdu_filter["_admin.operationalState"] = "ENABLED" |
| 1832 | available_k8sclusters = self.db.get_list("k8sclusters", kdu_filter) |
| 1833 | |
| 1834 | k8s_requirements = {} # just for logging |
| 1835 | for k8scluster in available_k8sclusters: |
| 1836 | if not vnfr.get("k8s-cluster"): |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1837 | break |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1838 | # restrict by cni |
| 1839 | if vnfr["k8s-cluster"].get("cni"): |
| 1840 | k8s_requirements["cni"] = vnfr["k8s-cluster"]["cni"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1841 | if not set(vnfr["k8s-cluster"]["cni"]).intersection( |
| 1842 | k8scluster.get("cni", ()) |
| 1843 | ): |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1844 | continue |
| 1845 | # restrict by version |
| 1846 | if vnfr["k8s-cluster"].get("version"): |
| 1847 | k8s_requirements["version"] = vnfr["k8s-cluster"]["version"] |
| 1848 | if k8scluster.get("k8s_version") not in vnfr["k8s-cluster"]["version"]: |
| 1849 | continue |
| 1850 | # restrict by number of networks |
| 1851 | if vnfr["k8s-cluster"].get("nets"): |
| 1852 | k8s_requirements["networks"] = len(vnfr["k8s-cluster"]["nets"]) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1853 | if not k8scluster.get("nets") or len(k8scluster["nets"]) < len( |
| 1854 | vnfr["k8s-cluster"]["nets"] |
| 1855 | ): |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1856 | continue |
| 1857 | break |
| 1858 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1859 | raise EngineException( |
| 1860 | "No k8scluster with requirements='{}' at vim_account={} found for member_vnf_index={}".format( |
| 1861 | k8s_requirements, vim_account, vnfr["member-vnf-index-ref"] |
| 1862 | ) |
| 1863 | ) |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1864 | |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1865 | for kdur_index, kdur in enumerate(get_iterable(vnfr.get("kdur"))): |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1866 | # step 3. Fill vnfr info by filling kdur |
| 1867 | kdu_text = "kdur.{}.".format(kdur_index) |
| 1868 | vnfr_update_rollback[kdu_text + "k8s-cluster.id"] = None |
| 1869 | vnfr_update[kdu_text + "k8s-cluster.id"] = k8scluster["_id"] |
| 1870 | |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1871 | # step 4. Check VIM networks that forces the selected k8s_cluster |
| 1872 | if vnfr.get("k8s-cluster") and vnfr["k8s-cluster"].get("nets"): |
| 1873 | k8scluster_net_list = list(k8scluster.get("nets").keys()) |
| 1874 | for net_index, kdur_net in enumerate(vnfr["k8s-cluster"]["nets"]): |
| 1875 | # get a network from k8s_cluster nets. If name matches use this, if not use other |
| 1876 | if kdur_net["id"] in k8scluster_net_list: # name matches |
| 1877 | vim_net = k8scluster["nets"][kdur_net["id"]] |
| 1878 | k8scluster_net_list.remove(kdur_net["id"]) |
| 1879 | else: |
| 1880 | vim_net = k8scluster["nets"][k8scluster_net_list[0]] |
| 1881 | k8scluster_net_list.pop(0) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1882 | vnfr_update_rollback[ |
| 1883 | "k8s-cluster.nets.{}.vim_net".format(net_index) |
| 1884 | ] = None |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1885 | vnfr_update["k8s-cluster.nets.{}.vim_net".format(net_index)] = vim_net |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 1886 | if vim_net and ( |
| 1887 | kdur_net.get("vnf-vld-id") or kdur_net.get("ns-vld-id") |
| 1888 | ): |
| 1889 | ifaces_forcing_vim_network.append( |
| 1890 | { |
| 1891 | "name": kdur_net.get("vnf-vld-id") |
| 1892 | or kdur_net.get("ns-vld-id"), |
| 1893 | "vnf-vld-id": kdur_net.get("vnf-vld-id"), |
| 1894 | "ns-vld-id": kdur_net.get("ns-vld-id"), |
| 1895 | "vim-network-name": vim_net, # TODO can it be vim-network-id ??? |
| 1896 | } |
| 1897 | ) |
| tierno | c67b0e9 | 2019-11-05 12:45:29 +0000 | [diff] [blame] | 1898 | # TODO check that this forcing is not incompatible with other forcing |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 1899 | return ifaces_forcing_vim_network |
| 1900 | |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1901 | def _update_vnfrs_from_nsd(self, nsr): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1902 | step = "Getting vnf_profiles from nsd" # first step must be defined outside try |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1903 | try: |
| 1904 | nsr_id = nsr["_id"] |
| 1905 | nsd = nsr["nsd"] |
| 1906 | |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1907 | vnf_profiles = nsd.get("df", [{}])[0].get("vnf-profile", ()) |
| 1908 | vld_fixed_ip_connection_point_data = {} |
| 1909 | |
| 1910 | step = "Getting ip-address info from vnf_profile if it exists" |
| 1911 | for vnfp in vnf_profiles: |
| 1912 | # Checking ip-address info from nsd.vnf_profile and storing |
| 1913 | for vlc in vnfp.get("virtual-link-connectivity", ()): |
| 1914 | for cpd in vlc.get("constituent-cpd-id", ()): |
| 1915 | if cpd.get("ip-address"): |
| 1916 | step = "Storing ip-address info" |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1917 | vld_fixed_ip_connection_point_data.update( |
| 1918 | { |
| 1919 | vlc.get("virtual-link-profile-id") |
| 1920 | + "." |
| 1921 | + cpd.get("constituent-base-element-id"): { |
| 1922 | "vnfd-connection-point-ref": cpd.get( |
| 1923 | "constituent-cpd-id" |
| 1924 | ), |
| 1925 | "ip-address": cpd.get("ip-address"), |
| 1926 | } |
| 1927 | } |
| 1928 | ) |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1929 | |
| 1930 | # Inserting ip address to vnfr |
| 1931 | if len(vld_fixed_ip_connection_point_data) > 0: |
| 1932 | step = "Getting vnfrs" |
| 1933 | vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| 1934 | for item in vld_fixed_ip_connection_point_data.keys(): |
| 1935 | step = "Filtering vnfrs" |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1936 | vnfr = next( |
| 1937 | filter( |
| 1938 | lambda vnfr: vnfr["member-vnf-index-ref"] |
| 1939 | == item.split(".")[1], |
| 1940 | vnfrs, |
| 1941 | ), |
| 1942 | None, |
| 1943 | ) |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1944 | if vnfr: |
| 1945 | vnfr_update = {} |
| 1946 | for vdur_index, vdur in enumerate(vnfr["vdur"]): |
| 1947 | for iface_index, iface in enumerate(vdur["interfaces"]): |
| 1948 | step = "Looking for matched interface" |
| 1949 | if ( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1950 | iface.get("external-connection-point-ref") |
| 1951 | == vld_fixed_ip_connection_point_data[item].get( |
| 1952 | "vnfd-connection-point-ref" |
| 1953 | ) |
| 1954 | and iface.get("ns-vld-id") == item.split(".")[0] |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1955 | ): |
| 1956 | vnfr_update_text = "vdur.{}.interfaces.{}".format( |
| 1957 | vdur_index, iface_index |
| 1958 | ) |
| 1959 | step = "Storing info in order to update vnfr" |
| 1960 | vnfr_update[ |
| 1961 | vnfr_update_text + ".ip-address" |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1962 | ] = increment_ip_mac( |
| 1963 | vld_fixed_ip_connection_point_data[item].get( |
| 1964 | "ip-address" |
| 1965 | ), |
| 1966 | vdur.get("count-index", 0), |
| 1967 | ) |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1968 | vnfr_update[vnfr_update_text + ".fixed-ip"] = True |
| 1969 | |
| 1970 | step = "updating vnfr at database" |
| 1971 | self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, vnfr_update) |
| 1972 | except ( |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 1973 | ValidationError, |
| 1974 | EngineException, |
| 1975 | DbException, |
| 1976 | MsgException, |
| 1977 | FsException, |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 1978 | ) as e: |
| 1979 | raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code) |
| 1980 | |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1981 | def _update_vnfrs(self, session, rollback, nsr, indata): |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1982 | # get vnfr |
| 1983 | nsr_id = nsr["_id"] |
| 1984 | vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| 1985 | |
| 1986 | for vnfr in vnfrs: |
| 1987 | vnfr_update = {} |
| 1988 | vnfr_update_rollback = {} |
| 1989 | member_vnf_index = vnfr["member-vnf-index-ref"] |
| 1990 | # update vim-account-id |
| 1991 | |
| 1992 | vim_account = indata["vimAccountId"] |
| David Garcia | 98de298 | 2021-10-13 17:14:01 +0200 | [diff] [blame] | 1993 | vca_id = self._get_vim_account(vim_account, session).get("vca") |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 1994 | # check instantiate parameters |
| 1995 | for vnf_inst_params in get_iterable(indata.get("vnf")): |
| 1996 | if vnf_inst_params["member-vnf-index"] != member_vnf_index: |
| 1997 | continue |
| 1998 | if vnf_inst_params.get("vimAccountId"): |
| 1999 | vim_account = vnf_inst_params.get("vimAccountId") |
| David Garcia | 98de298 | 2021-10-13 17:14:01 +0200 | [diff] [blame] | 2000 | vca_id = self._get_vim_account(vim_account, session).get("vca") |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 2001 | |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2002 | # get vnf.vdu.interface instantiation params to update vnfr.vdur.interfaces ip, mac |
| 2003 | for vdu_inst_param in get_iterable(vnf_inst_params.get("vdu")): |
| 2004 | for vdur_index, vdur in enumerate(vnfr["vdur"]): |
| 2005 | if vdu_inst_param["id"] != vdur["vdu-id-ref"]: |
| 2006 | continue |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2007 | for iface_inst_param in get_iterable( |
| 2008 | vdu_inst_param.get("interface") |
| 2009 | ): |
| 2010 | iface_index, _ = next( |
| 2011 | i |
| 2012 | for i in enumerate(vdur["interfaces"]) |
| 2013 | if i[1]["name"] == iface_inst_param["name"] |
| 2014 | ) |
| 2015 | vnfr_update_text = "vdur.{}.interfaces.{}".format( |
| 2016 | vdur_index, iface_index |
| 2017 | ) |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2018 | if iface_inst_param.get("ip-address"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2019 | vnfr_update[ |
| 2020 | vnfr_update_text + ".ip-address" |
| 2021 | ] = increment_ip_mac( |
| 2022 | iface_inst_param.get("ip-address"), |
| 2023 | vdur.get("count-index", 0), |
| 2024 | ) |
| tierno | 1bd9d95 | 2020-11-13 15:56:51 +0000 | [diff] [blame] | 2025 | vnfr_update[vnfr_update_text + ".fixed-ip"] = True |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2026 | if iface_inst_param.get("mac-address"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2027 | vnfr_update[ |
| 2028 | vnfr_update_text + ".mac-address" |
| 2029 | ] = increment_ip_mac( |
| 2030 | iface_inst_param.get("mac-address"), |
| 2031 | vdur.get("count-index", 0), |
| 2032 | ) |
| tierno | 1bd9d95 | 2020-11-13 15:56:51 +0000 | [diff] [blame] | 2033 | vnfr_update[vnfr_update_text + ".fixed-mac"] = True |
| bravof | e4254fd | 2021-02-03 15:22:06 -0300 | [diff] [blame] | 2034 | if iface_inst_param.get("floating-ip-required"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2035 | vnfr_update[ |
| 2036 | vnfr_update_text + ".floating-ip-required" |
| 2037 | ] = True |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2038 | # get vnf.internal-vld.internal-conection-point instantiation params to update vnfr.vdur.interfaces |
| 2039 | # TODO update vld with the ip-profile |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2040 | for ivld_inst_param in get_iterable( |
| 2041 | vnf_inst_params.get("internal-vld") |
| 2042 | ): |
| 2043 | for icp_inst_param in get_iterable( |
| 2044 | ivld_inst_param.get("internal-connection-point") |
| 2045 | ): |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2046 | # look for iface |
| 2047 | for vdur_index, vdur in enumerate(vnfr["vdur"]): |
| 2048 | for iface_index, iface in enumerate(vdur["interfaces"]): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2049 | if ( |
| 2050 | iface.get("internal-connection-point-ref") |
| 2051 | == icp_inst_param["id-ref"] |
| 2052 | ): |
| 2053 | vnfr_update_text = "vdur.{}.interfaces.{}".format( |
| 2054 | vdur_index, iface_index |
| 2055 | ) |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2056 | if icp_inst_param.get("ip-address"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2057 | vnfr_update[ |
| 2058 | vnfr_update_text + ".ip-address" |
| 2059 | ] = increment_ip_mac( |
| 2060 | icp_inst_param.get("ip-address"), |
| 2061 | vdur.get("count-index", 0), |
| 2062 | ) |
| 2063 | vnfr_update[ |
| 2064 | vnfr_update_text + ".fixed-ip" |
| 2065 | ] = True |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2066 | if icp_inst_param.get("mac-address"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2067 | vnfr_update[ |
| 2068 | vnfr_update_text + ".mac-address" |
| 2069 | ] = increment_ip_mac( |
| 2070 | icp_inst_param.get("mac-address"), |
| 2071 | vdur.get("count-index", 0), |
| 2072 | ) |
| 2073 | vnfr_update[ |
| 2074 | vnfr_update_text + ".fixed-mac" |
| 2075 | ] = True |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2076 | break |
| 2077 | # get ip address from instantiation parameters.vld.vnfd-connection-point-ref |
| 2078 | for vld_inst_param in get_iterable(indata.get("vld")): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2079 | for vnfcp_inst_param in get_iterable( |
| 2080 | vld_inst_param.get("vnfd-connection-point-ref") |
| 2081 | ): |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2082 | if vnfcp_inst_param["member-vnf-index-ref"] != member_vnf_index: |
| 2083 | continue |
| 2084 | # look for iface |
| 2085 | for vdur_index, vdur in enumerate(vnfr["vdur"]): |
| 2086 | for iface_index, iface in enumerate(vdur["interfaces"]): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2087 | if ( |
| 2088 | iface.get("external-connection-point-ref") |
| 2089 | == vnfcp_inst_param["vnfd-connection-point-ref"] |
| 2090 | ): |
| 2091 | vnfr_update_text = "vdur.{}.interfaces.{}".format( |
| 2092 | vdur_index, iface_index |
| 2093 | ) |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2094 | if vnfcp_inst_param.get("ip-address"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2095 | vnfr_update[ |
| 2096 | vnfr_update_text + ".ip-address" |
| 2097 | ] = increment_ip_mac( |
| 2098 | vnfcp_inst_param.get("ip-address"), |
| 2099 | vdur.get("count-index", 0), |
| 2100 | ) |
| tierno | 1bd9d95 | 2020-11-13 15:56:51 +0000 | [diff] [blame] | 2101 | vnfr_update[vnfr_update_text + ".fixed-ip"] = True |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2102 | if vnfcp_inst_param.get("mac-address"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2103 | vnfr_update[ |
| 2104 | vnfr_update_text + ".mac-address" |
| 2105 | ] = increment_ip_mac( |
| 2106 | vnfcp_inst_param.get("mac-address"), |
| 2107 | vdur.get("count-index", 0), |
| 2108 | ) |
| tierno | 1bd9d95 | 2020-11-13 15:56:51 +0000 | [diff] [blame] | 2109 | vnfr_update[vnfr_update_text + ".fixed-mac"] = True |
| tierno | cddb07d | 2020-10-06 08:28:00 +0000 | [diff] [blame] | 2110 | break |
| 2111 | |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 2112 | vnfr_update["vim-account-id"] = vim_account |
| 2113 | vnfr_update_rollback["vim-account-id"] = vnfr.get("vim-account-id") |
| 2114 | |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 2115 | if vca_id: |
| 2116 | vnfr_update["vca-id"] = vca_id |
| 2117 | vnfr_update_rollback["vca-id"] = vnfr.get("vca-id") |
| 2118 | |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 2119 | # get pdu |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2120 | ifaces_forcing_vim_network = self._look_for_pdu( |
| 2121 | session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback |
| 2122 | ) |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 2123 | |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 2124 | # get kdus |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2125 | ifaces_forcing_vim_network += self._look_for_k8scluster( |
| 2126 | session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback |
| 2127 | ) |
| tierno | 9cb7d67 | 2019-10-30 12:13:48 +0000 | [diff] [blame] | 2128 | # update database vnfr |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2129 | self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, vnfr_update) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2130 | rollback.append( |
| 2131 | { |
| 2132 | "topic": "vnfrs", |
| 2133 | "_id": vnfr["_id"], |
| 2134 | "operation": "set", |
| 2135 | "content": vnfr_update_rollback, |
| 2136 | } |
| 2137 | ) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2138 | |
| 2139 | # Update indada in case pdu forces to use a concrete vim-network-name |
| 2140 | # TODO check if user has already insert a vim-network-name and raises an error |
| 2141 | if not ifaces_forcing_vim_network: |
| 2142 | continue |
| 2143 | for iface_info in ifaces_forcing_vim_network: |
| 2144 | if iface_info.get("ns-vld-id"): |
| 2145 | if "vld" not in indata: |
| 2146 | indata["vld"] = [] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2147 | indata["vld"].append( |
| 2148 | { |
| 2149 | key: iface_info[key] |
| 2150 | for key in ("name", "vim-network-name", "vim-network-id") |
| 2151 | if iface_info.get(key) |
| 2152 | } |
| 2153 | ) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2154 | |
| 2155 | elif iface_info.get("vnf-vld-id"): |
| 2156 | if "vnf" not in indata: |
| 2157 | indata["vnf"] = [] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2158 | indata["vnf"].append( |
| 2159 | { |
| 2160 | "member-vnf-index": member_vnf_index, |
| 2161 | "internal-vld": [ |
| 2162 | { |
| 2163 | key: iface_info[key] |
| 2164 | for key in ( |
| 2165 | "name", |
| 2166 | "vim-network-name", |
| 2167 | "vim-network-id", |
| 2168 | ) |
| 2169 | if iface_info.get(key) |
| 2170 | } |
| 2171 | ], |
| 2172 | } |
| 2173 | ) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2174 | |
| 2175 | @staticmethod |
| 2176 | def _create_nslcmop(nsr_id, operation, params): |
| 2177 | """ |
| 2178 | Creates a ns-lcm-opp content to be stored at database. |
| 2179 | :param nsr_id: internal id of the instance |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 2180 | :param operation: instantiate, terminate, scale, action, update ... |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2181 | :param params: user parameters for the operation |
| 2182 | :return: dictionary following SOL005 format |
| 2183 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2184 | now = time() |
| 2185 | _id = str(uuid4()) |
| 2186 | nslcmop = { |
| 2187 | "id": _id, |
| 2188 | "_id": _id, |
| 2189 | "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK |
| tierno | ecf94bd | 2020-01-09 12:40:45 +0000 | [diff] [blame] | 2190 | "queuePosition": None, |
| 2191 | "stage": None, |
| 2192 | "errorMessage": None, |
| 2193 | "detailedStatus": None, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2194 | "statusEnteredTime": now, |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2195 | "nsInstanceId": nsr_id, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2196 | "lcmOperationType": operation, |
| 2197 | "startTime": now, |
| 2198 | "isAutomaticInvocation": False, |
| 2199 | "operationParams": params, |
| 2200 | "isCancelPending": False, |
| 2201 | "links": { |
| 2202 | "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id, |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2203 | "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2204 | }, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2205 | } |
| 2206 | return nslcmop |
| 2207 | |
| magnussonl | f318b30 | 2020-01-20 18:38:18 +0100 | [diff] [blame] | 2208 | def _get_enabled_vims(self, session): |
| 2209 | """ |
| 2210 | Retrieve and return VIM accounts that are accessible by current user and has state ENABLE |
| 2211 | :param session: current session with user information |
| 2212 | """ |
| 2213 | db_filter = self._get_project_filter(session) |
| 2214 | db_filter["_admin.operationalState"] = "ENABLED" |
| 2215 | vims = self.db.get_list("vim_accounts", db_filter) |
| 2216 | vimAccounts = [] |
| 2217 | for vim in vims: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2218 | vimAccounts.append(vim["_id"]) |
| magnussonl | f318b30 | 2020-01-20 18:38:18 +0100 | [diff] [blame] | 2219 | return vimAccounts |
| 2220 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2221 | def new( |
| 2222 | self, |
| 2223 | rollback, |
| 2224 | session, |
| 2225 | indata=None, |
| 2226 | kwargs=None, |
| 2227 | headers=None, |
| 2228 | slice_object=False, |
| 2229 | ): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2230 | """ |
| 2231 | Performs a new operation over a ns |
| 2232 | :param rollback: list to append created items at database in case a rollback must to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2233 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2234 | :param indata: descriptor with the parameters of the operation. It must contains among others |
| 2235 | nsInstanceId: _id of the nsr to perform the operation |
| aticig | 544a2ae | 2022-04-05 09:00:17 +0300 | [diff] [blame] | 2236 | operation: it can be: instantiate, terminate, action, update TODO: heal |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2237 | :param kwargs: used to override the indata descriptor |
| 2238 | :param headers: http request headers |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2239 | :return: id of the nslcmops |
| 2240 | """ |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2241 | |
| Felipe Vicens | 90fbc9c | 2019-06-06 01:03:00 +0200 | [diff] [blame] | 2242 | def check_if_nsr_is_not_slice_member(session, nsr_id): |
| 2243 | nsis = None |
| 2244 | db_filter = self._get_project_filter(session) |
| 2245 | db_filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2246 | nsis = self.db.get_one( |
| 2247 | "nsis", db_filter, fail_on_empty=False, fail_on_more=False |
| 2248 | ) |
| Felipe Vicens | 90fbc9c | 2019-06-06 01:03:00 +0200 | [diff] [blame] | 2249 | if nsis: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2250 | raise EngineException( |
| 2251 | "The NS instance {} cannot be terminated because is used by the slice {}".format( |
| 2252 | nsr_id, nsis["_id"] |
| 2253 | ), |
| 2254 | http_code=HTTPStatus.CONFLICT, |
| 2255 | ) |
| Felipe Vicens | 90fbc9c | 2019-06-06 01:03:00 +0200 | [diff] [blame] | 2256 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2257 | try: |
| 2258 | # Override descriptor with query string kwargs |
| tierno | 1c38f2f | 2020-03-24 11:51:39 +0000 | [diff] [blame] | 2259 | self._update_input_with_kwargs(indata, kwargs, yaml_format=True) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2260 | operation = indata["lcmOperationType"] |
| 2261 | nsInstanceId = indata["nsInstanceId"] |
| 2262 | |
| 2263 | validate_input(indata, self.operation_schema[operation]) |
| 2264 | # get ns from nsr_id |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2265 | _filter = BaseTopic._get_project_filter(session) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2266 | _filter["_id"] = nsInstanceId |
| 2267 | nsr = self.db.get_one("nsrs", _filter) |
| 2268 | |
| 2269 | # initial checking |
| Felipe Vicens | 90fbc9c | 2019-06-06 01:03:00 +0200 | [diff] [blame] | 2270 | if operation == "terminate" and slice_object is False: |
| 2271 | check_if_nsr_is_not_slice_member(session, nsr["_id"]) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2272 | if ( |
| 2273 | not nsr["_admin"].get("nsState") |
| 2274 | or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED" |
| 2275 | ): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2276 | if operation == "terminate" and indata.get("autoremove"): |
| 2277 | # NSR must be deleted |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2278 | return ( |
| 2279 | None, |
| 2280 | None, |
| 2281 | ) # a none in this case is used to indicate not instantiated. It can be removed |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2282 | if operation != "instantiate": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2283 | raise EngineException( |
| 2284 | "ns_instance '{}' cannot be '{}' because it is not instantiated".format( |
| 2285 | nsInstanceId, operation |
| 2286 | ), |
| 2287 | HTTPStatus.CONFLICT, |
| 2288 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2289 | else: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2290 | if operation == "instantiate" and not session["force"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2291 | raise EngineException( |
| 2292 | "ns_instance '{}' cannot be '{}' because it is already instantiated".format( |
| 2293 | nsInstanceId, operation |
| 2294 | ), |
| 2295 | HTTPStatus.CONFLICT, |
| 2296 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2297 | self._check_ns_operation(session, nsr, operation, indata) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2298 | if indata.get("primitive_params"): |
| Guillermo Calvino | 7fcbd4f | 2022-01-26 17:37:56 +0100 | [diff] [blame] | 2299 | indata["primitive_params"] = json.dumps(indata["primitive_params"]) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2300 | elif indata.get("additionalParamsForVnf"): |
| 2301 | indata["additionalParamsForVnf"] = json.dumps( |
| 2302 | indata["additionalParamsForVnf"] |
| 2303 | ) |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2304 | |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 2305 | if operation == "instantiate": |
| Gulsum Atici | e395aa4 | 2021-11-10 20:59:06 +0300 | [diff] [blame] | 2306 | self._update_vnfrs_from_nsd(nsr) |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 2307 | self._update_vnfrs(session, rollback, nsr, indata) |
| elumalai | 6c5ea6b | 2022-04-25 22:27:59 +0530 | [diff] [blame] | 2308 | if (operation == "update") and (indata["updateType"] == "CHANGE_VNFPKG"): |
| 2309 | nsr_update = {} |
| 2310 | vnfd_id = indata["changeVnfPackageData"]["vnfdId"] |
| 2311 | vnfd = self.db.get_one("vnfds", {"_id": vnfd_id}) |
| 2312 | nsd = self.db.get_one("nsds", {"_id": nsr["nsd-id"]}) |
| 2313 | ns_request = nsr["instantiate_params"] |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2314 | vnfr = self.db.get_one( |
| 2315 | "vnfrs", {"_id": indata["changeVnfPackageData"]["vnfInstanceId"]} |
| 2316 | ) |
| elumalai | 8bf978e | 2022-05-26 15:32:06 +0530 | [diff] [blame] | 2317 | latest_vnfd_revision = vnfd["_admin"].get("revision", 1) |
| 2318 | vnfr_vnfd_revision = vnfr.get("revision", 1) |
| 2319 | if latest_vnfd_revision != vnfr_vnfd_revision: |
| 2320 | old_vnfd_id = vnfd_id + ":" + str(vnfr_vnfd_revision) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2321 | old_db_vnfd = self.db.get_one( |
| 2322 | "vnfds_revisions", {"_id": old_vnfd_id} |
| 2323 | ) |
| elumalai | 8bf978e | 2022-05-26 15:32:06 +0530 | [diff] [blame] | 2324 | old_sw_version = old_db_vnfd.get("software-version", "1.0") |
| 2325 | new_sw_version = vnfd.get("software-version", "1.0") |
| 2326 | if new_sw_version != old_sw_version: |
| 2327 | vnf_index = vnfr["member-vnf-index-ref"] |
| 2328 | self.logger.info("nsr {}".format(nsr)) |
| 2329 | for vdu in vnfd["vdu"]: |
| vegall | 18101ea | 2023-03-06 13:49:21 +0000 | [diff] [blame] | 2330 | self.nsrtopic._add_shared_volumes_to_nsr( |
| 2331 | vdu, vnfd, nsr, vnf_index, latest_vnfd_revision |
| 2332 | ) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2333 | self.nsrtopic._add_flavor_to_nsr( |
| 2334 | vdu, vnfd, nsr, vnf_index, latest_vnfd_revision |
| 2335 | ) |
| elumalai | 8bf978e | 2022-05-26 15:32:06 +0530 | [diff] [blame] | 2336 | sw_image_id = vdu.get("sw-image-desc") |
| 2337 | if sw_image_id: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2338 | image_data = self.nsrtopic._get_image_data_from_vnfd( |
| 2339 | vnfd, sw_image_id |
| 2340 | ) |
| elumalai | 8bf978e | 2022-05-26 15:32:06 +0530 | [diff] [blame] | 2341 | self.nsrtopic._add_image_to_nsr(nsr, image_data) |
| 2342 | for alt_image in vdu.get("alternative-sw-image-desc", ()): |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2343 | image_data = self.nsrtopic._get_image_data_from_vnfd( |
| 2344 | vnfd, alt_image |
| 2345 | ) |
| elumalai | 8bf978e | 2022-05-26 15:32:06 +0530 | [diff] [blame] | 2346 | self.nsrtopic._add_image_to_nsr(nsr, image_data) |
| 2347 | nsr_update["image"] = nsr["image"] |
| 2348 | nsr_update["flavor"] = nsr["flavor"] |
| vegall | 18101ea | 2023-03-06 13:49:21 +0000 | [diff] [blame] | 2349 | nsr_update["shared-volumes"] = nsr["shared-volumes"] |
| elumalai | 8bf978e | 2022-05-26 15:32:06 +0530 | [diff] [blame] | 2350 | self.db.set_one("nsrs", {"_id": nsr["_id"]}, nsr_update) |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2351 | ns_k8s_namespace = self.nsrtopic._get_ns_k8s_namespace( |
| 2352 | nsd, ns_request, session |
| 2353 | ) |
| 2354 | vnfr_descriptor = ( |
| 2355 | self.nsrtopic._create_vnfr_descriptor_from_vnfd( |
| 2356 | nsd, |
| 2357 | vnfd, |
| 2358 | vnfd_id, |
| 2359 | vnf_index, |
| 2360 | nsr, |
| 2361 | ns_request, |
| 2362 | ns_k8s_namespace, |
| 2363 | latest_vnfd_revision, |
| 2364 | ) |
| elumalai | 8bf978e | 2022-05-26 15:32:06 +0530 | [diff] [blame] | 2365 | ) |
| 2366 | indata["newVdur"] = vnfr_descriptor["vdur"] |
| tierno | 36ec860 | 2018-11-02 17:27:11 +0100 | [diff] [blame] | 2367 | nslcmop_desc = self._create_nslcmop(nsInstanceId, operation, indata) |
| tierno | 1bfe4e2 | 2019-09-02 16:03:25 +0000 | [diff] [blame] | 2368 | _id = nslcmop_desc["_id"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2369 | self.format_on_new( |
| 2370 | nslcmop_desc, session["project_id"], make_public=session["public"] |
| 2371 | ) |
| magnussonl | f318b30 | 2020-01-20 18:38:18 +0100 | [diff] [blame] | 2372 | if indata.get("placement-engine"): |
| 2373 | # Save valid vim accounts in lcm operation descriptor |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2374 | nslcmop_desc["operationParams"][ |
| 2375 | "validVimAccounts" |
| 2376 | ] = self._get_enabled_vims(session) |
| tierno | 1bfe4e2 | 2019-09-02 16:03:25 +0000 | [diff] [blame] | 2377 | self.db.create("nslcmops", nslcmop_desc) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2378 | rollback.append({"topic": "nslcmops", "_id": _id}) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2379 | if not slice_object: |
| 2380 | self.msg.write("ns", operation, nslcmop_desc) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 2381 | return _id, None |
| 2382 | except ValidationError as e: # TODO remove try Except, it is captured at nbi.py |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2383 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 2384 | # except DbException as e: |
| 2385 | # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND) |
| 2386 | |
| Gabriel Cuba | 84a60df | 2023-10-30 14:01:54 -0500 | [diff] [blame] | 2387 | def cancel(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 2388 | validate_input(indata, self.operation_schema["cancel"]) |
| 2389 | # Override descriptor with query string kwargs |
| 2390 | self._update_input_with_kwargs(indata, kwargs, yaml_format=True) |
| 2391 | nsLcmOpOccId = indata["nsLcmOpOccId"] |
| 2392 | cancelMode = indata["cancelMode"] |
| 2393 | # get nslcmop from nsLcmOpOccId |
| 2394 | _filter = BaseTopic._get_project_filter(session) |
| 2395 | _filter["_id"] = nsLcmOpOccId |
| 2396 | nslcmop = self.db.get_one("nslcmops", _filter) |
| 2397 | # Fail is this is not an ongoing nslcmop |
| 2398 | if nslcmop.get("operationState") not in [ |
| 2399 | "STARTING", |
| 2400 | "PROCESSING", |
| 2401 | "ROLLING_BACK", |
| 2402 | ]: |
| 2403 | raise EngineException( |
| 2404 | "Operation is not in STARTING, PROCESSING or ROLLING_BACK state", |
| 2405 | http_code=HTTPStatus.CONFLICT, |
| 2406 | ) |
| 2407 | nsInstanceId = nslcmop["nsInstanceId"] |
| 2408 | update_dict = { |
| 2409 | "isCancelPending": True, |
| 2410 | "cancelMode": cancelMode, |
| 2411 | } |
| 2412 | self.db.set_one( |
| 2413 | "nslcmops", q_filter=_filter, update_dict=update_dict, fail_on_empty=False |
| 2414 | ) |
| 2415 | data = { |
| 2416 | "_id": nsLcmOpOccId, |
| 2417 | "nsInstanceId": nsInstanceId, |
| 2418 | "cancelMode": cancelMode, |
| 2419 | } |
| 2420 | self.msg.write("nslcmops", "cancel", data) |
| 2421 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 2422 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2423 | raise EngineException( |
| 2424 | "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 2425 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 2426 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2427 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2428 | raise EngineException( |
| 2429 | "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 2430 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2431 | |
| 2432 | |
| 2433 | class NsiTopic(BaseTopic): |
| 2434 | topic = "nsis" |
| 2435 | topic_msg = "nsi" |
| tierno | 6b02b05 | 2020-06-02 10:07:41 +0000 | [diff] [blame] | 2436 | quota_name = "slice_instances" |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2437 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 2438 | def __init__(self, db, fs, msg, auth): |
| 2439 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 2440 | self.nsrTopic = NsrTopic(db, fs, msg, auth) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2441 | |
| Felipe Vicens | c37b384 | 2019-01-12 12:24:42 +0100 | [diff] [blame] | 2442 | @staticmethod |
| 2443 | def _format_ns_request(ns_request): |
| 2444 | formated_request = copy(ns_request) |
| 2445 | # TODO: Add request params |
| 2446 | return formated_request |
| 2447 | |
| 2448 | @staticmethod |
| tierno | fd16057 | 2019-01-21 10:41:37 +0000 | [diff] [blame] | 2449 | def _format_addional_params(slice_request): |
| Felipe Vicens | c37b384 | 2019-01-12 12:24:42 +0100 | [diff] [blame] | 2450 | """ |
| 2451 | Get and format user additional params for NS or VNF |
| tierno | fd16057 | 2019-01-21 10:41:37 +0000 | [diff] [blame] | 2452 | :param slice_request: User instantiation additional parameters |
| 2453 | :return: a formatted copy of additional params or None if not supplied |
| Felipe Vicens | c37b384 | 2019-01-12 12:24:42 +0100 | [diff] [blame] | 2454 | """ |
| tierno | fd16057 | 2019-01-21 10:41:37 +0000 | [diff] [blame] | 2455 | additional_params = copy(slice_request.get("additionalParamsForNsi")) |
| 2456 | if additional_params: |
| 2457 | for k, v in additional_params.items(): |
| 2458 | if not isinstance(k, str): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2459 | raise EngineException( |
| 2460 | "Invalid param at additionalParamsForNsi:{}. Only string keys are allowed".format( |
| 2461 | k |
| 2462 | ) |
| 2463 | ) |
| tierno | fd16057 | 2019-01-21 10:41:37 +0000 | [diff] [blame] | 2464 | if "." in k or "$" in k: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2465 | raise EngineException( |
| 2466 | "Invalid param at additionalParamsForNsi:{}. Keys must not contain dots or $".format( |
| 2467 | k |
| 2468 | ) |
| 2469 | ) |
| tierno | fd16057 | 2019-01-21 10:41:37 +0000 | [diff] [blame] | 2470 | if isinstance(v, (dict, tuple, list)): |
| 2471 | additional_params[k] = "!!yaml " + safe_dump(v) |
| Felipe Vicens | c37b384 | 2019-01-12 12:24:42 +0100 | [diff] [blame] | 2472 | return additional_params |
| 2473 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2474 | def check_conflict_on_del(self, session, _id, db_content): |
| 2475 | """ |
| 2476 | Check that NSI is not instantiated |
| 2477 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 2478 | :param _id: nsi internal id |
| 2479 | :param db_content: The database content of the _id |
| 2480 | :return: None or raises EngineException with the conflict |
| 2481 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2482 | if session["force"]: |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2483 | return |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2484 | nsi = db_content |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2485 | if nsi["_admin"].get("nsiState") == "INSTANTIATED": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2486 | raise EngineException( |
| 2487 | "nsi '{}' cannot be deleted because it is in 'INSTANTIATED' state. " |
| 2488 | "Launch 'terminate' operation first; or force deletion".format(_id), |
| 2489 | http_code=HTTPStatus.CONFLICT, |
| 2490 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2491 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 2492 | def delete_extra(self, session, _id, db_content, not_send_msg=None): |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2493 | """ |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2494 | Deletes associated nsilcmops from database. Deletes associated filesystem. |
| 2495 | Set usageState of nst |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2496 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2497 | :param _id: server internal id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2498 | :param db_content: The database content of the descriptor |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 2499 | :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] | 2500 | :return: None if ok or raises EngineException with the problem |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2501 | """ |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2502 | |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2503 | # Deleting the nsrs belonging to nsir |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2504 | nsir = db_content |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2505 | for nsrs_detailed_item in nsir["_admin"]["nsrs-detailed-list"]: |
| 2506 | nsr_id = nsrs_detailed_item["nsrId"] |
| 2507 | if nsrs_detailed_item.get("shared"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2508 | _filter = { |
| 2509 | "_admin.nsrs-detailed-list.ANYINDEX.shared": True, |
| 2510 | "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nsr_id, |
| 2511 | "_id.ne": nsir["_id"], |
| 2512 | } |
| 2513 | nsi = self.db.get_one( |
| 2514 | "nsis", _filter, fail_on_empty=False, fail_on_more=False |
| 2515 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2516 | if nsi: # last one using nsr |
| 2517 | continue |
| 2518 | try: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2519 | self.nsrTopic.delete( |
| 2520 | session, nsr_id, dry_run=False, not_send_msg=not_send_msg |
| 2521 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2522 | except (DbException, EngineException) as e: |
| 2523 | if e.http_code == HTTPStatus.NOT_FOUND: |
| 2524 | pass |
| 2525 | else: |
| 2526 | raise |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2527 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2528 | # delete related nsilcmops database entries |
| 2529 | self.db.del_list("nsilcmops", {"netsliceInstanceId": _id}) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2530 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2531 | # Check and set used NST usage state |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2532 | nsir_admin = nsir.get("_admin") |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2533 | if nsir_admin and nsir_admin.get("nst-id"): |
| 2534 | # check if used by another NSI |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2535 | nsis_list = self.db.get_one( |
| 2536 | "nsis", |
| 2537 | {"nst-id": nsir_admin["nst-id"]}, |
| 2538 | fail_on_empty=False, |
| 2539 | fail_on_more=False, |
| 2540 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2541 | if not nsis_list: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2542 | self.db.set_one( |
| 2543 | "nsts", |
| 2544 | {"_id": nsir_admin["nst-id"]}, |
| 2545 | {"_admin.usageState": "NOT_IN_USE"}, |
| 2546 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2547 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2548 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2549 | """ |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2550 | Creates a new netslice instance record into database. It also creates needed nsrs and vnfrs |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2551 | :param rollback: list to append the created items at database in case a rollback must be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2552 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2553 | :param indata: params to be used for the nsir |
| 2554 | :param kwargs: used to override the indata descriptor |
| 2555 | :param headers: http request headers |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2556 | :return: the _id of nsi descriptor created at database |
| 2557 | """ |
| 2558 | |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2559 | step = "checking quotas" # first step must be defined outside try |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2560 | try: |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 2561 | self.check_quota(session) |
| 2562 | |
| tierno | 99d4b17 | 2019-07-02 09:28:40 +0000 | [diff] [blame] | 2563 | step = "" |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2564 | slice_request = self._remove_envelop(indata) |
| 2565 | # Override descriptor with query string kwargs |
| 2566 | self._update_input_with_kwargs(slice_request, kwargs) |
| bravof | b995ea2 | 2021-02-10 10:57:52 -0300 | [diff] [blame] | 2567 | slice_request = self._validate_input_new(slice_request, session["force"]) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2568 | |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2569 | # look for nstd |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2570 | step = "getting nstd id='{}' from database".format( |
| 2571 | slice_request.get("nstId") |
| 2572 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2573 | _filter = self._get_project_filter(session) |
| 2574 | _filter["_id"] = slice_request["nstId"] |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2575 | nstd = self.db.get_one("nsts", _filter) |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2576 | # check NST is not disabled |
| 2577 | step = "checking NST operationalState" |
| 2578 | if nstd["_admin"]["operationalState"] == "DISABLED": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2579 | raise EngineException( |
| 2580 | "nst with id '{}' is DISABLED, and thus cannot be used to create a netslice " |
| 2581 | "instance".format(slice_request["nstId"]), |
| 2582 | http_code=HTTPStatus.CONFLICT, |
| 2583 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2584 | del _filter["_id"] |
| 2585 | |
| Frank Bryden | b5a2ead | 2020-07-28 12:50:23 +0000 | [diff] [blame] | 2586 | # check NSD is not disabled |
| 2587 | step = "checking operationalState" |
| 2588 | if nstd["_admin"]["operationalState"] == "DISABLED": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2589 | raise EngineException( |
| 2590 | "nst with id '{}' is DISABLED, and thus cannot be used to create " |
| 2591 | "a network slice".format(slice_request["nstId"]), |
| 2592 | http_code=HTTPStatus.CONFLICT, |
| 2593 | ) |
| Frank Bryden | b5a2ead | 2020-07-28 12:50:23 +0000 | [diff] [blame] | 2594 | |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2595 | nstd.pop("_admin", None) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2596 | nstd_id = nstd.pop("_id", None) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2597 | nsi_id = str(uuid4()) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2598 | step = "filling nsi_descriptor with input data" |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2599 | |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2600 | # Creating the NSIR |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2601 | nsi_descriptor = { |
| 2602 | "id": nsi_id, |
| garciadeblas | c54d420 | 2018-11-29 23:41:37 +0100 | [diff] [blame] | 2603 | "name": slice_request["nsiName"], |
| 2604 | "description": slice_request.get("nsiDescription", ""), |
| 2605 | "datacenter": slice_request["vimAccountId"], |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2606 | "nst-ref": nstd["id"], |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2607 | "instantiation_parameters": slice_request, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2608 | "network-slice-template": nstd, |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2609 | "nsr-ref-list": [], |
| 2610 | "vlr-list": [], |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2611 | "_id": nsi_id, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2612 | "additionalParamsForNsi": self._format_addional_params(slice_request), |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2613 | } |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2614 | |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2615 | step = "creating nsi at database" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2616 | self.format_on_new( |
| 2617 | nsi_descriptor, session["project_id"], make_public=session["public"] |
| 2618 | ) |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2619 | nsi_descriptor["_admin"]["nsiState"] = "NOT_INSTANTIATED" |
| 2620 | nsi_descriptor["_admin"]["netslice-subnet"] = None |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2621 | nsi_descriptor["_admin"]["deployed"] = {} |
| 2622 | nsi_descriptor["_admin"]["deployed"]["RO"] = [] |
| 2623 | nsi_descriptor["_admin"]["nst-id"] = nstd_id |
| 2624 | |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2625 | # Creating netslice-vld for the RO. |
| 2626 | step = "creating netslice-vld at database" |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2627 | |
| 2628 | # Building the vlds list to be deployed |
| 2629 | # From netslice descriptors, creating the initial list |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2630 | nsi_vlds = [] |
| 2631 | |
| 2632 | for netslice_vlds in get_iterable(nstd.get("netslice-vld")): |
| 2633 | # Getting template Instantiation parameters from NST |
| 2634 | nsi_vld = deepcopy(netslice_vlds) |
| 2635 | nsi_vld["shared-nsrs-list"] = [] |
| 2636 | nsi_vld["vimAccountId"] = slice_request["vimAccountId"] |
| 2637 | nsi_vlds.append(nsi_vld) |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2638 | |
| 2639 | nsi_descriptor["_admin"]["netslice-vld"] = nsi_vlds |
| tierno | 3ffc7a4 | 2019-12-03 09:39:40 +0000 | [diff] [blame] | 2640 | # Creating netslice-subnet_record. |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2641 | needed_nsds = {} |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2642 | services = [] |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2643 | |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2644 | # Updating the nstd with the nsd["_id"] associated to the nss -> services list |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2645 | for member_ns in nstd["netslice-subnet"]: |
| 2646 | nsd_id = member_ns["nsd-ref"] |
| 2647 | step = "getting nstd id='{}' constituent-nsd='{}' from database".format( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2648 | member_ns["nsd-ref"], member_ns["id"] |
| 2649 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2650 | if nsd_id not in needed_nsds: |
| 2651 | # Obtain nsd |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2652 | _filter["id"] = nsd_id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2653 | nsd = self.db.get_one( |
| 2654 | "nsds", _filter, fail_on_empty=True, fail_on_more=True |
| 2655 | ) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2656 | del _filter["id"] |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2657 | nsd.pop("_admin") |
| 2658 | needed_nsds[nsd_id] = nsd |
| 2659 | else: |
| 2660 | nsd = needed_nsds[nsd_id] |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2661 | member_ns["_id"] = needed_nsds[nsd_id].get("_id") |
| 2662 | services.append(member_ns) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2663 | |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2664 | step = "filling nsir nsd-id='{}' constituent-nsd='{}' from database".format( |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2665 | member_ns["nsd-ref"], member_ns["id"] |
| 2666 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2667 | |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2668 | # creates Network Services records (NSRs) |
| 2669 | step = "creating nsrs at database using NsrTopic.new()" |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2670 | ns_params = slice_request.get("netslice-subnet") |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2671 | nsrs_list = [] |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2672 | nsi_netslice_subnet = [] |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2673 | for service in services: |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2674 | # Check if the netslice-subnet is shared and if it is share if the nss exists |
| 2675 | _id_nsr = None |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2676 | indata_ns = {} |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2677 | # Is the nss shared and instantiated? |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2678 | _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2679 | _filter["_admin.nsrs-detailed-list.ANYINDEX.nsd-id"] = service[ |
| 2680 | "nsd-ref" |
| 2681 | ] |
| Felipe Vicens | 08ddb14 | 2019-08-09 15:52:40 +0200 | [diff] [blame] | 2682 | _filter["_admin.nsrs-detailed-list.ANYINDEX.nss-id"] = service["id"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2683 | nsi = self.db.get_one( |
| 2684 | "nsis", _filter, fail_on_empty=False, fail_on_more=False |
| 2685 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2686 | if nsi and service.get("is-shared-nss"): |
| 2687 | nsrs_detailed_list = nsi["_admin"]["nsrs-detailed-list"] |
| 2688 | for nsrs_detailed_item in nsrs_detailed_list: |
| 2689 | if nsrs_detailed_item["nsd-id"] == service["nsd-ref"]: |
| Felipe Vicens | 08ddb14 | 2019-08-09 15:52:40 +0200 | [diff] [blame] | 2690 | if nsrs_detailed_item["nss-id"] == service["id"]: |
| 2691 | _id_nsr = nsrs_detailed_item["nsrId"] |
| 2692 | break |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2693 | for netslice_subnet in nsi["_admin"]["netslice-subnet"]: |
| 2694 | if netslice_subnet["nss-id"] == service["id"]: |
| 2695 | indata_ns = netslice_subnet |
| 2696 | break |
| 2697 | else: |
| 2698 | indata_ns = {} |
| 2699 | if service.get("instantiation-parameters"): |
| 2700 | indata_ns = deepcopy(service["instantiation-parameters"]) |
| 2701 | # del service["instantiation-parameters"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2702 | |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2703 | indata_ns["nsdId"] = service["_id"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2704 | indata_ns["nsName"] = ( |
| 2705 | slice_request.get("nsiName") + "." + service["id"] |
| 2706 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2707 | indata_ns["vimAccountId"] = slice_request.get("vimAccountId") |
| 2708 | indata_ns["nsDescription"] = service["description"] |
| tierno | 99d4b17 | 2019-07-02 09:28:40 +0000 | [diff] [blame] | 2709 | if slice_request.get("ssh_keys"): |
| 2710 | indata_ns["ssh_keys"] = slice_request.get("ssh_keys") |
| Felipe Vicens | c37b384 | 2019-01-12 12:24:42 +0100 | [diff] [blame] | 2711 | |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2712 | if ns_params: |
| 2713 | for ns_param in ns_params: |
| 2714 | if ns_param.get("id") == service["id"]: |
| 2715 | copy_ns_param = deepcopy(ns_param) |
| 2716 | del copy_ns_param["id"] |
| 2717 | indata_ns.update(copy_ns_param) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2718 | break |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2719 | |
| 2720 | # Creates Nsr objects |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2721 | _id_nsr, _ = self.nsrTopic.new( |
| 2722 | rollback, session, indata_ns, kwargs, headers |
| 2723 | ) |
| 2724 | nsrs_item = { |
| 2725 | "nsrId": _id_nsr, |
| 2726 | "shared": service.get("is-shared-nss"), |
| 2727 | "nsd-id": service["nsd-ref"], |
| 2728 | "nss-id": service["id"], |
| 2729 | "nslcmop_instantiate": None, |
| 2730 | } |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2731 | indata_ns["nss-id"] = service["id"] |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2732 | nsrs_list.append(nsrs_item) |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2733 | nsi_netslice_subnet.append(indata_ns) |
| 2734 | nsr_ref = {"nsr-ref": _id_nsr} |
| 2735 | nsi_descriptor["nsr-ref-list"].append(nsr_ref) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2736 | |
| 2737 | # Adding the nsrs list to the nsi |
| 2738 | nsi_descriptor["_admin"]["nsrs-detailed-list"] = nsrs_list |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2739 | nsi_descriptor["_admin"]["netslice-subnet"] = nsi_netslice_subnet |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2740 | self.db.set_one( |
| 2741 | "nsts", {"_id": slice_request["nstId"]}, {"_admin.usageState": "IN_USE"} |
| 2742 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2743 | |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2744 | # Creating the entry in the database |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2745 | self.db.create("nsis", nsi_descriptor) |
| 2746 | rollback.append({"topic": "nsis", "_id": nsi_id}) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 2747 | return nsi_id, None |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 2748 | except ValidationError as e: |
| 2749 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2750 | except Exception as e: # TODO remove try Except, it is captured at nbi.py |
| 2751 | self.logger.exception( |
| 2752 | "Exception {} at NsiTopic.new()".format(e), exc_info=True |
| 2753 | ) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2754 | raise EngineException("Error {}: {}".format(step, e)) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 2755 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2756 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2757 | raise EngineException( |
| 2758 | "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 2759 | ) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2760 | |
| 2761 | |
| 2762 | class NsiLcmOpTopic(BaseTopic): |
| 2763 | topic = "nsilcmops" |
| 2764 | topic_msg = "nsi" |
| 2765 | operation_schema = { # mapping between operation and jsonschema to validate |
| 2766 | "instantiate": nsi_instantiate, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2767 | "terminate": None, |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2768 | } |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2769 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 2770 | def __init__(self, db, fs, msg, auth): |
| 2771 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 2772 | self.nsi_NsLcmOpTopic = NsLcmOpTopic(self.db, self.fs, self.msg, self.auth) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2773 | |
| 2774 | def _check_nsi_operation(self, session, nsir, operation, indata): |
| 2775 | """ |
| 2776 | Check that user has enter right parameters for the operation |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2777 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2778 | :param operation: it can be: instantiate, terminate, action, TODO: update, heal |
| 2779 | :param indata: descriptor with the parameters of the operation |
| 2780 | :return: None |
| 2781 | """ |
| 2782 | nsds = {} |
| 2783 | nstd = nsir["network-slice-template"] |
| 2784 | |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2785 | def check_valid_netslice_subnet_id(nstId): |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2786 | # TODO change to vnfR (??) |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2787 | for netslice_subnet in nstd["netslice-subnet"]: |
| 2788 | if nstId == netslice_subnet["id"]: |
| 2789 | nsd_id = netslice_subnet["nsd-ref"] |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2790 | if nsd_id not in nsds: |
| Felipe Vicens | 5403f54 | 2020-05-22 16:37:39 +0200 | [diff] [blame] | 2791 | _filter = self._get_project_filter(session) |
| 2792 | _filter["id"] = nsd_id |
| 2793 | nsds[nsd_id] = self.db.get_one("nsds", _filter) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2794 | return nsds[nsd_id] |
| 2795 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2796 | raise EngineException( |
| 2797 | "Invalid parameter nstId='{}' is not one of the " |
| 2798 | "nst:netslice-subnet".format(nstId) |
| 2799 | ) |
| 2800 | |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2801 | if operation == "instantiate": |
| 2802 | # check the existance of netslice-subnet items |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2803 | for in_nst in get_iterable(indata.get("netslice-subnet")): |
| Felipe Vicens | c8bbaaa | 2018-12-01 04:42:40 +0100 | [diff] [blame] | 2804 | check_valid_netslice_subnet_id(in_nst["id"]) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2805 | |
| 2806 | def _create_nsilcmop(self, session, netsliceInstanceId, operation, params): |
| 2807 | now = time() |
| 2808 | _id = str(uuid4()) |
| 2809 | nsilcmop = { |
| 2810 | "id": _id, |
| 2811 | "_id": _id, |
| 2812 | "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK |
| 2813 | "statusEnteredTime": now, |
| 2814 | "netsliceInstanceId": netsliceInstanceId, |
| 2815 | "lcmOperationType": operation, |
| 2816 | "startTime": now, |
| 2817 | "isAutomaticInvocation": False, |
| 2818 | "operationParams": params, |
| 2819 | "isCancelPending": False, |
| 2820 | "links": { |
| 2821 | "self": "/osm/nsilcm/v1/nsi_lcm_op_occs/" + _id, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2822 | "netsliceInstanceId": "/osm/nsilcm/v1/netslice_instances/" |
| 2823 | + netsliceInstanceId, |
| 2824 | }, |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2825 | } |
| 2826 | return nsilcmop |
| 2827 | |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2828 | def add_shared_nsr_2vld(self, nsir, nsr_item): |
| 2829 | for nst_sb_item in nsir["network-slice-template"].get("netslice-subnet"): |
| 2830 | if nst_sb_item.get("is-shared-nss"): |
| 2831 | for admin_subnet_item in nsir["_admin"].get("netslice-subnet"): |
| 2832 | if admin_subnet_item["nss-id"] == nst_sb_item["id"]: |
| 2833 | for admin_vld_item in nsir["_admin"].get("netslice-vld"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2834 | for admin_vld_nss_cp_ref_item in admin_vld_item[ |
| 2835 | "nss-connection-point-ref" |
| 2836 | ]: |
| 2837 | if ( |
| 2838 | admin_subnet_item["nss-id"] |
| 2839 | == admin_vld_nss_cp_ref_item["nss-ref"] |
| 2840 | ): |
| 2841 | if ( |
| 2842 | not nsr_item["nsrId"] |
| 2843 | in admin_vld_item["shared-nsrs-list"] |
| 2844 | ): |
| 2845 | admin_vld_item["shared-nsrs-list"].append( |
| 2846 | nsr_item["nsrId"] |
| 2847 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2848 | break |
| 2849 | # self.db.set_one("nsis", {"_id": nsir["_id"]}, nsir) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2850 | self.db.set_one( |
| 2851 | "nsis", |
| 2852 | {"_id": nsir["_id"]}, |
| 2853 | {"_admin.netslice-vld": nsir["_admin"].get("netslice-vld")}, |
| 2854 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2855 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2856 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2857 | """ |
| 2858 | Performs a new operation over a ns |
| 2859 | :param rollback: list to append created items at database in case a rollback must to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2860 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2861 | :param indata: descriptor with the parameters of the operation. It must contains among others |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 2862 | netsliceInstanceId: _id of the nsir to perform the operation |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2863 | operation: it can be: instantiate, terminate, action, TODO: update, heal |
| 2864 | :param kwargs: used to override the indata descriptor |
| 2865 | :param headers: http request headers |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2866 | :return: id of the nslcmops |
| 2867 | """ |
| 2868 | try: |
| 2869 | # Override descriptor with query string kwargs |
| 2870 | self._update_input_with_kwargs(indata, kwargs) |
| 2871 | operation = indata["lcmOperationType"] |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 2872 | netsliceInstanceId = indata["netsliceInstanceId"] |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2873 | validate_input(indata, self.operation_schema[operation]) |
| 2874 | |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 2875 | # get nsi from netsliceInstanceId |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2876 | _filter = self._get_project_filter(session) |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 2877 | _filter["_id"] = netsliceInstanceId |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2878 | nsir = self.db.get_one("nsis", _filter) |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2879 | logging_prefix = "nsi={} {} ".format(netsliceInstanceId, operation) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 2880 | del _filter["_id"] |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2881 | |
| 2882 | # initial checking |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2883 | if ( |
| 2884 | not nsir["_admin"].get("nsiState") |
| 2885 | or nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED" |
| 2886 | ): |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2887 | if operation == "terminate" and indata.get("autoremove"): |
| 2888 | # NSIR must be deleted |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2889 | return ( |
| 2890 | None, |
| 2891 | None, |
| 2892 | ) # a none in this case is used to indicate not instantiated. It can be removed |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2893 | if operation != "instantiate": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2894 | raise EngineException( |
| 2895 | "netslice_instance '{}' cannot be '{}' because it is not instantiated".format( |
| 2896 | netsliceInstanceId, operation |
| 2897 | ), |
| 2898 | HTTPStatus.CONFLICT, |
| 2899 | ) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2900 | else: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 2901 | if operation == "instantiate" and not session["force"]: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2902 | raise EngineException( |
| 2903 | "netslice_instance '{}' cannot be '{}' because it is already instantiated".format( |
| 2904 | netsliceInstanceId, operation |
| 2905 | ), |
| 2906 | HTTPStatus.CONFLICT, |
| 2907 | ) |
| 2908 | |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2909 | # Creating all the NS_operation (nslcmop) |
| 2910 | # Get service list from db |
| 2911 | nsrs_list = nsir["_admin"]["nsrs-detailed-list"] |
| 2912 | nslcmops = [] |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2913 | # nslcmops_item = None |
| 2914 | for index, nsr_item in enumerate(nsrs_list): |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2915 | nsr_id = nsr_item["nsrId"] |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2916 | if nsr_item.get("shared"): |
| Felipe Vicens | 58e2d2f | 2019-05-30 13:01:20 +0200 | [diff] [blame] | 2917 | _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2918 | _filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_id |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2919 | _filter[ |
| 2920 | "_admin.nsrs-detailed-list.ANYINDEX.nslcmop_instantiate.ne" |
| 2921 | ] = None |
| Felipe Vicens | 126af57 | 2019-06-05 19:13:04 +0200 | [diff] [blame] | 2922 | _filter["_id.ne"] = netsliceInstanceId |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2923 | nsi = self.db.get_one( |
| 2924 | "nsis", _filter, fail_on_empty=False, fail_on_more=False |
| 2925 | ) |
| Felipe Vicens | 58e2d2f | 2019-05-30 13:01:20 +0200 | [diff] [blame] | 2926 | if operation == "terminate": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2927 | _update = { |
| 2928 | "_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format( |
| 2929 | index |
| 2930 | ): None |
| 2931 | } |
| Felipe Vicens | 58e2d2f | 2019-05-30 13:01:20 +0200 | [diff] [blame] | 2932 | self.db.set_one("nsis", {"_id": nsir["_id"]}, _update) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2933 | if ( |
| 2934 | nsi |
| 2935 | ): # other nsi is using this nsr and it needs this nsr instantiated |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2936 | continue # do not create nsilcmop |
| 2937 | else: # instantiate |
| 2938 | # looks the first nsi fulfilling the conditions but not being the current NSIR |
| 2939 | if nsi: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2940 | nsi_nsr_item = next( |
| 2941 | n |
| 2942 | for n in nsi["_admin"]["nsrs-detailed-list"] |
| 2943 | if n["nsrId"] == nsr_id |
| 2944 | and n["shared"] |
| 2945 | and n["nslcmop_instantiate"] |
| 2946 | ) |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2947 | self.add_shared_nsr_2vld(nsir, nsr_item) |
| 2948 | nslcmops.append(nsi_nsr_item["nslcmop_instantiate"]) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2949 | _update = { |
| 2950 | "_admin.nsrs-detailed-list.{}".format( |
| 2951 | index |
| 2952 | ): nsi_nsr_item |
| 2953 | } |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2954 | self.db.set_one("nsis", {"_id": nsir["_id"]}, _update) |
| 2955 | # continue to not create nslcmop since nsrs is shared and nsrs was created |
| 2956 | continue |
| 2957 | else: |
| 2958 | self.add_shared_nsr_2vld(nsir, nsr_item) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2959 | |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2960 | # create operation |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2961 | try: |
| tierno | 0b8752f | 2020-05-12 09:42:02 +0000 | [diff] [blame] | 2962 | indata_ns = { |
| 2963 | "lcmOperationType": operation, |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2964 | "nsInstanceId": nsr_id, |
| tierno | 0b8752f | 2020-05-12 09:42:02 +0000 | [diff] [blame] | 2965 | # Including netslice_id in the ns instantiate Operation |
| 2966 | "netsliceInstanceId": netsliceInstanceId, |
| 2967 | } |
| 2968 | if operation == "instantiate": |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2969 | service = self.db.get_one("nsrs", {"_id": nsr_id}) |
| tierno | 0b8752f | 2020-05-12 09:42:02 +0000 | [diff] [blame] | 2970 | indata_ns.update(service["instantiate_params"]) |
| 2971 | |
| tierno | 99d4b17 | 2019-07-02 09:28:40 +0000 | [diff] [blame] | 2972 | # Creating NS_LCM_OP with the flag slice_object=True to not trigger the service instantiation |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2973 | # message via kafka bus |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2974 | nslcmop, _ = self.nsi_NsLcmOpTopic.new( |
| 2975 | rollback, session, indata_ns, None, headers, slice_object=True |
| 2976 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2977 | nslcmops.append(nslcmop) |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2978 | if operation == "instantiate": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2979 | _update = { |
| 2980 | "_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format( |
| 2981 | index |
| 2982 | ): nslcmop |
| 2983 | } |
| tierno | 40f742b | 2020-06-23 15:25:26 +0000 | [diff] [blame] | 2984 | self.db.set_one("nsis", {"_id": nsir["_id"]}, _update) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2985 | except (DbException, EngineException) as e: |
| 2986 | if e.http_code == HTTPStatus.NOT_FOUND: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2987 | self.logger.info( |
| 2988 | logging_prefix |
| 2989 | + "skipping NS={} because not found".format(nsr_id) |
| 2990 | ) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2991 | pass |
| 2992 | else: |
| 2993 | raise |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 2994 | |
| 2995 | # Creates nsilcmop |
| 2996 | indata["nslcmops_ids"] = nslcmops |
| 2997 | self._check_nsi_operation(session, nsir, operation, indata) |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 2998 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 2999 | nsilcmop_desc = self._create_nsilcmop( |
| 3000 | session, netsliceInstanceId, operation, indata |
| 3001 | ) |
| 3002 | self.format_on_new( |
| 3003 | nsilcmop_desc, session["project_id"], make_public=session["public"] |
| 3004 | ) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 3005 | _id = self.db.create("nsilcmops", nsilcmop_desc) |
| 3006 | rollback.append({"topic": "nsilcmops", "_id": _id}) |
| 3007 | self.msg.write("nsi", operation, nsilcmop_desc) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 3008 | return _id, None |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 3009 | except ValidationError as e: |
| 3010 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 3011 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 3012 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 3013 | raise EngineException( |
| 3014 | "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 3015 | ) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 3016 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 3017 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 3018 | raise EngineException( |
| 3019 | "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR |
| 3020 | ) |