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