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