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