| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U. |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | ## |
| 18 | |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 19 | from http import HTTPStatus |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 20 | from itertools import product |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 21 | import logging |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 22 | from random import choice as random_choice |
| 23 | from threading import Lock |
| 24 | from time import time |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 25 | from traceback import format_exc as traceback_format_exc |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 26 | from typing import Any, Dict, List, Optional, Tuple, Type |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 27 | from uuid import uuid4 |
| 28 | |
| 29 | from cryptography.hazmat.backends import default_backend as crypto_default_backend |
| 30 | from cryptography.hazmat.primitives import serialization as crypto_serialization |
| 31 | from cryptography.hazmat.primitives.asymmetric import rsa |
| 32 | from jinja2 import ( |
| 33 | Environment, |
| aticig | 7b521f7 | 2022-07-15 00:43:09 +0300 | [diff] [blame] | 34 | select_autoescape, |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 35 | StrictUndefined, |
| 36 | TemplateError, |
| 37 | TemplateNotFound, |
| 38 | UndefinedError, |
| 39 | ) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 40 | from osm_common import ( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 41 | dbmemory, |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 42 | dbmongo, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 43 | fslocal, |
| 44 | fsmongo, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 45 | msgkafka, |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 46 | msglocal, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 47 | version as common_version, |
| 48 | ) |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 49 | from osm_common.dbbase import DbBase, DbException |
| 50 | from osm_common.fsbase import FsBase, FsException |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 51 | from osm_common.msgbase import MsgException |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 52 | from osm_ng_ro.ns_thread import deep_get, NsWorker, NsWorkerException |
| 53 | from osm_ng_ro.validation import deploy_schema, validate_input |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 54 | import yaml |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 55 | |
| 56 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 57 | min_common_version = "0.1.16" |
| 58 | |
| 59 | |
| 60 | class NsException(Exception): |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 61 | def __init__(self, message, http_code=HTTPStatus.BAD_REQUEST): |
| 62 | self.http_code = http_code |
| 63 | super(Exception, self).__init__(message) |
| 64 | |
| 65 | |
| 66 | def get_process_id(): |
| 67 | """ |
| 68 | Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it |
| 69 | will provide a random one |
| 70 | :return: Obtained ID |
| 71 | """ |
| 72 | # Try getting docker id. If fails, get pid |
| 73 | try: |
| 74 | with open("/proc/self/cgroup", "r") as f: |
| 75 | text_id_ = f.readline() |
| 76 | _, _, text_id = text_id_.rpartition("/") |
| 77 | text_id = text_id.replace("\n", "")[:12] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 78 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 79 | if text_id: |
| 80 | return text_id |
| aticig | 7b521f7 | 2022-07-15 00:43:09 +0300 | [diff] [blame] | 81 | except Exception as error: |
| 82 | logging.exception(f"{error} occured while getting process id") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 83 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 84 | # Return a random id |
| 85 | return "".join(random_choice("0123456789abcdef") for _ in range(12)) |
| 86 | |
| 87 | |
| 88 | def versiontuple(v): |
| 89 | """utility for compare dot separate versions. Fills with zeros to proper number comparison""" |
| 90 | filled = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 91 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 92 | for point in v.split("."): |
| 93 | filled.append(point.zfill(8)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 94 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 95 | return tuple(filled) |
| 96 | |
| 97 | |
| 98 | class Ns(object): |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 99 | def __init__(self): |
| 100 | self.db = None |
| 101 | self.fs = None |
| 102 | self.msg = None |
| 103 | self.config = None |
| 104 | # self.operations = None |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 105 | self.logger = None |
| 106 | # ^ Getting logger inside method self.start because parent logger (ro) is not available yet. |
| 107 | # If done now it will not be linked to parent not getting its handler and level |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 108 | self.map_topic = {} |
| 109 | self.write_lock = None |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 110 | self.vims_assigned = {} |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 111 | self.next_worker = 0 |
| 112 | self.plugins = {} |
| 113 | self.workers = [] |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 114 | self.process_params_function_map = { |
| 115 | "net": Ns._process_net_params, |
| 116 | "image": Ns._process_image_params, |
| 117 | "flavor": Ns._process_flavor_params, |
| 118 | "vdu": Ns._process_vdu_params, |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 119 | "affinity-or-anti-affinity-group": Ns._process_affinity_group_params, |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 120 | "shared-volumes": Ns._process_shared_volumes_params, |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 121 | } |
| 122 | self.db_path_map = { |
| 123 | "net": "vld", |
| 124 | "image": "image", |
| 125 | "flavor": "flavor", |
| 126 | "vdu": "vdur", |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 127 | "affinity-or-anti-affinity-group": "affinity-or-anti-affinity-group", |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 128 | "shared-volumes": "shared-volumes", |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 129 | } |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 130 | |
| 131 | def init_db(self, target_version): |
| 132 | pass |
| 133 | |
| 134 | def start(self, config): |
| 135 | """ |
| 136 | Connect to database, filesystem storage, and messaging |
| 137 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 138 | :param config: Configuration of db, storage, etc |
| 139 | :return: None |
| 140 | """ |
| 141 | self.config = config |
| 142 | self.config["process_id"] = get_process_id() # used for HA identity |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 143 | self.logger = logging.getLogger("ro.ns") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 144 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 145 | # check right version of common |
| 146 | if versiontuple(common_version) < versiontuple(min_common_version): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 147 | raise NsException( |
| 148 | "Not compatible osm/common version '{}'. Needed '{}' or higher".format( |
| 149 | common_version, min_common_version |
| 150 | ) |
| 151 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 152 | |
| 153 | try: |
| 154 | if not self.db: |
| 155 | if config["database"]["driver"] == "mongo": |
| 156 | self.db = dbmongo.DbMongo() |
| 157 | self.db.db_connect(config["database"]) |
| 158 | elif config["database"]["driver"] == "memory": |
| 159 | self.db = dbmemory.DbMemory() |
| 160 | self.db.db_connect(config["database"]) |
| 161 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 162 | raise NsException( |
| 163 | "Invalid configuration param '{}' at '[database]':'driver'".format( |
| 164 | config["database"]["driver"] |
| 165 | ) |
| 166 | ) |
| 167 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 168 | if not self.fs: |
| 169 | if config["storage"]["driver"] == "local": |
| 170 | self.fs = fslocal.FsLocal() |
| 171 | self.fs.fs_connect(config["storage"]) |
| 172 | elif config["storage"]["driver"] == "mongo": |
| 173 | self.fs = fsmongo.FsMongo() |
| 174 | self.fs.fs_connect(config["storage"]) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 175 | elif config["storage"]["driver"] is None: |
| 176 | pass |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 177 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 178 | raise NsException( |
| 179 | "Invalid configuration param '{}' at '[storage]':'driver'".format( |
| 180 | config["storage"]["driver"] |
| 181 | ) |
| 182 | ) |
| 183 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 184 | if not self.msg: |
| 185 | if config["message"]["driver"] == "local": |
| 186 | self.msg = msglocal.MsgLocal() |
| 187 | self.msg.connect(config["message"]) |
| 188 | elif config["message"]["driver"] == "kafka": |
| 189 | self.msg = msgkafka.MsgKafka() |
| 190 | self.msg.connect(config["message"]) |
| 191 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 192 | raise NsException( |
| 193 | "Invalid configuration param '{}' at '[message]':'driver'".format( |
| 194 | config["message"]["driver"] |
| 195 | ) |
| 196 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 197 | |
| 198 | # TODO load workers to deal with exising database tasks |
| 199 | |
| 200 | self.write_lock = Lock() |
| 201 | except (DbException, FsException, MsgException) as e: |
| 202 | raise NsException(str(e), http_code=e.http_code) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 203 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 204 | def get_assigned_vims(self): |
| 205 | return list(self.vims_assigned.keys()) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 206 | |
| 207 | def stop(self): |
| 208 | try: |
| 209 | if self.db: |
| 210 | self.db.db_disconnect() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 211 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 212 | if self.fs: |
| 213 | self.fs.fs_disconnect() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 214 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 215 | if self.msg: |
| 216 | self.msg.disconnect() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 217 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 218 | self.write_lock = None |
| 219 | except (DbException, FsException, MsgException) as e: |
| 220 | raise NsException(str(e), http_code=e.http_code) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 221 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 222 | for worker in self.workers: |
| 223 | worker.insert_task(("terminate",)) |
| 224 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 225 | def _create_worker(self): |
| 226 | """ |
| 227 | Look for a worker thread in idle status. If not found it creates one unless the number of threads reach the |
| 228 | limit of 'server.ns_threads' configuration. If reached, it just assigns one existing thread |
| 229 | return the index of the assigned worker thread. Worker threads are storead at self.workers |
| 230 | """ |
| 231 | # Look for a thread in idle status |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 232 | worker_id = next( |
| 233 | ( |
| 234 | i |
| 235 | for i in range(len(self.workers)) |
| 236 | if self.workers[i] and self.workers[i].idle |
| 237 | ), |
| 238 | None, |
| 239 | ) |
| 240 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 241 | if worker_id is not None: |
| 242 | # unset idle status to avoid race conditions |
| 243 | self.workers[worker_id].idle = False |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 244 | else: |
| 245 | worker_id = len(self.workers) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 246 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 247 | if worker_id < self.config["global"]["server.ns_threads"]: |
| 248 | # create a new worker |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 249 | self.workers.append( |
| 250 | NsWorker(worker_id, self.config, self.plugins, self.db) |
| 251 | ) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 252 | self.workers[worker_id].start() |
| 253 | else: |
| 254 | # reached maximum number of threads, assign VIM to an existing one |
| 255 | worker_id = self.next_worker |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 256 | self.next_worker = (self.next_worker + 1) % self.config["global"][ |
| 257 | "server.ns_threads" |
| 258 | ] |
| 259 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 260 | return worker_id |
| 261 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 262 | def assign_vim(self, target_id): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 263 | with self.write_lock: |
| 264 | return self._assign_vim(target_id) |
| 265 | |
| 266 | def _assign_vim(self, target_id): |
| 267 | if target_id not in self.vims_assigned: |
| 268 | worker_id = self.vims_assigned[target_id] = self._create_worker() |
| 269 | self.workers[worker_id].insert_task(("load_vim", target_id)) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 270 | |
| 271 | def reload_vim(self, target_id): |
| 272 | # send reload_vim to the thread working with this VIM and inform all that a VIM has been changed, |
| 273 | # this is because database VIM information is cached for threads working with SDN |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 274 | with self.write_lock: |
| 275 | for worker in self.workers: |
| 276 | if worker and not worker.idle: |
| 277 | worker.insert_task(("reload_vim", target_id)) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 278 | |
| 279 | def unload_vim(self, target_id): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 280 | with self.write_lock: |
| 281 | return self._unload_vim(target_id) |
| 282 | |
| 283 | def _unload_vim(self, target_id): |
| 284 | if target_id in self.vims_assigned: |
| 285 | worker_id = self.vims_assigned[target_id] |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 286 | self.workers[worker_id].insert_task(("unload_vim", target_id)) |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 287 | del self.vims_assigned[target_id] |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 288 | |
| 289 | def check_vim(self, target_id): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 290 | with self.write_lock: |
| 291 | if target_id in self.vims_assigned: |
| 292 | worker_id = self.vims_assigned[target_id] |
| 293 | else: |
| 294 | worker_id = self._create_worker() |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 295 | |
| 296 | worker = self.workers[worker_id] |
| 297 | worker.insert_task(("check_vim", target_id)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 298 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 299 | def unload_unused_vims(self): |
| 300 | with self.write_lock: |
| 301 | vims_to_unload = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 302 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 303 | for target_id in self.vims_assigned: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 304 | if not self.db.get_one( |
| 305 | "ro_tasks", |
| 306 | q_filter={ |
| 307 | "target_id": target_id, |
| 308 | "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"], |
| 309 | }, |
| 310 | fail_on_empty=False, |
| 311 | ): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 312 | vims_to_unload.append(target_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 313 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 314 | for target_id in vims_to_unload: |
| 315 | self._unload_vim(target_id) |
| 316 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 317 | @staticmethod |
| 318 | def _get_cloud_init( |
| 319 | db: Type[DbBase], |
| 320 | fs: Type[FsBase], |
| 321 | location: str, |
| 322 | ) -> str: |
| 323 | """This method reads cloud init from a file. |
| 324 | |
| 325 | Note: Not used as cloud init content is provided in the http body. |
| 326 | |
| 327 | Args: |
| 328 | db (Type[DbBase]): [description] |
| 329 | fs (Type[FsBase]): [description] |
| 330 | location (str): can be 'vnfr_id:file:file_name' or 'vnfr_id:vdu:vdu_idex' |
| 331 | |
| 332 | Raises: |
| 333 | NsException: [description] |
| 334 | NsException: [description] |
| 335 | |
| 336 | Returns: |
| 337 | str: [description] |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 338 | """ |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 339 | vnfd_id, _, other = location.partition(":") |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 340 | _type, _, name = other.partition(":") |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 341 | vnfd = db.get_one("vnfds", {"_id": vnfd_id}) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 342 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 343 | if _type == "file": |
| 344 | base_folder = vnfd["_admin"]["storage"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 345 | cloud_init_file = "{}/{}/cloud_init/{}".format( |
| 346 | base_folder["folder"], base_folder["pkg-dir"], name |
| 347 | ) |
| 348 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 349 | if not fs: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 350 | raise NsException( |
| 351 | "Cannot read file '{}'. Filesystem not loaded, change configuration at storage.driver".format( |
| 352 | cloud_init_file |
| 353 | ) |
| 354 | ) |
| 355 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 356 | with fs.file_open(cloud_init_file, "r") as ci_file: |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 357 | cloud_init_content = ci_file.read() |
| 358 | elif _type == "vdu": |
| 359 | cloud_init_content = vnfd["vdu"][int(name)]["cloud-init"] |
| 360 | else: |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 361 | raise NsException("Mismatch descriptor for cloud init: {}".format(location)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 362 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 363 | return cloud_init_content |
| 364 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 365 | @staticmethod |
| 366 | def _parse_jinja2( |
| 367 | cloud_init_content: str, |
| 368 | params: Dict[str, Any], |
| 369 | context: str, |
| 370 | ) -> str: |
| 371 | """Function that processes the cloud init to replace Jinja2 encoded parameters. |
| 372 | |
| 373 | Args: |
| 374 | cloud_init_content (str): [description] |
| 375 | params (Dict[str, Any]): [description] |
| 376 | context (str): [description] |
| 377 | |
| 378 | Raises: |
| 379 | NsException: [description] |
| 380 | NsException: [description] |
| 381 | |
| 382 | Returns: |
| 383 | str: [description] |
| 384 | """ |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 385 | try: |
| aticig | 7b521f7 | 2022-07-15 00:43:09 +0300 | [diff] [blame] | 386 | env = Environment( |
| 387 | undefined=StrictUndefined, |
| 388 | autoescape=select_autoescape(default_for_string=True, default=True), |
| 389 | ) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 390 | template = env.from_string(cloud_init_content) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 391 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 392 | return template.render(params or {}) |
| 393 | except UndefinedError as e: |
| 394 | raise NsException( |
| 395 | "Variable '{}' defined at vnfd='{}' must be provided in the instantiation parameters" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 396 | "inside the 'additionalParamsForVnf' block".format(e, context) |
| 397 | ) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 398 | except (TemplateError, TemplateNotFound) as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 399 | raise NsException( |
| 400 | "Error parsing Jinja2 to cloud-init content at vnfd='{}': {}".format( |
| 401 | context, e |
| 402 | ) |
| 403 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 404 | |
| 405 | def _create_db_ro_nsrs(self, nsr_id, now): |
| 406 | try: |
| 407 | key = rsa.generate_private_key( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 408 | backend=crypto_default_backend(), public_exponent=65537, key_size=2048 |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 409 | ) |
| 410 | private_key = key.private_bytes( |
| 411 | crypto_serialization.Encoding.PEM, |
| 412 | crypto_serialization.PrivateFormat.PKCS8, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 413 | crypto_serialization.NoEncryption(), |
| 414 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 415 | public_key = key.public_key().public_bytes( |
| 416 | crypto_serialization.Encoding.OpenSSH, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 417 | crypto_serialization.PublicFormat.OpenSSH, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 418 | ) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 419 | private_key = private_key.decode("utf8") |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 420 | # Change first line because Paramiko needs a explicit start with 'BEGIN RSA PRIVATE KEY' |
| 421 | i = private_key.find("\n") |
| 422 | private_key = "-----BEGIN RSA PRIVATE KEY-----" + private_key[i:] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 423 | public_key = public_key.decode("utf8") |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 424 | except Exception as e: |
| 425 | raise NsException("Cannot create ssh-keys: {}".format(e)) |
| 426 | |
| 427 | schema_version = "1.1" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 428 | private_key_encrypted = self.db.encrypt( |
| 429 | private_key, schema_version=schema_version, salt=nsr_id |
| 430 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 431 | db_content = { |
| 432 | "_id": nsr_id, |
| 433 | "_admin": { |
| 434 | "created": now, |
| 435 | "modified": now, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 436 | "schema_version": schema_version, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 437 | }, |
| 438 | "public_key": public_key, |
| 439 | "private_key": private_key_encrypted, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 440 | "actions": [], |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 441 | } |
| 442 | self.db.create("ro_nsrs", db_content) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 443 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 444 | return db_content |
| 445 | |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 446 | @staticmethod |
| 447 | def _create_task( |
| 448 | deployment_info: Dict[str, Any], |
| 449 | target_id: str, |
| 450 | item: str, |
| 451 | action: str, |
| 452 | target_record: str, |
| 453 | target_record_id: str, |
| 454 | extra_dict: Dict[str, Any] = None, |
| 455 | ) -> Dict[str, Any]: |
| 456 | """Function to create task dict from deployment information. |
| 457 | |
| 458 | Args: |
| 459 | deployment_info (Dict[str, Any]): [description] |
| 460 | target_id (str): [description] |
| 461 | item (str): [description] |
| 462 | action (str): [description] |
| 463 | target_record (str): [description] |
| 464 | target_record_id (str): [description] |
| 465 | extra_dict (Dict[str, Any], optional): [description]. Defaults to None. |
| 466 | |
| 467 | Returns: |
| 468 | Dict[str, Any]: [description] |
| 469 | """ |
| 470 | task = { |
| 471 | "target_id": target_id, # it will be removed before pushing at database |
| 472 | "action_id": deployment_info.get("action_id"), |
| 473 | "nsr_id": deployment_info.get("nsr_id"), |
| 474 | "task_id": f"{deployment_info.get('action_id')}:{deployment_info.get('task_index')}", |
| 475 | "status": "SCHEDULED", |
| 476 | "action": action, |
| 477 | "item": item, |
| 478 | "target_record": target_record, |
| 479 | "target_record_id": target_record_id, |
| 480 | } |
| 481 | |
| 482 | if extra_dict: |
| 483 | task.update(extra_dict) # params, find_params, depends_on |
| 484 | |
| 485 | deployment_info["task_index"] = deployment_info.get("task_index", 0) + 1 |
| 486 | |
| 487 | return task |
| 488 | |
| sousaedu | 839e5ca | 2021-11-19 16:41:38 +0000 | [diff] [blame] | 489 | @staticmethod |
| 490 | def _create_ro_task( |
| 491 | target_id: str, |
| 492 | task: Dict[str, Any], |
| 493 | ) -> Dict[str, Any]: |
| 494 | """Function to create an RO task from task information. |
| 495 | |
| 496 | Args: |
| 497 | target_id (str): [description] |
| 498 | task (Dict[str, Any]): [description] |
| 499 | |
| 500 | Returns: |
| 501 | Dict[str, Any]: [description] |
| 502 | """ |
| 503 | now = time() |
| 504 | |
| 505 | _id = task.get("task_id") |
| 506 | db_ro_task = { |
| 507 | "_id": _id, |
| 508 | "locked_by": None, |
| 509 | "locked_at": 0.0, |
| 510 | "target_id": target_id, |
| 511 | "vim_info": { |
| 512 | "created": False, |
| 513 | "created_items": None, |
| 514 | "vim_id": None, |
| 515 | "vim_name": None, |
| 516 | "vim_status": None, |
| 517 | "vim_details": None, |
| aticig | 79ac6df | 2022-05-06 16:09:52 +0300 | [diff] [blame] | 518 | "vim_message": None, |
| sousaedu | 839e5ca | 2021-11-19 16:41:38 +0000 | [diff] [blame] | 519 | "refresh_at": None, |
| 520 | }, |
| 521 | "modified_at": now, |
| 522 | "created_at": now, |
| 523 | "to_check_at": now, |
| 524 | "tasks": [task], |
| 525 | } |
| 526 | |
| 527 | return db_ro_task |
| 528 | |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 529 | @staticmethod |
| 530 | def _process_image_params( |
| 531 | target_image: Dict[str, Any], |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 532 | indata: Dict[str, Any], |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 533 | vim_info: Dict[str, Any], |
| 534 | target_record_id: str, |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 535 | **kwargs: Dict[str, Any], |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 536 | ) -> Dict[str, Any]: |
| 537 | """Function to process VDU image parameters. |
| 538 | |
| 539 | Args: |
| 540 | target_image (Dict[str, Any]): [description] |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 541 | indata (Dict[str, Any]): [description] |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 542 | vim_info (Dict[str, Any]): [description] |
| 543 | target_record_id (str): [description] |
| 544 | |
| 545 | Returns: |
| 546 | Dict[str, Any]: [description] |
| 547 | """ |
| 548 | find_params = {} |
| 549 | |
| 550 | if target_image.get("image"): |
| 551 | find_params["filter_dict"] = {"name": target_image.get("image")} |
| 552 | |
| 553 | if target_image.get("vim_image_id"): |
| 554 | find_params["filter_dict"] = {"id": target_image.get("vim_image_id")} |
| 555 | |
| 556 | if target_image.get("image_checksum"): |
| 557 | find_params["filter_dict"] = { |
| 558 | "checksum": target_image.get("image_checksum") |
| 559 | } |
| 560 | |
| 561 | return {"find_params": find_params} |
| 562 | |
| sousaedu | abdfe78 | 2021-11-22 23:56:28 +0000 | [diff] [blame] | 563 | @staticmethod |
| 564 | def _get_resource_allocation_params( |
| 565 | quota_descriptor: Dict[str, Any], |
| 566 | ) -> Dict[str, Any]: |
| 567 | """Read the quota_descriptor from vnfd and fetch the resource allocation properties from the |
| 568 | descriptor object. |
| 569 | |
| 570 | Args: |
| 571 | quota_descriptor (Dict[str, Any]): cpu/mem/vif/disk-io quota descriptor |
| 572 | |
| 573 | Returns: |
| 574 | Dict[str, Any]: quota params for limit, reserve, shares from the descriptor object |
| 575 | """ |
| 576 | quota = {} |
| 577 | |
| 578 | if quota_descriptor.get("limit"): |
| 579 | quota["limit"] = int(quota_descriptor["limit"]) |
| 580 | |
| 581 | if quota_descriptor.get("reserve"): |
| 582 | quota["reserve"] = int(quota_descriptor["reserve"]) |
| 583 | |
| 584 | if quota_descriptor.get("shares"): |
| 585 | quota["shares"] = int(quota_descriptor["shares"]) |
| 586 | |
| 587 | return quota |
| 588 | |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 589 | @staticmethod |
| 590 | def _process_guest_epa_quota_params( |
| 591 | guest_epa_quota: Dict[str, Any], |
| 592 | epa_vcpu_set: bool, |
| 593 | ) -> Dict[str, Any]: |
| 594 | """Function to extract the guest epa quota parameters. |
| 595 | |
| 596 | Args: |
| 597 | guest_epa_quota (Dict[str, Any]): [description] |
| 598 | epa_vcpu_set (bool): [description] |
| 599 | |
| 600 | Returns: |
| 601 | Dict[str, Any]: [description] |
| 602 | """ |
| 603 | result = {} |
| 604 | |
| 605 | if guest_epa_quota.get("cpu-quota") and not epa_vcpu_set: |
| 606 | cpuquota = Ns._get_resource_allocation_params( |
| 607 | guest_epa_quota.get("cpu-quota") |
| 608 | ) |
| 609 | |
| 610 | if cpuquota: |
| 611 | result["cpu-quota"] = cpuquota |
| 612 | |
| 613 | if guest_epa_quota.get("mem-quota"): |
| 614 | vduquota = Ns._get_resource_allocation_params( |
| 615 | guest_epa_quota.get("mem-quota") |
| 616 | ) |
| 617 | |
| 618 | if vduquota: |
| 619 | result["mem-quota"] = vduquota |
| 620 | |
| 621 | if guest_epa_quota.get("disk-io-quota"): |
| 622 | diskioquota = Ns._get_resource_allocation_params( |
| 623 | guest_epa_quota.get("disk-io-quota") |
| 624 | ) |
| 625 | |
| 626 | if diskioquota: |
| 627 | result["disk-io-quota"] = diskioquota |
| 628 | |
| 629 | if guest_epa_quota.get("vif-quota"): |
| 630 | vifquota = Ns._get_resource_allocation_params( |
| 631 | guest_epa_quota.get("vif-quota") |
| 632 | ) |
| 633 | |
| 634 | if vifquota: |
| 635 | result["vif-quota"] = vifquota |
| 636 | |
| 637 | return result |
| 638 | |
| 639 | @staticmethod |
| 640 | def _process_guest_epa_numa_params( |
| 641 | guest_epa_quota: Dict[str, Any], |
| 642 | ) -> Tuple[Dict[str, Any], bool]: |
| 643 | """[summary] |
| 644 | |
| 645 | Args: |
| 646 | guest_epa_quota (Dict[str, Any]): [description] |
| 647 | |
| 648 | Returns: |
| 649 | Tuple[Dict[str, Any], bool]: [description] |
| 650 | """ |
| 651 | numa = {} |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 652 | numa_list = [] |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 653 | epa_vcpu_set = False |
| 654 | |
| 655 | if guest_epa_quota.get("numa-node-policy"): |
| 656 | numa_node_policy = guest_epa_quota.get("numa-node-policy") |
| 657 | |
| 658 | if numa_node_policy.get("node"): |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 659 | for numa_node in numa_node_policy["node"]: |
| 660 | vcpu_list = [] |
| 661 | if numa_node.get("id"): |
| 662 | numa["id"] = int(numa_node["id"]) |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 663 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 664 | if numa_node.get("vcpu"): |
| 665 | for vcpu in numa_node.get("vcpu"): |
| 666 | vcpu_id = int(vcpu.get("id")) |
| 667 | vcpu_list.append(vcpu_id) |
| 668 | numa["vcpu"] = vcpu_list |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 669 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 670 | if numa_node.get("num-cores"): |
| 671 | numa["cores"] = numa_node["num-cores"] |
| 672 | epa_vcpu_set = True |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 673 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 674 | paired_threads = numa_node.get("paired-threads", {}) |
| 675 | if paired_threads.get("num-paired-threads"): |
| 676 | numa["paired_threads"] = int( |
| 677 | numa_node["paired-threads"]["num-paired-threads"] |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 678 | ) |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 679 | epa_vcpu_set = True |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 680 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 681 | if paired_threads.get("paired-thread-ids"): |
| 682 | numa["paired-threads-id"] = [] |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 683 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 684 | for pair in paired_threads["paired-thread-ids"]: |
| 685 | numa["paired-threads-id"].append( |
| 686 | ( |
| 687 | str(pair["thread-a"]), |
| 688 | str(pair["thread-b"]), |
| 689 | ) |
| 690 | ) |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 691 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 692 | if numa_node.get("num-threads"): |
| 693 | numa["threads"] = int(numa_node["num-threads"]) |
| 694 | epa_vcpu_set = True |
| 695 | |
| 696 | if numa_node.get("memory-mb"): |
| 697 | numa["memory"] = max(int(int(numa_node["memory-mb"]) / 1024), 1) |
| 698 | |
| 699 | numa_list.append(numa) |
| 700 | numa = {} |
| 701 | |
| 702 | return numa_list, epa_vcpu_set |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 703 | |
| 704 | @staticmethod |
| 705 | def _process_guest_epa_cpu_pinning_params( |
| 706 | guest_epa_quota: Dict[str, Any], |
| 707 | vcpu_count: int, |
| 708 | epa_vcpu_set: bool, |
| 709 | ) -> Tuple[Dict[str, Any], bool]: |
| 710 | """[summary] |
| 711 | |
| 712 | Args: |
| 713 | guest_epa_quota (Dict[str, Any]): [description] |
| 714 | vcpu_count (int): [description] |
| 715 | epa_vcpu_set (bool): [description] |
| 716 | |
| 717 | Returns: |
| 718 | Tuple[Dict[str, Any], bool]: [description] |
| 719 | """ |
| 720 | numa = {} |
| 721 | local_epa_vcpu_set = epa_vcpu_set |
| 722 | |
| 723 | if ( |
| 724 | guest_epa_quota.get("cpu-pinning-policy") == "DEDICATED" |
| 725 | and not epa_vcpu_set |
| 726 | ): |
| Gulsum Atici | 6a6e334 | 2023-01-23 16:22:59 +0300 | [diff] [blame] | 727 | # Pinning policy "REQUIRE" uses threads as host should support SMT architecture |
| 728 | # Pinning policy "ISOLATE" uses cores as host should not support SMT architecture |
| 729 | # Pinning policy "PREFER" uses threads in case host supports SMT architecture |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 730 | numa[ |
| 731 | "cores" |
| Gulsum Atici | 6a6e334 | 2023-01-23 16:22:59 +0300 | [diff] [blame] | 732 | if guest_epa_quota.get("cpu-thread-pinning-policy") == "ISOLATE" |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 733 | else "threads" |
| 734 | ] = max(vcpu_count, 1) |
| 735 | local_epa_vcpu_set = True |
| 736 | |
| 737 | return numa, local_epa_vcpu_set |
| 738 | |
| 739 | @staticmethod |
| 740 | def _process_epa_params( |
| 741 | target_flavor: Dict[str, Any], |
| 742 | ) -> Dict[str, Any]: |
| 743 | """[summary] |
| 744 | |
| 745 | Args: |
| 746 | target_flavor (Dict[str, Any]): [description] |
| 747 | |
| 748 | Returns: |
| 749 | Dict[str, Any]: [description] |
| 750 | """ |
| 751 | extended = {} |
| 752 | numa = {} |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 753 | numa_list = [] |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 754 | |
| 755 | if target_flavor.get("guest-epa"): |
| 756 | guest_epa = target_flavor["guest-epa"] |
| 757 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 758 | numa_list, epa_vcpu_set = Ns._process_guest_epa_numa_params( |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 759 | guest_epa_quota=guest_epa |
| 760 | ) |
| 761 | |
| 762 | if guest_epa.get("mempage-size"): |
| 763 | extended["mempage-size"] = guest_epa.get("mempage-size") |
| 764 | |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 765 | if guest_epa.get("cpu-pinning-policy"): |
| 766 | extended["cpu-pinning-policy"] = guest_epa.get("cpu-pinning-policy") |
| 767 | |
| 768 | if guest_epa.get("cpu-thread-pinning-policy"): |
| 769 | extended["cpu-thread-pinning-policy"] = guest_epa.get( |
| 770 | "cpu-thread-pinning-policy" |
| 771 | ) |
| 772 | |
| 773 | if guest_epa.get("numa-node-policy"): |
| 774 | if guest_epa.get("numa-node-policy").get("mem-policy"): |
| 775 | extended["mem-policy"] = guest_epa.get("numa-node-policy").get( |
| 776 | "mem-policy" |
| 777 | ) |
| 778 | |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 779 | tmp_numa, epa_vcpu_set = Ns._process_guest_epa_cpu_pinning_params( |
| 780 | guest_epa_quota=guest_epa, |
| 781 | vcpu_count=int(target_flavor.get("vcpu-count", 1)), |
| 782 | epa_vcpu_set=epa_vcpu_set, |
| 783 | ) |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 784 | for numa in numa_list: |
| 785 | numa.update(tmp_numa) |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 786 | |
| 787 | extended.update( |
| 788 | Ns._process_guest_epa_quota_params( |
| 789 | guest_epa_quota=guest_epa, |
| 790 | epa_vcpu_set=epa_vcpu_set, |
| 791 | ) |
| 792 | ) |
| 793 | |
| 794 | if numa: |
| sritharan | 09dcc58 | 2022-10-13 05:43:13 +0000 | [diff] [blame] | 795 | extended["numas"] = numa_list |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 796 | |
| 797 | return extended |
| 798 | |
| 799 | @staticmethod |
| 800 | def _process_flavor_params( |
| 801 | target_flavor: Dict[str, Any], |
| 802 | indata: Dict[str, Any], |
| 803 | vim_info: Dict[str, Any], |
| 804 | target_record_id: str, |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 805 | **kwargs: Dict[str, Any], |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 806 | ) -> Dict[str, Any]: |
| 807 | """[summary] |
| 808 | |
| 809 | Args: |
| 810 | target_flavor (Dict[str, Any]): [description] |
| 811 | indata (Dict[str, Any]): [description] |
| 812 | vim_info (Dict[str, Any]): [description] |
| 813 | target_record_id (str): [description] |
| 814 | |
| 815 | Returns: |
| 816 | Dict[str, Any]: [description] |
| 817 | """ |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 818 | db = kwargs.get("db") |
| 819 | target_vdur = {} |
| 820 | |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 821 | flavor_data = { |
| 822 | "disk": int(target_flavor["storage-gb"]), |
| 823 | "ram": int(target_flavor["memory-mb"]), |
| 824 | "vcpus": int(target_flavor["vcpu-count"]), |
| 825 | } |
| 826 | |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 827 | for vnf in indata.get("vnf", []): |
| 828 | for vdur in vnf.get("vdur", []): |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 829 | if vdur.get("ns-flavor-id") == target_flavor.get("id"): |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 830 | target_vdur = vdur |
| 831 | |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 832 | if db and isinstance(indata.get("vnf"), list): |
| 833 | vnfd_id = indata.get("vnf")[0].get("vnfd-id") |
| 834 | vnfd = db.get_one("vnfds", {"_id": vnfd_id}) |
| 835 | # check if there is persistent root disk |
| 836 | for vdu in vnfd.get("vdu", ()): |
| 837 | if vdu["name"] == target_vdur.get("vdu-name"): |
| 838 | for vsd in vnfd.get("virtual-storage-desc", ()): |
| 839 | if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]: |
| 840 | root_disk = vsd |
| 841 | if ( |
| 842 | root_disk.get("type-of-storage") |
| 843 | == "persistent-storage:persistent-storage" |
| 844 | ): |
| 845 | flavor_data["disk"] = 0 |
| 846 | |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 847 | for storage in target_vdur.get("virtual-storages", []): |
| 848 | if ( |
| 849 | storage.get("type-of-storage") |
| 850 | == "etsi-nfv-descriptors:ephemeral-storage" |
| 851 | ): |
| 852 | flavor_data["ephemeral"] = int(storage.get("size-of-storage", 0)) |
| 853 | elif storage.get("type-of-storage") == "etsi-nfv-descriptors:swap-storage": |
| 854 | flavor_data["swap"] = int(storage.get("size-of-storage", 0)) |
| 855 | |
| 856 | extended = Ns._process_epa_params(target_flavor) |
| 857 | if extended: |
| 858 | flavor_data["extended"] = extended |
| 859 | |
| 860 | extra_dict = {"find_params": {"flavor_data": flavor_data}} |
| 861 | flavor_data_name = flavor_data.copy() |
| 862 | flavor_data_name["name"] = target_flavor["name"] |
| 863 | extra_dict["params"] = {"flavor_data": flavor_data_name} |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 864 | return extra_dict |
| 865 | |
| sousaedu | a4bac08 | 2021-12-05 19:31:03 +0000 | [diff] [blame] | 866 | @staticmethod |
| sousaedu | 0ee9fdb | 2021-12-06 00:17:54 +0000 | [diff] [blame] | 867 | def _process_net_params( |
| 868 | target_vld: Dict[str, Any], |
| 869 | indata: Dict[str, Any], |
| 870 | vim_info: Dict[str, Any], |
| 871 | target_record_id: str, |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 872 | **kwargs: Dict[str, Any], |
| sousaedu | 0ee9fdb | 2021-12-06 00:17:54 +0000 | [diff] [blame] | 873 | ) -> Dict[str, Any]: |
| 874 | """Function to process network parameters. |
| 875 | |
| 876 | Args: |
| 877 | target_vld (Dict[str, Any]): [description] |
| 878 | indata (Dict[str, Any]): [description] |
| 879 | vim_info (Dict[str, Any]): [description] |
| 880 | target_record_id (str): [description] |
| 881 | |
| 882 | Returns: |
| 883 | Dict[str, Any]: [description] |
| 884 | """ |
| 885 | extra_dict = {} |
| 886 | |
| 887 | if vim_info.get("sdn"): |
| 888 | # vnf_preffix = "vnfrs:{}".format(vnfr_id) |
| 889 | # ns_preffix = "nsrs:{}".format(nsr_id) |
| 890 | # remove the ending ".sdn |
| 891 | vld_target_record_id, _, _ = target_record_id.rpartition(".") |
| 892 | extra_dict["params"] = { |
| 893 | k: vim_info[k] |
| 894 | for k in ("sdn-ports", "target_vim", "vlds", "type") |
| 895 | if vim_info.get(k) |
| 896 | } |
| 897 | |
| 898 | # TODO needed to add target_id in the dependency. |
| 899 | if vim_info.get("target_vim"): |
| 900 | extra_dict["depends_on"] = [ |
| 901 | f"{vim_info.get('target_vim')} {vld_target_record_id}" |
| 902 | ] |
| 903 | |
| 904 | return extra_dict |
| 905 | |
| 906 | if vim_info.get("vim_network_name"): |
| 907 | extra_dict["find_params"] = { |
| 908 | "filter_dict": { |
| 909 | "name": vim_info.get("vim_network_name"), |
| 910 | }, |
| 911 | } |
| 912 | elif vim_info.get("vim_network_id"): |
| 913 | extra_dict["find_params"] = { |
| 914 | "filter_dict": { |
| 915 | "id": vim_info.get("vim_network_id"), |
| 916 | }, |
| 917 | } |
| Gabriel Cuba | 0d8ce07 | 2022-12-14 18:33:50 -0500 | [diff] [blame] | 918 | elif target_vld.get("mgmt-network") and not vim_info.get("provider_network"): |
| sousaedu | 0ee9fdb | 2021-12-06 00:17:54 +0000 | [diff] [blame] | 919 | extra_dict["find_params"] = { |
| 920 | "mgmt": True, |
| 921 | "name": target_vld["id"], |
| 922 | } |
| 923 | else: |
| 924 | # create |
| 925 | extra_dict["params"] = { |
| 926 | "net_name": ( |
| 927 | f"{indata.get('name')[:16]}-{target_vld.get('name', target_vld.get('id'))[:16]}" |
| 928 | ), |
| Gabriel Cuba | 4c1dd54 | 2023-02-14 12:43:32 -0500 | [diff] [blame] | 929 | "ip_profile": vim_info.get("ip_profile"), |
| sousaedu | 0ee9fdb | 2021-12-06 00:17:54 +0000 | [diff] [blame] | 930 | "provider_network_profile": vim_info.get("provider_network"), |
| 931 | } |
| 932 | |
| 933 | if not target_vld.get("underlay"): |
| 934 | extra_dict["params"]["net_type"] = "bridge" |
| 935 | else: |
| 936 | extra_dict["params"]["net_type"] = ( |
| 937 | "ptp" if target_vld.get("type") == "ELINE" else "data" |
| 938 | ) |
| 939 | |
| 940 | return extra_dict |
| 941 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 942 | @staticmethod |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 943 | def find_persistent_root_volumes( |
| 944 | vnfd: dict, |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 945 | target_vdu: dict, |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 946 | vdu_instantiation_volumes_list: list, |
| 947 | disk_list: list, |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 948 | ) -> Dict[str, any]: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 949 | """Find the persistent root volumes and add them to the disk_list |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 950 | by parsing the instantiation parameters. |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 951 | |
| 952 | Args: |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 953 | vnfd (dict): VNF descriptor |
| 954 | target_vdu (dict): processed VDU |
| 955 | vdu_instantiation_volumes_list (list): instantiation parameters for the each VDU as a list |
| 956 | disk_list (list): to be filled up |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 957 | |
| 958 | Returns: |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 959 | persistent_root_disk (dict): Details of persistent root disk |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 960 | |
| 961 | """ |
| 962 | persistent_root_disk = {} |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 963 | # There can be only one root disk, when we find it, it will return the result |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 964 | |
| 965 | for vdu, vsd in product( |
| 966 | vnfd.get("vdu", ()), vnfd.get("virtual-storage-desc", ()) |
| 967 | ): |
| 968 | if ( |
| 969 | vdu["name"] == target_vdu["vdu-name"] |
| 970 | and vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0] |
| 971 | ): |
| 972 | root_disk = vsd |
| 973 | if ( |
| 974 | root_disk.get("type-of-storage") |
| 975 | == "persistent-storage:persistent-storage" |
| 976 | ): |
| 977 | for vdu_volume in vdu_instantiation_volumes_list: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 978 | if ( |
| 979 | vdu_volume["vim-volume-id"] |
| 980 | and root_disk["id"] == vdu_volume["name"] |
| 981 | ): |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 982 | persistent_root_disk[vsd["id"]] = { |
| 983 | "vim_volume_id": vdu_volume["vim-volume-id"], |
| 984 | "image_id": vdu.get("sw-image-desc"), |
| 985 | } |
| 986 | |
| 987 | disk_list.append(persistent_root_disk[vsd["id"]]) |
| 988 | |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 989 | return persistent_root_disk |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 990 | |
| 991 | else: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 992 | if root_disk.get("size-of-storage"): |
| 993 | persistent_root_disk[vsd["id"]] = { |
| 994 | "image_id": vdu.get("sw-image-desc"), |
| 995 | "size": root_disk.get("size-of-storage"), |
| aticig | 2f4ab6c | 2022-09-03 18:15:20 +0300 | [diff] [blame] | 996 | "keep": Ns.is_volume_keeping_required(root_disk), |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | disk_list.append(persistent_root_disk[vsd["id"]]) |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1000 | |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1001 | return persistent_root_disk |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1002 | |
| 1003 | @staticmethod |
| 1004 | def find_persistent_volumes( |
| 1005 | persistent_root_disk: dict, |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1006 | target_vdu: dict, |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1007 | vdu_instantiation_volumes_list: list, |
| 1008 | disk_list: list, |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1009 | ) -> None: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1010 | """Find the ordinary persistent volumes and add them to the disk_list |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1011 | by parsing the instantiation parameters. |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1012 | |
| 1013 | Args: |
| 1014 | persistent_root_disk: persistent root disk dictionary |
| 1015 | target_vdu: processed VDU |
| 1016 | vdu_instantiation_volumes_list: instantiation parameters for the each VDU as a list |
| 1017 | disk_list: to be filled up |
| 1018 | |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1019 | """ |
| 1020 | # Find the ordinary volumes which are not added to the persistent_root_disk |
| 1021 | persistent_disk = {} |
| 1022 | for disk in target_vdu.get("virtual-storages", {}): |
| 1023 | if ( |
| 1024 | disk.get("type-of-storage") == "persistent-storage:persistent-storage" |
| 1025 | and disk["id"] not in persistent_root_disk.keys() |
| 1026 | ): |
| 1027 | for vdu_volume in vdu_instantiation_volumes_list: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1028 | if vdu_volume["vim-volume-id"] and disk["id"] == vdu_volume["name"]: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1029 | persistent_disk[disk["id"]] = { |
| 1030 | "vim_volume_id": vdu_volume["vim-volume-id"], |
| 1031 | } |
| 1032 | disk_list.append(persistent_disk[disk["id"]]) |
| 1033 | |
| 1034 | else: |
| 1035 | if disk["id"] not in persistent_disk.keys(): |
| 1036 | persistent_disk[disk["id"]] = { |
| 1037 | "size": disk.get("size-of-storage"), |
| aticig | 2f4ab6c | 2022-09-03 18:15:20 +0300 | [diff] [blame] | 1038 | "keep": Ns.is_volume_keeping_required(disk), |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1039 | } |
| 1040 | disk_list.append(persistent_disk[disk["id"]]) |
| 1041 | |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1042 | @staticmethod |
| aticig | 2f4ab6c | 2022-09-03 18:15:20 +0300 | [diff] [blame] | 1043 | def is_volume_keeping_required(virtual_storage_desc: Dict[str, Any]) -> bool: |
| 1044 | """Function to decide keeping persistent volume |
| 1045 | upon VDU deletion. |
| 1046 | |
| 1047 | Args: |
| 1048 | virtual_storage_desc (Dict[str, Any]): virtual storage description dictionary |
| 1049 | |
| 1050 | Returns: |
| 1051 | bool (True/False) |
| 1052 | """ |
| 1053 | |
| 1054 | if not virtual_storage_desc.get("vdu-storage-requirements"): |
| 1055 | return False |
| 1056 | for item in virtual_storage_desc.get("vdu-storage-requirements", {}): |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1057 | if item.get("key") == "keep-volume" and item.get("value").lower() == "true": |
| aticig | 2f4ab6c | 2022-09-03 18:15:20 +0300 | [diff] [blame] | 1058 | return True |
| 1059 | return False |
| 1060 | |
| 1061 | @staticmethod |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1062 | def is_shared_volume( |
| 1063 | virtual_storage_desc: Dict[str, Any], vnfd_id: str |
| 1064 | ) -> (str, bool): |
| 1065 | """Function to decide if the volume type is multi attached or not . |
| 1066 | |
| 1067 | Args: |
| 1068 | virtual_storage_desc (Dict[str, Any]): virtual storage description dictionary |
| 1069 | vnfd_id (str): vnfd id |
| 1070 | |
| 1071 | Returns: |
| 1072 | bool (True/False) |
| 1073 | name (str) New name if it is a multiattach disk |
| 1074 | """ |
| 1075 | |
| 1076 | if vdu_storage_requirements := virtual_storage_desc.get( |
| 1077 | "vdu-storage-requirements", {} |
| 1078 | ): |
| 1079 | for item in vdu_storage_requirements: |
| 1080 | if ( |
| 1081 | item.get("key") == "multiattach" |
| 1082 | and item.get("value").lower() == "true" |
| 1083 | ): |
| 1084 | name = f"shared-{virtual_storage_desc['id']}-{vnfd_id}" |
| 1085 | return name, True |
| 1086 | return virtual_storage_desc["id"], False |
| 1087 | |
| 1088 | @staticmethod |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1089 | def _sort_vdu_interfaces(target_vdu: dict) -> None: |
| 1090 | """Sort the interfaces according to position number. |
| 1091 | |
| 1092 | Args: |
| 1093 | target_vdu (dict): Details of VDU to be created |
| 1094 | |
| 1095 | """ |
| 1096 | # If the position info is provided for all the interfaces, it will be sorted |
| 1097 | # according to position number ascendingly. |
| 1098 | sorted_interfaces = sorted( |
| 1099 | target_vdu["interfaces"], |
| 1100 | key=lambda x: (x.get("position") is None, x.get("position")), |
| 1101 | ) |
| 1102 | target_vdu["interfaces"] = sorted_interfaces |
| 1103 | |
| 1104 | @staticmethod |
| 1105 | def _partially_locate_vdu_interfaces(target_vdu: dict) -> None: |
| 1106 | """Only place the interfaces which has specific position. |
| 1107 | |
| 1108 | Args: |
| 1109 | target_vdu (dict): Details of VDU to be created |
| 1110 | |
| 1111 | """ |
| 1112 | # If the position info is provided for some interfaces but not all of them, the interfaces |
| 1113 | # which has specific position numbers will be placed and others' positions will not be taken care. |
| 1114 | if any( |
| 1115 | i.get("position") + 1 |
| 1116 | for i in target_vdu["interfaces"] |
| 1117 | if i.get("position") is not None |
| 1118 | ): |
| 1119 | n = len(target_vdu["interfaces"]) |
| 1120 | sorted_interfaces = [-1] * n |
| 1121 | k, m = 0, 0 |
| 1122 | |
| 1123 | while k < n: |
| 1124 | if target_vdu["interfaces"][k].get("position") is not None: |
| 1125 | if any(i.get("position") == 0 for i in target_vdu["interfaces"]): |
| 1126 | idx = target_vdu["interfaces"][k]["position"] + 1 |
| 1127 | else: |
| 1128 | idx = target_vdu["interfaces"][k]["position"] |
| 1129 | sorted_interfaces[idx - 1] = target_vdu["interfaces"][k] |
| 1130 | k += 1 |
| 1131 | |
| 1132 | while m < n: |
| 1133 | if target_vdu["interfaces"][m].get("position") is None: |
| 1134 | idy = sorted_interfaces.index(-1) |
| 1135 | sorted_interfaces[idy] = target_vdu["interfaces"][m] |
| 1136 | m += 1 |
| 1137 | |
| 1138 | target_vdu["interfaces"] = sorted_interfaces |
| 1139 | |
| 1140 | @staticmethod |
| 1141 | def _prepare_vdu_cloud_init( |
| 1142 | target_vdu: dict, vdu2cloud_init: dict, db: object, fs: object |
| 1143 | ) -> Dict: |
| 1144 | """Fill cloud_config dict with cloud init details. |
| 1145 | |
| 1146 | Args: |
| 1147 | target_vdu (dict): Details of VDU to be created |
| 1148 | vdu2cloud_init (dict): Cloud init dict |
| 1149 | db (object): DB object |
| 1150 | fs (object): FS object |
| 1151 | |
| 1152 | Returns: |
| 1153 | cloud_config (dict): Cloud config details of VDU |
| 1154 | |
| 1155 | """ |
| 1156 | # cloud config |
| 1157 | cloud_config = {} |
| 1158 | |
| 1159 | if target_vdu.get("cloud-init"): |
| 1160 | if target_vdu["cloud-init"] not in vdu2cloud_init: |
| 1161 | vdu2cloud_init[target_vdu["cloud-init"]] = Ns._get_cloud_init( |
| 1162 | db=db, |
| 1163 | fs=fs, |
| 1164 | location=target_vdu["cloud-init"], |
| 1165 | ) |
| 1166 | |
| 1167 | cloud_content_ = vdu2cloud_init[target_vdu["cloud-init"]] |
| 1168 | cloud_config["user-data"] = Ns._parse_jinja2( |
| 1169 | cloud_init_content=cloud_content_, |
| 1170 | params=target_vdu.get("additionalParams"), |
| 1171 | context=target_vdu["cloud-init"], |
| 1172 | ) |
| 1173 | |
| 1174 | if target_vdu.get("boot-data-drive"): |
| 1175 | cloud_config["boot-data-drive"] = target_vdu.get("boot-data-drive") |
| 1176 | |
| 1177 | return cloud_config |
| 1178 | |
| 1179 | @staticmethod |
| 1180 | def _check_vld_information_of_interfaces( |
| 1181 | interface: dict, ns_preffix: str, vnf_preffix: str |
| 1182 | ) -> Optional[str]: |
| 1183 | """Prepare the net_text by the virtual link information for vnf and ns level. |
| 1184 | Args: |
| 1185 | interface (dict): Interface details |
| 1186 | ns_preffix (str): Prefix of NS |
| 1187 | vnf_preffix (str): Prefix of VNF |
| 1188 | |
| 1189 | Returns: |
| 1190 | net_text (str): information of net |
| 1191 | |
| 1192 | """ |
| 1193 | net_text = "" |
| 1194 | if interface.get("ns-vld-id"): |
| 1195 | net_text = ns_preffix + ":vld." + interface["ns-vld-id"] |
| 1196 | elif interface.get("vnf-vld-id"): |
| 1197 | net_text = vnf_preffix + ":vld." + interface["vnf-vld-id"] |
| 1198 | |
| 1199 | return net_text |
| 1200 | |
| 1201 | @staticmethod |
| 1202 | def _prepare_interface_port_security(interface: dict) -> None: |
| 1203 | """ |
| 1204 | |
| 1205 | Args: |
| 1206 | interface (dict): Interface details |
| 1207 | |
| 1208 | """ |
| 1209 | if "port-security-enabled" in interface: |
| 1210 | interface["port_security"] = interface.pop("port-security-enabled") |
| 1211 | |
| 1212 | if "port-security-disable-strategy" in interface: |
| 1213 | interface["port_security_disable_strategy"] = interface.pop( |
| 1214 | "port-security-disable-strategy" |
| 1215 | ) |
| 1216 | |
| 1217 | @staticmethod |
| 1218 | def _create_net_item_of_interface(interface: dict, net_text: str) -> dict: |
| 1219 | """Prepare net item including name, port security, floating ip etc. |
| 1220 | |
| 1221 | Args: |
| 1222 | interface (dict): Interface details |
| 1223 | net_text (str): information of net |
| 1224 | |
| 1225 | Returns: |
| 1226 | net_item (dict): Dict including net details |
| 1227 | |
| 1228 | """ |
| 1229 | |
| 1230 | net_item = { |
| 1231 | x: v |
| 1232 | for x, v in interface.items() |
| 1233 | if x |
| 1234 | in ( |
| 1235 | "name", |
| 1236 | "vpci", |
| 1237 | "port_security", |
| 1238 | "port_security_disable_strategy", |
| 1239 | "floating_ip", |
| 1240 | ) |
| 1241 | } |
| 1242 | net_item["net_id"] = "TASK-" + net_text |
| 1243 | net_item["type"] = "virtual" |
| 1244 | |
| 1245 | return net_item |
| 1246 | |
| 1247 | @staticmethod |
| 1248 | def _prepare_type_of_interface( |
| 1249 | interface: dict, tasks_by_target_record_id: dict, net_text: str, net_item: dict |
| 1250 | ) -> None: |
| 1251 | """Fill the net item type by interface type such as SR-IOV, OM-MGMT, bridge etc. |
| 1252 | |
| 1253 | Args: |
| 1254 | interface (dict): Interface details |
| 1255 | tasks_by_target_record_id (dict): Task details |
| 1256 | net_text (str): information of net |
| 1257 | net_item (dict): Dict including net details |
| 1258 | |
| 1259 | """ |
| 1260 | # TODO mac_address: used for SR-IOV ifaces #TODO for other types |
| 1261 | # TODO floating_ip: True/False (or it can be None) |
| 1262 | |
| 1263 | if interface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 1264 | # Mark the net create task as type data |
| 1265 | if deep_get( |
| 1266 | tasks_by_target_record_id, |
| 1267 | net_text, |
| 1268 | "extra_dict", |
| 1269 | "params", |
| 1270 | "net_type", |
| 1271 | ): |
| 1272 | tasks_by_target_record_id[net_text]["extra_dict"]["params"][ |
| 1273 | "net_type" |
| 1274 | ] = "data" |
| 1275 | |
| 1276 | net_item["use"] = "data" |
| 1277 | net_item["model"] = interface["type"] |
| 1278 | net_item["type"] = interface["type"] |
| 1279 | |
| 1280 | elif ( |
| 1281 | interface.get("type") == "OM-MGMT" |
| 1282 | or interface.get("mgmt-interface") |
| 1283 | or interface.get("mgmt-vnf") |
| 1284 | ): |
| 1285 | net_item["use"] = "mgmt" |
| 1286 | |
| 1287 | else: |
| 1288 | # If interface.get("type") in ("VIRTIO", "E1000", "PARAVIRT"): |
| 1289 | net_item["use"] = "bridge" |
| 1290 | net_item["model"] = interface.get("type") |
| 1291 | |
| 1292 | @staticmethod |
| 1293 | def _prepare_vdu_interfaces( |
| 1294 | target_vdu: dict, |
| 1295 | extra_dict: dict, |
| 1296 | ns_preffix: str, |
| 1297 | vnf_preffix: str, |
| 1298 | logger: object, |
| 1299 | tasks_by_target_record_id: dict, |
| 1300 | net_list: list, |
| 1301 | ) -> None: |
| 1302 | """Prepare the net_item and add net_list, add mgmt interface to extra_dict. |
| 1303 | |
| 1304 | Args: |
| 1305 | target_vdu (dict): VDU to be created |
| 1306 | extra_dict (dict): Dictionary to be filled |
| 1307 | ns_preffix (str): NS prefix as string |
| 1308 | vnf_preffix (str): VNF prefix as string |
| 1309 | logger (object): Logger Object |
| 1310 | tasks_by_target_record_id (dict): Task details |
| 1311 | net_list (list): Net list of VDU |
| 1312 | """ |
| 1313 | for iface_index, interface in enumerate(target_vdu["interfaces"]): |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1314 | net_text = Ns._check_vld_information_of_interfaces( |
| 1315 | interface, ns_preffix, vnf_preffix |
| 1316 | ) |
| 1317 | if not net_text: |
| 1318 | # Interface not connected to any vld |
| 1319 | logger.error( |
| 1320 | "Interface {} from vdu {} not connected to any vld".format( |
| 1321 | iface_index, target_vdu["vdu-name"] |
| 1322 | ) |
| 1323 | ) |
| 1324 | continue |
| 1325 | |
| 1326 | extra_dict["depends_on"].append(net_text) |
| 1327 | |
| 1328 | Ns._prepare_interface_port_security(interface) |
| 1329 | |
| 1330 | net_item = Ns._create_net_item_of_interface(interface, net_text) |
| 1331 | |
| 1332 | Ns._prepare_type_of_interface( |
| 1333 | interface, tasks_by_target_record_id, net_text, net_item |
| 1334 | ) |
| 1335 | |
| 1336 | if interface.get("ip-address"): |
| 1337 | net_item["ip_address"] = interface["ip-address"] |
| 1338 | |
| 1339 | if interface.get("mac-address"): |
| 1340 | net_item["mac_address"] = interface["mac-address"] |
| 1341 | |
| 1342 | net_list.append(net_item) |
| 1343 | |
| 1344 | if interface.get("mgmt-vnf"): |
| 1345 | extra_dict["mgmt_vnf_interface"] = iface_index |
| 1346 | elif interface.get("mgmt-interface"): |
| 1347 | extra_dict["mgmt_vdu_interface"] = iface_index |
| 1348 | |
| 1349 | @staticmethod |
| 1350 | def _prepare_vdu_ssh_keys( |
| 1351 | target_vdu: dict, ro_nsr_public_key: dict, cloud_config: dict |
| 1352 | ) -> None: |
| 1353 | """Add ssh keys to cloud config. |
| 1354 | |
| 1355 | Args: |
| 1356 | target_vdu (dict): Details of VDU to be created |
| 1357 | ro_nsr_public_key (dict): RO NSR public Key |
| 1358 | cloud_config (dict): Cloud config details |
| 1359 | |
| 1360 | """ |
| 1361 | ssh_keys = [] |
| 1362 | |
| 1363 | if target_vdu.get("ssh-keys"): |
| 1364 | ssh_keys += target_vdu.get("ssh-keys") |
| 1365 | |
| 1366 | if target_vdu.get("ssh-access-required"): |
| 1367 | ssh_keys.append(ro_nsr_public_key) |
| 1368 | |
| 1369 | if ssh_keys: |
| 1370 | cloud_config["key-pairs"] = ssh_keys |
| 1371 | |
| 1372 | @staticmethod |
| 1373 | def _select_persistent_root_disk(vsd: dict, vdu: dict) -> dict: |
| 1374 | """Selects the persistent root disk if exists. |
| 1375 | Args: |
| 1376 | vsd (dict): Virtual storage descriptors in VNFD |
| 1377 | vdu (dict): VNF descriptor |
| 1378 | |
| 1379 | Returns: |
| 1380 | root_disk (dict): Selected persistent root disk |
| 1381 | """ |
| 1382 | if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]: |
| 1383 | root_disk = vsd |
| 1384 | if root_disk.get( |
| 1385 | "type-of-storage" |
| 1386 | ) == "persistent-storage:persistent-storage" and root_disk.get( |
| 1387 | "size-of-storage" |
| 1388 | ): |
| 1389 | return root_disk |
| 1390 | |
| 1391 | @staticmethod |
| 1392 | def _add_persistent_root_disk_to_disk_list( |
| 1393 | vnfd: dict, target_vdu: dict, persistent_root_disk: dict, disk_list: list |
| 1394 | ) -> None: |
| 1395 | """Find the persistent root disk and add to disk list. |
| 1396 | |
| 1397 | Args: |
| 1398 | vnfd (dict): VNF descriptor |
| 1399 | target_vdu (dict): Details of VDU to be created |
| 1400 | persistent_root_disk (dict): Details of persistent root disk |
| 1401 | disk_list (list): Disks of VDU |
| 1402 | |
| 1403 | """ |
| 1404 | for vdu in vnfd.get("vdu", ()): |
| 1405 | if vdu["name"] == target_vdu["vdu-name"]: |
| 1406 | for vsd in vnfd.get("virtual-storage-desc", ()): |
| 1407 | root_disk = Ns._select_persistent_root_disk(vsd, vdu) |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1408 | if not root_disk: |
| 1409 | continue |
| 1410 | |
| 1411 | persistent_root_disk[vsd["id"]] = { |
| 1412 | "image_id": vdu.get("sw-image-desc"), |
| 1413 | "size": root_disk["size-of-storage"], |
| aticig | 2f4ab6c | 2022-09-03 18:15:20 +0300 | [diff] [blame] | 1414 | "keep": Ns.is_volume_keeping_required(root_disk), |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1415 | } |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1416 | disk_list.append(persistent_root_disk[vsd["id"]]) |
| 1417 | break |
| 1418 | |
| 1419 | @staticmethod |
| 1420 | def _add_persistent_ordinary_disks_to_disk_list( |
| 1421 | target_vdu: dict, |
| 1422 | persistent_root_disk: dict, |
| 1423 | persistent_ordinary_disk: dict, |
| 1424 | disk_list: list, |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1425 | vnf_id: str = None, |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1426 | ) -> None: |
| 1427 | """Fill the disk list by adding persistent ordinary disks. |
| 1428 | |
| 1429 | Args: |
| 1430 | target_vdu (dict): Details of VDU to be created |
| 1431 | persistent_root_disk (dict): Details of persistent root disk |
| 1432 | persistent_ordinary_disk (dict): Details of persistent ordinary disk |
| 1433 | disk_list (list): Disks of VDU |
| 1434 | |
| 1435 | """ |
| 1436 | if target_vdu.get("virtual-storages"): |
| 1437 | for disk in target_vdu["virtual-storages"]: |
| 1438 | if ( |
| 1439 | disk.get("type-of-storage") |
| 1440 | == "persistent-storage:persistent-storage" |
| 1441 | and disk["id"] not in persistent_root_disk.keys() |
| 1442 | ): |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1443 | name, multiattach = Ns.is_shared_volume(disk, vnf_id) |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1444 | persistent_ordinary_disk[disk["id"]] = { |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1445 | "name": name, |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1446 | "size": disk["size-of-storage"], |
| aticig | 2f4ab6c | 2022-09-03 18:15:20 +0300 | [diff] [blame] | 1447 | "keep": Ns.is_volume_keeping_required(disk), |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1448 | "multiattach": multiattach, |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1449 | } |
| 1450 | disk_list.append(persistent_ordinary_disk[disk["id"]]) |
| 1451 | |
| 1452 | @staticmethod |
| 1453 | def _prepare_vdu_affinity_group_list( |
| 1454 | target_vdu: dict, extra_dict: dict, ns_preffix: str |
| 1455 | ) -> List[Dict[str, any]]: |
| 1456 | """Process affinity group details to prepare affinity group list. |
| 1457 | |
| 1458 | Args: |
| 1459 | target_vdu (dict): Details of VDU to be created |
| 1460 | extra_dict (dict): Dictionary to be filled |
| 1461 | ns_preffix (str): Prefix as string |
| 1462 | |
| 1463 | Returns: |
| 1464 | |
| 1465 | affinity_group_list (list): Affinity group details |
| 1466 | |
| 1467 | """ |
| 1468 | affinity_group_list = [] |
| 1469 | |
| 1470 | if target_vdu.get("affinity-or-anti-affinity-group-id"): |
| 1471 | for affinity_group_id in target_vdu["affinity-or-anti-affinity-group-id"]: |
| 1472 | affinity_group = {} |
| 1473 | affinity_group_text = ( |
| 1474 | ns_preffix + ":affinity-or-anti-affinity-group." + affinity_group_id |
| 1475 | ) |
| 1476 | |
| 1477 | if not isinstance(extra_dict.get("depends_on"), list): |
| 1478 | raise NsException("Invalid extra_dict format.") |
| 1479 | |
| 1480 | extra_dict["depends_on"].append(affinity_group_text) |
| 1481 | affinity_group["affinity_group_id"] = "TASK-" + affinity_group_text |
| 1482 | affinity_group_list.append(affinity_group) |
| 1483 | |
| 1484 | return affinity_group_list |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1485 | |
| 1486 | @staticmethod |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 1487 | def _process_vdu_params( |
| 1488 | target_vdu: Dict[str, Any], |
| 1489 | indata: Dict[str, Any], |
| 1490 | vim_info: Dict[str, Any], |
| 1491 | target_record_id: str, |
| 1492 | **kwargs: Dict[str, Any], |
| 1493 | ) -> Dict[str, Any]: |
| 1494 | """Function to process VDU parameters. |
| 1495 | |
| 1496 | Args: |
| 1497 | target_vdu (Dict[str, Any]): [description] |
| 1498 | indata (Dict[str, Any]): [description] |
| 1499 | vim_info (Dict[str, Any]): [description] |
| 1500 | target_record_id (str): [description] |
| 1501 | |
| 1502 | Returns: |
| 1503 | Dict[str, Any]: [description] |
| 1504 | """ |
| 1505 | vnfr_id = kwargs.get("vnfr_id") |
| 1506 | nsr_id = kwargs.get("nsr_id") |
| 1507 | vnfr = kwargs.get("vnfr") |
| 1508 | vdu2cloud_init = kwargs.get("vdu2cloud_init") |
| 1509 | tasks_by_target_record_id = kwargs.get("tasks_by_target_record_id") |
| 1510 | logger = kwargs.get("logger") |
| 1511 | db = kwargs.get("db") |
| 1512 | fs = kwargs.get("fs") |
| 1513 | ro_nsr_public_key = kwargs.get("ro_nsr_public_key") |
| 1514 | |
| 1515 | vnf_preffix = "vnfrs:{}".format(vnfr_id) |
| 1516 | ns_preffix = "nsrs:{}".format(nsr_id) |
| 1517 | image_text = ns_preffix + ":image." + target_vdu["ns-image-id"] |
| Gabriel Cuba | 730cfaf | 2023-03-13 22:26:38 -0500 | [diff] [blame] | 1518 | extra_dict = {"depends_on": [image_text]} |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 1519 | net_list = [] |
| aticig | 179e002 | 2022-03-29 13:15:45 +0300 | [diff] [blame] | 1520 | persistent_root_disk = {} |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1521 | persistent_ordinary_disk = {} |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1522 | vdu_instantiation_volumes_list = [] |
| Gabriel Cuba | 730cfaf | 2023-03-13 22:26:38 -0500 | [diff] [blame] | 1523 | vdu_instantiation_flavor_id = None |
| aticig | 179e002 | 2022-03-29 13:15:45 +0300 | [diff] [blame] | 1524 | disk_list = [] |
| 1525 | vnfd_id = vnfr["vnfd-id"] |
| 1526 | vnfd = db.get_one("vnfds", {"_id": vnfd_id}) |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1527 | # If the position info is provided for all the interfaces, it will be sorted |
| 1528 | # according to position number ascendingly. |
| 1529 | if all( |
| 1530 | True if i.get("position") is not None else False |
| 1531 | for i in target_vdu["interfaces"] |
| 1532 | ): |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1533 | Ns._sort_vdu_interfaces(target_vdu) |
| 1534 | |
| 1535 | # If the position info is provided for some interfaces but not all of them, the interfaces |
| 1536 | # which has specific position numbers will be placed and others' positions will not be taken care. |
| 1537 | else: |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1538 | Ns._partially_locate_vdu_interfaces(target_vdu) |
| 1539 | |
| 1540 | # If the position info is not provided for the interfaces, interfaces will be attached |
| 1541 | # according to the order in the VNFD. |
| 1542 | Ns._prepare_vdu_interfaces( |
| 1543 | target_vdu, |
| 1544 | extra_dict, |
| 1545 | ns_preffix, |
| 1546 | vnf_preffix, |
| 1547 | logger, |
| 1548 | tasks_by_target_record_id, |
| 1549 | net_list, |
| 1550 | ) |
| 1551 | |
| 1552 | # cloud config |
| 1553 | cloud_config = Ns._prepare_vdu_cloud_init(target_vdu, vdu2cloud_init, db, fs) |
| 1554 | |
| 1555 | # Prepare VDU ssh keys |
| 1556 | Ns._prepare_vdu_ssh_keys(target_vdu, ro_nsr_public_key, cloud_config) |
| 1557 | |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1558 | if target_vdu.get("additionalParams"): |
| 1559 | vdu_instantiation_volumes_list = ( |
| Gabriel Cuba | 730cfaf | 2023-03-13 22:26:38 -0500 | [diff] [blame] | 1560 | target_vdu.get("additionalParams").get("OSM", {}).get("vdu_volumes") |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1561 | ) |
| Gabriel Cuba | 730cfaf | 2023-03-13 22:26:38 -0500 | [diff] [blame] | 1562 | vdu_instantiation_flavor_id = ( |
| 1563 | target_vdu.get("additionalParams").get("OSM", {}).get("vim_flavor_id") |
| 1564 | ) |
| 1565 | |
| 1566 | # flavor id |
| 1567 | if vdu_instantiation_flavor_id: |
| 1568 | flavor_id = vdu_instantiation_flavor_id |
| 1569 | else: |
| 1570 | flavor_text = ns_preffix + ":flavor." + target_vdu["ns-flavor-id"] |
| 1571 | flavor_id = "TASK-" + flavor_text |
| 1572 | extra_dict["depends_on"].append(flavor_text) |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1573 | |
| 1574 | if vdu_instantiation_volumes_list: |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1575 | # Find the root volumes and add to the disk_list |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1576 | persistent_root_disk = Ns.find_persistent_root_volumes( |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1577 | vnfd, target_vdu, vdu_instantiation_volumes_list, disk_list |
| 1578 | ) |
| 1579 | |
| 1580 | # Find the ordinary volumes which are not added to the persistent_root_disk |
| 1581 | # and put them to the disk list |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1582 | Ns.find_persistent_volumes( |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1583 | persistent_root_disk, |
| 1584 | target_vdu, |
| 1585 | vdu_instantiation_volumes_list, |
| 1586 | disk_list, |
| 1587 | ) |
| 1588 | |
| 1589 | else: |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1590 | # Vdu_instantiation_volumes_list is empty |
| 1591 | # First get add the persistent root disks to disk_list |
| 1592 | Ns._add_persistent_root_disk_to_disk_list( |
| 1593 | vnfd, target_vdu, persistent_root_disk, disk_list |
| 1594 | ) |
| 1595 | # Add the persistent non-root disks to disk_list |
| 1596 | Ns._add_persistent_ordinary_disks_to_disk_list( |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1597 | target_vdu, |
| 1598 | persistent_root_disk, |
| 1599 | persistent_ordinary_disk, |
| 1600 | disk_list, |
| 1601 | vnfd["id"], |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1602 | ) |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 1603 | |
| Gulsum Atici | 4bc8eb9 | 2022-11-21 14:11:02 +0300 | [diff] [blame] | 1604 | affinity_group_list = Ns._prepare_vdu_affinity_group_list( |
| 1605 | target_vdu, extra_dict, ns_preffix |
| 1606 | ) |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1607 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 1608 | extra_dict["params"] = { |
| 1609 | "name": "{}-{}-{}-{}".format( |
| 1610 | indata["name"][:16], |
| 1611 | vnfr["member-vnf-index-ref"][:16], |
| 1612 | target_vdu["vdu-name"][:32], |
| 1613 | target_vdu.get("count-index") or 0, |
| 1614 | ), |
| 1615 | "description": target_vdu["vdu-name"], |
| 1616 | "start": True, |
| 1617 | "image_id": "TASK-" + image_text, |
| Gabriel Cuba | 730cfaf | 2023-03-13 22:26:38 -0500 | [diff] [blame] | 1618 | "flavor_id": flavor_id, |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1619 | "affinity_group_list": affinity_group_list, |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 1620 | "net_list": net_list, |
| 1621 | "cloud_config": cloud_config or None, |
| 1622 | "disk_list": disk_list, |
| 1623 | "availability_zone_index": None, # TODO |
| 1624 | "availability_zone_list": None, # TODO |
| 1625 | } |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1626 | return extra_dict |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 1627 | |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1628 | @staticmethod |
| 1629 | def _process_shared_volumes_params( |
| 1630 | target_shared_volume: Dict[str, Any], |
| 1631 | indata: Dict[str, Any], |
| 1632 | vim_info: Dict[str, Any], |
| 1633 | target_record_id: str, |
| 1634 | **kwargs: Dict[str, Any], |
| 1635 | ) -> Dict[str, Any]: |
| 1636 | extra_dict = {} |
| 1637 | shared_volume_data = { |
| 1638 | "size": target_shared_volume["size-of-storage"], |
| 1639 | "name": target_shared_volume["id"], |
| 1640 | "type": target_shared_volume["type-of-storage"], |
| 1641 | } |
| 1642 | extra_dict["params"] = shared_volume_data |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 1643 | return extra_dict |
| 1644 | |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1645 | @staticmethod |
| 1646 | def _process_affinity_group_params( |
| 1647 | target_affinity_group: Dict[str, Any], |
| 1648 | indata: Dict[str, Any], |
| 1649 | vim_info: Dict[str, Any], |
| 1650 | target_record_id: str, |
| 1651 | **kwargs: Dict[str, Any], |
| 1652 | ) -> Dict[str, Any]: |
| 1653 | """Get affinity or anti-affinity group parameters. |
| 1654 | |
| 1655 | Args: |
| 1656 | target_affinity_group (Dict[str, Any]): [description] |
| 1657 | indata (Dict[str, Any]): [description] |
| 1658 | vim_info (Dict[str, Any]): [description] |
| 1659 | target_record_id (str): [description] |
| 1660 | |
| 1661 | Returns: |
| 1662 | Dict[str, Any]: [description] |
| 1663 | """ |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1664 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1665 | extra_dict = {} |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1666 | affinity_group_data = { |
| 1667 | "name": target_affinity_group["name"], |
| 1668 | "type": target_affinity_group["type"], |
| 1669 | "scope": target_affinity_group["scope"], |
| 1670 | } |
| 1671 | |
| Alexis Romero | 123de18 | 2022-04-26 19:24:40 +0200 | [diff] [blame] | 1672 | if target_affinity_group.get("vim-affinity-group-id"): |
| 1673 | affinity_group_data["vim-affinity-group-id"] = target_affinity_group[ |
| 1674 | "vim-affinity-group-id" |
| 1675 | ] |
| 1676 | |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1677 | extra_dict["params"] = { |
| 1678 | "affinity_group_data": affinity_group_data, |
| 1679 | } |
| Alexis Romero | b70f4ed | 2022-03-11 18:00:49 +0100 | [diff] [blame] | 1680 | return extra_dict |
| 1681 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1682 | @staticmethod |
| 1683 | def _process_recreate_vdu_params( |
| 1684 | existing_vdu: Dict[str, Any], |
| 1685 | db_nsr: Dict[str, Any], |
| 1686 | vim_info: Dict[str, Any], |
| 1687 | target_record_id: str, |
| 1688 | target_id: str, |
| 1689 | **kwargs: Dict[str, Any], |
| 1690 | ) -> Dict[str, Any]: |
| 1691 | """Function to process VDU parameters to recreate. |
| 1692 | |
| 1693 | Args: |
| 1694 | existing_vdu (Dict[str, Any]): [description] |
| 1695 | db_nsr (Dict[str, Any]): [description] |
| 1696 | vim_info (Dict[str, Any]): [description] |
| 1697 | target_record_id (str): [description] |
| 1698 | target_id (str): [description] |
| 1699 | |
| 1700 | Returns: |
| 1701 | Dict[str, Any]: [description] |
| 1702 | """ |
| 1703 | vnfr = kwargs.get("vnfr") |
| 1704 | vdu2cloud_init = kwargs.get("vdu2cloud_init") |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 1705 | # logger = kwargs.get("logger") |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1706 | db = kwargs.get("db") |
| 1707 | fs = kwargs.get("fs") |
| 1708 | ro_nsr_public_key = kwargs.get("ro_nsr_public_key") |
| 1709 | |
| 1710 | extra_dict = {} |
| 1711 | net_list = [] |
| 1712 | |
| 1713 | vim_details = {} |
| 1714 | vim_details_text = existing_vdu["vim_info"][target_id].get("vim_details", None) |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1715 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1716 | if vim_details_text: |
| 1717 | vim_details = yaml.safe_load(f"{vim_details_text}") |
| 1718 | |
| 1719 | for iface_index, interface in enumerate(existing_vdu["interfaces"]): |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1720 | if "port-security-enabled" in interface: |
| 1721 | interface["port_security"] = interface.pop("port-security-enabled") |
| 1722 | |
| 1723 | if "port-security-disable-strategy" in interface: |
| 1724 | interface["port_security_disable_strategy"] = interface.pop( |
| 1725 | "port-security-disable-strategy" |
| 1726 | ) |
| 1727 | |
| 1728 | net_item = { |
| 1729 | x: v |
| 1730 | for x, v in interface.items() |
| 1731 | if x |
| 1732 | in ( |
| 1733 | "name", |
| 1734 | "vpci", |
| 1735 | "port_security", |
| 1736 | "port_security_disable_strategy", |
| 1737 | "floating_ip", |
| 1738 | ) |
| 1739 | } |
| garciadeblas | bf23519 | 2022-06-13 10:02:51 +0200 | [diff] [blame] | 1740 | existing_ifaces = existing_vdu["vim_info"][target_id].get( |
| 1741 | "interfaces_backup", [] |
| 1742 | ) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1743 | net_id = next( |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 1744 | ( |
| 1745 | i["vim_net_id"] |
| 1746 | for i in existing_ifaces |
| 1747 | if i["ip_address"] == interface["ip-address"] |
| 1748 | ), |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1749 | None, |
| 1750 | ) |
| 1751 | |
| 1752 | net_item["net_id"] = net_id |
| 1753 | net_item["type"] = "virtual" |
| 1754 | |
| 1755 | # TODO mac_address: used for SR-IOV ifaces #TODO for other types |
| 1756 | # TODO floating_ip: True/False (or it can be None) |
| 1757 | if interface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 1758 | net_item["use"] = "data" |
| 1759 | net_item["model"] = interface["type"] |
| 1760 | net_item["type"] = interface["type"] |
| 1761 | elif ( |
| 1762 | interface.get("type") == "OM-MGMT" |
| 1763 | or interface.get("mgmt-interface") |
| 1764 | or interface.get("mgmt-vnf") |
| 1765 | ): |
| 1766 | net_item["use"] = "mgmt" |
| 1767 | else: |
| 1768 | # if interface.get("type") in ("VIRTIO", "E1000", "PARAVIRT"): |
| 1769 | net_item["use"] = "bridge" |
| 1770 | net_item["model"] = interface.get("type") |
| 1771 | |
| 1772 | if interface.get("ip-address"): |
| 1773 | net_item["ip_address"] = interface["ip-address"] |
| 1774 | |
| 1775 | if interface.get("mac-address"): |
| 1776 | net_item["mac_address"] = interface["mac-address"] |
| 1777 | |
| 1778 | net_list.append(net_item) |
| 1779 | |
| 1780 | if interface.get("mgmt-vnf"): |
| 1781 | extra_dict["mgmt_vnf_interface"] = iface_index |
| 1782 | elif interface.get("mgmt-interface"): |
| 1783 | extra_dict["mgmt_vdu_interface"] = iface_index |
| 1784 | |
| 1785 | # cloud config |
| 1786 | cloud_config = {} |
| 1787 | |
| 1788 | if existing_vdu.get("cloud-init"): |
| 1789 | if existing_vdu["cloud-init"] not in vdu2cloud_init: |
| 1790 | vdu2cloud_init[existing_vdu["cloud-init"]] = Ns._get_cloud_init( |
| 1791 | db=db, |
| 1792 | fs=fs, |
| 1793 | location=existing_vdu["cloud-init"], |
| 1794 | ) |
| 1795 | |
| 1796 | cloud_content_ = vdu2cloud_init[existing_vdu["cloud-init"]] |
| 1797 | cloud_config["user-data"] = Ns._parse_jinja2( |
| 1798 | cloud_init_content=cloud_content_, |
| 1799 | params=existing_vdu.get("additionalParams"), |
| 1800 | context=existing_vdu["cloud-init"], |
| 1801 | ) |
| 1802 | |
| 1803 | if existing_vdu.get("boot-data-drive"): |
| 1804 | cloud_config["boot-data-drive"] = existing_vdu.get("boot-data-drive") |
| 1805 | |
| 1806 | ssh_keys = [] |
| 1807 | |
| 1808 | if existing_vdu.get("ssh-keys"): |
| 1809 | ssh_keys += existing_vdu.get("ssh-keys") |
| 1810 | |
| 1811 | if existing_vdu.get("ssh-access-required"): |
| 1812 | ssh_keys.append(ro_nsr_public_key) |
| 1813 | |
| 1814 | if ssh_keys: |
| 1815 | cloud_config["key-pairs"] = ssh_keys |
| 1816 | |
| 1817 | disk_list = [] |
| 1818 | for vol_id in vim_details.get("os-extended-volumes:volumes_attached", []): |
| 1819 | disk_list.append({"vim_id": vol_id["id"]}) |
| 1820 | |
| 1821 | affinity_group_list = [] |
| 1822 | |
| 1823 | if existing_vdu.get("affinity-or-anti-affinity-group-id"): |
| 1824 | affinity_group = {} |
| 1825 | for affinity_group_id in existing_vdu["affinity-or-anti-affinity-group-id"]: |
| 1826 | for group in db_nsr.get("affinity-or-anti-affinity-group"): |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 1827 | if ( |
| 1828 | group["id"] == affinity_group_id |
| 1829 | and group["vim_info"][target_id].get("vim_id", None) is not None |
| 1830 | ): |
| 1831 | affinity_group["affinity_group_id"] = group["vim_info"][ |
| 1832 | target_id |
| 1833 | ].get("vim_id", None) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1834 | affinity_group_list.append(affinity_group) |
| 1835 | |
| 1836 | extra_dict["params"] = { |
| 1837 | "name": "{}-{}-{}-{}".format( |
| 1838 | db_nsr["name"][:16], |
| 1839 | vnfr["member-vnf-index-ref"][:16], |
| 1840 | existing_vdu["vdu-name"][:32], |
| 1841 | existing_vdu.get("count-index") or 0, |
| 1842 | ), |
| 1843 | "description": existing_vdu["vdu-name"], |
| 1844 | "start": True, |
| 1845 | "image_id": vim_details["image"]["id"], |
| 1846 | "flavor_id": vim_details["flavor"]["id"], |
| 1847 | "affinity_group_list": affinity_group_list, |
| 1848 | "net_list": net_list, |
| 1849 | "cloud_config": cloud_config or None, |
| 1850 | "disk_list": disk_list, |
| 1851 | "availability_zone_index": None, # TODO |
| 1852 | "availability_zone_list": None, # TODO |
| 1853 | } |
| 1854 | |
| 1855 | return extra_dict |
| 1856 | |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1857 | def calculate_diff_items( |
| 1858 | self, |
| 1859 | indata, |
| 1860 | db_nsr, |
| 1861 | db_ro_nsr, |
| 1862 | db_nsr_update, |
| 1863 | item, |
| 1864 | tasks_by_target_record_id, |
| 1865 | action_id, |
| 1866 | nsr_id, |
| 1867 | task_index, |
| 1868 | vnfr_id=None, |
| 1869 | vnfr=None, |
| 1870 | ): |
| 1871 | """Function that returns the incremental changes (creation, deletion) |
| 1872 | related to a specific item `item` to be done. This function should be |
| 1873 | called for NS instantiation, NS termination, NS update to add a new VNF |
| 1874 | or a new VLD, remove a VNF or VLD, etc. |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1875 | Item can be `net`, `flavor`, `image` or `vdu`. |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1876 | It takes a list of target items from indata (which came from the REST API) |
| 1877 | and compares with the existing items from db_ro_nsr, identifying the |
| 1878 | incremental changes to be done. During the comparison, it calls the method |
| 1879 | `process_params` (which was passed as parameter, and is particular for each |
| 1880 | `item`) |
| 1881 | |
| 1882 | Args: |
| 1883 | indata (Dict[str, Any]): deployment info |
| 1884 | db_nsr: NSR record from DB |
| 1885 | db_ro_nsr (Dict[str, Any]): record from "ro_nsrs" |
| 1886 | db_nsr_update (Dict[str, Any]): NSR info to update in DB |
| 1887 | item (str): element to process (net, vdu...) |
| 1888 | tasks_by_target_record_id (Dict[str, Any]): |
| 1889 | [<target_record_id>, <task>] |
| 1890 | action_id (str): action id |
| 1891 | nsr_id (str): NSR id |
| 1892 | task_index (number): task index to add to task name |
| 1893 | vnfr_id (str): VNFR id |
| 1894 | vnfr (Dict[str, Any]): VNFR info |
| 1895 | |
| 1896 | Returns: |
| 1897 | List: list with the incremental changes (deletes, creates) for each item |
| 1898 | number: current task index |
| 1899 | """ |
| 1900 | |
| 1901 | diff_items = [] |
| 1902 | db_path = "" |
| 1903 | db_record = "" |
| 1904 | target_list = [] |
| 1905 | existing_list = [] |
| 1906 | process_params = None |
| 1907 | vdu2cloud_init = indata.get("cloud_init_content") or {} |
| 1908 | ro_nsr_public_key = db_ro_nsr["public_key"] |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1909 | # According to the type of item, the path, the target_list, |
| 1910 | # the existing_list and the method to process params are set |
| 1911 | db_path = self.db_path_map[item] |
| 1912 | process_params = self.process_params_function_map[item] |
| 1913 | if item in ("net", "vdu"): |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1914 | # This case is specific for the NS VLD (not applied to VDU) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1915 | if vnfr is None: |
| 1916 | db_record = "nsrs:{}:{}".format(nsr_id, db_path) |
| aticig | 2b24d62 | 2022-03-11 15:03:55 +0300 | [diff] [blame] | 1917 | target_list = indata.get("ns", []).get(db_path, []) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1918 | existing_list = db_nsr.get(db_path, []) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 1919 | # This case is common for VNF VLDs and VNF VDUs |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1920 | else: |
| 1921 | db_record = "vnfrs:{}:{}".format(vnfr_id, db_path) |
| 1922 | target_vnf = next( |
| 1923 | (vnf for vnf in indata.get("vnf", ()) if vnf["_id"] == vnfr_id), |
| 1924 | None, |
| 1925 | ) |
| 1926 | target_list = target_vnf.get(db_path, []) if target_vnf else [] |
| 1927 | existing_list = vnfr.get(db_path, []) |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 1928 | elif item in ( |
| 1929 | "image", |
| 1930 | "flavor", |
| 1931 | "affinity-or-anti-affinity-group", |
| 1932 | "shared-volumes", |
| 1933 | ): |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1934 | db_record = "nsrs:{}:{}".format(nsr_id, db_path) |
| 1935 | target_list = indata.get(item, []) |
| 1936 | existing_list = db_nsr.get(item, []) |
| 1937 | else: |
| 1938 | raise NsException("Item not supported: {}", item) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1939 | # ensure all the target_list elements has an "id". If not assign the index as id |
| 1940 | if target_list is None: |
| 1941 | target_list = [] |
| 1942 | for target_index, tl in enumerate(target_list): |
| 1943 | if tl and not tl.get("id"): |
| 1944 | tl["id"] = str(target_index) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1945 | # step 1 items (networks,vdus,...) to be deleted/updated |
| 1946 | for item_index, existing_item in enumerate(existing_list): |
| 1947 | target_item = next( |
| 1948 | (t for t in target_list if t["id"] == existing_item["id"]), |
| 1949 | None, |
| 1950 | ) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1951 | for target_vim, existing_viminfo in existing_item.get( |
| 1952 | "vim_info", {} |
| 1953 | ).items(): |
| 1954 | if existing_viminfo is None: |
| 1955 | continue |
| 1956 | |
| 1957 | if target_item: |
| 1958 | target_viminfo = target_item.get("vim_info", {}).get(target_vim) |
| 1959 | else: |
| 1960 | target_viminfo = None |
| 1961 | |
| 1962 | if target_viminfo is None: |
| 1963 | # must be deleted |
| 1964 | self._assign_vim(target_vim) |
| 1965 | target_record_id = "{}.{}".format(db_record, existing_item["id"]) |
| 1966 | item_ = item |
| 1967 | |
| gifrerenom | bf3e9a8 | 2022-03-07 17:55:20 +0000 | [diff] [blame] | 1968 | if target_vim.startswith("sdn") or target_vim.startswith("wim"): |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1969 | # item must be sdn-net instead of net if target_vim is a sdn |
| 1970 | item_ = "sdn_net" |
| 1971 | target_record_id += ".sdn" |
| 1972 | |
| 1973 | deployment_info = { |
| 1974 | "action_id": action_id, |
| 1975 | "nsr_id": nsr_id, |
| 1976 | "task_index": task_index, |
| 1977 | } |
| 1978 | |
| 1979 | diff_items.append( |
| 1980 | { |
| 1981 | "deployment_info": deployment_info, |
| 1982 | "target_id": target_vim, |
| 1983 | "item": item_, |
| 1984 | "action": "DELETE", |
| 1985 | "target_record": f"{db_record}.{item_index}.vim_info.{target_vim}", |
| 1986 | "target_record_id": target_record_id, |
| 1987 | } |
| 1988 | ) |
| 1989 | task_index += 1 |
| 1990 | |
| 1991 | # step 2 items (networks,vdus,...) to be created |
| 1992 | for target_item in target_list: |
| 1993 | item_index = -1 |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 1994 | for item_index, existing_item in enumerate(existing_list): |
| 1995 | if existing_item["id"] == target_item["id"]: |
| 1996 | break |
| 1997 | else: |
| 1998 | item_index += 1 |
| 1999 | db_nsr_update[db_path + ".{}".format(item_index)] = target_item |
| 2000 | existing_list.append(target_item) |
| 2001 | existing_item = None |
| 2002 | |
| 2003 | for target_vim, target_viminfo in target_item.get("vim_info", {}).items(): |
| 2004 | existing_viminfo = None |
| 2005 | |
| 2006 | if existing_item: |
| 2007 | existing_viminfo = existing_item.get("vim_info", {}).get(target_vim) |
| 2008 | |
| 2009 | if existing_viminfo is not None: |
| 2010 | continue |
| 2011 | |
| 2012 | target_record_id = "{}.{}".format(db_record, target_item["id"]) |
| 2013 | item_ = item |
| 2014 | |
| gifrerenom | bf3e9a8 | 2022-03-07 17:55:20 +0000 | [diff] [blame] | 2015 | if target_vim.startswith("sdn") or target_vim.startswith("wim"): |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2016 | # item must be sdn-net instead of net if target_vim is a sdn |
| 2017 | item_ = "sdn_net" |
| 2018 | target_record_id += ".sdn" |
| 2019 | |
| 2020 | kwargs = {} |
| aticig | ddff2b0 | 2022-09-03 23:06:37 +0300 | [diff] [blame] | 2021 | self.logger.debug( |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2022 | "ns.calculate_diff_items target_item={}".format(target_item) |
| 2023 | ) |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 2024 | if process_params == Ns._process_flavor_params: |
| 2025 | kwargs.update( |
| 2026 | { |
| 2027 | "db": self.db, |
| 2028 | } |
| 2029 | ) |
| aticig | ddff2b0 | 2022-09-03 23:06:37 +0300 | [diff] [blame] | 2030 | self.logger.debug( |
| aticig | cf14bb1 | 2022-05-19 13:03:17 +0300 | [diff] [blame] | 2031 | "calculate_diff_items for flavor kwargs={}".format(kwargs) |
| 2032 | ) |
| 2033 | |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2034 | if process_params == Ns._process_vdu_params: |
| aticig | ddff2b0 | 2022-09-03 23:06:37 +0300 | [diff] [blame] | 2035 | self.logger.debug("calculate_diff_items self.fs={}".format(self.fs)) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2036 | kwargs.update( |
| 2037 | { |
| 2038 | "vnfr_id": vnfr_id, |
| 2039 | "nsr_id": nsr_id, |
| 2040 | "vnfr": vnfr, |
| 2041 | "vdu2cloud_init": vdu2cloud_init, |
| 2042 | "tasks_by_target_record_id": tasks_by_target_record_id, |
| 2043 | "logger": self.logger, |
| 2044 | "db": self.db, |
| 2045 | "fs": self.fs, |
| 2046 | "ro_nsr_public_key": ro_nsr_public_key, |
| 2047 | } |
| 2048 | ) |
| aticig | ddff2b0 | 2022-09-03 23:06:37 +0300 | [diff] [blame] | 2049 | self.logger.debug("calculate_diff_items kwargs={}".format(kwargs)) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2050 | extra_dict = process_params( |
| 2051 | target_item, |
| 2052 | indata, |
| 2053 | target_viminfo, |
| 2054 | target_record_id, |
| 2055 | **kwargs, |
| 2056 | ) |
| 2057 | self._assign_vim(target_vim) |
| 2058 | |
| 2059 | deployment_info = { |
| 2060 | "action_id": action_id, |
| 2061 | "nsr_id": nsr_id, |
| 2062 | "task_index": task_index, |
| 2063 | } |
| 2064 | |
| gallardo | 0108e94 | 2022-03-14 18:16:41 +0000 | [diff] [blame] | 2065 | new_item = { |
| 2066 | "deployment_info": deployment_info, |
| 2067 | "target_id": target_vim, |
| 2068 | "item": item_, |
| 2069 | "action": "CREATE", |
| 2070 | "target_record": f"{db_record}.{item_index}.vim_info.{target_vim}", |
| 2071 | "target_record_id": target_record_id, |
| 2072 | "extra_dict": extra_dict, |
| 2073 | "common_id": target_item.get("common_id", None), |
| 2074 | } |
| 2075 | diff_items.append(new_item) |
| 2076 | tasks_by_target_record_id[target_record_id] = new_item |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2077 | task_index += 1 |
| 2078 | |
| 2079 | db_nsr_update[db_path + ".{}".format(item_index)] = target_item |
| 2080 | |
| 2081 | return diff_items, task_index |
| 2082 | |
| 2083 | def calculate_all_differences_to_deploy( |
| 2084 | self, |
| 2085 | indata, |
| 2086 | nsr_id, |
| 2087 | db_nsr, |
| 2088 | db_vnfrs, |
| 2089 | db_ro_nsr, |
| 2090 | db_nsr_update, |
| 2091 | db_vnfrs_update, |
| 2092 | action_id, |
| 2093 | tasks_by_target_record_id, |
| 2094 | ): |
| 2095 | """This method calculates the ordered list of items (`changes_list`) |
| 2096 | to be created and deleted. |
| 2097 | |
| 2098 | Args: |
| 2099 | indata (Dict[str, Any]): deployment info |
| 2100 | nsr_id (str): NSR id |
| 2101 | db_nsr: NSR record from DB |
| 2102 | db_vnfrs: VNFRS record from DB |
| 2103 | db_ro_nsr (Dict[str, Any]): record from "ro_nsrs" |
| 2104 | db_nsr_update (Dict[str, Any]): NSR info to update in DB |
| 2105 | db_vnfrs_update (Dict[str, Any]): VNFRS info to update in DB |
| 2106 | action_id (str): action id |
| 2107 | tasks_by_target_record_id (Dict[str, Any]): |
| 2108 | [<target_record_id>, <task>] |
| 2109 | |
| 2110 | Returns: |
| 2111 | List: ordered list of items to be created and deleted. |
| 2112 | """ |
| 2113 | |
| 2114 | task_index = 0 |
| 2115 | # set list with diffs: |
| 2116 | changes_list = [] |
| 2117 | |
| 2118 | # NS vld, image and flavor |
| vegall | 364627c | 2023-03-17 15:09:50 +0000 | [diff] [blame] | 2119 | for item in [ |
| 2120 | "net", |
| 2121 | "image", |
| 2122 | "flavor", |
| 2123 | "affinity-or-anti-affinity-group", |
| 2124 | "shared-volumes", |
| 2125 | ]: |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2126 | self.logger.debug("process NS={} {}".format(nsr_id, item)) |
| 2127 | diff_items, task_index = self.calculate_diff_items( |
| 2128 | indata=indata, |
| 2129 | db_nsr=db_nsr, |
| 2130 | db_ro_nsr=db_ro_nsr, |
| 2131 | db_nsr_update=db_nsr_update, |
| 2132 | item=item, |
| 2133 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 2134 | action_id=action_id, |
| 2135 | nsr_id=nsr_id, |
| 2136 | task_index=task_index, |
| 2137 | vnfr_id=None, |
| 2138 | ) |
| 2139 | changes_list += diff_items |
| 2140 | |
| 2141 | # VNF vlds and vdus |
| 2142 | for vnfr_id, vnfr in db_vnfrs.items(): |
| 2143 | # vnfr_id need to be set as global variable for among others nested method _process_vdu_params |
| 2144 | for item in ["net", "vdu"]: |
| 2145 | self.logger.debug("process VNF={} {}".format(vnfr_id, item)) |
| 2146 | diff_items, task_index = self.calculate_diff_items( |
| 2147 | indata=indata, |
| 2148 | db_nsr=db_nsr, |
| 2149 | db_ro_nsr=db_ro_nsr, |
| 2150 | db_nsr_update=db_vnfrs_update[vnfr["_id"]], |
| 2151 | item=item, |
| 2152 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 2153 | action_id=action_id, |
| 2154 | nsr_id=nsr_id, |
| 2155 | task_index=task_index, |
| 2156 | vnfr_id=vnfr_id, |
| 2157 | vnfr=vnfr, |
| 2158 | ) |
| 2159 | changes_list += diff_items |
| 2160 | |
| 2161 | return changes_list |
| 2162 | |
| 2163 | def define_all_tasks( |
| 2164 | self, |
| 2165 | changes_list, |
| 2166 | db_new_tasks, |
| 2167 | tasks_by_target_record_id, |
| 2168 | ): |
| 2169 | """Function to create all the task structures obtanied from |
| 2170 | the method calculate_all_differences_to_deploy |
| 2171 | |
| 2172 | Args: |
| 2173 | changes_list (List): ordered list of items to be created or deleted |
| 2174 | db_new_tasks (List): tasks list to be created |
| 2175 | action_id (str): action id |
| 2176 | tasks_by_target_record_id (Dict[str, Any]): |
| 2177 | [<target_record_id>, <task>] |
| 2178 | |
| 2179 | """ |
| 2180 | |
| 2181 | for change in changes_list: |
| 2182 | task = Ns._create_task( |
| 2183 | deployment_info=change["deployment_info"], |
| 2184 | target_id=change["target_id"], |
| 2185 | item=change["item"], |
| 2186 | action=change["action"], |
| 2187 | target_record=change["target_record"], |
| 2188 | target_record_id=change["target_record_id"], |
| 2189 | extra_dict=change.get("extra_dict", None), |
| 2190 | ) |
| 2191 | |
| aticig | ddff2b0 | 2022-09-03 23:06:37 +0300 | [diff] [blame] | 2192 | self.logger.debug("ns.define_all_tasks task={}".format(task)) |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2193 | tasks_by_target_record_id[change["target_record_id"]] = task |
| 2194 | db_new_tasks.append(task) |
| 2195 | |
| 2196 | if change.get("common_id"): |
| 2197 | task["common_id"] = change["common_id"] |
| 2198 | |
| 2199 | def upload_all_tasks( |
| 2200 | self, |
| 2201 | db_new_tasks, |
| 2202 | now, |
| 2203 | ): |
| 2204 | """Function to save all tasks in the common DB |
| 2205 | |
| 2206 | Args: |
| 2207 | db_new_tasks (List): tasks list to be created |
| 2208 | now (time): current time |
| 2209 | |
| 2210 | """ |
| 2211 | |
| 2212 | nb_ro_tasks = 0 # for logging |
| 2213 | |
| 2214 | for db_task in db_new_tasks: |
| 2215 | target_id = db_task.pop("target_id") |
| 2216 | common_id = db_task.get("common_id") |
| 2217 | |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2218 | # Do not chek tasks with vim_status DELETED |
| 2219 | # because in manual heealing there are two tasks for the same vdur: |
| 2220 | # one with vim_status deleted and the other one with the actual VM status. |
| 2221 | |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2222 | if common_id: |
| 2223 | if self.db.set_one( |
| 2224 | "ro_tasks", |
| 2225 | q_filter={ |
| 2226 | "target_id": target_id, |
| 2227 | "tasks.common_id": common_id, |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2228 | "vim_info.vim_status.ne": "DELETED", |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2229 | }, |
| 2230 | update_dict={"to_check_at": now, "modified_at": now}, |
| 2231 | push={"tasks": db_task}, |
| 2232 | fail_on_empty=False, |
| 2233 | ): |
| 2234 | continue |
| 2235 | |
| 2236 | if not self.db.set_one( |
| 2237 | "ro_tasks", |
| 2238 | q_filter={ |
| 2239 | "target_id": target_id, |
| 2240 | "tasks.target_record": db_task["target_record"], |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2241 | "vim_info.vim_status.ne": "DELETED", |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2242 | }, |
| 2243 | update_dict={"to_check_at": now, "modified_at": now}, |
| 2244 | push={"tasks": db_task}, |
| 2245 | fail_on_empty=False, |
| 2246 | ): |
| 2247 | # Create a ro_task |
| 2248 | self.logger.debug("Updating database, Creating ro_tasks") |
| 2249 | db_ro_task = Ns._create_ro_task(target_id, db_task) |
| 2250 | nb_ro_tasks += 1 |
| 2251 | self.db.create("ro_tasks", db_ro_task) |
| 2252 | |
| 2253 | self.logger.debug( |
| 2254 | "Created {} ro_tasks; {} tasks - db_new_tasks={}".format( |
| 2255 | nb_ro_tasks, len(db_new_tasks), db_new_tasks |
| 2256 | ) |
| 2257 | ) |
| 2258 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2259 | def upload_recreate_tasks( |
| 2260 | self, |
| 2261 | db_new_tasks, |
| 2262 | now, |
| 2263 | ): |
| 2264 | """Function to save recreate tasks in the common DB |
| 2265 | |
| 2266 | Args: |
| 2267 | db_new_tasks (List): tasks list to be created |
| 2268 | now (time): current time |
| 2269 | |
| 2270 | """ |
| 2271 | |
| 2272 | nb_ro_tasks = 0 # for logging |
| 2273 | |
| 2274 | for db_task in db_new_tasks: |
| 2275 | target_id = db_task.pop("target_id") |
| aticig | ddff2b0 | 2022-09-03 23:06:37 +0300 | [diff] [blame] | 2276 | self.logger.debug("target_id={} db_task={}".format(target_id, db_task)) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2277 | |
| 2278 | action = db_task.get("action", None) |
| 2279 | |
| 2280 | # Create a ro_task |
| 2281 | self.logger.debug("Updating database, Creating ro_tasks") |
| 2282 | db_ro_task = Ns._create_ro_task(target_id, db_task) |
| 2283 | |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 2284 | # If DELETE task: the associated created items should be removed |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2285 | # (except persistent volumes): |
| 2286 | if action == "DELETE": |
| 2287 | db_ro_task["vim_info"]["created"] = True |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 2288 | db_ro_task["vim_info"]["created_items"] = db_task.get( |
| 2289 | "created_items", {} |
| 2290 | ) |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2291 | db_ro_task["vim_info"]["volumes_to_hold"] = db_task.get( |
| 2292 | "volumes_to_hold", [] |
| 2293 | ) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2294 | db_ro_task["vim_info"]["vim_id"] = db_task.get("vim_id", None) |
| 2295 | |
| 2296 | nb_ro_tasks += 1 |
| aticig | ddff2b0 | 2022-09-03 23:06:37 +0300 | [diff] [blame] | 2297 | self.logger.debug("upload_all_tasks db_ro_task={}".format(db_ro_task)) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2298 | self.db.create("ro_tasks", db_ro_task) |
| 2299 | |
| 2300 | self.logger.debug( |
| 2301 | "Created {} ro_tasks; {} tasks - db_new_tasks={}".format( |
| 2302 | nb_ro_tasks, len(db_new_tasks), db_new_tasks |
| 2303 | ) |
| 2304 | ) |
| 2305 | |
| 2306 | def _prepare_created_items_for_healing( |
| 2307 | self, |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2308 | nsr_id, |
| 2309 | target_record, |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2310 | ): |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2311 | created_items = {} |
| 2312 | # Get created_items from ro_task |
| 2313 | ro_tasks = self.db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id}) |
| 2314 | for ro_task in ro_tasks: |
| 2315 | for task in ro_task["tasks"]: |
| 2316 | if ( |
| 2317 | task["target_record"] == target_record |
| 2318 | and task["action"] == "CREATE" |
| 2319 | and ro_task["vim_info"]["created_items"] |
| 2320 | ): |
| 2321 | created_items = ro_task["vim_info"]["created_items"] |
| 2322 | break |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2323 | |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2324 | return created_items |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2325 | |
| 2326 | def _prepare_persistent_volumes_for_healing( |
| 2327 | self, |
| 2328 | target_id, |
| 2329 | existing_vdu, |
| 2330 | ): |
| 2331 | # The associated volumes of the VM shouldn't be removed |
| 2332 | volumes_list = [] |
| 2333 | vim_details = {} |
| 2334 | vim_details_text = existing_vdu["vim_info"][target_id].get("vim_details", None) |
| 2335 | if vim_details_text: |
| 2336 | vim_details = yaml.safe_load(f"{vim_details_text}") |
| 2337 | |
| 2338 | for vol_id in vim_details.get("os-extended-volumes:volumes_attached", []): |
| 2339 | volumes_list.append(vol_id["id"]) |
| 2340 | |
| 2341 | return volumes_list |
| 2342 | |
| 2343 | def prepare_changes_to_recreate( |
| 2344 | self, |
| 2345 | indata, |
| 2346 | nsr_id, |
| 2347 | db_nsr, |
| 2348 | db_vnfrs, |
| 2349 | db_ro_nsr, |
| 2350 | action_id, |
| 2351 | tasks_by_target_record_id, |
| 2352 | ): |
| 2353 | """This method will obtain an ordered list of items (`changes_list`) |
| 2354 | to be created and deleted to meet the recreate request. |
| 2355 | """ |
| 2356 | |
| 2357 | self.logger.debug( |
| 2358 | "ns.prepare_changes_to_recreate nsr_id={} indata={}".format(nsr_id, indata) |
| 2359 | ) |
| 2360 | |
| 2361 | task_index = 0 |
| 2362 | # set list with diffs: |
| 2363 | changes_list = [] |
| 2364 | db_path = self.db_path_map["vdu"] |
| 2365 | target_list = indata.get("healVnfData", {}) |
| 2366 | vdu2cloud_init = indata.get("cloud_init_content") or {} |
| 2367 | ro_nsr_public_key = db_ro_nsr["public_key"] |
| 2368 | |
| 2369 | # Check each VNF of the target |
| 2370 | for target_vnf in target_list: |
| Gabriel Cuba | b1bc669 | 2022-08-19 18:23:00 -0500 | [diff] [blame] | 2371 | # Find this VNF in the list from DB, raise exception if vnfInstanceId is not found |
| 2372 | vnfr_id = target_vnf["vnfInstanceId"] |
| elumalai | 321d2e9 | 2023-04-28 17:03:07 +0530 | [diff] [blame^] | 2373 | existing_vnf = db_vnfrs.get(vnfr_id, {}) |
| Gabriel Cuba | b1bc669 | 2022-08-19 18:23:00 -0500 | [diff] [blame] | 2374 | db_record = "vnfrs:{}:{}".format(vnfr_id, db_path) |
| 2375 | # vim_account_id = existing_vnf.get("vim-account-id", "") |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2376 | |
| Gabriel Cuba | b1bc669 | 2022-08-19 18:23:00 -0500 | [diff] [blame] | 2377 | target_vdus = target_vnf.get("additionalParams", {}).get("vdu", []) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2378 | # Check each VDU of this VNF |
| Gabriel Cuba | b1bc669 | 2022-08-19 18:23:00 -0500 | [diff] [blame] | 2379 | if not target_vdus: |
| 2380 | # Create target_vdu_list from DB, if VDUs are not specified |
| 2381 | target_vdus = [] |
| 2382 | for existing_vdu in existing_vnf.get("vdur"): |
| 2383 | vdu_name = existing_vdu.get("vdu-name", None) |
| 2384 | vdu_index = existing_vdu.get("count-index", 0) |
| 2385 | vdu_to_be_healed = {"vdu-id": vdu_name, "count-index": vdu_index} |
| 2386 | target_vdus.append(vdu_to_be_healed) |
| 2387 | for target_vdu in target_vdus: |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2388 | vdu_name = target_vdu.get("vdu-id", None) |
| 2389 | # For multi instance VDU count-index is mandatory |
| 2390 | # For single session VDU count-indes is 0 |
| 2391 | count_index = target_vdu.get("count-index", 0) |
| 2392 | item_index = 0 |
| elumalai | 321d2e9 | 2023-04-28 17:03:07 +0530 | [diff] [blame^] | 2393 | existing_instance = {} |
| 2394 | if existing_vnf: |
| 2395 | for instance in existing_vnf.get("vdur", {}): |
| 2396 | if ( |
| 2397 | instance["vdu-name"] == vdu_name |
| 2398 | and instance["count-index"] == count_index |
| 2399 | ): |
| 2400 | existing_instance = instance |
| 2401 | break |
| 2402 | else: |
| 2403 | item_index += 1 |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2404 | |
| 2405 | target_record_id = "{}.{}".format(db_record, existing_instance["id"]) |
| 2406 | |
| 2407 | # The target VIM is the one already existing in DB to recreate |
| 2408 | for target_vim, target_viminfo in existing_instance.get( |
| 2409 | "vim_info", {} |
| 2410 | ).items(): |
| 2411 | # step 1 vdu to be deleted |
| 2412 | self._assign_vim(target_vim) |
| 2413 | deployment_info = { |
| 2414 | "action_id": action_id, |
| 2415 | "nsr_id": nsr_id, |
| 2416 | "task_index": task_index, |
| 2417 | } |
| 2418 | |
| 2419 | target_record = f"{db_record}.{item_index}.vim_info.{target_vim}" |
| 2420 | created_items = self._prepare_created_items_for_healing( |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2421 | nsr_id, target_record |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2422 | ) |
| 2423 | |
| 2424 | volumes_to_hold = self._prepare_persistent_volumes_for_healing( |
| 2425 | target_vim, existing_instance |
| 2426 | ) |
| 2427 | |
| 2428 | # Specific extra params for recreate tasks: |
| 2429 | extra_dict = { |
| 2430 | "created_items": created_items, |
| 2431 | "vim_id": existing_instance["vim-id"], |
| 2432 | "volumes_to_hold": volumes_to_hold, |
| 2433 | } |
| 2434 | |
| 2435 | changes_list.append( |
| 2436 | { |
| 2437 | "deployment_info": deployment_info, |
| 2438 | "target_id": target_vim, |
| 2439 | "item": "vdu", |
| 2440 | "action": "DELETE", |
| 2441 | "target_record": target_record, |
| 2442 | "target_record_id": target_record_id, |
| 2443 | "extra_dict": extra_dict, |
| 2444 | } |
| 2445 | ) |
| 2446 | delete_task_id = f"{action_id}:{task_index}" |
| 2447 | task_index += 1 |
| 2448 | |
| 2449 | # step 2 vdu to be created |
| 2450 | kwargs = {} |
| 2451 | kwargs.update( |
| 2452 | { |
| 2453 | "vnfr_id": vnfr_id, |
| 2454 | "nsr_id": nsr_id, |
| 2455 | "vnfr": existing_vnf, |
| 2456 | "vdu2cloud_init": vdu2cloud_init, |
| 2457 | "tasks_by_target_record_id": tasks_by_target_record_id, |
| 2458 | "logger": self.logger, |
| 2459 | "db": self.db, |
| 2460 | "fs": self.fs, |
| 2461 | "ro_nsr_public_key": ro_nsr_public_key, |
| 2462 | } |
| 2463 | ) |
| 2464 | |
| 2465 | extra_dict = self._process_recreate_vdu_params( |
| 2466 | existing_instance, |
| 2467 | db_nsr, |
| 2468 | target_viminfo, |
| 2469 | target_record_id, |
| 2470 | target_vim, |
| 2471 | **kwargs, |
| 2472 | ) |
| 2473 | |
| 2474 | # The CREATE task depens on the DELETE task |
| 2475 | extra_dict["depends_on"] = [delete_task_id] |
| 2476 | |
| palaciosj | 42eb06f | 2022-05-05 14:59:36 +0000 | [diff] [blame] | 2477 | # Add volumes created from created_items if any |
| 2478 | # Ports should be deleted with delete task and automatically created with create task |
| 2479 | volumes = {} |
| 2480 | for k, v in created_items.items(): |
| 2481 | try: |
| 2482 | k_item, _, k_id = k.partition(":") |
| 2483 | if k_item == "volume": |
| 2484 | volumes[k] = v |
| 2485 | except Exception as e: |
| 2486 | self.logger.error( |
| 2487 | "Error evaluating created item {}: {}".format(k, e) |
| 2488 | ) |
| 2489 | extra_dict["previous_created_volumes"] = volumes |
| 2490 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2491 | deployment_info = { |
| 2492 | "action_id": action_id, |
| 2493 | "nsr_id": nsr_id, |
| 2494 | "task_index": task_index, |
| 2495 | } |
| 2496 | self._assign_vim(target_vim) |
| 2497 | |
| 2498 | new_item = { |
| 2499 | "deployment_info": deployment_info, |
| 2500 | "target_id": target_vim, |
| 2501 | "item": "vdu", |
| 2502 | "action": "CREATE", |
| 2503 | "target_record": target_record, |
| 2504 | "target_record_id": target_record_id, |
| 2505 | "extra_dict": extra_dict, |
| 2506 | } |
| 2507 | changes_list.append(new_item) |
| 2508 | tasks_by_target_record_id[target_record_id] = new_item |
| 2509 | task_index += 1 |
| 2510 | |
| 2511 | return changes_list |
| 2512 | |
| 2513 | def recreate(self, session, indata, version, nsr_id, *args, **kwargs): |
| 2514 | self.logger.debug("ns.recreate nsr_id={} indata={}".format(nsr_id, indata)) |
| 2515 | # TODO: validate_input(indata, recreate_schema) |
| 2516 | action_id = indata.get("action_id", str(uuid4())) |
| 2517 | # get current deployment |
| 2518 | db_vnfrs = {} # vnf's info indexed by _id |
| 2519 | step = "" |
| 2520 | logging_text = "Recreate nsr_id={} action_id={} indata={}".format( |
| 2521 | nsr_id, action_id, indata |
| 2522 | ) |
| 2523 | self.logger.debug(logging_text + "Enter") |
| 2524 | |
| 2525 | try: |
| 2526 | step = "Getting ns and vnfr record from db" |
| 2527 | db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) |
| 2528 | db_new_tasks = [] |
| 2529 | tasks_by_target_record_id = {} |
| 2530 | # read from db: vnf's of this ns |
| 2531 | step = "Getting vnfrs from db" |
| 2532 | db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| 2533 | self.logger.debug("ns.recreate: db_vnfrs_list={}".format(db_vnfrs_list)) |
| 2534 | |
| 2535 | if not db_vnfrs_list: |
| 2536 | raise NsException("Cannot obtain associated VNF for ns") |
| 2537 | |
| 2538 | for vnfr in db_vnfrs_list: |
| 2539 | db_vnfrs[vnfr["_id"]] = vnfr |
| 2540 | |
| 2541 | now = time() |
| 2542 | db_ro_nsr = self.db.get_one("ro_nsrs", {"_id": nsr_id}, fail_on_empty=False) |
| 2543 | self.logger.debug("ns.recreate: db_ro_nsr={}".format(db_ro_nsr)) |
| 2544 | |
| 2545 | if not db_ro_nsr: |
| 2546 | db_ro_nsr = self._create_db_ro_nsrs(nsr_id, now) |
| 2547 | |
| 2548 | with self.write_lock: |
| 2549 | # NS |
| 2550 | step = "process NS elements" |
| 2551 | changes_list = self.prepare_changes_to_recreate( |
| 2552 | indata=indata, |
| 2553 | nsr_id=nsr_id, |
| 2554 | db_nsr=db_nsr, |
| 2555 | db_vnfrs=db_vnfrs, |
| 2556 | db_ro_nsr=db_ro_nsr, |
| 2557 | action_id=action_id, |
| 2558 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 2559 | ) |
| 2560 | |
| 2561 | self.define_all_tasks( |
| 2562 | changes_list=changes_list, |
| 2563 | db_new_tasks=db_new_tasks, |
| 2564 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 2565 | ) |
| 2566 | |
| 2567 | # Delete all ro_tasks registered for the targets vdurs (target_record) |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 2568 | # If task of type CREATE exist then vim will try to get info form deleted VMs. |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2569 | # So remove all task related to target record. |
| 2570 | ro_tasks = self.db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id}) |
| 2571 | for change in changes_list: |
| 2572 | for ro_task in ro_tasks: |
| 2573 | for task in ro_task["tasks"]: |
| 2574 | if task["target_record"] == change["target_record"]: |
| 2575 | self.db.del_one( |
| 2576 | "ro_tasks", |
| 2577 | q_filter={ |
| 2578 | "_id": ro_task["_id"], |
| 2579 | "modified_at": ro_task["modified_at"], |
| 2580 | }, |
| 2581 | fail_on_empty=False, |
| 2582 | ) |
| aticig | 285185e | 2022-05-02 21:23:48 +0300 | [diff] [blame] | 2583 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2584 | step = "Updating database, Appending tasks to ro_tasks" |
| 2585 | self.upload_recreate_tasks( |
| 2586 | db_new_tasks=db_new_tasks, |
| 2587 | now=now, |
| 2588 | ) |
| 2589 | |
| 2590 | self.logger.debug( |
| 2591 | logging_text + "Exit. Created {} tasks".format(len(db_new_tasks)) |
| 2592 | ) |
| 2593 | |
| 2594 | return ( |
| 2595 | {"status": "ok", "nsr_id": nsr_id, "action_id": action_id}, |
| 2596 | action_id, |
| 2597 | True, |
| 2598 | ) |
| 2599 | except Exception as e: |
| 2600 | if isinstance(e, (DbException, NsException)): |
| 2601 | self.logger.error( |
| 2602 | logging_text + "Exit Exception while '{}': {}".format(step, e) |
| 2603 | ) |
| 2604 | else: |
| 2605 | e = traceback_format_exc() |
| 2606 | self.logger.critical( |
| 2607 | logging_text + "Exit Exception while '{}': {}".format(step, e), |
| 2608 | exc_info=True, |
| 2609 | ) |
| 2610 | |
| 2611 | raise NsException(e) |
| 2612 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2613 | def deploy(self, session, indata, version, nsr_id, *args, **kwargs): |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2614 | self.logger.debug("ns.deploy nsr_id={} indata={}".format(nsr_id, indata)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2615 | validate_input(indata, deploy_schema) |
| 2616 | action_id = indata.get("action_id", str(uuid4())) |
| 2617 | task_index = 0 |
| 2618 | # get current deployment |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2619 | db_nsr_update = {} # update operation on nsrs |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2620 | db_vnfrs_update = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2621 | db_vnfrs = {} # vnf's info indexed by _id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2622 | step = "" |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2623 | logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id) |
| 2624 | self.logger.debug(logging_text + "Enter") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2625 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2626 | try: |
| 2627 | step = "Getting ns and vnfr record from db" |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2628 | db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2629 | self.logger.debug("ns.deploy: db_nsr={}".format(db_nsr)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2630 | db_new_tasks = [] |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2631 | tasks_by_target_record_id = {} |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2632 | # read from db: vnf's of this ns |
| 2633 | step = "Getting vnfrs from db" |
| 2634 | db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2635 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2636 | if not db_vnfrs_list: |
| 2637 | raise NsException("Cannot obtain associated VNF for ns") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2638 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2639 | for vnfr in db_vnfrs_list: |
| 2640 | db_vnfrs[vnfr["_id"]] = vnfr |
| 2641 | db_vnfrs_update[vnfr["_id"]] = {} |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2642 | self.logger.debug("ns.deploy db_vnfrs={}".format(db_vnfrs)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2643 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2644 | now = time() |
| 2645 | db_ro_nsr = self.db.get_one("ro_nsrs", {"_id": nsr_id}, fail_on_empty=False) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2646 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2647 | if not db_ro_nsr: |
| 2648 | db_ro_nsr = self._create_db_ro_nsrs(nsr_id, now) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2649 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2650 | # check that action_id is not in the list of actions. Suffixed with :index |
| 2651 | if action_id in db_ro_nsr["actions"]: |
| 2652 | index = 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2653 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2654 | while True: |
| 2655 | new_action_id = "{}:{}".format(action_id, index) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2656 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2657 | if new_action_id not in db_ro_nsr["actions"]: |
| 2658 | action_id = new_action_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2659 | self.logger.debug( |
| 2660 | logging_text |
| 2661 | + "Changing action_id in use to {}".format(action_id) |
| 2662 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2663 | break |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2664 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2665 | index += 1 |
| 2666 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2667 | def _process_action(indata): |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2668 | nonlocal db_new_tasks |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 2669 | nonlocal action_id |
| 2670 | nonlocal nsr_id |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2671 | nonlocal task_index |
| 2672 | nonlocal db_vnfrs |
| 2673 | nonlocal db_ro_nsr |
| 2674 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2675 | if indata["action"]["action"] == "inject_ssh_key": |
| 2676 | key = indata["action"].get("key") |
| 2677 | user = indata["action"].get("user") |
| 2678 | password = indata["action"].get("password") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2679 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2680 | for vnf in indata.get("vnf", ()): |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2681 | if vnf["_id"] not in db_vnfrs: |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2682 | raise NsException("Invalid vnf={}".format(vnf["_id"])) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2683 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2684 | db_vnfr = db_vnfrs[vnf["_id"]] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2685 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2686 | for target_vdu in vnf.get("vdur", ()): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2687 | vdu_index, vdur = next( |
| 2688 | ( |
| 2689 | i_v |
| 2690 | for i_v in enumerate(db_vnfr["vdur"]) |
| 2691 | if i_v[1]["id"] == target_vdu["id"] |
| 2692 | ), |
| 2693 | (None, None), |
| 2694 | ) |
| 2695 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2696 | if not vdur: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2697 | raise NsException( |
| 2698 | "Invalid vdu vnf={}.{}".format( |
| 2699 | vnf["_id"], target_vdu["id"] |
| 2700 | ) |
| 2701 | ) |
| 2702 | |
| 2703 | target_vim, vim_info = next( |
| 2704 | k_v for k_v in vdur["vim_info"].items() |
| 2705 | ) |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 2706 | self._assign_vim(target_vim) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2707 | target_record = "vnfrs:{}:vdur.{}.ssh_keys".format( |
| 2708 | vnf["_id"], vdu_index |
| 2709 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2710 | extra_dict = { |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2711 | "depends_on": [ |
| 2712 | "vnfrs:{}:vdur.{}".format(vnf["_id"], vdur["id"]) |
| 2713 | ], |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2714 | "params": { |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2715 | "ip_address": vdur.get("ip-address"), |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2716 | "user": user, |
| 2717 | "key": key, |
| 2718 | "password": password, |
| 2719 | "private_key": db_ro_nsr["private_key"], |
| 2720 | "salt": db_ro_nsr["_id"], |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2721 | "schema_version": db_ro_nsr["_admin"][ |
| 2722 | "schema_version" |
| 2723 | ], |
| 2724 | }, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2725 | } |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 2726 | |
| 2727 | deployment_info = { |
| 2728 | "action_id": action_id, |
| 2729 | "nsr_id": nsr_id, |
| 2730 | "task_index": task_index, |
| 2731 | } |
| 2732 | |
| 2733 | task = Ns._create_task( |
| 2734 | deployment_info=deployment_info, |
| 2735 | target_id=target_vim, |
| 2736 | item="vdu", |
| 2737 | action="EXEC", |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2738 | target_record=target_record, |
| 2739 | target_record_id=None, |
| 2740 | extra_dict=extra_dict, |
| 2741 | ) |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 2742 | |
| 2743 | task_index = deployment_info.get("task_index") |
| 2744 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2745 | db_new_tasks.append(task) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2746 | |
| 2747 | with self.write_lock: |
| 2748 | if indata.get("action"): |
| 2749 | _process_action(indata) |
| 2750 | else: |
| 2751 | # compute network differences |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2752 | # NS |
| 2753 | step = "process NS elements" |
| 2754 | changes_list = self.calculate_all_differences_to_deploy( |
| 2755 | indata=indata, |
| 2756 | nsr_id=nsr_id, |
| 2757 | db_nsr=db_nsr, |
| 2758 | db_vnfrs=db_vnfrs, |
| 2759 | db_ro_nsr=db_ro_nsr, |
| 2760 | db_nsr_update=db_nsr_update, |
| 2761 | db_vnfrs_update=db_vnfrs_update, |
| 2762 | action_id=action_id, |
| 2763 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 2764 | ) |
| 2765 | self.define_all_tasks( |
| 2766 | changes_list=changes_list, |
| 2767 | db_new_tasks=db_new_tasks, |
| 2768 | tasks_by_target_record_id=tasks_by_target_record_id, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2769 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2770 | |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2771 | step = "Updating database, Appending tasks to ro_tasks" |
| 2772 | self.upload_all_tasks( |
| 2773 | db_new_tasks=db_new_tasks, |
| 2774 | now=now, |
| 2775 | ) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2776 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2777 | step = "Updating database, nsrs" |
| 2778 | if db_nsr_update: |
| 2779 | self.db.set_one("nsrs", {"_id": nsr_id}, db_nsr_update) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2780 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2781 | for vnfr_id, db_vnfr_update in db_vnfrs_update.items(): |
| 2782 | if db_vnfr_update: |
| 2783 | step = "Updating database, vnfrs={}".format(vnfr_id) |
| 2784 | self.db.set_one("vnfrs", {"_id": vnfr_id}, db_vnfr_update) |
| 2785 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2786 | self.logger.debug( |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame] | 2787 | logging_text + "Exit. Created {} tasks".format(len(db_new_tasks)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2788 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2789 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2790 | return ( |
| 2791 | {"status": "ok", "nsr_id": nsr_id, "action_id": action_id}, |
| 2792 | action_id, |
| 2793 | True, |
| 2794 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2795 | except Exception as e: |
| 2796 | if isinstance(e, (DbException, NsException)): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2797 | self.logger.error( |
| 2798 | logging_text + "Exit Exception while '{}': {}".format(step, e) |
| 2799 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2800 | else: |
| 2801 | e = traceback_format_exc() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2802 | self.logger.critical( |
| 2803 | logging_text + "Exit Exception while '{}': {}".format(step, e), |
| 2804 | exc_info=True, |
| 2805 | ) |
| 2806 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2807 | raise NsException(e) |
| 2808 | |
| 2809 | def delete(self, session, indata, version, nsr_id, *args, **kwargs): |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2810 | self.logger.debug("ns.delete version={} nsr_id={}".format(version, nsr_id)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2811 | # self.db.del_list({"_id": ro_task["_id"], "tasks.nsr_id.ne": nsr_id}) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2812 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2813 | with self.write_lock: |
| 2814 | try: |
| 2815 | NsWorker.delete_db_tasks(self.db, nsr_id, None) |
| 2816 | except NsWorkerException as e: |
| 2817 | raise NsException(e) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2818 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2819 | return None, None, True |
| 2820 | |
| 2821 | def status(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2822 | self.logger.debug( |
| 2823 | "ns.status version={} nsr_id={}, action_id={} indata={}".format( |
| 2824 | version, nsr_id, action_id, indata |
| 2825 | ) |
| 2826 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2827 | task_list = [] |
| 2828 | done = 0 |
| 2829 | total = 0 |
| 2830 | ro_tasks = self.db.get_list("ro_tasks", {"tasks.action_id": action_id}) |
| 2831 | global_status = "DONE" |
| 2832 | details = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2833 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2834 | for ro_task in ro_tasks: |
| 2835 | for task in ro_task["tasks"]: |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2836 | if task and task["action_id"] == action_id: |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2837 | task_list.append(task) |
| 2838 | total += 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2839 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2840 | if task["status"] == "FAILED": |
| 2841 | global_status = "FAILED" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2842 | error_text = "Error at {} {}: {}".format( |
| 2843 | task["action"].lower(), |
| 2844 | task["item"], |
| aticig | 79ac6df | 2022-05-06 16:09:52 +0300 | [diff] [blame] | 2845 | ro_task["vim_info"].get("vim_message") or "unknown", |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2846 | ) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 2847 | details.append(error_text) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2848 | elif task["status"] in ("SCHEDULED", "BUILD"): |
| 2849 | if global_status != "FAILED": |
| 2850 | global_status = "BUILD" |
| 2851 | else: |
| 2852 | done += 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2853 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2854 | return_data = { |
| 2855 | "status": global_status, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2856 | "details": ". ".join(details) |
| 2857 | if details |
| 2858 | else "progress {}/{}".format(done, total), |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2859 | "nsr_id": nsr_id, |
| 2860 | "action_id": action_id, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2861 | "tasks": task_list, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2862 | } |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2863 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2864 | return return_data, None, True |
| 2865 | |
| palaciosj | 8f2060b | 2022-02-24 12:05:59 +0000 | [diff] [blame] | 2866 | def recreate_status( |
| 2867 | self, session, indata, version, nsr_id, action_id, *args, **kwargs |
| 2868 | ): |
| 2869 | return self.status(session, indata, version, nsr_id, action_id, *args, **kwargs) |
| 2870 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2871 | def cancel(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2872 | print( |
| 2873 | "ns.cancel session={} indata={} version={} nsr_id={}, action_id={}".format( |
| 2874 | session, indata, version, nsr_id, action_id |
| 2875 | ) |
| 2876 | ) |
| 2877 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2878 | return None, None, True |
| 2879 | |
| k4.rahul | 78f474e | 2022-05-02 15:47:57 +0000 | [diff] [blame] | 2880 | def rebuild_start_stop_task( |
| 2881 | self, |
| 2882 | vdu_id, |
| 2883 | vnf_id, |
| 2884 | vdu_index, |
| 2885 | action_id, |
| 2886 | nsr_id, |
| 2887 | task_index, |
| 2888 | target_vim, |
| 2889 | extra_dict, |
| 2890 | ): |
| 2891 | self._assign_vim(target_vim) |
| 2892 | target_record = "vnfrs:{}:vdur.{}".format(vnf_id, vdu_index) |
| 2893 | target_record_id = "vnfrs:{}:vdur.{}".format(vnf_id, vdu_id) |
| 2894 | deployment_info = { |
| 2895 | "action_id": action_id, |
| 2896 | "nsr_id": nsr_id, |
| 2897 | "task_index": task_index, |
| 2898 | } |
| 2899 | |
| 2900 | task = Ns._create_task( |
| 2901 | deployment_info=deployment_info, |
| 2902 | target_id=target_vim, |
| 2903 | item="update", |
| 2904 | action="EXEC", |
| 2905 | target_record=target_record, |
| 2906 | target_record_id=target_record_id, |
| 2907 | extra_dict=extra_dict, |
| 2908 | ) |
| 2909 | return task |
| 2910 | |
| 2911 | def rebuild_start_stop( |
| 2912 | self, session, action_dict, version, nsr_id, *args, **kwargs |
| 2913 | ): |
| 2914 | task_index = 0 |
| 2915 | extra_dict = {} |
| 2916 | now = time() |
| 2917 | action_id = action_dict.get("action_id", str(uuid4())) |
| 2918 | step = "" |
| 2919 | logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id) |
| 2920 | self.logger.debug(logging_text + "Enter") |
| 2921 | |
| 2922 | action = list(action_dict.keys())[0] |
| 2923 | task_dict = action_dict.get(action) |
| 2924 | vim_vm_id = action_dict.get(action).get("vim_vm_id") |
| 2925 | |
| 2926 | if action_dict.get("stop"): |
| 2927 | action = "shutoff" |
| 2928 | db_new_tasks = [] |
| 2929 | try: |
| 2930 | step = "lock the operation & do task creation" |
| 2931 | with self.write_lock: |
| 2932 | extra_dict["params"] = { |
| 2933 | "vim_vm_id": vim_vm_id, |
| 2934 | "action": action, |
| 2935 | } |
| 2936 | task = self.rebuild_start_stop_task( |
| 2937 | task_dict["vdu_id"], |
| 2938 | task_dict["vnf_id"], |
| 2939 | task_dict["vdu_index"], |
| 2940 | action_id, |
| 2941 | nsr_id, |
| 2942 | task_index, |
| 2943 | task_dict["target_vim"], |
| 2944 | extra_dict, |
| 2945 | ) |
| 2946 | db_new_tasks.append(task) |
| 2947 | step = "upload Task to db" |
| 2948 | self.upload_all_tasks( |
| 2949 | db_new_tasks=db_new_tasks, |
| 2950 | now=now, |
| 2951 | ) |
| 2952 | self.logger.debug( |
| 2953 | logging_text + "Exit. Created {} tasks".format(len(db_new_tasks)) |
| 2954 | ) |
| 2955 | return ( |
| 2956 | {"status": "ok", "nsr_id": nsr_id, "action_id": action_id}, |
| 2957 | action_id, |
| 2958 | True, |
| 2959 | ) |
| 2960 | except Exception as e: |
| 2961 | if isinstance(e, (DbException, NsException)): |
| 2962 | self.logger.error( |
| 2963 | logging_text + "Exit Exception while '{}': {}".format(step, e) |
| 2964 | ) |
| 2965 | else: |
| 2966 | e = traceback_format_exc() |
| 2967 | self.logger.critical( |
| 2968 | logging_text + "Exit Exception while '{}': {}".format(step, e), |
| 2969 | exc_info=True, |
| 2970 | ) |
| 2971 | raise NsException(e) |
| 2972 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2973 | def get_deploy(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| 2974 | nsrs = self.db.get_list("nsrs", {}) |
| 2975 | return_data = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2976 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2977 | for ns in nsrs: |
| 2978 | return_data.append({"_id": ns["_id"], "name": ns["name"]}) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2979 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2980 | return return_data, None, True |
| 2981 | |
| 2982 | def get_actions(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| 2983 | ro_tasks = self.db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id}) |
| 2984 | return_data = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2985 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2986 | for ro_task in ro_tasks: |
| 2987 | for task in ro_task["tasks"]: |
| 2988 | if task["action_id"] not in return_data: |
| 2989 | return_data.append(task["action_id"]) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 2990 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 2991 | return return_data, None, True |
| elumalai | 8658c2c | 2022-04-28 19:09:31 +0530 | [diff] [blame] | 2992 | |
| 2993 | def migrate_task( |
| 2994 | self, vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict |
| 2995 | ): |
| 2996 | target_vim, vim_info = next(k_v for k_v in vdu["vim_info"].items()) |
| 2997 | self._assign_vim(target_vim) |
| 2998 | target_record = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu_index) |
| 2999 | target_record_id = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu["id"]) |
| 3000 | deployment_info = { |
| 3001 | "action_id": action_id, |
| 3002 | "nsr_id": nsr_id, |
| 3003 | "task_index": task_index, |
| 3004 | } |
| 3005 | |
| 3006 | task = Ns._create_task( |
| 3007 | deployment_info=deployment_info, |
| 3008 | target_id=target_vim, |
| 3009 | item="migrate", |
| 3010 | action="EXEC", |
| 3011 | target_record=target_record, |
| 3012 | target_record_id=target_record_id, |
| 3013 | extra_dict=extra_dict, |
| 3014 | ) |
| 3015 | |
| 3016 | return task |
| 3017 | |
| 3018 | def migrate(self, session, indata, version, nsr_id, *args, **kwargs): |
| 3019 | task_index = 0 |
| 3020 | extra_dict = {} |
| 3021 | now = time() |
| 3022 | action_id = indata.get("action_id", str(uuid4())) |
| 3023 | step = "" |
| 3024 | logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id) |
| 3025 | self.logger.debug(logging_text + "Enter") |
| 3026 | try: |
| 3027 | vnf_instance_id = indata["vnfInstanceId"] |
| 3028 | step = "Getting vnfrs from db" |
| 3029 | db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_instance_id}) |
| 3030 | vdu = indata.get("vdu") |
| 3031 | migrateToHost = indata.get("migrateToHost") |
| 3032 | db_new_tasks = [] |
| 3033 | |
| 3034 | with self.write_lock: |
| 3035 | if vdu is not None: |
| 3036 | vdu_id = indata["vdu"]["vduId"] |
| 3037 | vdu_count_index = indata["vdu"].get("vduCountIndex", 0) |
| 3038 | for vdu_index, vdu in enumerate(db_vnfr["vdur"]): |
| 3039 | if ( |
| 3040 | vdu["vdu-id-ref"] == vdu_id |
| 3041 | and vdu["count-index"] == vdu_count_index |
| 3042 | ): |
| 3043 | extra_dict["params"] = { |
| 3044 | "vim_vm_id": vdu["vim-id"], |
| 3045 | "migrate_host": migrateToHost, |
| 3046 | "vdu_vim_info": vdu["vim_info"], |
| 3047 | } |
| 3048 | step = "Creating migration task for vdu:{}".format(vdu) |
| 3049 | task = self.migrate_task( |
| 3050 | vdu, |
| 3051 | db_vnfr, |
| 3052 | vdu_index, |
| 3053 | action_id, |
| 3054 | nsr_id, |
| 3055 | task_index, |
| 3056 | extra_dict, |
| 3057 | ) |
| 3058 | db_new_tasks.append(task) |
| 3059 | task_index += 1 |
| 3060 | break |
| 3061 | else: |
| elumalai | 8658c2c | 2022-04-28 19:09:31 +0530 | [diff] [blame] | 3062 | for vdu_index, vdu in enumerate(db_vnfr["vdur"]): |
| 3063 | extra_dict["params"] = { |
| 3064 | "vim_vm_id": vdu["vim-id"], |
| 3065 | "migrate_host": migrateToHost, |
| 3066 | "vdu_vim_info": vdu["vim_info"], |
| 3067 | } |
| 3068 | step = "Creating migration task for vdu:{}".format(vdu) |
| 3069 | task = self.migrate_task( |
| 3070 | vdu, |
| 3071 | db_vnfr, |
| 3072 | vdu_index, |
| 3073 | action_id, |
| 3074 | nsr_id, |
| 3075 | task_index, |
| 3076 | extra_dict, |
| 3077 | ) |
| 3078 | db_new_tasks.append(task) |
| 3079 | task_index += 1 |
| 3080 | |
| 3081 | self.upload_all_tasks( |
| 3082 | db_new_tasks=db_new_tasks, |
| 3083 | now=now, |
| 3084 | ) |
| 3085 | |
| 3086 | self.logger.debug( |
| 3087 | logging_text + "Exit. Created {} tasks".format(len(db_new_tasks)) |
| 3088 | ) |
| 3089 | return ( |
| 3090 | {"status": "ok", "nsr_id": nsr_id, "action_id": action_id}, |
| 3091 | action_id, |
| 3092 | True, |
| 3093 | ) |
| 3094 | except Exception as e: |
| 3095 | if isinstance(e, (DbException, NsException)): |
| 3096 | self.logger.error( |
| 3097 | logging_text + "Exit Exception while '{}': {}".format(step, e) |
| 3098 | ) |
| 3099 | else: |
| 3100 | e = traceback_format_exc() |
| 3101 | self.logger.critical( |
| 3102 | logging_text + "Exit Exception while '{}': {}".format(step, e), |
| 3103 | exc_info=True, |
| 3104 | ) |
| 3105 | raise NsException(e) |
| sritharan | 29a4c1a | 2022-05-05 12:15:04 +0000 | [diff] [blame] | 3106 | |
| 3107 | def verticalscale_task( |
| 3108 | self, vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict |
| 3109 | ): |
| 3110 | target_vim, vim_info = next(k_v for k_v in vdu["vim_info"].items()) |
| 3111 | self._assign_vim(target_vim) |
| 3112 | target_record = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu_index) |
| 3113 | target_record_id = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu["id"]) |
| 3114 | deployment_info = { |
| 3115 | "action_id": action_id, |
| 3116 | "nsr_id": nsr_id, |
| 3117 | "task_index": task_index, |
| 3118 | } |
| 3119 | |
| 3120 | task = Ns._create_task( |
| 3121 | deployment_info=deployment_info, |
| 3122 | target_id=target_vim, |
| 3123 | item="verticalscale", |
| 3124 | action="EXEC", |
| 3125 | target_record=target_record, |
| 3126 | target_record_id=target_record_id, |
| 3127 | extra_dict=extra_dict, |
| 3128 | ) |
| 3129 | return task |
| 3130 | |
| 3131 | def verticalscale(self, session, indata, version, nsr_id, *args, **kwargs): |
| 3132 | task_index = 0 |
| 3133 | extra_dict = {} |
| 3134 | now = time() |
| 3135 | action_id = indata.get("action_id", str(uuid4())) |
| 3136 | step = "" |
| 3137 | logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id) |
| 3138 | self.logger.debug(logging_text + "Enter") |
| 3139 | try: |
| 3140 | VnfFlavorData = indata.get("changeVnfFlavorData") |
| 3141 | vnf_instance_id = VnfFlavorData["vnfInstanceId"] |
| 3142 | step = "Getting vnfrs from db" |
| 3143 | db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_instance_id}) |
| 3144 | vduid = VnfFlavorData["additionalParams"]["vduid"] |
| 3145 | vduCountIndex = VnfFlavorData["additionalParams"]["vduCountIndex"] |
| 3146 | virtualMemory = VnfFlavorData["additionalParams"]["virtualMemory"] |
| 3147 | numVirtualCpu = VnfFlavorData["additionalParams"]["numVirtualCpu"] |
| 3148 | sizeOfStorage = VnfFlavorData["additionalParams"]["sizeOfStorage"] |
| 3149 | flavor_dict = { |
| 3150 | "name": vduid + "-flv", |
| 3151 | "ram": virtualMemory, |
| 3152 | "vcpus": numVirtualCpu, |
| 3153 | "disk": sizeOfStorage, |
| 3154 | } |
| 3155 | db_new_tasks = [] |
| 3156 | step = "Creating Tasks for vertical scaling" |
| 3157 | with self.write_lock: |
| 3158 | for vdu_index, vdu in enumerate(db_vnfr["vdur"]): |
| 3159 | if ( |
| 3160 | vdu["vdu-id-ref"] == vduid |
| 3161 | and vdu["count-index"] == vduCountIndex |
| 3162 | ): |
| 3163 | extra_dict["params"] = { |
| 3164 | "vim_vm_id": vdu["vim-id"], |
| 3165 | "flavor_dict": flavor_dict, |
| 3166 | } |
| 3167 | task = self.verticalscale_task( |
| 3168 | vdu, |
| 3169 | db_vnfr, |
| 3170 | vdu_index, |
| 3171 | action_id, |
| 3172 | nsr_id, |
| 3173 | task_index, |
| 3174 | extra_dict, |
| 3175 | ) |
| 3176 | db_new_tasks.append(task) |
| 3177 | task_index += 1 |
| 3178 | break |
| 3179 | self.upload_all_tasks( |
| 3180 | db_new_tasks=db_new_tasks, |
| 3181 | now=now, |
| 3182 | ) |
| 3183 | self.logger.debug( |
| 3184 | logging_text + "Exit. Created {} tasks".format(len(db_new_tasks)) |
| 3185 | ) |
| 3186 | return ( |
| 3187 | {"status": "ok", "nsr_id": nsr_id, "action_id": action_id}, |
| 3188 | action_id, |
| 3189 | True, |
| 3190 | ) |
| 3191 | except Exception as e: |
| 3192 | if isinstance(e, (DbException, NsException)): |
| 3193 | self.logger.error( |
| 3194 | logging_text + "Exit Exception while '{}': {}".format(step, e) |
| 3195 | ) |
| 3196 | else: |
| 3197 | e = traceback_format_exc() |
| 3198 | self.logger.critical( |
| 3199 | logging_text + "Exit Exception while '{}': {}".format(step, e), |
| 3200 | exc_info=True, |
| 3201 | ) |
| 3202 | raise NsException(e) |