| 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 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 20 | import logging |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 21 | from random import choice as random_choice |
| 22 | from threading import Lock |
| 23 | from time import time |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 24 | from traceback import format_exc as traceback_format_exc |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 25 | from typing import Any, Dict, Tuple, Type |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 26 | from uuid import uuid4 |
| 27 | |
| 28 | from cryptography.hazmat.backends import default_backend as crypto_default_backend |
| 29 | from cryptography.hazmat.primitives import serialization as crypto_serialization |
| 30 | from cryptography.hazmat.primitives.asymmetric import rsa |
| 31 | from jinja2 import ( |
| 32 | Environment, |
| 33 | StrictUndefined, |
| 34 | TemplateError, |
| 35 | TemplateNotFound, |
| 36 | UndefinedError, |
| 37 | ) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 38 | from osm_common import ( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 39 | dbmemory, |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 40 | dbmongo, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 41 | fslocal, |
| 42 | fsmongo, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 43 | msgkafka, |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 44 | msglocal, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 45 | version as common_version, |
| 46 | ) |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 47 | from osm_common.dbbase import DbBase, DbException |
| 48 | from osm_common.fsbase import FsBase, FsException |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 49 | from osm_common.msgbase import MsgException |
| sousaedu | 049cbb1 | 2022-01-05 11:39:35 +0000 | [diff] [blame] | 50 | from osm_ng_ro.ns_thread import deep_get, NsWorker, NsWorkerException |
| 51 | from osm_ng_ro.validation import deploy_schema, validate_input |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 52 | |
| 53 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 54 | min_common_version = "0.1.16" |
| 55 | |
| 56 | |
| 57 | class NsException(Exception): |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 58 | def __init__(self, message, http_code=HTTPStatus.BAD_REQUEST): |
| 59 | self.http_code = http_code |
| 60 | super(Exception, self).__init__(message) |
| 61 | |
| 62 | |
| 63 | def get_process_id(): |
| 64 | """ |
| 65 | Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it |
| 66 | will provide a random one |
| 67 | :return: Obtained ID |
| 68 | """ |
| 69 | # Try getting docker id. If fails, get pid |
| 70 | try: |
| 71 | with open("/proc/self/cgroup", "r") as f: |
| 72 | text_id_ = f.readline() |
| 73 | _, _, text_id = text_id_.rpartition("/") |
| 74 | text_id = text_id.replace("\n", "")[:12] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 75 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 76 | if text_id: |
| 77 | return text_id |
| 78 | except Exception: |
| 79 | pass |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 80 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 81 | # Return a random id |
| 82 | return "".join(random_choice("0123456789abcdef") for _ in range(12)) |
| 83 | |
| 84 | |
| 85 | def versiontuple(v): |
| 86 | """utility for compare dot separate versions. Fills with zeros to proper number comparison""" |
| 87 | filled = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 88 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 89 | for point in v.split("."): |
| 90 | filled.append(point.zfill(8)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 91 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 92 | return tuple(filled) |
| 93 | |
| 94 | |
| 95 | class Ns(object): |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 96 | def __init__(self): |
| 97 | self.db = None |
| 98 | self.fs = None |
| 99 | self.msg = None |
| 100 | self.config = None |
| 101 | # self.operations = None |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 102 | self.logger = None |
| 103 | # ^ Getting logger inside method self.start because parent logger (ro) is not available yet. |
| 104 | # 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] | 105 | self.map_topic = {} |
| 106 | self.write_lock = None |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 107 | self.vims_assigned = {} |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 108 | self.next_worker = 0 |
| 109 | self.plugins = {} |
| 110 | self.workers = [] |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame^] | 111 | self.process_params_function_map = { |
| 112 | "net": Ns._process_net_params, |
| 113 | "image": Ns._process_image_params, |
| 114 | "flavor": Ns._process_flavor_params, |
| 115 | "vdu": Ns._process_vdu_params, |
| 116 | } |
| 117 | self.db_path_map = { |
| 118 | "net": "vld", |
| 119 | "image": "image", |
| 120 | "flavor": "flavor", |
| 121 | "vdu": "vdur", |
| 122 | } |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 123 | |
| 124 | def init_db(self, target_version): |
| 125 | pass |
| 126 | |
| 127 | def start(self, config): |
| 128 | """ |
| 129 | Connect to database, filesystem storage, and messaging |
| 130 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 131 | :param config: Configuration of db, storage, etc |
| 132 | :return: None |
| 133 | """ |
| 134 | self.config = config |
| 135 | self.config["process_id"] = get_process_id() # used for HA identity |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 136 | self.logger = logging.getLogger("ro.ns") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 137 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 138 | # check right version of common |
| 139 | if versiontuple(common_version) < versiontuple(min_common_version): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 140 | raise NsException( |
| 141 | "Not compatible osm/common version '{}'. Needed '{}' or higher".format( |
| 142 | common_version, min_common_version |
| 143 | ) |
| 144 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 145 | |
| 146 | try: |
| 147 | if not self.db: |
| 148 | if config["database"]["driver"] == "mongo": |
| 149 | self.db = dbmongo.DbMongo() |
| 150 | self.db.db_connect(config["database"]) |
| 151 | elif config["database"]["driver"] == "memory": |
| 152 | self.db = dbmemory.DbMemory() |
| 153 | self.db.db_connect(config["database"]) |
| 154 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 155 | raise NsException( |
| 156 | "Invalid configuration param '{}' at '[database]':'driver'".format( |
| 157 | config["database"]["driver"] |
| 158 | ) |
| 159 | ) |
| 160 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 161 | if not self.fs: |
| 162 | if config["storage"]["driver"] == "local": |
| 163 | self.fs = fslocal.FsLocal() |
| 164 | self.fs.fs_connect(config["storage"]) |
| 165 | elif config["storage"]["driver"] == "mongo": |
| 166 | self.fs = fsmongo.FsMongo() |
| 167 | self.fs.fs_connect(config["storage"]) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 168 | elif config["storage"]["driver"] is None: |
| 169 | pass |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 170 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 171 | raise NsException( |
| 172 | "Invalid configuration param '{}' at '[storage]':'driver'".format( |
| 173 | config["storage"]["driver"] |
| 174 | ) |
| 175 | ) |
| 176 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 177 | if not self.msg: |
| 178 | if config["message"]["driver"] == "local": |
| 179 | self.msg = msglocal.MsgLocal() |
| 180 | self.msg.connect(config["message"]) |
| 181 | elif config["message"]["driver"] == "kafka": |
| 182 | self.msg = msgkafka.MsgKafka() |
| 183 | self.msg.connect(config["message"]) |
| 184 | else: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 185 | raise NsException( |
| 186 | "Invalid configuration param '{}' at '[message]':'driver'".format( |
| 187 | config["message"]["driver"] |
| 188 | ) |
| 189 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 190 | |
| 191 | # TODO load workers to deal with exising database tasks |
| 192 | |
| 193 | self.write_lock = Lock() |
| 194 | except (DbException, FsException, MsgException) as e: |
| 195 | raise NsException(str(e), http_code=e.http_code) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 196 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 197 | def get_assigned_vims(self): |
| 198 | return list(self.vims_assigned.keys()) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 199 | |
| 200 | def stop(self): |
| 201 | try: |
| 202 | if self.db: |
| 203 | self.db.db_disconnect() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 204 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 205 | if self.fs: |
| 206 | self.fs.fs_disconnect() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 207 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 208 | if self.msg: |
| 209 | self.msg.disconnect() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 210 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 211 | self.write_lock = None |
| 212 | except (DbException, FsException, MsgException) as e: |
| 213 | raise NsException(str(e), http_code=e.http_code) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 214 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 215 | for worker in self.workers: |
| 216 | worker.insert_task(("terminate",)) |
| 217 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 218 | def _create_worker(self): |
| 219 | """ |
| 220 | Look for a worker thread in idle status. If not found it creates one unless the number of threads reach the |
| 221 | limit of 'server.ns_threads' configuration. If reached, it just assigns one existing thread |
| 222 | return the index of the assigned worker thread. Worker threads are storead at self.workers |
| 223 | """ |
| 224 | # Look for a thread in idle status |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 225 | worker_id = next( |
| 226 | ( |
| 227 | i |
| 228 | for i in range(len(self.workers)) |
| 229 | if self.workers[i] and self.workers[i].idle |
| 230 | ), |
| 231 | None, |
| 232 | ) |
| 233 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 234 | if worker_id is not None: |
| 235 | # unset idle status to avoid race conditions |
| 236 | self.workers[worker_id].idle = False |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 237 | else: |
| 238 | worker_id = len(self.workers) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 239 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 240 | if worker_id < self.config["global"]["server.ns_threads"]: |
| 241 | # create a new worker |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 242 | self.workers.append( |
| 243 | NsWorker(worker_id, self.config, self.plugins, self.db) |
| 244 | ) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 245 | self.workers[worker_id].start() |
| 246 | else: |
| 247 | # reached maximum number of threads, assign VIM to an existing one |
| 248 | worker_id = self.next_worker |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 249 | self.next_worker = (self.next_worker + 1) % self.config["global"][ |
| 250 | "server.ns_threads" |
| 251 | ] |
| 252 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 253 | return worker_id |
| 254 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 255 | def assign_vim(self, target_id): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 256 | with self.write_lock: |
| 257 | return self._assign_vim(target_id) |
| 258 | |
| 259 | def _assign_vim(self, target_id): |
| 260 | if target_id not in self.vims_assigned: |
| 261 | worker_id = self.vims_assigned[target_id] = self._create_worker() |
| 262 | self.workers[worker_id].insert_task(("load_vim", target_id)) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 263 | |
| 264 | def reload_vim(self, target_id): |
| 265 | # send reload_vim to the thread working with this VIM and inform all that a VIM has been changed, |
| 266 | # this is because database VIM information is cached for threads working with SDN |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 267 | with self.write_lock: |
| 268 | for worker in self.workers: |
| 269 | if worker and not worker.idle: |
| 270 | worker.insert_task(("reload_vim", target_id)) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 271 | |
| 272 | def unload_vim(self, target_id): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 273 | with self.write_lock: |
| 274 | return self._unload_vim(target_id) |
| 275 | |
| 276 | def _unload_vim(self, target_id): |
| 277 | if target_id in self.vims_assigned: |
| 278 | worker_id = self.vims_assigned[target_id] |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 279 | self.workers[worker_id].insert_task(("unload_vim", target_id)) |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 280 | del self.vims_assigned[target_id] |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 281 | |
| 282 | def check_vim(self, target_id): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 283 | with self.write_lock: |
| 284 | if target_id in self.vims_assigned: |
| 285 | worker_id = self.vims_assigned[target_id] |
| 286 | else: |
| 287 | worker_id = self._create_worker() |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 288 | |
| 289 | worker = self.workers[worker_id] |
| 290 | worker.insert_task(("check_vim", target_id)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 291 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 292 | def unload_unused_vims(self): |
| 293 | with self.write_lock: |
| 294 | vims_to_unload = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 295 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 296 | for target_id in self.vims_assigned: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 297 | if not self.db.get_one( |
| 298 | "ro_tasks", |
| 299 | q_filter={ |
| 300 | "target_id": target_id, |
| 301 | "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"], |
| 302 | }, |
| 303 | fail_on_empty=False, |
| 304 | ): |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 305 | vims_to_unload.append(target_id) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 306 | |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 307 | for target_id in vims_to_unload: |
| 308 | self._unload_vim(target_id) |
| 309 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 310 | @staticmethod |
| 311 | def _get_cloud_init( |
| 312 | db: Type[DbBase], |
| 313 | fs: Type[FsBase], |
| 314 | location: str, |
| 315 | ) -> str: |
| 316 | """This method reads cloud init from a file. |
| 317 | |
| 318 | Note: Not used as cloud init content is provided in the http body. |
| 319 | |
| 320 | Args: |
| 321 | db (Type[DbBase]): [description] |
| 322 | fs (Type[FsBase]): [description] |
| 323 | location (str): can be 'vnfr_id:file:file_name' or 'vnfr_id:vdu:vdu_idex' |
| 324 | |
| 325 | Raises: |
| 326 | NsException: [description] |
| 327 | NsException: [description] |
| 328 | |
| 329 | Returns: |
| 330 | str: [description] |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 331 | """ |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 332 | vnfd_id, _, other = location.partition(":") |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 333 | _type, _, name = other.partition(":") |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 334 | vnfd = db.get_one("vnfds", {"_id": vnfd_id}) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 335 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 336 | if _type == "file": |
| 337 | base_folder = vnfd["_admin"]["storage"] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 338 | cloud_init_file = "{}/{}/cloud_init/{}".format( |
| 339 | base_folder["folder"], base_folder["pkg-dir"], name |
| 340 | ) |
| 341 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 342 | if not fs: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 343 | raise NsException( |
| 344 | "Cannot read file '{}'. Filesystem not loaded, change configuration at storage.driver".format( |
| 345 | cloud_init_file |
| 346 | ) |
| 347 | ) |
| 348 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 349 | with fs.file_open(cloud_init_file, "r") as ci_file: |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 350 | cloud_init_content = ci_file.read() |
| 351 | elif _type == "vdu": |
| 352 | cloud_init_content = vnfd["vdu"][int(name)]["cloud-init"] |
| 353 | else: |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 354 | raise NsException("Mismatch descriptor for cloud init: {}".format(location)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 355 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 356 | return cloud_init_content |
| 357 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 358 | @staticmethod |
| 359 | def _parse_jinja2( |
| 360 | cloud_init_content: str, |
| 361 | params: Dict[str, Any], |
| 362 | context: str, |
| 363 | ) -> str: |
| 364 | """Function that processes the cloud init to replace Jinja2 encoded parameters. |
| 365 | |
| 366 | Args: |
| 367 | cloud_init_content (str): [description] |
| 368 | params (Dict[str, Any]): [description] |
| 369 | context (str): [description] |
| 370 | |
| 371 | Raises: |
| 372 | NsException: [description] |
| 373 | NsException: [description] |
| 374 | |
| 375 | Returns: |
| 376 | str: [description] |
| 377 | """ |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 378 | try: |
| 379 | env = Environment(undefined=StrictUndefined) |
| 380 | template = env.from_string(cloud_init_content) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 381 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 382 | return template.render(params or {}) |
| 383 | except UndefinedError as e: |
| 384 | raise NsException( |
| 385 | "Variable '{}' defined at vnfd='{}' must be provided in the instantiation parameters" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 386 | "inside the 'additionalParamsForVnf' block".format(e, context) |
| 387 | ) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 388 | except (TemplateError, TemplateNotFound) as e: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 389 | raise NsException( |
| 390 | "Error parsing Jinja2 to cloud-init content at vnfd='{}': {}".format( |
| 391 | context, e |
| 392 | ) |
| 393 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 394 | |
| 395 | def _create_db_ro_nsrs(self, nsr_id, now): |
| 396 | try: |
| 397 | key = rsa.generate_private_key( |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 398 | backend=crypto_default_backend(), public_exponent=65537, key_size=2048 |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 399 | ) |
| 400 | private_key = key.private_bytes( |
| 401 | crypto_serialization.Encoding.PEM, |
| 402 | crypto_serialization.PrivateFormat.PKCS8, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 403 | crypto_serialization.NoEncryption(), |
| 404 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 405 | public_key = key.public_key().public_bytes( |
| 406 | crypto_serialization.Encoding.OpenSSH, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 407 | crypto_serialization.PublicFormat.OpenSSH, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 408 | ) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 409 | private_key = private_key.decode("utf8") |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 410 | # Change first line because Paramiko needs a explicit start with 'BEGIN RSA PRIVATE KEY' |
| 411 | i = private_key.find("\n") |
| 412 | private_key = "-----BEGIN RSA PRIVATE KEY-----" + private_key[i:] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 413 | public_key = public_key.decode("utf8") |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 414 | except Exception as e: |
| 415 | raise NsException("Cannot create ssh-keys: {}".format(e)) |
| 416 | |
| 417 | schema_version = "1.1" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 418 | private_key_encrypted = self.db.encrypt( |
| 419 | private_key, schema_version=schema_version, salt=nsr_id |
| 420 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 421 | db_content = { |
| 422 | "_id": nsr_id, |
| 423 | "_admin": { |
| 424 | "created": now, |
| 425 | "modified": now, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 426 | "schema_version": schema_version, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 427 | }, |
| 428 | "public_key": public_key, |
| 429 | "private_key": private_key_encrypted, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 430 | "actions": [], |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 431 | } |
| 432 | self.db.create("ro_nsrs", db_content) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 433 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 434 | return db_content |
| 435 | |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 436 | @staticmethod |
| 437 | def _create_task( |
| 438 | deployment_info: Dict[str, Any], |
| 439 | target_id: str, |
| 440 | item: str, |
| 441 | action: str, |
| 442 | target_record: str, |
| 443 | target_record_id: str, |
| 444 | extra_dict: Dict[str, Any] = None, |
| 445 | ) -> Dict[str, Any]: |
| 446 | """Function to create task dict from deployment information. |
| 447 | |
| 448 | Args: |
| 449 | deployment_info (Dict[str, Any]): [description] |
| 450 | target_id (str): [description] |
| 451 | item (str): [description] |
| 452 | action (str): [description] |
| 453 | target_record (str): [description] |
| 454 | target_record_id (str): [description] |
| 455 | extra_dict (Dict[str, Any], optional): [description]. Defaults to None. |
| 456 | |
| 457 | Returns: |
| 458 | Dict[str, Any]: [description] |
| 459 | """ |
| 460 | task = { |
| 461 | "target_id": target_id, # it will be removed before pushing at database |
| 462 | "action_id": deployment_info.get("action_id"), |
| 463 | "nsr_id": deployment_info.get("nsr_id"), |
| 464 | "task_id": f"{deployment_info.get('action_id')}:{deployment_info.get('task_index')}", |
| 465 | "status": "SCHEDULED", |
| 466 | "action": action, |
| 467 | "item": item, |
| 468 | "target_record": target_record, |
| 469 | "target_record_id": target_record_id, |
| 470 | } |
| 471 | |
| 472 | if extra_dict: |
| 473 | task.update(extra_dict) # params, find_params, depends_on |
| 474 | |
| 475 | deployment_info["task_index"] = deployment_info.get("task_index", 0) + 1 |
| 476 | |
| 477 | return task |
| 478 | |
| sousaedu | 839e5ca | 2021-11-19 16:41:38 +0000 | [diff] [blame] | 479 | @staticmethod |
| 480 | def _create_ro_task( |
| 481 | target_id: str, |
| 482 | task: Dict[str, Any], |
| 483 | ) -> Dict[str, Any]: |
| 484 | """Function to create an RO task from task information. |
| 485 | |
| 486 | Args: |
| 487 | target_id (str): [description] |
| 488 | task (Dict[str, Any]): [description] |
| 489 | |
| 490 | Returns: |
| 491 | Dict[str, Any]: [description] |
| 492 | """ |
| 493 | now = time() |
| 494 | |
| 495 | _id = task.get("task_id") |
| 496 | db_ro_task = { |
| 497 | "_id": _id, |
| 498 | "locked_by": None, |
| 499 | "locked_at": 0.0, |
| 500 | "target_id": target_id, |
| 501 | "vim_info": { |
| 502 | "created": False, |
| 503 | "created_items": None, |
| 504 | "vim_id": None, |
| 505 | "vim_name": None, |
| 506 | "vim_status": None, |
| 507 | "vim_details": None, |
| 508 | "refresh_at": None, |
| 509 | }, |
| 510 | "modified_at": now, |
| 511 | "created_at": now, |
| 512 | "to_check_at": now, |
| 513 | "tasks": [task], |
| 514 | } |
| 515 | |
| 516 | return db_ro_task |
| 517 | |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 518 | @staticmethod |
| 519 | def _process_image_params( |
| 520 | target_image: Dict[str, Any], |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 521 | indata: Dict[str, Any], |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 522 | vim_info: Dict[str, Any], |
| 523 | target_record_id: str, |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 524 | **kwargs: Dict[str, Any], |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 525 | ) -> Dict[str, Any]: |
| 526 | """Function to process VDU image parameters. |
| 527 | |
| 528 | Args: |
| 529 | target_image (Dict[str, Any]): [description] |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 530 | indata (Dict[str, Any]): [description] |
| sousaedu | a0a3330 | 2021-11-21 16:21:13 +0000 | [diff] [blame] | 531 | vim_info (Dict[str, Any]): [description] |
| 532 | target_record_id (str): [description] |
| 533 | |
| 534 | Returns: |
| 535 | Dict[str, Any]: [description] |
| 536 | """ |
| 537 | find_params = {} |
| 538 | |
| 539 | if target_image.get("image"): |
| 540 | find_params["filter_dict"] = {"name": target_image.get("image")} |
| 541 | |
| 542 | if target_image.get("vim_image_id"): |
| 543 | find_params["filter_dict"] = {"id": target_image.get("vim_image_id")} |
| 544 | |
| 545 | if target_image.get("image_checksum"): |
| 546 | find_params["filter_dict"] = { |
| 547 | "checksum": target_image.get("image_checksum") |
| 548 | } |
| 549 | |
| 550 | return {"find_params": find_params} |
| 551 | |
| sousaedu | abdfe78 | 2021-11-22 23:56:28 +0000 | [diff] [blame] | 552 | @staticmethod |
| 553 | def _get_resource_allocation_params( |
| 554 | quota_descriptor: Dict[str, Any], |
| 555 | ) -> Dict[str, Any]: |
| 556 | """Read the quota_descriptor from vnfd and fetch the resource allocation properties from the |
| 557 | descriptor object. |
| 558 | |
| 559 | Args: |
| 560 | quota_descriptor (Dict[str, Any]): cpu/mem/vif/disk-io quota descriptor |
| 561 | |
| 562 | Returns: |
| 563 | Dict[str, Any]: quota params for limit, reserve, shares from the descriptor object |
| 564 | """ |
| 565 | quota = {} |
| 566 | |
| 567 | if quota_descriptor.get("limit"): |
| 568 | quota["limit"] = int(quota_descriptor["limit"]) |
| 569 | |
| 570 | if quota_descriptor.get("reserve"): |
| 571 | quota["reserve"] = int(quota_descriptor["reserve"]) |
| 572 | |
| 573 | if quota_descriptor.get("shares"): |
| 574 | quota["shares"] = int(quota_descriptor["shares"]) |
| 575 | |
| 576 | return quota |
| 577 | |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 578 | @staticmethod |
| 579 | def _process_guest_epa_quota_params( |
| 580 | guest_epa_quota: Dict[str, Any], |
| 581 | epa_vcpu_set: bool, |
| 582 | ) -> Dict[str, Any]: |
| 583 | """Function to extract the guest epa quota parameters. |
| 584 | |
| 585 | Args: |
| 586 | guest_epa_quota (Dict[str, Any]): [description] |
| 587 | epa_vcpu_set (bool): [description] |
| 588 | |
| 589 | Returns: |
| 590 | Dict[str, Any]: [description] |
| 591 | """ |
| 592 | result = {} |
| 593 | |
| 594 | if guest_epa_quota.get("cpu-quota") and not epa_vcpu_set: |
| 595 | cpuquota = Ns._get_resource_allocation_params( |
| 596 | guest_epa_quota.get("cpu-quota") |
| 597 | ) |
| 598 | |
| 599 | if cpuquota: |
| 600 | result["cpu-quota"] = cpuquota |
| 601 | |
| 602 | if guest_epa_quota.get("mem-quota"): |
| 603 | vduquota = Ns._get_resource_allocation_params( |
| 604 | guest_epa_quota.get("mem-quota") |
| 605 | ) |
| 606 | |
| 607 | if vduquota: |
| 608 | result["mem-quota"] = vduquota |
| 609 | |
| 610 | if guest_epa_quota.get("disk-io-quota"): |
| 611 | diskioquota = Ns._get_resource_allocation_params( |
| 612 | guest_epa_quota.get("disk-io-quota") |
| 613 | ) |
| 614 | |
| 615 | if diskioquota: |
| 616 | result["disk-io-quota"] = diskioquota |
| 617 | |
| 618 | if guest_epa_quota.get("vif-quota"): |
| 619 | vifquota = Ns._get_resource_allocation_params( |
| 620 | guest_epa_quota.get("vif-quota") |
| 621 | ) |
| 622 | |
| 623 | if vifquota: |
| 624 | result["vif-quota"] = vifquota |
| 625 | |
| 626 | return result |
| 627 | |
| 628 | @staticmethod |
| 629 | def _process_guest_epa_numa_params( |
| 630 | guest_epa_quota: Dict[str, Any], |
| 631 | ) -> Tuple[Dict[str, Any], bool]: |
| 632 | """[summary] |
| 633 | |
| 634 | Args: |
| 635 | guest_epa_quota (Dict[str, Any]): [description] |
| 636 | |
| 637 | Returns: |
| 638 | Tuple[Dict[str, Any], bool]: [description] |
| 639 | """ |
| 640 | numa = {} |
| 641 | epa_vcpu_set = False |
| 642 | |
| 643 | if guest_epa_quota.get("numa-node-policy"): |
| 644 | numa_node_policy = guest_epa_quota.get("numa-node-policy") |
| 645 | |
| 646 | if numa_node_policy.get("node"): |
| 647 | numa_node = numa_node_policy["node"][0] |
| 648 | |
| 649 | if numa_node.get("num-cores"): |
| 650 | numa["cores"] = numa_node["num-cores"] |
| 651 | epa_vcpu_set = True |
| 652 | |
| 653 | paired_threads = numa_node.get("paired-threads", {}) |
| 654 | if paired_threads.get("num-paired-threads"): |
| 655 | numa["paired-threads"] = int( |
| 656 | numa_node["paired-threads"]["num-paired-threads"] |
| 657 | ) |
| 658 | epa_vcpu_set = True |
| 659 | |
| 660 | if paired_threads.get("paired-thread-ids"): |
| 661 | numa["paired-threads-id"] = [] |
| 662 | |
| 663 | for pair in paired_threads["paired-thread-ids"]: |
| 664 | numa["paired-threads-id"].append( |
| 665 | ( |
| 666 | str(pair["thread-a"]), |
| 667 | str(pair["thread-b"]), |
| 668 | ) |
| 669 | ) |
| 670 | |
| 671 | if numa_node.get("num-threads"): |
| 672 | numa["threads"] = int(numa_node["num-threads"]) |
| 673 | epa_vcpu_set = True |
| 674 | |
| 675 | if numa_node.get("memory-mb"): |
| 676 | numa["memory"] = max(int(int(numa_node["memory-mb"]) / 1024), 1) |
| 677 | |
| 678 | return numa, epa_vcpu_set |
| 679 | |
| 680 | @staticmethod |
| 681 | def _process_guest_epa_cpu_pinning_params( |
| 682 | guest_epa_quota: Dict[str, Any], |
| 683 | vcpu_count: int, |
| 684 | epa_vcpu_set: bool, |
| 685 | ) -> Tuple[Dict[str, Any], bool]: |
| 686 | """[summary] |
| 687 | |
| 688 | Args: |
| 689 | guest_epa_quota (Dict[str, Any]): [description] |
| 690 | vcpu_count (int): [description] |
| 691 | epa_vcpu_set (bool): [description] |
| 692 | |
| 693 | Returns: |
| 694 | Tuple[Dict[str, Any], bool]: [description] |
| 695 | """ |
| 696 | numa = {} |
| 697 | local_epa_vcpu_set = epa_vcpu_set |
| 698 | |
| 699 | if ( |
| 700 | guest_epa_quota.get("cpu-pinning-policy") == "DEDICATED" |
| 701 | and not epa_vcpu_set |
| 702 | ): |
| 703 | numa[ |
| 704 | "cores" |
| 705 | if guest_epa_quota.get("cpu-thread-pinning-policy") != "PREFER" |
| 706 | else "threads" |
| 707 | ] = max(vcpu_count, 1) |
| 708 | local_epa_vcpu_set = True |
| 709 | |
| 710 | return numa, local_epa_vcpu_set |
| 711 | |
| 712 | @staticmethod |
| 713 | def _process_epa_params( |
| 714 | target_flavor: Dict[str, Any], |
| 715 | ) -> Dict[str, Any]: |
| 716 | """[summary] |
| 717 | |
| 718 | Args: |
| 719 | target_flavor (Dict[str, Any]): [description] |
| 720 | |
| 721 | Returns: |
| 722 | Dict[str, Any]: [description] |
| 723 | """ |
| 724 | extended = {} |
| 725 | numa = {} |
| 726 | |
| 727 | if target_flavor.get("guest-epa"): |
| 728 | guest_epa = target_flavor["guest-epa"] |
| 729 | |
| 730 | numa, epa_vcpu_set = Ns._process_guest_epa_numa_params( |
| 731 | guest_epa_quota=guest_epa |
| 732 | ) |
| 733 | |
| 734 | if guest_epa.get("mempage-size"): |
| 735 | extended["mempage-size"] = guest_epa.get("mempage-size") |
| 736 | |
| 737 | tmp_numa, epa_vcpu_set = Ns._process_guest_epa_cpu_pinning_params( |
| 738 | guest_epa_quota=guest_epa, |
| 739 | vcpu_count=int(target_flavor.get("vcpu-count", 1)), |
| 740 | epa_vcpu_set=epa_vcpu_set, |
| 741 | ) |
| 742 | numa.update(tmp_numa) |
| 743 | |
| 744 | extended.update( |
| 745 | Ns._process_guest_epa_quota_params( |
| 746 | guest_epa_quota=guest_epa, |
| 747 | epa_vcpu_set=epa_vcpu_set, |
| 748 | ) |
| 749 | ) |
| 750 | |
| 751 | if numa: |
| 752 | extended["numas"] = [numa] |
| 753 | |
| 754 | return extended |
| 755 | |
| 756 | @staticmethod |
| 757 | def _process_flavor_params( |
| 758 | target_flavor: Dict[str, Any], |
| 759 | indata: Dict[str, Any], |
| 760 | vim_info: Dict[str, Any], |
| 761 | target_record_id: str, |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 762 | **kwargs: Dict[str, Any], |
| sousaedu | 686720b | 2021-11-24 02:16:11 +0000 | [diff] [blame] | 763 | ) -> Dict[str, Any]: |
| 764 | """[summary] |
| 765 | |
| 766 | Args: |
| 767 | target_flavor (Dict[str, Any]): [description] |
| 768 | indata (Dict[str, Any]): [description] |
| 769 | vim_info (Dict[str, Any]): [description] |
| 770 | target_record_id (str): [description] |
| 771 | |
| 772 | Returns: |
| 773 | Dict[str, Any]: [description] |
| 774 | """ |
| 775 | flavor_data = { |
| 776 | "disk": int(target_flavor["storage-gb"]), |
| 777 | "ram": int(target_flavor["memory-mb"]), |
| 778 | "vcpus": int(target_flavor["vcpu-count"]), |
| 779 | } |
| 780 | |
| 781 | target_vdur = {} |
| 782 | for vnf in indata.get("vnf", []): |
| 783 | for vdur in vnf.get("vdur", []): |
| 784 | if vdur.get("ns-flavor-id") == target_flavor["id"]: |
| 785 | target_vdur = vdur |
| 786 | |
| 787 | for storage in target_vdur.get("virtual-storages", []): |
| 788 | if ( |
| 789 | storage.get("type-of-storage") |
| 790 | == "etsi-nfv-descriptors:ephemeral-storage" |
| 791 | ): |
| 792 | flavor_data["ephemeral"] = int(storage.get("size-of-storage", 0)) |
| 793 | elif storage.get("type-of-storage") == "etsi-nfv-descriptors:swap-storage": |
| 794 | flavor_data["swap"] = int(storage.get("size-of-storage", 0)) |
| 795 | |
| 796 | extended = Ns._process_epa_params(target_flavor) |
| 797 | if extended: |
| 798 | flavor_data["extended"] = extended |
| 799 | |
| 800 | extra_dict = {"find_params": {"flavor_data": flavor_data}} |
| 801 | flavor_data_name = flavor_data.copy() |
| 802 | flavor_data_name["name"] = target_flavor["name"] |
| 803 | extra_dict["params"] = {"flavor_data": flavor_data_name} |
| 804 | |
| 805 | return extra_dict |
| 806 | |
| sousaedu | a4bac08 | 2021-12-05 19:31:03 +0000 | [diff] [blame] | 807 | @staticmethod |
| 808 | def _ip_profile_to_ro( |
| 809 | ip_profile: Dict[str, Any], |
| 810 | ) -> Dict[str, Any]: |
| 811 | """[summary] |
| 812 | |
| 813 | Args: |
| 814 | ip_profile (Dict[str, Any]): [description] |
| 815 | |
| 816 | Returns: |
| 817 | Dict[str, Any]: [description] |
| 818 | """ |
| 819 | if not ip_profile: |
| 820 | return None |
| 821 | |
| 822 | ro_ip_profile = { |
| 823 | "ip_version": "IPv4" |
| 824 | if "v4" in ip_profile.get("ip-version", "ipv4") |
| 825 | else "IPv6", |
| 826 | "subnet_address": ip_profile.get("subnet-address"), |
| 827 | "gateway_address": ip_profile.get("gateway-address"), |
| 828 | "dhcp_enabled": ip_profile.get("dhcp-params", {}).get("enabled", False), |
| 829 | "dhcp_start_address": ip_profile.get("dhcp-params", {}).get( |
| 830 | "start-address", None |
| 831 | ), |
| 832 | "dhcp_count": ip_profile.get("dhcp-params", {}).get("count", None), |
| 833 | } |
| 834 | |
| 835 | if ip_profile.get("dns-server"): |
| 836 | ro_ip_profile["dns_address"] = ";".join( |
| 837 | [v["address"] for v in ip_profile["dns-server"] if v.get("address")] |
| 838 | ) |
| 839 | |
| 840 | if ip_profile.get("security-group"): |
| 841 | ro_ip_profile["security_group"] = ip_profile["security-group"] |
| 842 | |
| 843 | return ro_ip_profile |
| 844 | |
| sousaedu | 0ee9fdb | 2021-12-06 00:17:54 +0000 | [diff] [blame] | 845 | @staticmethod |
| 846 | def _process_net_params( |
| 847 | target_vld: Dict[str, Any], |
| 848 | indata: Dict[str, Any], |
| 849 | vim_info: Dict[str, Any], |
| 850 | target_record_id: str, |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 851 | **kwargs: Dict[str, Any], |
| sousaedu | 0ee9fdb | 2021-12-06 00:17:54 +0000 | [diff] [blame] | 852 | ) -> Dict[str, Any]: |
| 853 | """Function to process network parameters. |
| 854 | |
| 855 | Args: |
| 856 | target_vld (Dict[str, Any]): [description] |
| 857 | indata (Dict[str, Any]): [description] |
| 858 | vim_info (Dict[str, Any]): [description] |
| 859 | target_record_id (str): [description] |
| 860 | |
| 861 | Returns: |
| 862 | Dict[str, Any]: [description] |
| 863 | """ |
| 864 | extra_dict = {} |
| 865 | |
| 866 | if vim_info.get("sdn"): |
| 867 | # vnf_preffix = "vnfrs:{}".format(vnfr_id) |
| 868 | # ns_preffix = "nsrs:{}".format(nsr_id) |
| 869 | # remove the ending ".sdn |
| 870 | vld_target_record_id, _, _ = target_record_id.rpartition(".") |
| 871 | extra_dict["params"] = { |
| 872 | k: vim_info[k] |
| 873 | for k in ("sdn-ports", "target_vim", "vlds", "type") |
| 874 | if vim_info.get(k) |
| 875 | } |
| 876 | |
| 877 | # TODO needed to add target_id in the dependency. |
| 878 | if vim_info.get("target_vim"): |
| 879 | extra_dict["depends_on"] = [ |
| 880 | f"{vim_info.get('target_vim')} {vld_target_record_id}" |
| 881 | ] |
| 882 | |
| 883 | return extra_dict |
| 884 | |
| 885 | if vim_info.get("vim_network_name"): |
| 886 | extra_dict["find_params"] = { |
| 887 | "filter_dict": { |
| 888 | "name": vim_info.get("vim_network_name"), |
| 889 | }, |
| 890 | } |
| 891 | elif vim_info.get("vim_network_id"): |
| 892 | extra_dict["find_params"] = { |
| 893 | "filter_dict": { |
| 894 | "id": vim_info.get("vim_network_id"), |
| 895 | }, |
| 896 | } |
| 897 | elif target_vld.get("mgmt-network"): |
| 898 | extra_dict["find_params"] = { |
| 899 | "mgmt": True, |
| 900 | "name": target_vld["id"], |
| 901 | } |
| 902 | else: |
| 903 | # create |
| 904 | extra_dict["params"] = { |
| 905 | "net_name": ( |
| 906 | f"{indata.get('name')[:16]}-{target_vld.get('name', target_vld.get('id'))[:16]}" |
| 907 | ), |
| 908 | "ip_profile": Ns._ip_profile_to_ro(vim_info.get("ip_profile")), |
| 909 | "provider_network_profile": vim_info.get("provider_network"), |
| 910 | } |
| 911 | |
| 912 | if not target_vld.get("underlay"): |
| 913 | extra_dict["params"]["net_type"] = "bridge" |
| 914 | else: |
| 915 | extra_dict["params"]["net_type"] = ( |
| 916 | "ptp" if target_vld.get("type") == "ELINE" else "data" |
| 917 | ) |
| 918 | |
| 919 | return extra_dict |
| 920 | |
| sousaedu | 0b1e734 | 2021-12-07 15:33:46 +0000 | [diff] [blame] | 921 | @staticmethod |
| 922 | def _process_vdu_params( |
| 923 | target_vdu: Dict[str, Any], |
| 924 | indata: Dict[str, Any], |
| 925 | vim_info: Dict[str, Any], |
| 926 | target_record_id: str, |
| 927 | **kwargs: Dict[str, Any], |
| 928 | ) -> Dict[str, Any]: |
| 929 | """Function to process VDU parameters. |
| 930 | |
| 931 | Args: |
| 932 | target_vdu (Dict[str, Any]): [description] |
| 933 | indata (Dict[str, Any]): [description] |
| 934 | vim_info (Dict[str, Any]): [description] |
| 935 | target_record_id (str): [description] |
| 936 | |
| 937 | Returns: |
| 938 | Dict[str, Any]: [description] |
| 939 | """ |
| 940 | vnfr_id = kwargs.get("vnfr_id") |
| 941 | nsr_id = kwargs.get("nsr_id") |
| 942 | vnfr = kwargs.get("vnfr") |
| 943 | vdu2cloud_init = kwargs.get("vdu2cloud_init") |
| 944 | tasks_by_target_record_id = kwargs.get("tasks_by_target_record_id") |
| 945 | logger = kwargs.get("logger") |
| 946 | db = kwargs.get("db") |
| 947 | fs = kwargs.get("fs") |
| 948 | ro_nsr_public_key = kwargs.get("ro_nsr_public_key") |
| 949 | |
| 950 | vnf_preffix = "vnfrs:{}".format(vnfr_id) |
| 951 | ns_preffix = "nsrs:{}".format(nsr_id) |
| 952 | image_text = ns_preffix + ":image." + target_vdu["ns-image-id"] |
| 953 | flavor_text = ns_preffix + ":flavor." + target_vdu["ns-flavor-id"] |
| 954 | extra_dict = {"depends_on": [image_text, flavor_text]} |
| 955 | net_list = [] |
| 956 | |
| 957 | for iface_index, interface in enumerate(target_vdu["interfaces"]): |
| 958 | if interface.get("ns-vld-id"): |
| 959 | net_text = ns_preffix + ":vld." + interface["ns-vld-id"] |
| 960 | elif interface.get("vnf-vld-id"): |
| 961 | net_text = vnf_preffix + ":vld." + interface["vnf-vld-id"] |
| 962 | else: |
| 963 | logger.error( |
| 964 | "Interface {} from vdu {} not connected to any vld".format( |
| 965 | iface_index, target_vdu["vdu-name"] |
| 966 | ) |
| 967 | ) |
| 968 | |
| 969 | continue # interface not connected to any vld |
| 970 | |
| 971 | extra_dict["depends_on"].append(net_text) |
| 972 | |
| 973 | if "port-security-enabled" in interface: |
| 974 | interface["port_security"] = interface.pop("port-security-enabled") |
| 975 | |
| 976 | if "port-security-disable-strategy" in interface: |
| 977 | interface["port_security_disable_strategy"] = interface.pop( |
| 978 | "port-security-disable-strategy" |
| 979 | ) |
| 980 | |
| 981 | net_item = { |
| 982 | x: v |
| 983 | for x, v in interface.items() |
| 984 | if x |
| 985 | in ( |
| 986 | "name", |
| 987 | "vpci", |
| 988 | "port_security", |
| 989 | "port_security_disable_strategy", |
| 990 | "floating_ip", |
| 991 | ) |
| 992 | } |
| 993 | net_item["net_id"] = "TASK-" + net_text |
| 994 | net_item["type"] = "virtual" |
| 995 | |
| 996 | # TODO mac_address: used for SR-IOV ifaces #TODO for other types |
| 997 | # TODO floating_ip: True/False (or it can be None) |
| 998 | if interface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"): |
| 999 | # mark the net create task as type data |
| 1000 | if deep_get( |
| 1001 | tasks_by_target_record_id, |
| 1002 | net_text, |
| 1003 | "params", |
| 1004 | "net_type", |
| 1005 | ): |
| 1006 | tasks_by_target_record_id[net_text]["params"]["net_type"] = "data" |
| 1007 | |
| 1008 | net_item["use"] = "data" |
| 1009 | net_item["model"] = interface["type"] |
| 1010 | net_item["type"] = interface["type"] |
| 1011 | elif ( |
| 1012 | interface.get("type") == "OM-MGMT" |
| 1013 | or interface.get("mgmt-interface") |
| 1014 | or interface.get("mgmt-vnf") |
| 1015 | ): |
| 1016 | net_item["use"] = "mgmt" |
| 1017 | else: |
| 1018 | # if interface.get("type") in ("VIRTIO", "E1000", "PARAVIRT"): |
| 1019 | net_item["use"] = "bridge" |
| 1020 | net_item["model"] = interface.get("type") |
| 1021 | |
| 1022 | if interface.get("ip-address"): |
| 1023 | net_item["ip_address"] = interface["ip-address"] |
| 1024 | |
| 1025 | if interface.get("mac-address"): |
| 1026 | net_item["mac_address"] = interface["mac-address"] |
| 1027 | |
| 1028 | net_list.append(net_item) |
| 1029 | |
| 1030 | if interface.get("mgmt-vnf"): |
| 1031 | extra_dict["mgmt_vnf_interface"] = iface_index |
| 1032 | elif interface.get("mgmt-interface"): |
| 1033 | extra_dict["mgmt_vdu_interface"] = iface_index |
| 1034 | |
| 1035 | # cloud config |
| 1036 | cloud_config = {} |
| 1037 | |
| 1038 | if target_vdu.get("cloud-init"): |
| 1039 | if target_vdu["cloud-init"] not in vdu2cloud_init: |
| 1040 | vdu2cloud_init[target_vdu["cloud-init"]] = Ns._get_cloud_init( |
| 1041 | db=db, |
| 1042 | fs=fs, |
| 1043 | location=target_vdu["cloud-init"], |
| 1044 | ) |
| 1045 | |
| 1046 | cloud_content_ = vdu2cloud_init[target_vdu["cloud-init"]] |
| 1047 | cloud_config["user-data"] = Ns._parse_jinja2( |
| 1048 | cloud_init_content=cloud_content_, |
| 1049 | params=target_vdu.get("additionalParams"), |
| 1050 | context=target_vdu["cloud-init"], |
| 1051 | ) |
| 1052 | |
| 1053 | if target_vdu.get("boot-data-drive"): |
| 1054 | cloud_config["boot-data-drive"] = target_vdu.get("boot-data-drive") |
| 1055 | |
| 1056 | ssh_keys = [] |
| 1057 | |
| 1058 | if target_vdu.get("ssh-keys"): |
| 1059 | ssh_keys += target_vdu.get("ssh-keys") |
| 1060 | |
| 1061 | if target_vdu.get("ssh-access-required"): |
| 1062 | ssh_keys.append(ro_nsr_public_key) |
| 1063 | |
| 1064 | if ssh_keys: |
| 1065 | cloud_config["key-pairs"] = ssh_keys |
| 1066 | |
| 1067 | disk_list = None |
| 1068 | if target_vdu.get("virtual-storages"): |
| 1069 | disk_list = [ |
| 1070 | {"size": disk["size-of-storage"]} |
| 1071 | for disk in target_vdu["virtual-storages"] |
| 1072 | if disk.get("type-of-storage") |
| 1073 | == "persistent-storage:persistent-storage" |
| 1074 | ] |
| 1075 | |
| 1076 | extra_dict["params"] = { |
| 1077 | "name": "{}-{}-{}-{}".format( |
| 1078 | indata["name"][:16], |
| 1079 | vnfr["member-vnf-index-ref"][:16], |
| 1080 | target_vdu["vdu-name"][:32], |
| 1081 | target_vdu.get("count-index") or 0, |
| 1082 | ), |
| 1083 | "description": target_vdu["vdu-name"], |
| 1084 | "start": True, |
| 1085 | "image_id": "TASK-" + image_text, |
| 1086 | "flavor_id": "TASK-" + flavor_text, |
| 1087 | "net_list": net_list, |
| 1088 | "cloud_config": cloud_config or None, |
| 1089 | "disk_list": disk_list, |
| 1090 | "availability_zone_index": None, # TODO |
| 1091 | "availability_zone_list": None, # TODO |
| 1092 | } |
| 1093 | |
| 1094 | return extra_dict |
| 1095 | |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame^] | 1096 | def calculate_diff_items( |
| 1097 | self, |
| 1098 | indata, |
| 1099 | db_nsr, |
| 1100 | db_ro_nsr, |
| 1101 | db_nsr_update, |
| 1102 | item, |
| 1103 | tasks_by_target_record_id, |
| 1104 | action_id, |
| 1105 | nsr_id, |
| 1106 | task_index, |
| 1107 | vnfr_id=None, |
| 1108 | vnfr=None, |
| 1109 | ): |
| 1110 | """Function that returns the incremental changes (creation, deletion) |
| 1111 | related to a specific item `item` to be done. This function should be |
| 1112 | called for NS instantiation, NS termination, NS update to add a new VNF |
| 1113 | or a new VLD, remove a VNF or VLD, etc. |
| 1114 | Item can be `net, `flavor`, `image` or `vdu`. |
| 1115 | It takes a list of target items from indata (which came from the REST API) |
| 1116 | and compares with the existing items from db_ro_nsr, identifying the |
| 1117 | incremental changes to be done. During the comparison, it calls the method |
| 1118 | `process_params` (which was passed as parameter, and is particular for each |
| 1119 | `item`) |
| 1120 | |
| 1121 | Args: |
| 1122 | indata (Dict[str, Any]): deployment info |
| 1123 | db_nsr: NSR record from DB |
| 1124 | db_ro_nsr (Dict[str, Any]): record from "ro_nsrs" |
| 1125 | db_nsr_update (Dict[str, Any]): NSR info to update in DB |
| 1126 | item (str): element to process (net, vdu...) |
| 1127 | tasks_by_target_record_id (Dict[str, Any]): |
| 1128 | [<target_record_id>, <task>] |
| 1129 | action_id (str): action id |
| 1130 | nsr_id (str): NSR id |
| 1131 | task_index (number): task index to add to task name |
| 1132 | vnfr_id (str): VNFR id |
| 1133 | vnfr (Dict[str, Any]): VNFR info |
| 1134 | |
| 1135 | Returns: |
| 1136 | List: list with the incremental changes (deletes, creates) for each item |
| 1137 | number: current task index |
| 1138 | """ |
| 1139 | |
| 1140 | diff_items = [] |
| 1141 | db_path = "" |
| 1142 | db_record = "" |
| 1143 | target_list = [] |
| 1144 | existing_list = [] |
| 1145 | process_params = None |
| 1146 | vdu2cloud_init = indata.get("cloud_init_content") or {} |
| 1147 | ro_nsr_public_key = db_ro_nsr["public_key"] |
| 1148 | |
| 1149 | # According to the type of item, the path, the target_list, |
| 1150 | # the existing_list and the method to process params are set |
| 1151 | db_path = self.db_path_map[item] |
| 1152 | process_params = self.process_params_function_map[item] |
| 1153 | if item in ("net", "vdu"): |
| 1154 | if vnfr is None: |
| 1155 | db_record = "nsrs:{}:{}".format(nsr_id, db_path) |
| 1156 | target_list = indata.get("ns", []).get(db_path, []) |
| 1157 | existing_list = db_nsr.get(db_path, []) |
| 1158 | else: |
| 1159 | db_record = "vnfrs:{}:{}".format(vnfr_id, db_path) |
| 1160 | target_vnf = next( |
| 1161 | (vnf for vnf in indata.get("vnf", ()) if vnf["_id"] == vnfr_id), |
| 1162 | None, |
| 1163 | ) |
| 1164 | target_list = target_vnf.get(db_path, []) if target_vnf else [] |
| 1165 | existing_list = vnfr.get(db_path, []) |
| 1166 | elif item in ("image", "flavor"): |
| 1167 | db_record = "nsrs:{}:{}".format(nsr_id, db_path) |
| 1168 | target_list = indata.get(item, []) |
| 1169 | existing_list = db_nsr.get(item, []) |
| 1170 | else: |
| 1171 | raise NsException("Item not supported: {}", item) |
| 1172 | |
| 1173 | # ensure all the target_list elements has an "id". If not assign the index as id |
| 1174 | if target_list is None: |
| 1175 | target_list = [] |
| 1176 | for target_index, tl in enumerate(target_list): |
| 1177 | if tl and not tl.get("id"): |
| 1178 | tl["id"] = str(target_index) |
| 1179 | |
| 1180 | # step 1 items (networks,vdus,...) to be deleted/updated |
| 1181 | for item_index, existing_item in enumerate(existing_list): |
| 1182 | target_item = next( |
| 1183 | (t for t in target_list if t["id"] == existing_item["id"]), |
| 1184 | None, |
| 1185 | ) |
| 1186 | |
| 1187 | for target_vim, existing_viminfo in existing_item.get( |
| 1188 | "vim_info", {} |
| 1189 | ).items(): |
| 1190 | if existing_viminfo is None: |
| 1191 | continue |
| 1192 | |
| 1193 | if target_item: |
| 1194 | target_viminfo = target_item.get("vim_info", {}).get(target_vim) |
| 1195 | else: |
| 1196 | target_viminfo = None |
| 1197 | |
| 1198 | if target_viminfo is None: |
| 1199 | # must be deleted |
| 1200 | self._assign_vim(target_vim) |
| 1201 | target_record_id = "{}.{}".format(db_record, existing_item["id"]) |
| 1202 | item_ = item |
| 1203 | |
| 1204 | if target_vim.startswith("sdn"): |
| 1205 | # item must be sdn-net instead of net if target_vim is a sdn |
| 1206 | item_ = "sdn_net" |
| 1207 | target_record_id += ".sdn" |
| 1208 | |
| 1209 | deployment_info = { |
| 1210 | "action_id": action_id, |
| 1211 | "nsr_id": nsr_id, |
| 1212 | "task_index": task_index, |
| 1213 | } |
| 1214 | |
| 1215 | diff_items.append( |
| 1216 | { |
| 1217 | "deployment_info": deployment_info, |
| 1218 | "target_id": target_vim, |
| 1219 | "item": item_, |
| 1220 | "action": "DELETE", |
| 1221 | "target_record": f"{db_record}.{item_index}.vim_info.{target_vim}", |
| 1222 | "target_record_id": target_record_id, |
| 1223 | } |
| 1224 | ) |
| 1225 | task_index += 1 |
| 1226 | |
| 1227 | # step 2 items (networks,vdus,...) to be created |
| 1228 | for target_item in target_list: |
| 1229 | item_index = -1 |
| 1230 | |
| 1231 | for item_index, existing_item in enumerate(existing_list): |
| 1232 | if existing_item["id"] == target_item["id"]: |
| 1233 | break |
| 1234 | else: |
| 1235 | item_index += 1 |
| 1236 | db_nsr_update[db_path + ".{}".format(item_index)] = target_item |
| 1237 | existing_list.append(target_item) |
| 1238 | existing_item = None |
| 1239 | |
| 1240 | for target_vim, target_viminfo in target_item.get("vim_info", {}).items(): |
| 1241 | existing_viminfo = None |
| 1242 | |
| 1243 | if existing_item: |
| 1244 | existing_viminfo = existing_item.get("vim_info", {}).get(target_vim) |
| 1245 | |
| 1246 | if existing_viminfo is not None: |
| 1247 | continue |
| 1248 | |
| 1249 | target_record_id = "{}.{}".format(db_record, target_item["id"]) |
| 1250 | item_ = item |
| 1251 | |
| 1252 | if target_vim.startswith("sdn"): |
| 1253 | # item must be sdn-net instead of net if target_vim is a sdn |
| 1254 | item_ = "sdn_net" |
| 1255 | target_record_id += ".sdn" |
| 1256 | |
| 1257 | kwargs = {} |
| 1258 | if process_params == Ns._process_vdu_params: |
| 1259 | kwargs.update( |
| 1260 | { |
| 1261 | "vnfr_id": vnfr_id, |
| 1262 | "nsr_id": nsr_id, |
| 1263 | "vnfr": vnfr, |
| 1264 | "vdu2cloud_init": vdu2cloud_init, |
| 1265 | "tasks_by_target_record_id": tasks_by_target_record_id, |
| 1266 | "logger": self.logger, |
| 1267 | "db": self.db, |
| 1268 | "fs": self.fs, |
| 1269 | "ro_nsr_public_key": ro_nsr_public_key, |
| 1270 | } |
| 1271 | ) |
| 1272 | |
| 1273 | extra_dict = process_params( |
| 1274 | target_item, |
| 1275 | indata, |
| 1276 | target_viminfo, |
| 1277 | target_record_id, |
| 1278 | **kwargs, |
| 1279 | ) |
| 1280 | self._assign_vim(target_vim) |
| 1281 | |
| 1282 | deployment_info = { |
| 1283 | "action_id": action_id, |
| 1284 | "nsr_id": nsr_id, |
| 1285 | "task_index": task_index, |
| 1286 | } |
| 1287 | |
| 1288 | diff_items.append( |
| 1289 | { |
| 1290 | "deployment_info": deployment_info, |
| 1291 | "target_id": target_vim, |
| 1292 | "item": item_, |
| 1293 | "action": "CREATE", |
| 1294 | "target_record": f"{db_record}.{item_index}.vim_info.{target_vim}", |
| 1295 | "target_record_id": target_record_id, |
| 1296 | "extra_dict": extra_dict, |
| 1297 | "common_id": target_item.get("common_id", None), |
| 1298 | } |
| 1299 | ) |
| 1300 | task_index += 1 |
| 1301 | |
| 1302 | db_nsr_update[db_path + ".{}".format(item_index)] = target_item |
| 1303 | |
| 1304 | return diff_items, task_index |
| 1305 | |
| 1306 | def calculate_all_differences_to_deploy( |
| 1307 | self, |
| 1308 | indata, |
| 1309 | nsr_id, |
| 1310 | db_nsr, |
| 1311 | db_vnfrs, |
| 1312 | db_ro_nsr, |
| 1313 | db_nsr_update, |
| 1314 | db_vnfrs_update, |
| 1315 | action_id, |
| 1316 | tasks_by_target_record_id, |
| 1317 | ): |
| 1318 | """This method calculates the ordered list of items (`changes_list`) |
| 1319 | to be created and deleted. |
| 1320 | |
| 1321 | Args: |
| 1322 | indata (Dict[str, Any]): deployment info |
| 1323 | nsr_id (str): NSR id |
| 1324 | db_nsr: NSR record from DB |
| 1325 | db_vnfrs: VNFRS record from DB |
| 1326 | db_ro_nsr (Dict[str, Any]): record from "ro_nsrs" |
| 1327 | db_nsr_update (Dict[str, Any]): NSR info to update in DB |
| 1328 | db_vnfrs_update (Dict[str, Any]): VNFRS info to update in DB |
| 1329 | action_id (str): action id |
| 1330 | tasks_by_target_record_id (Dict[str, Any]): |
| 1331 | [<target_record_id>, <task>] |
| 1332 | |
| 1333 | Returns: |
| 1334 | List: ordered list of items to be created and deleted. |
| 1335 | """ |
| 1336 | |
| 1337 | task_index = 0 |
| 1338 | # set list with diffs: |
| 1339 | changes_list = [] |
| 1340 | |
| 1341 | # NS vld, image and flavor |
| 1342 | for item in ["net", "image", "flavor"]: |
| 1343 | self.logger.debug("process NS={} {}".format(nsr_id, item)) |
| 1344 | diff_items, task_index = self.calculate_diff_items( |
| 1345 | indata=indata, |
| 1346 | db_nsr=db_nsr, |
| 1347 | db_ro_nsr=db_ro_nsr, |
| 1348 | db_nsr_update=db_nsr_update, |
| 1349 | item=item, |
| 1350 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 1351 | action_id=action_id, |
| 1352 | nsr_id=nsr_id, |
| 1353 | task_index=task_index, |
| 1354 | vnfr_id=None, |
| 1355 | ) |
| 1356 | changes_list += diff_items |
| 1357 | |
| 1358 | # VNF vlds and vdus |
| 1359 | for vnfr_id, vnfr in db_vnfrs.items(): |
| 1360 | # vnfr_id need to be set as global variable for among others nested method _process_vdu_params |
| 1361 | for item in ["net", "vdu"]: |
| 1362 | self.logger.debug("process VNF={} {}".format(vnfr_id, item)) |
| 1363 | diff_items, task_index = self.calculate_diff_items( |
| 1364 | indata=indata, |
| 1365 | db_nsr=db_nsr, |
| 1366 | db_ro_nsr=db_ro_nsr, |
| 1367 | db_nsr_update=db_vnfrs_update[vnfr["_id"]], |
| 1368 | item=item, |
| 1369 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 1370 | action_id=action_id, |
| 1371 | nsr_id=nsr_id, |
| 1372 | task_index=task_index, |
| 1373 | vnfr_id=vnfr_id, |
| 1374 | vnfr=vnfr, |
| 1375 | ) |
| 1376 | changes_list += diff_items |
| 1377 | |
| 1378 | return changes_list |
| 1379 | |
| 1380 | def define_all_tasks( |
| 1381 | self, |
| 1382 | changes_list, |
| 1383 | db_new_tasks, |
| 1384 | tasks_by_target_record_id, |
| 1385 | ): |
| 1386 | """Function to create all the task structures obtanied from |
| 1387 | the method calculate_all_differences_to_deploy |
| 1388 | |
| 1389 | Args: |
| 1390 | changes_list (List): ordered list of items to be created or deleted |
| 1391 | db_new_tasks (List): tasks list to be created |
| 1392 | action_id (str): action id |
| 1393 | tasks_by_target_record_id (Dict[str, Any]): |
| 1394 | [<target_record_id>, <task>] |
| 1395 | |
| 1396 | """ |
| 1397 | |
| 1398 | for change in changes_list: |
| 1399 | task = Ns._create_task( |
| 1400 | deployment_info=change["deployment_info"], |
| 1401 | target_id=change["target_id"], |
| 1402 | item=change["item"], |
| 1403 | action=change["action"], |
| 1404 | target_record=change["target_record"], |
| 1405 | target_record_id=change["target_record_id"], |
| 1406 | extra_dict=change.get("extra_dict", None), |
| 1407 | ) |
| 1408 | |
| 1409 | tasks_by_target_record_id[change["target_record_id"]] = task |
| 1410 | db_new_tasks.append(task) |
| 1411 | |
| 1412 | if change.get("common_id"): |
| 1413 | task["common_id"] = change["common_id"] |
| 1414 | |
| 1415 | def upload_all_tasks( |
| 1416 | self, |
| 1417 | db_new_tasks, |
| 1418 | now, |
| 1419 | ): |
| 1420 | """Function to save all tasks in the common DB |
| 1421 | |
| 1422 | Args: |
| 1423 | db_new_tasks (List): tasks list to be created |
| 1424 | now (time): current time |
| 1425 | |
| 1426 | """ |
| 1427 | |
| 1428 | nb_ro_tasks = 0 # for logging |
| 1429 | |
| 1430 | for db_task in db_new_tasks: |
| 1431 | target_id = db_task.pop("target_id") |
| 1432 | common_id = db_task.get("common_id") |
| 1433 | |
| 1434 | if common_id: |
| 1435 | if self.db.set_one( |
| 1436 | "ro_tasks", |
| 1437 | q_filter={ |
| 1438 | "target_id": target_id, |
| 1439 | "tasks.common_id": common_id, |
| 1440 | }, |
| 1441 | update_dict={"to_check_at": now, "modified_at": now}, |
| 1442 | push={"tasks": db_task}, |
| 1443 | fail_on_empty=False, |
| 1444 | ): |
| 1445 | continue |
| 1446 | |
| 1447 | if not self.db.set_one( |
| 1448 | "ro_tasks", |
| 1449 | q_filter={ |
| 1450 | "target_id": target_id, |
| 1451 | "tasks.target_record": db_task["target_record"], |
| 1452 | }, |
| 1453 | update_dict={"to_check_at": now, "modified_at": now}, |
| 1454 | push={"tasks": db_task}, |
| 1455 | fail_on_empty=False, |
| 1456 | ): |
| 1457 | # Create a ro_task |
| 1458 | self.logger.debug("Updating database, Creating ro_tasks") |
| 1459 | db_ro_task = Ns._create_ro_task(target_id, db_task) |
| 1460 | nb_ro_tasks += 1 |
| 1461 | self.db.create("ro_tasks", db_ro_task) |
| 1462 | |
| 1463 | self.logger.debug( |
| 1464 | "Created {} ro_tasks; {} tasks - db_new_tasks={}".format( |
| 1465 | nb_ro_tasks, len(db_new_tasks), db_new_tasks |
| 1466 | ) |
| 1467 | ) |
| 1468 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1469 | def deploy(self, session, indata, version, nsr_id, *args, **kwargs): |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1470 | self.logger.debug("ns.deploy nsr_id={} indata={}".format(nsr_id, indata)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1471 | validate_input(indata, deploy_schema) |
| 1472 | action_id = indata.get("action_id", str(uuid4())) |
| 1473 | task_index = 0 |
| 1474 | # get current deployment |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1475 | db_nsr_update = {} # update operation on nsrs |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1476 | db_vnfrs_update = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1477 | db_vnfrs = {} # vnf's info indexed by _id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1478 | step = "" |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1479 | logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id) |
| 1480 | self.logger.debug(logging_text + "Enter") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1481 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1482 | try: |
| 1483 | step = "Getting ns and vnfr record from db" |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1484 | db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1485 | db_new_tasks = [] |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1486 | tasks_by_target_record_id = {} |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1487 | # read from db: vnf's of this ns |
| 1488 | step = "Getting vnfrs from db" |
| 1489 | db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1490 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1491 | if not db_vnfrs_list: |
| 1492 | raise NsException("Cannot obtain associated VNF for ns") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1493 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1494 | for vnfr in db_vnfrs_list: |
| 1495 | db_vnfrs[vnfr["_id"]] = vnfr |
| 1496 | db_vnfrs_update[vnfr["_id"]] = {} |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1497 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1498 | now = time() |
| 1499 | 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] | 1500 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1501 | if not db_ro_nsr: |
| 1502 | db_ro_nsr = self._create_db_ro_nsrs(nsr_id, now) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1503 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1504 | # check that action_id is not in the list of actions. Suffixed with :index |
| 1505 | if action_id in db_ro_nsr["actions"]: |
| 1506 | index = 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1507 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1508 | while True: |
| 1509 | new_action_id = "{}:{}".format(action_id, index) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1510 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1511 | if new_action_id not in db_ro_nsr["actions"]: |
| 1512 | action_id = new_action_id |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1513 | self.logger.debug( |
| 1514 | logging_text |
| 1515 | + "Changing action_id in use to {}".format(action_id) |
| 1516 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1517 | break |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1518 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1519 | index += 1 |
| 1520 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1521 | def _process_action(indata): |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1522 | nonlocal db_new_tasks |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 1523 | nonlocal action_id |
| 1524 | nonlocal nsr_id |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1525 | nonlocal task_index |
| 1526 | nonlocal db_vnfrs |
| 1527 | nonlocal db_ro_nsr |
| 1528 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1529 | if indata["action"]["action"] == "inject_ssh_key": |
| 1530 | key = indata["action"].get("key") |
| 1531 | user = indata["action"].get("user") |
| 1532 | password = indata["action"].get("password") |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1533 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1534 | for vnf in indata.get("vnf", ()): |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1535 | if vnf["_id"] not in db_vnfrs: |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1536 | raise NsException("Invalid vnf={}".format(vnf["_id"])) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1537 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1538 | db_vnfr = db_vnfrs[vnf["_id"]] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1539 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1540 | for target_vdu in vnf.get("vdur", ()): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1541 | vdu_index, vdur = next( |
| 1542 | ( |
| 1543 | i_v |
| 1544 | for i_v in enumerate(db_vnfr["vdur"]) |
| 1545 | if i_v[1]["id"] == target_vdu["id"] |
| 1546 | ), |
| 1547 | (None, None), |
| 1548 | ) |
| 1549 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1550 | if not vdur: |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1551 | raise NsException( |
| 1552 | "Invalid vdu vnf={}.{}".format( |
| 1553 | vnf["_id"], target_vdu["id"] |
| 1554 | ) |
| 1555 | ) |
| 1556 | |
| 1557 | target_vim, vim_info = next( |
| 1558 | k_v for k_v in vdur["vim_info"].items() |
| 1559 | ) |
| tierno | 8615352 | 2020-12-06 18:27:16 +0000 | [diff] [blame] | 1560 | self._assign_vim(target_vim) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1561 | target_record = "vnfrs:{}:vdur.{}.ssh_keys".format( |
| 1562 | vnf["_id"], vdu_index |
| 1563 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1564 | extra_dict = { |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1565 | "depends_on": [ |
| 1566 | "vnfrs:{}:vdur.{}".format(vnf["_id"], vdur["id"]) |
| 1567 | ], |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1568 | "params": { |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1569 | "ip_address": vdur.get("ip-address"), |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1570 | "user": user, |
| 1571 | "key": key, |
| 1572 | "password": password, |
| 1573 | "private_key": db_ro_nsr["private_key"], |
| 1574 | "salt": db_ro_nsr["_id"], |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1575 | "schema_version": db_ro_nsr["_admin"][ |
| 1576 | "schema_version" |
| 1577 | ], |
| 1578 | }, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1579 | } |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 1580 | |
| 1581 | deployment_info = { |
| 1582 | "action_id": action_id, |
| 1583 | "nsr_id": nsr_id, |
| 1584 | "task_index": task_index, |
| 1585 | } |
| 1586 | |
| 1587 | task = Ns._create_task( |
| 1588 | deployment_info=deployment_info, |
| 1589 | target_id=target_vim, |
| 1590 | item="vdu", |
| 1591 | action="EXEC", |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1592 | target_record=target_record, |
| 1593 | target_record_id=None, |
| 1594 | extra_dict=extra_dict, |
| 1595 | ) |
| sousaedu | 89278b8 | 2021-11-19 01:01:32 +0000 | [diff] [blame] | 1596 | |
| 1597 | task_index = deployment_info.get("task_index") |
| 1598 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1599 | db_new_tasks.append(task) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1600 | |
| 1601 | with self.write_lock: |
| 1602 | if indata.get("action"): |
| 1603 | _process_action(indata) |
| 1604 | else: |
| 1605 | # compute network differences |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame^] | 1606 | # NS |
| 1607 | step = "process NS elements" |
| 1608 | changes_list = self.calculate_all_differences_to_deploy( |
| 1609 | indata=indata, |
| 1610 | nsr_id=nsr_id, |
| 1611 | db_nsr=db_nsr, |
| 1612 | db_vnfrs=db_vnfrs, |
| 1613 | db_ro_nsr=db_ro_nsr, |
| 1614 | db_nsr_update=db_nsr_update, |
| 1615 | db_vnfrs_update=db_vnfrs_update, |
| 1616 | action_id=action_id, |
| 1617 | tasks_by_target_record_id=tasks_by_target_record_id, |
| 1618 | ) |
| 1619 | self.define_all_tasks( |
| 1620 | changes_list=changes_list, |
| 1621 | db_new_tasks=db_new_tasks, |
| 1622 | tasks_by_target_record_id=tasks_by_target_record_id, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1623 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1624 | |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame^] | 1625 | step = "Updating database, Appending tasks to ro_tasks" |
| 1626 | self.upload_all_tasks( |
| 1627 | db_new_tasks=db_new_tasks, |
| 1628 | now=now, |
| 1629 | ) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1630 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1631 | step = "Updating database, nsrs" |
| 1632 | if db_nsr_update: |
| 1633 | self.db.set_one("nsrs", {"_id": nsr_id}, db_nsr_update) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1634 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1635 | for vnfr_id, db_vnfr_update in db_vnfrs_update.items(): |
| 1636 | if db_vnfr_update: |
| 1637 | step = "Updating database, vnfrs={}".format(vnfr_id) |
| 1638 | self.db.set_one("vnfrs", {"_id": vnfr_id}, db_vnfr_update) |
| 1639 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1640 | self.logger.debug( |
| gallardo | a1c2b40 | 2022-02-11 12:41:59 +0000 | [diff] [blame^] | 1641 | logging_text + "Exit. Created {} tasks".format(len(db_new_tasks)) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1642 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1643 | |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1644 | return ( |
| 1645 | {"status": "ok", "nsr_id": nsr_id, "action_id": action_id}, |
| 1646 | action_id, |
| 1647 | True, |
| 1648 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1649 | except Exception as e: |
| 1650 | if isinstance(e, (DbException, NsException)): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1651 | self.logger.error( |
| 1652 | logging_text + "Exit Exception while '{}': {}".format(step, e) |
| 1653 | ) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1654 | else: |
| 1655 | e = traceback_format_exc() |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1656 | self.logger.critical( |
| 1657 | logging_text + "Exit Exception while '{}': {}".format(step, e), |
| 1658 | exc_info=True, |
| 1659 | ) |
| 1660 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1661 | raise NsException(e) |
| 1662 | |
| 1663 | def delete(self, session, indata, version, nsr_id, *args, **kwargs): |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1664 | self.logger.debug("ns.delete version={} nsr_id={}".format(version, nsr_id)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1665 | # 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] | 1666 | |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1667 | with self.write_lock: |
| 1668 | try: |
| 1669 | NsWorker.delete_db_tasks(self.db, nsr_id, None) |
| 1670 | except NsWorkerException as e: |
| 1671 | raise NsException(e) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1672 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1673 | return None, None, True |
| 1674 | |
| 1675 | def status(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1676 | # self.logger.debug("ns.status version={} nsr_id={}, action_id={} indata={}" |
| 1677 | # .format(version, nsr_id, action_id, indata)) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1678 | task_list = [] |
| 1679 | done = 0 |
| 1680 | total = 0 |
| 1681 | ro_tasks = self.db.get_list("ro_tasks", {"tasks.action_id": action_id}) |
| 1682 | global_status = "DONE" |
| 1683 | details = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1684 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1685 | for ro_task in ro_tasks: |
| 1686 | for task in ro_task["tasks"]: |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1687 | if task and task["action_id"] == action_id: |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1688 | task_list.append(task) |
| 1689 | total += 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1690 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1691 | if task["status"] == "FAILED": |
| 1692 | global_status = "FAILED" |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1693 | error_text = "Error at {} {}: {}".format( |
| 1694 | task["action"].lower(), |
| 1695 | task["item"], |
| 1696 | ro_task["vim_info"].get("vim_details") or "unknown", |
| 1697 | ) |
| tierno | 70eeb18 | 2020-10-19 16:38:00 +0000 | [diff] [blame] | 1698 | details.append(error_text) |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1699 | elif task["status"] in ("SCHEDULED", "BUILD"): |
| 1700 | if global_status != "FAILED": |
| 1701 | global_status = "BUILD" |
| 1702 | else: |
| 1703 | done += 1 |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1704 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1705 | return_data = { |
| 1706 | "status": global_status, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1707 | "details": ". ".join(details) |
| 1708 | if details |
| 1709 | else "progress {}/{}".format(done, total), |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1710 | "nsr_id": nsr_id, |
| 1711 | "action_id": action_id, |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1712 | "tasks": task_list, |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1713 | } |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1714 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1715 | return return_data, None, True |
| 1716 | |
| 1717 | def cancel(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1718 | print( |
| 1719 | "ns.cancel session={} indata={} version={} nsr_id={}, action_id={}".format( |
| 1720 | session, indata, version, nsr_id, action_id |
| 1721 | ) |
| 1722 | ) |
| 1723 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1724 | return None, None, True |
| 1725 | |
| 1726 | def get_deploy(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| 1727 | nsrs = self.db.get_list("nsrs", {}) |
| 1728 | return_data = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1729 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1730 | for ns in nsrs: |
| 1731 | return_data.append({"_id": ns["_id"], "name": ns["name"]}) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1732 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1733 | return return_data, None, True |
| 1734 | |
| 1735 | def get_actions(self, session, indata, version, nsr_id, action_id, *args, **kwargs): |
| 1736 | ro_tasks = self.db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id}) |
| 1737 | return_data = [] |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1738 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1739 | for ro_task in ro_tasks: |
| 1740 | for task in ro_task["tasks"]: |
| 1741 | if task["action_id"] not in return_data: |
| 1742 | return_data.append(task["action_id"]) |
| sousaedu | 80135b9 | 2021-02-17 15:05:18 +0100 | [diff] [blame] | 1743 | |
| tierno | 1d213f4 | 2020-04-24 14:02:51 +0000 | [diff] [blame] | 1744 | return return_data, None, True |