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