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