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