Refactor ns.py so that RO uses the IP profile as it comes from LCM
[osm/RO.git] / NG-RO / osm_ng_ro / ns.py
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
19 from http import HTTPStatus
20 from itertools import product
21 import logging
22 from random import choice as random_choice
23 from threading import Lock
24 from time import time
25 from traceback import format_exc as traceback_format_exc
26 from typing import Any, Dict, List, Optional, Tuple, Type
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,
34 select_autoescape,
35 StrictUndefined,
36 TemplateError,
37 TemplateNotFound,
38 UndefinedError,
39 )
40 from osm_common import (
41 dbmemory,
42 dbmongo,
43 fslocal,
44 fsmongo,
45 msgkafka,
46 msglocal,
47 version as common_version,
48 )
49 from osm_common.dbbase import DbBase, DbException
50 from osm_common.fsbase import FsBase, FsException
51 from osm_common.msgbase import MsgException
52 from osm_ng_ro.ns_thread import deep_get, NsWorker, NsWorkerException
53 from osm_ng_ro.validation import deploy_schema, validate_input
54 import yaml
55
56 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
57 min_common_version = "0.1.16"
58
59
60 class NsException(Exception):
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]
78
79 if text_id:
80 return text_id
81 except Exception as error:
82 logging.exception(f"{error} occured while getting process id")
83
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 = []
91
92 for point in v.split("."):
93 filled.append(point.zfill(8))
94
95 return tuple(filled)
96
97
98 class Ns(object):
99 def __init__(self):
100 self.db = None
101 self.fs = None
102 self.msg = None
103 self.config = None
104 # self.operations = None
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
108 self.map_topic = {}
109 self.write_lock = None
110 self.vims_assigned = {}
111 self.next_worker = 0
112 self.plugins = {}
113 self.workers = []
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,
119 "affinity-or-anti-affinity-group": Ns._process_affinity_group_params,
120 }
121 self.db_path_map = {
122 "net": "vld",
123 "image": "image",
124 "flavor": "flavor",
125 "vdu": "vdur",
126 "affinity-or-anti-affinity-group": "affinity-or-anti-affinity-group",
127 }
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
141 self.logger = logging.getLogger("ro.ns")
142
143 # check right version of common
144 if versiontuple(common_version) < versiontuple(min_common_version):
145 raise NsException(
146 "Not compatible osm/common version '{}'. Needed '{}' or higher".format(
147 common_version, min_common_version
148 )
149 )
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:
160 raise NsException(
161 "Invalid configuration param '{}' at '[database]':'driver'".format(
162 config["database"]["driver"]
163 )
164 )
165
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"])
173 elif config["storage"]["driver"] is None:
174 pass
175 else:
176 raise NsException(
177 "Invalid configuration param '{}' at '[storage]':'driver'".format(
178 config["storage"]["driver"]
179 )
180 )
181
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:
190 raise NsException(
191 "Invalid configuration param '{}' at '[message]':'driver'".format(
192 config["message"]["driver"]
193 )
194 )
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)
201
202 def get_assigned_vims(self):
203 return list(self.vims_assigned.keys())
204
205 def stop(self):
206 try:
207 if self.db:
208 self.db.db_disconnect()
209
210 if self.fs:
211 self.fs.fs_disconnect()
212
213 if self.msg:
214 self.msg.disconnect()
215
216 self.write_lock = None
217 except (DbException, FsException, MsgException) as e:
218 raise NsException(str(e), http_code=e.http_code)
219
220 for worker in self.workers:
221 worker.insert_task(("terminate",))
222
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
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
239 if worker_id is not None:
240 # unset idle status to avoid race conditions
241 self.workers[worker_id].idle = False
242 else:
243 worker_id = len(self.workers)
244
245 if worker_id < self.config["global"]["server.ns_threads"]:
246 # create a new worker
247 self.workers.append(
248 NsWorker(worker_id, self.config, self.plugins, self.db)
249 )
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
254 self.next_worker = (self.next_worker + 1) % self.config["global"][
255 "server.ns_threads"
256 ]
257
258 return worker_id
259
260 def assign_vim(self, target_id):
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))
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
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))
276
277 def unload_vim(self, target_id):
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]
284 self.workers[worker_id].insert_task(("unload_vim", target_id))
285 del self.vims_assigned[target_id]
286
287 def check_vim(self, target_id):
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()
293
294 worker = self.workers[worker_id]
295 worker.insert_task(("check_vim", target_id))
296
297 def unload_unused_vims(self):
298 with self.write_lock:
299 vims_to_unload = []
300
301 for target_id in self.vims_assigned:
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 ):
310 vims_to_unload.append(target_id)
311
312 for target_id in vims_to_unload:
313 self._unload_vim(target_id)
314
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]
336 """
337 vnfd_id, _, other = location.partition(":")
338 _type, _, name = other.partition(":")
339 vnfd = db.get_one("vnfds", {"_id": vnfd_id})
340
341 if _type == "file":
342 base_folder = vnfd["_admin"]["storage"]
343 cloud_init_file = "{}/{}/cloud_init/{}".format(
344 base_folder["folder"], base_folder["pkg-dir"], name
345 )
346
347 if not fs:
348 raise NsException(
349 "Cannot read file '{}'. Filesystem not loaded, change configuration at storage.driver".format(
350 cloud_init_file
351 )
352 )
353
354 with fs.file_open(cloud_init_file, "r") as ci_file:
355 cloud_init_content = ci_file.read()
356 elif _type == "vdu":
357 cloud_init_content = vnfd["vdu"][int(name)]["cloud-init"]
358 else:
359 raise NsException("Mismatch descriptor for cloud init: {}".format(location))
360
361 return cloud_init_content
362
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 """
383 try:
384 env = Environment(
385 undefined=StrictUndefined,
386 autoescape=select_autoescape(default_for_string=True, default=True),
387 )
388 template = env.from_string(cloud_init_content)
389
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"
394 "inside the 'additionalParamsForVnf' block".format(e, context)
395 )
396 except (TemplateError, TemplateNotFound) as e:
397 raise NsException(
398 "Error parsing Jinja2 to cloud-init content at vnfd='{}': {}".format(
399 context, e
400 )
401 )
402
403 def _create_db_ro_nsrs(self, nsr_id, now):
404 try:
405 key = rsa.generate_private_key(
406 backend=crypto_default_backend(), public_exponent=65537, key_size=2048
407 )
408 private_key = key.private_bytes(
409 crypto_serialization.Encoding.PEM,
410 crypto_serialization.PrivateFormat.PKCS8,
411 crypto_serialization.NoEncryption(),
412 )
413 public_key = key.public_key().public_bytes(
414 crypto_serialization.Encoding.OpenSSH,
415 crypto_serialization.PublicFormat.OpenSSH,
416 )
417 private_key = private_key.decode("utf8")
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:]
421 public_key = public_key.decode("utf8")
422 except Exception as e:
423 raise NsException("Cannot create ssh-keys: {}".format(e))
424
425 schema_version = "1.1"
426 private_key_encrypted = self.db.encrypt(
427 private_key, schema_version=schema_version, salt=nsr_id
428 )
429 db_content = {
430 "_id": nsr_id,
431 "_admin": {
432 "created": now,
433 "modified": now,
434 "schema_version": schema_version,
435 },
436 "public_key": public_key,
437 "private_key": private_key_encrypted,
438 "actions": [],
439 }
440 self.db.create("ro_nsrs", db_content)
441
442 return db_content
443
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
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,
516 "vim_message": None,
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
527 @staticmethod
528 def _process_image_params(
529 target_image: Dict[str, Any],
530 indata: Dict[str, Any],
531 vim_info: Dict[str, Any],
532 target_record_id: str,
533 **kwargs: Dict[str, Any],
534 ) -> Dict[str, Any]:
535 """Function to process VDU image parameters.
536
537 Args:
538 target_image (Dict[str, Any]): [description]
539 indata (Dict[str, Any]): [description]
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
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
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 = {}
650 numa_list = []
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"):
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"])
661
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
667
668 if numa_node.get("num-cores"):
669 numa["cores"] = numa_node["num-cores"]
670 epa_vcpu_set = True
671
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"]
676 )
677 epa_vcpu_set = True
678
679 if paired_threads.get("paired-thread-ids"):
680 numa["paired-threads-id"] = []
681
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 )
689
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
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 ):
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
728 numa[
729 "cores"
730 if guest_epa_quota.get("cpu-thread-pinning-policy") == "ISOLATE"
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 = {}
751 numa_list = []
752
753 if target_flavor.get("guest-epa"):
754 guest_epa = target_flavor["guest-epa"]
755
756 numa_list, epa_vcpu_set = Ns._process_guest_epa_numa_params(
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
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
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 )
782 for numa in numa_list:
783 numa.update(tmp_numa)
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:
793 extended["numas"] = numa_list
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,
803 **kwargs: Dict[str, Any],
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 """
816 db = kwargs.get("db")
817 target_vdur = {}
818
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
825 for vnf in indata.get("vnf", []):
826 for vdur in vnf.get("vdur", []):
827 if vdur.get("ns-flavor-id") == target_flavor.get("id"):
828 target_vdur = vdur
829
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
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
865 @staticmethod
866 def _process_net_params(
867 target_vld: Dict[str, Any],
868 indata: Dict[str, Any],
869 vim_info: Dict[str, Any],
870 target_record_id: str,
871 **kwargs: Dict[str, Any],
872 ) -> Dict[str, Any]:
873 """Function to process network parameters.
874
875 Args:
876 target_vld (Dict[str, Any]): [description]
877 indata (Dict[str, Any]): [description]
878 vim_info (Dict[str, Any]): [description]
879 target_record_id (str): [description]
880
881 Returns:
882 Dict[str, Any]: [description]
883 """
884 extra_dict = {}
885
886 if vim_info.get("sdn"):
887 # vnf_preffix = "vnfrs:{}".format(vnfr_id)
888 # ns_preffix = "nsrs:{}".format(nsr_id)
889 # remove the ending ".sdn
890 vld_target_record_id, _, _ = target_record_id.rpartition(".")
891 extra_dict["params"] = {
892 k: vim_info[k]
893 for k in ("sdn-ports", "target_vim", "vlds", "type")
894 if vim_info.get(k)
895 }
896
897 # TODO needed to add target_id in the dependency.
898 if vim_info.get("target_vim"):
899 extra_dict["depends_on"] = [
900 f"{vim_info.get('target_vim')} {vld_target_record_id}"
901 ]
902
903 return extra_dict
904
905 if vim_info.get("vim_network_name"):
906 extra_dict["find_params"] = {
907 "filter_dict": {
908 "name": vim_info.get("vim_network_name"),
909 },
910 }
911 elif vim_info.get("vim_network_id"):
912 extra_dict["find_params"] = {
913 "filter_dict": {
914 "id": vim_info.get("vim_network_id"),
915 },
916 }
917 elif target_vld.get("mgmt-network") and not vim_info.get("provider_network"):
918 extra_dict["find_params"] = {
919 "mgmt": True,
920 "name": target_vld["id"],
921 }
922 else:
923 # create
924 extra_dict["params"] = {
925 "net_name": (
926 f"{indata.get('name')[:16]}-{target_vld.get('name', target_vld.get('id'))[:16]}"
927 ),
928 "ip_profile": vim_info.get("ip_profile"),
929 "provider_network_profile": vim_info.get("provider_network"),
930 }
931
932 if not target_vld.get("underlay"):
933 extra_dict["params"]["net_type"] = "bridge"
934 else:
935 extra_dict["params"]["net_type"] = (
936 "ptp" if target_vld.get("type") == "ELINE" else "data"
937 )
938
939 return extra_dict
940
941 @staticmethod
942 def find_persistent_root_volumes(
943 vnfd: dict,
944 target_vdu: dict,
945 vdu_instantiation_volumes_list: list,
946 disk_list: list,
947 ) -> Dict[str, any]:
948 """Find the persistent root volumes and add them to the disk_list
949 by parsing the instantiation parameters.
950
951 Args:
952 vnfd (dict): VNF descriptor
953 target_vdu (dict): processed VDU
954 vdu_instantiation_volumes_list (list): instantiation parameters for the each VDU as a list
955 disk_list (list): to be filled up
956
957 Returns:
958 persistent_root_disk (dict): Details of persistent root disk
959
960 """
961 persistent_root_disk = {}
962 # There can be only one root disk, when we find it, it will return the result
963
964 for vdu, vsd in product(
965 vnfd.get("vdu", ()), vnfd.get("virtual-storage-desc", ())
966 ):
967 if (
968 vdu["name"] == target_vdu["vdu-name"]
969 and vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]
970 ):
971 root_disk = vsd
972 if (
973 root_disk.get("type-of-storage")
974 == "persistent-storage:persistent-storage"
975 ):
976 for vdu_volume in vdu_instantiation_volumes_list:
977 if (
978 vdu_volume["vim-volume-id"]
979 and root_disk["id"] == vdu_volume["name"]
980 ):
981 persistent_root_disk[vsd["id"]] = {
982 "vim_volume_id": vdu_volume["vim-volume-id"],
983 "image_id": vdu.get("sw-image-desc"),
984 }
985
986 disk_list.append(persistent_root_disk[vsd["id"]])
987
988 return persistent_root_disk
989
990 else:
991 if root_disk.get("size-of-storage"):
992 persistent_root_disk[vsd["id"]] = {
993 "image_id": vdu.get("sw-image-desc"),
994 "size": root_disk.get("size-of-storage"),
995 "keep": Ns.is_volume_keeping_required(root_disk),
996 }
997
998 disk_list.append(persistent_root_disk[vsd["id"]])
999
1000 return persistent_root_disk
1001
1002 @staticmethod
1003 def find_persistent_volumes(
1004 persistent_root_disk: dict,
1005 target_vdu: dict,
1006 vdu_instantiation_volumes_list: list,
1007 disk_list: list,
1008 ) -> None:
1009 """Find the ordinary persistent volumes and add them to the disk_list
1010 by parsing the instantiation parameters.
1011
1012 Args:
1013 persistent_root_disk: persistent root disk dictionary
1014 target_vdu: processed VDU
1015 vdu_instantiation_volumes_list: instantiation parameters for the each VDU as a list
1016 disk_list: to be filled up
1017
1018 """
1019 # Find the ordinary volumes which are not added to the persistent_root_disk
1020 persistent_disk = {}
1021 for disk in target_vdu.get("virtual-storages", {}):
1022 if (
1023 disk.get("type-of-storage") == "persistent-storage:persistent-storage"
1024 and disk["id"] not in persistent_root_disk.keys()
1025 ):
1026 for vdu_volume in vdu_instantiation_volumes_list:
1027 if vdu_volume["vim-volume-id"] and disk["id"] == vdu_volume["name"]:
1028 persistent_disk[disk["id"]] = {
1029 "vim_volume_id": vdu_volume["vim-volume-id"],
1030 }
1031 disk_list.append(persistent_disk[disk["id"]])
1032
1033 else:
1034 if disk["id"] not in persistent_disk.keys():
1035 persistent_disk[disk["id"]] = {
1036 "size": disk.get("size-of-storage"),
1037 "keep": Ns.is_volume_keeping_required(disk),
1038 }
1039 disk_list.append(persistent_disk[disk["id"]])
1040
1041 @staticmethod
1042 def is_volume_keeping_required(virtual_storage_desc: Dict[str, Any]) -> bool:
1043 """Function to decide keeping persistent volume
1044 upon VDU deletion.
1045
1046 Args:
1047 virtual_storage_desc (Dict[str, Any]): virtual storage description dictionary
1048
1049 Returns:
1050 bool (True/False)
1051 """
1052
1053 if not virtual_storage_desc.get("vdu-storage-requirements"):
1054 return False
1055 for item in virtual_storage_desc.get("vdu-storage-requirements", {}):
1056 if item.get("key") == "keep-volume" and item.get("value") == "true":
1057 return True
1058 return False
1059
1060 @staticmethod
1061 def _sort_vdu_interfaces(target_vdu: dict) -> None:
1062 """Sort the interfaces according to position number.
1063
1064 Args:
1065 target_vdu (dict): Details of VDU to be created
1066
1067 """
1068 # If the position info is provided for all the interfaces, it will be sorted
1069 # according to position number ascendingly.
1070 sorted_interfaces = sorted(
1071 target_vdu["interfaces"],
1072 key=lambda x: (x.get("position") is None, x.get("position")),
1073 )
1074 target_vdu["interfaces"] = sorted_interfaces
1075
1076 @staticmethod
1077 def _partially_locate_vdu_interfaces(target_vdu: dict) -> None:
1078 """Only place the interfaces which has specific position.
1079
1080 Args:
1081 target_vdu (dict): Details of VDU to be created
1082
1083 """
1084 # If the position info is provided for some interfaces but not all of them, the interfaces
1085 # which has specific position numbers will be placed and others' positions will not be taken care.
1086 if any(
1087 i.get("position") + 1
1088 for i in target_vdu["interfaces"]
1089 if i.get("position") is not None
1090 ):
1091 n = len(target_vdu["interfaces"])
1092 sorted_interfaces = [-1] * n
1093 k, m = 0, 0
1094
1095 while k < n:
1096 if target_vdu["interfaces"][k].get("position") is not None:
1097 if any(i.get("position") == 0 for i in target_vdu["interfaces"]):
1098 idx = target_vdu["interfaces"][k]["position"] + 1
1099 else:
1100 idx = target_vdu["interfaces"][k]["position"]
1101 sorted_interfaces[idx - 1] = target_vdu["interfaces"][k]
1102 k += 1
1103
1104 while m < n:
1105 if target_vdu["interfaces"][m].get("position") is None:
1106 idy = sorted_interfaces.index(-1)
1107 sorted_interfaces[idy] = target_vdu["interfaces"][m]
1108 m += 1
1109
1110 target_vdu["interfaces"] = sorted_interfaces
1111
1112 @staticmethod
1113 def _prepare_vdu_cloud_init(
1114 target_vdu: dict, vdu2cloud_init: dict, db: object, fs: object
1115 ) -> Dict:
1116 """Fill cloud_config dict with cloud init details.
1117
1118 Args:
1119 target_vdu (dict): Details of VDU to be created
1120 vdu2cloud_init (dict): Cloud init dict
1121 db (object): DB object
1122 fs (object): FS object
1123
1124 Returns:
1125 cloud_config (dict): Cloud config details of VDU
1126
1127 """
1128 # cloud config
1129 cloud_config = {}
1130
1131 if target_vdu.get("cloud-init"):
1132 if target_vdu["cloud-init"] not in vdu2cloud_init:
1133 vdu2cloud_init[target_vdu["cloud-init"]] = Ns._get_cloud_init(
1134 db=db,
1135 fs=fs,
1136 location=target_vdu["cloud-init"],
1137 )
1138
1139 cloud_content_ = vdu2cloud_init[target_vdu["cloud-init"]]
1140 cloud_config["user-data"] = Ns._parse_jinja2(
1141 cloud_init_content=cloud_content_,
1142 params=target_vdu.get("additionalParams"),
1143 context=target_vdu["cloud-init"],
1144 )
1145
1146 if target_vdu.get("boot-data-drive"):
1147 cloud_config["boot-data-drive"] = target_vdu.get("boot-data-drive")
1148
1149 return cloud_config
1150
1151 @staticmethod
1152 def _check_vld_information_of_interfaces(
1153 interface: dict, ns_preffix: str, vnf_preffix: str
1154 ) -> Optional[str]:
1155 """Prepare the net_text by the virtual link information for vnf and ns level.
1156 Args:
1157 interface (dict): Interface details
1158 ns_preffix (str): Prefix of NS
1159 vnf_preffix (str): Prefix of VNF
1160
1161 Returns:
1162 net_text (str): information of net
1163
1164 """
1165 net_text = ""
1166 if interface.get("ns-vld-id"):
1167 net_text = ns_preffix + ":vld." + interface["ns-vld-id"]
1168 elif interface.get("vnf-vld-id"):
1169 net_text = vnf_preffix + ":vld." + interface["vnf-vld-id"]
1170
1171 return net_text
1172
1173 @staticmethod
1174 def _prepare_interface_port_security(interface: dict) -> None:
1175 """
1176
1177 Args:
1178 interface (dict): Interface details
1179
1180 """
1181 if "port-security-enabled" in interface:
1182 interface["port_security"] = interface.pop("port-security-enabled")
1183
1184 if "port-security-disable-strategy" in interface:
1185 interface["port_security_disable_strategy"] = interface.pop(
1186 "port-security-disable-strategy"
1187 )
1188
1189 @staticmethod
1190 def _create_net_item_of_interface(interface: dict, net_text: str) -> dict:
1191 """Prepare net item including name, port security, floating ip etc.
1192
1193 Args:
1194 interface (dict): Interface details
1195 net_text (str): information of net
1196
1197 Returns:
1198 net_item (dict): Dict including net details
1199
1200 """
1201
1202 net_item = {
1203 x: v
1204 for x, v in interface.items()
1205 if x
1206 in (
1207 "name",
1208 "vpci",
1209 "port_security",
1210 "port_security_disable_strategy",
1211 "floating_ip",
1212 )
1213 }
1214 net_item["net_id"] = "TASK-" + net_text
1215 net_item["type"] = "virtual"
1216
1217 return net_item
1218
1219 @staticmethod
1220 def _prepare_type_of_interface(
1221 interface: dict, tasks_by_target_record_id: dict, net_text: str, net_item: dict
1222 ) -> None:
1223 """Fill the net item type by interface type such as SR-IOV, OM-MGMT, bridge etc.
1224
1225 Args:
1226 interface (dict): Interface details
1227 tasks_by_target_record_id (dict): Task details
1228 net_text (str): information of net
1229 net_item (dict): Dict including net details
1230
1231 """
1232 # TODO mac_address: used for SR-IOV ifaces #TODO for other types
1233 # TODO floating_ip: True/False (or it can be None)
1234
1235 if interface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1236 # Mark the net create task as type data
1237 if deep_get(
1238 tasks_by_target_record_id,
1239 net_text,
1240 "extra_dict",
1241 "params",
1242 "net_type",
1243 ):
1244 tasks_by_target_record_id[net_text]["extra_dict"]["params"][
1245 "net_type"
1246 ] = "data"
1247
1248 net_item["use"] = "data"
1249 net_item["model"] = interface["type"]
1250 net_item["type"] = interface["type"]
1251
1252 elif (
1253 interface.get("type") == "OM-MGMT"
1254 or interface.get("mgmt-interface")
1255 or interface.get("mgmt-vnf")
1256 ):
1257 net_item["use"] = "mgmt"
1258
1259 else:
1260 # If interface.get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1261 net_item["use"] = "bridge"
1262 net_item["model"] = interface.get("type")
1263
1264 @staticmethod
1265 def _prepare_vdu_interfaces(
1266 target_vdu: dict,
1267 extra_dict: dict,
1268 ns_preffix: str,
1269 vnf_preffix: str,
1270 logger: object,
1271 tasks_by_target_record_id: dict,
1272 net_list: list,
1273 ) -> None:
1274 """Prepare the net_item and add net_list, add mgmt interface to extra_dict.
1275
1276 Args:
1277 target_vdu (dict): VDU to be created
1278 extra_dict (dict): Dictionary to be filled
1279 ns_preffix (str): NS prefix as string
1280 vnf_preffix (str): VNF prefix as string
1281 logger (object): Logger Object
1282 tasks_by_target_record_id (dict): Task details
1283 net_list (list): Net list of VDU
1284 """
1285 for iface_index, interface in enumerate(target_vdu["interfaces"]):
1286 net_text = Ns._check_vld_information_of_interfaces(
1287 interface, ns_preffix, vnf_preffix
1288 )
1289 if not net_text:
1290 # Interface not connected to any vld
1291 logger.error(
1292 "Interface {} from vdu {} not connected to any vld".format(
1293 iface_index, target_vdu["vdu-name"]
1294 )
1295 )
1296 continue
1297
1298 extra_dict["depends_on"].append(net_text)
1299
1300 Ns._prepare_interface_port_security(interface)
1301
1302 net_item = Ns._create_net_item_of_interface(interface, net_text)
1303
1304 Ns._prepare_type_of_interface(
1305 interface, tasks_by_target_record_id, net_text, net_item
1306 )
1307
1308 if interface.get("ip-address"):
1309 net_item["ip_address"] = interface["ip-address"]
1310
1311 if interface.get("mac-address"):
1312 net_item["mac_address"] = interface["mac-address"]
1313
1314 net_list.append(net_item)
1315
1316 if interface.get("mgmt-vnf"):
1317 extra_dict["mgmt_vnf_interface"] = iface_index
1318 elif interface.get("mgmt-interface"):
1319 extra_dict["mgmt_vdu_interface"] = iface_index
1320
1321 @staticmethod
1322 def _prepare_vdu_ssh_keys(
1323 target_vdu: dict, ro_nsr_public_key: dict, cloud_config: dict
1324 ) -> None:
1325 """Add ssh keys to cloud config.
1326
1327 Args:
1328 target_vdu (dict): Details of VDU to be created
1329 ro_nsr_public_key (dict): RO NSR public Key
1330 cloud_config (dict): Cloud config details
1331
1332 """
1333 ssh_keys = []
1334
1335 if target_vdu.get("ssh-keys"):
1336 ssh_keys += target_vdu.get("ssh-keys")
1337
1338 if target_vdu.get("ssh-access-required"):
1339 ssh_keys.append(ro_nsr_public_key)
1340
1341 if ssh_keys:
1342 cloud_config["key-pairs"] = ssh_keys
1343
1344 @staticmethod
1345 def _select_persistent_root_disk(vsd: dict, vdu: dict) -> dict:
1346 """Selects the persistent root disk if exists.
1347 Args:
1348 vsd (dict): Virtual storage descriptors in VNFD
1349 vdu (dict): VNF descriptor
1350
1351 Returns:
1352 root_disk (dict): Selected persistent root disk
1353 """
1354 if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]:
1355 root_disk = vsd
1356 if root_disk.get(
1357 "type-of-storage"
1358 ) == "persistent-storage:persistent-storage" and root_disk.get(
1359 "size-of-storage"
1360 ):
1361 return root_disk
1362
1363 @staticmethod
1364 def _add_persistent_root_disk_to_disk_list(
1365 vnfd: dict, target_vdu: dict, persistent_root_disk: dict, disk_list: list
1366 ) -> None:
1367 """Find the persistent root disk and add to disk list.
1368
1369 Args:
1370 vnfd (dict): VNF descriptor
1371 target_vdu (dict): Details of VDU to be created
1372 persistent_root_disk (dict): Details of persistent root disk
1373 disk_list (list): Disks of VDU
1374
1375 """
1376 for vdu in vnfd.get("vdu", ()):
1377 if vdu["name"] == target_vdu["vdu-name"]:
1378 for vsd in vnfd.get("virtual-storage-desc", ()):
1379 root_disk = Ns._select_persistent_root_disk(vsd, vdu)
1380 if not root_disk:
1381 continue
1382
1383 persistent_root_disk[vsd["id"]] = {
1384 "image_id": vdu.get("sw-image-desc"),
1385 "size": root_disk["size-of-storage"],
1386 "keep": Ns.is_volume_keeping_required(root_disk),
1387 }
1388
1389 disk_list.append(persistent_root_disk[vsd["id"]])
1390 break
1391
1392 @staticmethod
1393 def _add_persistent_ordinary_disks_to_disk_list(
1394 target_vdu: dict,
1395 persistent_root_disk: dict,
1396 persistent_ordinary_disk: dict,
1397 disk_list: list,
1398 ) -> None:
1399 """Fill the disk list by adding persistent ordinary disks.
1400
1401 Args:
1402 target_vdu (dict): Details of VDU to be created
1403 persistent_root_disk (dict): Details of persistent root disk
1404 persistent_ordinary_disk (dict): Details of persistent ordinary disk
1405 disk_list (list): Disks of VDU
1406
1407 """
1408 if target_vdu.get("virtual-storages"):
1409 for disk in target_vdu["virtual-storages"]:
1410 if (
1411 disk.get("type-of-storage")
1412 == "persistent-storage:persistent-storage"
1413 and disk["id"] not in persistent_root_disk.keys()
1414 ):
1415 persistent_ordinary_disk[disk["id"]] = {
1416 "size": disk["size-of-storage"],
1417 "keep": Ns.is_volume_keeping_required(disk),
1418 }
1419 disk_list.append(persistent_ordinary_disk[disk["id"]])
1420
1421 @staticmethod
1422 def _prepare_vdu_affinity_group_list(
1423 target_vdu: dict, extra_dict: dict, ns_preffix: str
1424 ) -> List[Dict[str, any]]:
1425 """Process affinity group details to prepare affinity group list.
1426
1427 Args:
1428 target_vdu (dict): Details of VDU to be created
1429 extra_dict (dict): Dictionary to be filled
1430 ns_preffix (str): Prefix as string
1431
1432 Returns:
1433
1434 affinity_group_list (list): Affinity group details
1435
1436 """
1437 affinity_group_list = []
1438
1439 if target_vdu.get("affinity-or-anti-affinity-group-id"):
1440 for affinity_group_id in target_vdu["affinity-or-anti-affinity-group-id"]:
1441 affinity_group = {}
1442 affinity_group_text = (
1443 ns_preffix + ":affinity-or-anti-affinity-group." + affinity_group_id
1444 )
1445
1446 if not isinstance(extra_dict.get("depends_on"), list):
1447 raise NsException("Invalid extra_dict format.")
1448
1449 extra_dict["depends_on"].append(affinity_group_text)
1450 affinity_group["affinity_group_id"] = "TASK-" + affinity_group_text
1451 affinity_group_list.append(affinity_group)
1452
1453 return affinity_group_list
1454
1455 @staticmethod
1456 def _process_vdu_params(
1457 target_vdu: Dict[str, Any],
1458 indata: Dict[str, Any],
1459 vim_info: Dict[str, Any],
1460 target_record_id: str,
1461 **kwargs: Dict[str, Any],
1462 ) -> Dict[str, Any]:
1463 """Function to process VDU parameters.
1464
1465 Args:
1466 target_vdu (Dict[str, Any]): [description]
1467 indata (Dict[str, Any]): [description]
1468 vim_info (Dict[str, Any]): [description]
1469 target_record_id (str): [description]
1470
1471 Returns:
1472 Dict[str, Any]: [description]
1473 """
1474 vnfr_id = kwargs.get("vnfr_id")
1475 nsr_id = kwargs.get("nsr_id")
1476 vnfr = kwargs.get("vnfr")
1477 vdu2cloud_init = kwargs.get("vdu2cloud_init")
1478 tasks_by_target_record_id = kwargs.get("tasks_by_target_record_id")
1479 logger = kwargs.get("logger")
1480 db = kwargs.get("db")
1481 fs = kwargs.get("fs")
1482 ro_nsr_public_key = kwargs.get("ro_nsr_public_key")
1483
1484 vnf_preffix = "vnfrs:{}".format(vnfr_id)
1485 ns_preffix = "nsrs:{}".format(nsr_id)
1486 image_text = ns_preffix + ":image." + target_vdu["ns-image-id"]
1487 flavor_text = ns_preffix + ":flavor." + target_vdu["ns-flavor-id"]
1488 extra_dict = {"depends_on": [image_text, flavor_text]}
1489 net_list = []
1490
1491 persistent_root_disk = {}
1492 persistent_ordinary_disk = {}
1493 vdu_instantiation_volumes_list = []
1494 disk_list = []
1495 vnfd_id = vnfr["vnfd-id"]
1496 vnfd = db.get_one("vnfds", {"_id": vnfd_id})
1497
1498 # If the position info is provided for all the interfaces, it will be sorted
1499 # according to position number ascendingly.
1500 if all(
1501 True if i.get("position") is not None else False
1502 for i in target_vdu["interfaces"]
1503 ):
1504 Ns._sort_vdu_interfaces(target_vdu)
1505
1506 # If the position info is provided for some interfaces but not all of them, the interfaces
1507 # which has specific position numbers will be placed and others' positions will not be taken care.
1508 else:
1509 Ns._partially_locate_vdu_interfaces(target_vdu)
1510
1511 # If the position info is not provided for the interfaces, interfaces will be attached
1512 # according to the order in the VNFD.
1513 Ns._prepare_vdu_interfaces(
1514 target_vdu,
1515 extra_dict,
1516 ns_preffix,
1517 vnf_preffix,
1518 logger,
1519 tasks_by_target_record_id,
1520 net_list,
1521 )
1522
1523 # cloud config
1524 cloud_config = Ns._prepare_vdu_cloud_init(target_vdu, vdu2cloud_init, db, fs)
1525
1526 # Prepare VDU ssh keys
1527 Ns._prepare_vdu_ssh_keys(target_vdu, ro_nsr_public_key, cloud_config)
1528
1529 if target_vdu.get("additionalParams"):
1530 vdu_instantiation_volumes_list = (
1531 target_vdu.get("additionalParams").get("OSM").get("vdu_volumes")
1532 )
1533
1534 if vdu_instantiation_volumes_list:
1535 # Find the root volumes and add to the disk_list
1536 persistent_root_disk = Ns.find_persistent_root_volumes(
1537 vnfd, target_vdu, vdu_instantiation_volumes_list, disk_list
1538 )
1539
1540 # Find the ordinary volumes which are not added to the persistent_root_disk
1541 # and put them to the disk list
1542 Ns.find_persistent_volumes(
1543 persistent_root_disk,
1544 target_vdu,
1545 vdu_instantiation_volumes_list,
1546 disk_list,
1547 )
1548
1549 else:
1550 # Vdu_instantiation_volumes_list is empty
1551 # First get add the persistent root disks to disk_list
1552 Ns._add_persistent_root_disk_to_disk_list(
1553 vnfd, target_vdu, persistent_root_disk, disk_list
1554 )
1555 # Add the persistent non-root disks to disk_list
1556 Ns._add_persistent_ordinary_disks_to_disk_list(
1557 target_vdu, persistent_root_disk, persistent_ordinary_disk, disk_list
1558 )
1559
1560 affinity_group_list = Ns._prepare_vdu_affinity_group_list(
1561 target_vdu, extra_dict, ns_preffix
1562 )
1563
1564 extra_dict["params"] = {
1565 "name": "{}-{}-{}-{}".format(
1566 indata["name"][:16],
1567 vnfr["member-vnf-index-ref"][:16],
1568 target_vdu["vdu-name"][:32],
1569 target_vdu.get("count-index") or 0,
1570 ),
1571 "description": target_vdu["vdu-name"],
1572 "start": True,
1573 "image_id": "TASK-" + image_text,
1574 "flavor_id": "TASK-" + flavor_text,
1575 "affinity_group_list": affinity_group_list,
1576 "net_list": net_list,
1577 "cloud_config": cloud_config or None,
1578 "disk_list": disk_list,
1579 "availability_zone_index": None, # TODO
1580 "availability_zone_list": None, # TODO
1581 }
1582
1583 return extra_dict
1584
1585 @staticmethod
1586 def _process_affinity_group_params(
1587 target_affinity_group: Dict[str, Any],
1588 indata: Dict[str, Any],
1589 vim_info: Dict[str, Any],
1590 target_record_id: str,
1591 **kwargs: Dict[str, Any],
1592 ) -> Dict[str, Any]:
1593 """Get affinity or anti-affinity group parameters.
1594
1595 Args:
1596 target_affinity_group (Dict[str, Any]): [description]
1597 indata (Dict[str, Any]): [description]
1598 vim_info (Dict[str, Any]): [description]
1599 target_record_id (str): [description]
1600
1601 Returns:
1602 Dict[str, Any]: [description]
1603 """
1604
1605 extra_dict = {}
1606 affinity_group_data = {
1607 "name": target_affinity_group["name"],
1608 "type": target_affinity_group["type"],
1609 "scope": target_affinity_group["scope"],
1610 }
1611
1612 if target_affinity_group.get("vim-affinity-group-id"):
1613 affinity_group_data["vim-affinity-group-id"] = target_affinity_group[
1614 "vim-affinity-group-id"
1615 ]
1616
1617 extra_dict["params"] = {
1618 "affinity_group_data": affinity_group_data,
1619 }
1620
1621 return extra_dict
1622
1623 @staticmethod
1624 def _process_recreate_vdu_params(
1625 existing_vdu: Dict[str, Any],
1626 db_nsr: Dict[str, Any],
1627 vim_info: Dict[str, Any],
1628 target_record_id: str,
1629 target_id: str,
1630 **kwargs: Dict[str, Any],
1631 ) -> Dict[str, Any]:
1632 """Function to process VDU parameters to recreate.
1633
1634 Args:
1635 existing_vdu (Dict[str, Any]): [description]
1636 db_nsr (Dict[str, Any]): [description]
1637 vim_info (Dict[str, Any]): [description]
1638 target_record_id (str): [description]
1639 target_id (str): [description]
1640
1641 Returns:
1642 Dict[str, Any]: [description]
1643 """
1644 vnfr = kwargs.get("vnfr")
1645 vdu2cloud_init = kwargs.get("vdu2cloud_init")
1646 # logger = kwargs.get("logger")
1647 db = kwargs.get("db")
1648 fs = kwargs.get("fs")
1649 ro_nsr_public_key = kwargs.get("ro_nsr_public_key")
1650
1651 extra_dict = {}
1652 net_list = []
1653
1654 vim_details = {}
1655 vim_details_text = existing_vdu["vim_info"][target_id].get("vim_details", None)
1656 if vim_details_text:
1657 vim_details = yaml.safe_load(f"{vim_details_text}")
1658
1659 for iface_index, interface in enumerate(existing_vdu["interfaces"]):
1660 if "port-security-enabled" in interface:
1661 interface["port_security"] = interface.pop("port-security-enabled")
1662
1663 if "port-security-disable-strategy" in interface:
1664 interface["port_security_disable_strategy"] = interface.pop(
1665 "port-security-disable-strategy"
1666 )
1667
1668 net_item = {
1669 x: v
1670 for x, v in interface.items()
1671 if x
1672 in (
1673 "name",
1674 "vpci",
1675 "port_security",
1676 "port_security_disable_strategy",
1677 "floating_ip",
1678 )
1679 }
1680 existing_ifaces = existing_vdu["vim_info"][target_id].get(
1681 "interfaces_backup", []
1682 )
1683 net_id = next(
1684 (
1685 i["vim_net_id"]
1686 for i in existing_ifaces
1687 if i["ip_address"] == interface["ip-address"]
1688 ),
1689 None,
1690 )
1691
1692 net_item["net_id"] = net_id
1693 net_item["type"] = "virtual"
1694
1695 # TODO mac_address: used for SR-IOV ifaces #TODO for other types
1696 # TODO floating_ip: True/False (or it can be None)
1697 if interface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1698 net_item["use"] = "data"
1699 net_item["model"] = interface["type"]
1700 net_item["type"] = interface["type"]
1701 elif (
1702 interface.get("type") == "OM-MGMT"
1703 or interface.get("mgmt-interface")
1704 or interface.get("mgmt-vnf")
1705 ):
1706 net_item["use"] = "mgmt"
1707 else:
1708 # if interface.get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1709 net_item["use"] = "bridge"
1710 net_item["model"] = interface.get("type")
1711
1712 if interface.get("ip-address"):
1713 net_item["ip_address"] = interface["ip-address"]
1714
1715 if interface.get("mac-address"):
1716 net_item["mac_address"] = interface["mac-address"]
1717
1718 net_list.append(net_item)
1719
1720 if interface.get("mgmt-vnf"):
1721 extra_dict["mgmt_vnf_interface"] = iface_index
1722 elif interface.get("mgmt-interface"):
1723 extra_dict["mgmt_vdu_interface"] = iface_index
1724
1725 # cloud config
1726 cloud_config = {}
1727
1728 if existing_vdu.get("cloud-init"):
1729 if existing_vdu["cloud-init"] not in vdu2cloud_init:
1730 vdu2cloud_init[existing_vdu["cloud-init"]] = Ns._get_cloud_init(
1731 db=db,
1732 fs=fs,
1733 location=existing_vdu["cloud-init"],
1734 )
1735
1736 cloud_content_ = vdu2cloud_init[existing_vdu["cloud-init"]]
1737 cloud_config["user-data"] = Ns._parse_jinja2(
1738 cloud_init_content=cloud_content_,
1739 params=existing_vdu.get("additionalParams"),
1740 context=existing_vdu["cloud-init"],
1741 )
1742
1743 if existing_vdu.get("boot-data-drive"):
1744 cloud_config["boot-data-drive"] = existing_vdu.get("boot-data-drive")
1745
1746 ssh_keys = []
1747
1748 if existing_vdu.get("ssh-keys"):
1749 ssh_keys += existing_vdu.get("ssh-keys")
1750
1751 if existing_vdu.get("ssh-access-required"):
1752 ssh_keys.append(ro_nsr_public_key)
1753
1754 if ssh_keys:
1755 cloud_config["key-pairs"] = ssh_keys
1756
1757 disk_list = []
1758 for vol_id in vim_details.get("os-extended-volumes:volumes_attached", []):
1759 disk_list.append({"vim_id": vol_id["id"]})
1760
1761 affinity_group_list = []
1762
1763 if existing_vdu.get("affinity-or-anti-affinity-group-id"):
1764 affinity_group = {}
1765 for affinity_group_id in existing_vdu["affinity-or-anti-affinity-group-id"]:
1766 for group in db_nsr.get("affinity-or-anti-affinity-group"):
1767 if (
1768 group["id"] == affinity_group_id
1769 and group["vim_info"][target_id].get("vim_id", None) is not None
1770 ):
1771 affinity_group["affinity_group_id"] = group["vim_info"][
1772 target_id
1773 ].get("vim_id", None)
1774 affinity_group_list.append(affinity_group)
1775
1776 extra_dict["params"] = {
1777 "name": "{}-{}-{}-{}".format(
1778 db_nsr["name"][:16],
1779 vnfr["member-vnf-index-ref"][:16],
1780 existing_vdu["vdu-name"][:32],
1781 existing_vdu.get("count-index") or 0,
1782 ),
1783 "description": existing_vdu["vdu-name"],
1784 "start": True,
1785 "image_id": vim_details["image"]["id"],
1786 "flavor_id": vim_details["flavor"]["id"],
1787 "affinity_group_list": affinity_group_list,
1788 "net_list": net_list,
1789 "cloud_config": cloud_config or None,
1790 "disk_list": disk_list,
1791 "availability_zone_index": None, # TODO
1792 "availability_zone_list": None, # TODO
1793 }
1794
1795 return extra_dict
1796
1797 def calculate_diff_items(
1798 self,
1799 indata,
1800 db_nsr,
1801 db_ro_nsr,
1802 db_nsr_update,
1803 item,
1804 tasks_by_target_record_id,
1805 action_id,
1806 nsr_id,
1807 task_index,
1808 vnfr_id=None,
1809 vnfr=None,
1810 ):
1811 """Function that returns the incremental changes (creation, deletion)
1812 related to a specific item `item` to be done. This function should be
1813 called for NS instantiation, NS termination, NS update to add a new VNF
1814 or a new VLD, remove a VNF or VLD, etc.
1815 Item can be `net`, `flavor`, `image` or `vdu`.
1816 It takes a list of target items from indata (which came from the REST API)
1817 and compares with the existing items from db_ro_nsr, identifying the
1818 incremental changes to be done. During the comparison, it calls the method
1819 `process_params` (which was passed as parameter, and is particular for each
1820 `item`)
1821
1822 Args:
1823 indata (Dict[str, Any]): deployment info
1824 db_nsr: NSR record from DB
1825 db_ro_nsr (Dict[str, Any]): record from "ro_nsrs"
1826 db_nsr_update (Dict[str, Any]): NSR info to update in DB
1827 item (str): element to process (net, vdu...)
1828 tasks_by_target_record_id (Dict[str, Any]):
1829 [<target_record_id>, <task>]
1830 action_id (str): action id
1831 nsr_id (str): NSR id
1832 task_index (number): task index to add to task name
1833 vnfr_id (str): VNFR id
1834 vnfr (Dict[str, Any]): VNFR info
1835
1836 Returns:
1837 List: list with the incremental changes (deletes, creates) for each item
1838 number: current task index
1839 """
1840
1841 diff_items = []
1842 db_path = ""
1843 db_record = ""
1844 target_list = []
1845 existing_list = []
1846 process_params = None
1847 vdu2cloud_init = indata.get("cloud_init_content") or {}
1848 ro_nsr_public_key = db_ro_nsr["public_key"]
1849
1850 # According to the type of item, the path, the target_list,
1851 # the existing_list and the method to process params are set
1852 db_path = self.db_path_map[item]
1853 process_params = self.process_params_function_map[item]
1854 if item in ("net", "vdu"):
1855 # This case is specific for the NS VLD (not applied to VDU)
1856 if vnfr is None:
1857 db_record = "nsrs:{}:{}".format(nsr_id, db_path)
1858 target_list = indata.get("ns", []).get(db_path, [])
1859 existing_list = db_nsr.get(db_path, [])
1860 # This case is common for VNF VLDs and VNF VDUs
1861 else:
1862 db_record = "vnfrs:{}:{}".format(vnfr_id, db_path)
1863 target_vnf = next(
1864 (vnf for vnf in indata.get("vnf", ()) if vnf["_id"] == vnfr_id),
1865 None,
1866 )
1867 target_list = target_vnf.get(db_path, []) if target_vnf else []
1868 existing_list = vnfr.get(db_path, [])
1869 elif item in ("image", "flavor", "affinity-or-anti-affinity-group"):
1870 db_record = "nsrs:{}:{}".format(nsr_id, db_path)
1871 target_list = indata.get(item, [])
1872 existing_list = db_nsr.get(item, [])
1873 else:
1874 raise NsException("Item not supported: {}", item)
1875
1876 # ensure all the target_list elements has an "id". If not assign the index as id
1877 if target_list is None:
1878 target_list = []
1879 for target_index, tl in enumerate(target_list):
1880 if tl and not tl.get("id"):
1881 tl["id"] = str(target_index)
1882
1883 # step 1 items (networks,vdus,...) to be deleted/updated
1884 for item_index, existing_item in enumerate(existing_list):
1885 target_item = next(
1886 (t for t in target_list if t["id"] == existing_item["id"]),
1887 None,
1888 )
1889
1890 for target_vim, existing_viminfo in existing_item.get(
1891 "vim_info", {}
1892 ).items():
1893 if existing_viminfo is None:
1894 continue
1895
1896 if target_item:
1897 target_viminfo = target_item.get("vim_info", {}).get(target_vim)
1898 else:
1899 target_viminfo = None
1900
1901 if target_viminfo is None:
1902 # must be deleted
1903 self._assign_vim(target_vim)
1904 target_record_id = "{}.{}".format(db_record, existing_item["id"])
1905 item_ = item
1906
1907 if target_vim.startswith("sdn") or target_vim.startswith("wim"):
1908 # item must be sdn-net instead of net if target_vim is a sdn
1909 item_ = "sdn_net"
1910 target_record_id += ".sdn"
1911
1912 deployment_info = {
1913 "action_id": action_id,
1914 "nsr_id": nsr_id,
1915 "task_index": task_index,
1916 }
1917
1918 diff_items.append(
1919 {
1920 "deployment_info": deployment_info,
1921 "target_id": target_vim,
1922 "item": item_,
1923 "action": "DELETE",
1924 "target_record": f"{db_record}.{item_index}.vim_info.{target_vim}",
1925 "target_record_id": target_record_id,
1926 }
1927 )
1928 task_index += 1
1929
1930 # step 2 items (networks,vdus,...) to be created
1931 for target_item in target_list:
1932 item_index = -1
1933
1934 for item_index, existing_item in enumerate(existing_list):
1935 if existing_item["id"] == target_item["id"]:
1936 break
1937 else:
1938 item_index += 1
1939 db_nsr_update[db_path + ".{}".format(item_index)] = target_item
1940 existing_list.append(target_item)
1941 existing_item = None
1942
1943 for target_vim, target_viminfo in target_item.get("vim_info", {}).items():
1944 existing_viminfo = None
1945
1946 if existing_item:
1947 existing_viminfo = existing_item.get("vim_info", {}).get(target_vim)
1948
1949 if existing_viminfo is not None:
1950 continue
1951
1952 target_record_id = "{}.{}".format(db_record, target_item["id"])
1953 item_ = item
1954
1955 if target_vim.startswith("sdn") or target_vim.startswith("wim"):
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 kwargs = {}
1961 self.logger.debug(
1962 "ns.calculate_diff_items target_item={}".format(target_item)
1963 )
1964 if process_params == Ns._process_flavor_params:
1965 kwargs.update(
1966 {
1967 "db": self.db,
1968 }
1969 )
1970 self.logger.debug(
1971 "calculate_diff_items for flavor kwargs={}".format(kwargs)
1972 )
1973
1974 if process_params == Ns._process_vdu_params:
1975 self.logger.debug("calculate_diff_items self.fs={}".format(self.fs))
1976 kwargs.update(
1977 {
1978 "vnfr_id": vnfr_id,
1979 "nsr_id": nsr_id,
1980 "vnfr": vnfr,
1981 "vdu2cloud_init": vdu2cloud_init,
1982 "tasks_by_target_record_id": tasks_by_target_record_id,
1983 "logger": self.logger,
1984 "db": self.db,
1985 "fs": self.fs,
1986 "ro_nsr_public_key": ro_nsr_public_key,
1987 }
1988 )
1989 self.logger.debug("calculate_diff_items kwargs={}".format(kwargs))
1990
1991 extra_dict = process_params(
1992 target_item,
1993 indata,
1994 target_viminfo,
1995 target_record_id,
1996 **kwargs,
1997 )
1998 self._assign_vim(target_vim)
1999
2000 deployment_info = {
2001 "action_id": action_id,
2002 "nsr_id": nsr_id,
2003 "task_index": task_index,
2004 }
2005
2006 new_item = {
2007 "deployment_info": deployment_info,
2008 "target_id": target_vim,
2009 "item": item_,
2010 "action": "CREATE",
2011 "target_record": f"{db_record}.{item_index}.vim_info.{target_vim}",
2012 "target_record_id": target_record_id,
2013 "extra_dict": extra_dict,
2014 "common_id": target_item.get("common_id", None),
2015 }
2016 diff_items.append(new_item)
2017 tasks_by_target_record_id[target_record_id] = new_item
2018 task_index += 1
2019
2020 db_nsr_update[db_path + ".{}".format(item_index)] = target_item
2021
2022 return diff_items, task_index
2023
2024 def calculate_all_differences_to_deploy(
2025 self,
2026 indata,
2027 nsr_id,
2028 db_nsr,
2029 db_vnfrs,
2030 db_ro_nsr,
2031 db_nsr_update,
2032 db_vnfrs_update,
2033 action_id,
2034 tasks_by_target_record_id,
2035 ):
2036 """This method calculates the ordered list of items (`changes_list`)
2037 to be created and deleted.
2038
2039 Args:
2040 indata (Dict[str, Any]): deployment info
2041 nsr_id (str): NSR id
2042 db_nsr: NSR record from DB
2043 db_vnfrs: VNFRS record from DB
2044 db_ro_nsr (Dict[str, Any]): record from "ro_nsrs"
2045 db_nsr_update (Dict[str, Any]): NSR info to update in DB
2046 db_vnfrs_update (Dict[str, Any]): VNFRS info to update in DB
2047 action_id (str): action id
2048 tasks_by_target_record_id (Dict[str, Any]):
2049 [<target_record_id>, <task>]
2050
2051 Returns:
2052 List: ordered list of items to be created and deleted.
2053 """
2054
2055 task_index = 0
2056 # set list with diffs:
2057 changes_list = []
2058
2059 # NS vld, image and flavor
2060 for item in ["net", "image", "flavor", "affinity-or-anti-affinity-group"]:
2061 self.logger.debug("process NS={} {}".format(nsr_id, item))
2062 diff_items, task_index = self.calculate_diff_items(
2063 indata=indata,
2064 db_nsr=db_nsr,
2065 db_ro_nsr=db_ro_nsr,
2066 db_nsr_update=db_nsr_update,
2067 item=item,
2068 tasks_by_target_record_id=tasks_by_target_record_id,
2069 action_id=action_id,
2070 nsr_id=nsr_id,
2071 task_index=task_index,
2072 vnfr_id=None,
2073 )
2074 changes_list += diff_items
2075
2076 # VNF vlds and vdus
2077 for vnfr_id, vnfr in db_vnfrs.items():
2078 # vnfr_id need to be set as global variable for among others nested method _process_vdu_params
2079 for item in ["net", "vdu"]:
2080 self.logger.debug("process VNF={} {}".format(vnfr_id, item))
2081 diff_items, task_index = self.calculate_diff_items(
2082 indata=indata,
2083 db_nsr=db_nsr,
2084 db_ro_nsr=db_ro_nsr,
2085 db_nsr_update=db_vnfrs_update[vnfr["_id"]],
2086 item=item,
2087 tasks_by_target_record_id=tasks_by_target_record_id,
2088 action_id=action_id,
2089 nsr_id=nsr_id,
2090 task_index=task_index,
2091 vnfr_id=vnfr_id,
2092 vnfr=vnfr,
2093 )
2094 changes_list += diff_items
2095
2096 return changes_list
2097
2098 def define_all_tasks(
2099 self,
2100 changes_list,
2101 db_new_tasks,
2102 tasks_by_target_record_id,
2103 ):
2104 """Function to create all the task structures obtanied from
2105 the method calculate_all_differences_to_deploy
2106
2107 Args:
2108 changes_list (List): ordered list of items to be created or deleted
2109 db_new_tasks (List): tasks list to be created
2110 action_id (str): action id
2111 tasks_by_target_record_id (Dict[str, Any]):
2112 [<target_record_id>, <task>]
2113
2114 """
2115
2116 for change in changes_list:
2117 task = Ns._create_task(
2118 deployment_info=change["deployment_info"],
2119 target_id=change["target_id"],
2120 item=change["item"],
2121 action=change["action"],
2122 target_record=change["target_record"],
2123 target_record_id=change["target_record_id"],
2124 extra_dict=change.get("extra_dict", None),
2125 )
2126
2127 self.logger.debug("ns.define_all_tasks task={}".format(task))
2128 tasks_by_target_record_id[change["target_record_id"]] = task
2129 db_new_tasks.append(task)
2130
2131 if change.get("common_id"):
2132 task["common_id"] = change["common_id"]
2133
2134 def upload_all_tasks(
2135 self,
2136 db_new_tasks,
2137 now,
2138 ):
2139 """Function to save all tasks in the common DB
2140
2141 Args:
2142 db_new_tasks (List): tasks list to be created
2143 now (time): current time
2144
2145 """
2146
2147 nb_ro_tasks = 0 # for logging
2148
2149 for db_task in db_new_tasks:
2150 target_id = db_task.pop("target_id")
2151 common_id = db_task.get("common_id")
2152
2153 # Do not chek tasks with vim_status DELETED
2154 # because in manual heealing there are two tasks for the same vdur:
2155 # one with vim_status deleted and the other one with the actual VM status.
2156
2157 if common_id:
2158 if self.db.set_one(
2159 "ro_tasks",
2160 q_filter={
2161 "target_id": target_id,
2162 "tasks.common_id": common_id,
2163 "vim_info.vim_status.ne": "DELETED",
2164 },
2165 update_dict={"to_check_at": now, "modified_at": now},
2166 push={"tasks": db_task},
2167 fail_on_empty=False,
2168 ):
2169 continue
2170
2171 if not self.db.set_one(
2172 "ro_tasks",
2173 q_filter={
2174 "target_id": target_id,
2175 "tasks.target_record": db_task["target_record"],
2176 "vim_info.vim_status.ne": "DELETED",
2177 },
2178 update_dict={"to_check_at": now, "modified_at": now},
2179 push={"tasks": db_task},
2180 fail_on_empty=False,
2181 ):
2182 # Create a ro_task
2183 self.logger.debug("Updating database, Creating ro_tasks")
2184 db_ro_task = Ns._create_ro_task(target_id, db_task)
2185 nb_ro_tasks += 1
2186 self.db.create("ro_tasks", db_ro_task)
2187
2188 self.logger.debug(
2189 "Created {} ro_tasks; {} tasks - db_new_tasks={}".format(
2190 nb_ro_tasks, len(db_new_tasks), db_new_tasks
2191 )
2192 )
2193
2194 def upload_recreate_tasks(
2195 self,
2196 db_new_tasks,
2197 now,
2198 ):
2199 """Function to save recreate tasks in the common DB
2200
2201 Args:
2202 db_new_tasks (List): tasks list to be created
2203 now (time): current time
2204
2205 """
2206
2207 nb_ro_tasks = 0 # for logging
2208
2209 for db_task in db_new_tasks:
2210 target_id = db_task.pop("target_id")
2211 self.logger.debug("target_id={} db_task={}".format(target_id, db_task))
2212
2213 action = db_task.get("action", None)
2214
2215 # Create a ro_task
2216 self.logger.debug("Updating database, Creating ro_tasks")
2217 db_ro_task = Ns._create_ro_task(target_id, db_task)
2218
2219 # If DELETE task: the associated created items should be removed
2220 # (except persistent volumes):
2221 if action == "DELETE":
2222 db_ro_task["vim_info"]["created"] = True
2223 db_ro_task["vim_info"]["created_items"] = db_task.get(
2224 "created_items", {}
2225 )
2226 db_ro_task["vim_info"]["volumes_to_hold"] = db_task.get(
2227 "volumes_to_hold", []
2228 )
2229 db_ro_task["vim_info"]["vim_id"] = db_task.get("vim_id", None)
2230
2231 nb_ro_tasks += 1
2232 self.logger.debug("upload_all_tasks db_ro_task={}".format(db_ro_task))
2233 self.db.create("ro_tasks", db_ro_task)
2234
2235 self.logger.debug(
2236 "Created {} ro_tasks; {} tasks - db_new_tasks={}".format(
2237 nb_ro_tasks, len(db_new_tasks), db_new_tasks
2238 )
2239 )
2240
2241 def _prepare_created_items_for_healing(
2242 self,
2243 nsr_id,
2244 target_record,
2245 ):
2246 created_items = {}
2247 # Get created_items from ro_task
2248 ro_tasks = self.db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id})
2249 for ro_task in ro_tasks:
2250 for task in ro_task["tasks"]:
2251 if (
2252 task["target_record"] == target_record
2253 and task["action"] == "CREATE"
2254 and ro_task["vim_info"]["created_items"]
2255 ):
2256 created_items = ro_task["vim_info"]["created_items"]
2257 break
2258
2259 return created_items
2260
2261 def _prepare_persistent_volumes_for_healing(
2262 self,
2263 target_id,
2264 existing_vdu,
2265 ):
2266 # The associated volumes of the VM shouldn't be removed
2267 volumes_list = []
2268 vim_details = {}
2269 vim_details_text = existing_vdu["vim_info"][target_id].get("vim_details", None)
2270 if vim_details_text:
2271 vim_details = yaml.safe_load(f"{vim_details_text}")
2272
2273 for vol_id in vim_details.get("os-extended-volumes:volumes_attached", []):
2274 volumes_list.append(vol_id["id"])
2275
2276 return volumes_list
2277
2278 def prepare_changes_to_recreate(
2279 self,
2280 indata,
2281 nsr_id,
2282 db_nsr,
2283 db_vnfrs,
2284 db_ro_nsr,
2285 action_id,
2286 tasks_by_target_record_id,
2287 ):
2288 """This method will obtain an ordered list of items (`changes_list`)
2289 to be created and deleted to meet the recreate request.
2290 """
2291
2292 self.logger.debug(
2293 "ns.prepare_changes_to_recreate nsr_id={} indata={}".format(nsr_id, indata)
2294 )
2295
2296 task_index = 0
2297 # set list with diffs:
2298 changes_list = []
2299 db_path = self.db_path_map["vdu"]
2300 target_list = indata.get("healVnfData", {})
2301 vdu2cloud_init = indata.get("cloud_init_content") or {}
2302 ro_nsr_public_key = db_ro_nsr["public_key"]
2303
2304 # Check each VNF of the target
2305 for target_vnf in target_list:
2306 # Find this VNF in the list from DB
2307 vnfr_id = target_vnf.get("vnfInstanceId", None)
2308 if vnfr_id:
2309 existing_vnf = db_vnfrs.get(vnfr_id)
2310 db_record = "vnfrs:{}:{}".format(vnfr_id, db_path)
2311 # vim_account_id = existing_vnf.get("vim-account-id", "")
2312
2313 # Check each VDU of this VNF
2314 for target_vdu in target_vnf["additionalParams"].get("vdu", None):
2315 vdu_name = target_vdu.get("vdu-id", None)
2316 # For multi instance VDU count-index is mandatory
2317 # For single session VDU count-indes is 0
2318 count_index = target_vdu.get("count-index", 0)
2319 item_index = 0
2320 existing_instance = None
2321 for instance in existing_vnf.get("vdur", None):
2322 if (
2323 instance["vdu-name"] == vdu_name
2324 and instance["count-index"] == count_index
2325 ):
2326 existing_instance = instance
2327 break
2328 else:
2329 item_index += 1
2330
2331 target_record_id = "{}.{}".format(db_record, existing_instance["id"])
2332
2333 # The target VIM is the one already existing in DB to recreate
2334 for target_vim, target_viminfo in existing_instance.get(
2335 "vim_info", {}
2336 ).items():
2337 # step 1 vdu to be deleted
2338 self._assign_vim(target_vim)
2339 deployment_info = {
2340 "action_id": action_id,
2341 "nsr_id": nsr_id,
2342 "task_index": task_index,
2343 }
2344
2345 target_record = f"{db_record}.{item_index}.vim_info.{target_vim}"
2346 created_items = self._prepare_created_items_for_healing(
2347 nsr_id, target_record
2348 )
2349
2350 volumes_to_hold = self._prepare_persistent_volumes_for_healing(
2351 target_vim, existing_instance
2352 )
2353
2354 # Specific extra params for recreate tasks:
2355 extra_dict = {
2356 "created_items": created_items,
2357 "vim_id": existing_instance["vim-id"],
2358 "volumes_to_hold": volumes_to_hold,
2359 }
2360
2361 changes_list.append(
2362 {
2363 "deployment_info": deployment_info,
2364 "target_id": target_vim,
2365 "item": "vdu",
2366 "action": "DELETE",
2367 "target_record": target_record,
2368 "target_record_id": target_record_id,
2369 "extra_dict": extra_dict,
2370 }
2371 )
2372 delete_task_id = f"{action_id}:{task_index}"
2373 task_index += 1
2374
2375 # step 2 vdu to be created
2376 kwargs = {}
2377 kwargs.update(
2378 {
2379 "vnfr_id": vnfr_id,
2380 "nsr_id": nsr_id,
2381 "vnfr": existing_vnf,
2382 "vdu2cloud_init": vdu2cloud_init,
2383 "tasks_by_target_record_id": tasks_by_target_record_id,
2384 "logger": self.logger,
2385 "db": self.db,
2386 "fs": self.fs,
2387 "ro_nsr_public_key": ro_nsr_public_key,
2388 }
2389 )
2390
2391 extra_dict = self._process_recreate_vdu_params(
2392 existing_instance,
2393 db_nsr,
2394 target_viminfo,
2395 target_record_id,
2396 target_vim,
2397 **kwargs,
2398 )
2399
2400 # The CREATE task depens on the DELETE task
2401 extra_dict["depends_on"] = [delete_task_id]
2402
2403 # Add volumes created from created_items if any
2404 # Ports should be deleted with delete task and automatically created with create task
2405 volumes = {}
2406 for k, v in created_items.items():
2407 try:
2408 k_item, _, k_id = k.partition(":")
2409 if k_item == "volume":
2410 volumes[k] = v
2411 except Exception as e:
2412 self.logger.error(
2413 "Error evaluating created item {}: {}".format(k, e)
2414 )
2415 extra_dict["previous_created_volumes"] = volumes
2416
2417 deployment_info = {
2418 "action_id": action_id,
2419 "nsr_id": nsr_id,
2420 "task_index": task_index,
2421 }
2422 self._assign_vim(target_vim)
2423
2424 new_item = {
2425 "deployment_info": deployment_info,
2426 "target_id": target_vim,
2427 "item": "vdu",
2428 "action": "CREATE",
2429 "target_record": target_record,
2430 "target_record_id": target_record_id,
2431 "extra_dict": extra_dict,
2432 }
2433 changes_list.append(new_item)
2434 tasks_by_target_record_id[target_record_id] = new_item
2435 task_index += 1
2436
2437 return changes_list
2438
2439 def recreate(self, session, indata, version, nsr_id, *args, **kwargs):
2440 self.logger.debug("ns.recreate nsr_id={} indata={}".format(nsr_id, indata))
2441 # TODO: validate_input(indata, recreate_schema)
2442 action_id = indata.get("action_id", str(uuid4()))
2443 # get current deployment
2444 db_vnfrs = {} # vnf's info indexed by _id
2445 step = ""
2446 logging_text = "Recreate nsr_id={} action_id={} indata={}".format(
2447 nsr_id, action_id, indata
2448 )
2449 self.logger.debug(logging_text + "Enter")
2450
2451 try:
2452 step = "Getting ns and vnfr record from db"
2453 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
2454 db_new_tasks = []
2455 tasks_by_target_record_id = {}
2456 # read from db: vnf's of this ns
2457 step = "Getting vnfrs from db"
2458 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
2459 self.logger.debug("ns.recreate: db_vnfrs_list={}".format(db_vnfrs_list))
2460
2461 if not db_vnfrs_list:
2462 raise NsException("Cannot obtain associated VNF for ns")
2463
2464 for vnfr in db_vnfrs_list:
2465 db_vnfrs[vnfr["_id"]] = vnfr
2466
2467 now = time()
2468 db_ro_nsr = self.db.get_one("ro_nsrs", {"_id": nsr_id}, fail_on_empty=False)
2469 self.logger.debug("ns.recreate: db_ro_nsr={}".format(db_ro_nsr))
2470
2471 if not db_ro_nsr:
2472 db_ro_nsr = self._create_db_ro_nsrs(nsr_id, now)
2473
2474 with self.write_lock:
2475 # NS
2476 step = "process NS elements"
2477 changes_list = self.prepare_changes_to_recreate(
2478 indata=indata,
2479 nsr_id=nsr_id,
2480 db_nsr=db_nsr,
2481 db_vnfrs=db_vnfrs,
2482 db_ro_nsr=db_ro_nsr,
2483 action_id=action_id,
2484 tasks_by_target_record_id=tasks_by_target_record_id,
2485 )
2486
2487 self.define_all_tasks(
2488 changes_list=changes_list,
2489 db_new_tasks=db_new_tasks,
2490 tasks_by_target_record_id=tasks_by_target_record_id,
2491 )
2492
2493 # Delete all ro_tasks registered for the targets vdurs (target_record)
2494 # If task of type CREATE exist then vim will try to get info form deleted VMs.
2495 # So remove all task related to target record.
2496 ro_tasks = self.db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id})
2497 for change in changes_list:
2498 for ro_task in ro_tasks:
2499 for task in ro_task["tasks"]:
2500 if task["target_record"] == change["target_record"]:
2501 self.db.del_one(
2502 "ro_tasks",
2503 q_filter={
2504 "_id": ro_task["_id"],
2505 "modified_at": ro_task["modified_at"],
2506 },
2507 fail_on_empty=False,
2508 )
2509
2510 step = "Updating database, Appending tasks to ro_tasks"
2511 self.upload_recreate_tasks(
2512 db_new_tasks=db_new_tasks,
2513 now=now,
2514 )
2515
2516 self.logger.debug(
2517 logging_text + "Exit. Created {} tasks".format(len(db_new_tasks))
2518 )
2519
2520 return (
2521 {"status": "ok", "nsr_id": nsr_id, "action_id": action_id},
2522 action_id,
2523 True,
2524 )
2525 except Exception as e:
2526 if isinstance(e, (DbException, NsException)):
2527 self.logger.error(
2528 logging_text + "Exit Exception while '{}': {}".format(step, e)
2529 )
2530 else:
2531 e = traceback_format_exc()
2532 self.logger.critical(
2533 logging_text + "Exit Exception while '{}': {}".format(step, e),
2534 exc_info=True,
2535 )
2536
2537 raise NsException(e)
2538
2539 def deploy(self, session, indata, version, nsr_id, *args, **kwargs):
2540 self.logger.debug("ns.deploy nsr_id={} indata={}".format(nsr_id, indata))
2541 validate_input(indata, deploy_schema)
2542 action_id = indata.get("action_id", str(uuid4()))
2543 task_index = 0
2544 # get current deployment
2545 db_nsr_update = {} # update operation on nsrs
2546 db_vnfrs_update = {}
2547 db_vnfrs = {} # vnf's info indexed by _id
2548 step = ""
2549 logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id)
2550 self.logger.debug(logging_text + "Enter")
2551
2552 try:
2553 step = "Getting ns and vnfr record from db"
2554 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
2555 self.logger.debug("ns.deploy: db_nsr={}".format(db_nsr))
2556 db_new_tasks = []
2557 tasks_by_target_record_id = {}
2558 # read from db: vnf's of this ns
2559 step = "Getting vnfrs from db"
2560 db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
2561
2562 if not db_vnfrs_list:
2563 raise NsException("Cannot obtain associated VNF for ns")
2564
2565 for vnfr in db_vnfrs_list:
2566 db_vnfrs[vnfr["_id"]] = vnfr
2567 db_vnfrs_update[vnfr["_id"]] = {}
2568 self.logger.debug("ns.deploy db_vnfrs={}".format(db_vnfrs))
2569
2570 now = time()
2571 db_ro_nsr = self.db.get_one("ro_nsrs", {"_id": nsr_id}, fail_on_empty=False)
2572
2573 if not db_ro_nsr:
2574 db_ro_nsr = self._create_db_ro_nsrs(nsr_id, now)
2575
2576 # check that action_id is not in the list of actions. Suffixed with :index
2577 if action_id in db_ro_nsr["actions"]:
2578 index = 1
2579
2580 while True:
2581 new_action_id = "{}:{}".format(action_id, index)
2582
2583 if new_action_id not in db_ro_nsr["actions"]:
2584 action_id = new_action_id
2585 self.logger.debug(
2586 logging_text
2587 + "Changing action_id in use to {}".format(action_id)
2588 )
2589 break
2590
2591 index += 1
2592
2593 def _process_action(indata):
2594 nonlocal db_new_tasks
2595 nonlocal action_id
2596 nonlocal nsr_id
2597 nonlocal task_index
2598 nonlocal db_vnfrs
2599 nonlocal db_ro_nsr
2600
2601 if indata["action"]["action"] == "inject_ssh_key":
2602 key = indata["action"].get("key")
2603 user = indata["action"].get("user")
2604 password = indata["action"].get("password")
2605
2606 for vnf in indata.get("vnf", ()):
2607 if vnf["_id"] not in db_vnfrs:
2608 raise NsException("Invalid vnf={}".format(vnf["_id"]))
2609
2610 db_vnfr = db_vnfrs[vnf["_id"]]
2611
2612 for target_vdu in vnf.get("vdur", ()):
2613 vdu_index, vdur = next(
2614 (
2615 i_v
2616 for i_v in enumerate(db_vnfr["vdur"])
2617 if i_v[1]["id"] == target_vdu["id"]
2618 ),
2619 (None, None),
2620 )
2621
2622 if not vdur:
2623 raise NsException(
2624 "Invalid vdu vnf={}.{}".format(
2625 vnf["_id"], target_vdu["id"]
2626 )
2627 )
2628
2629 target_vim, vim_info = next(
2630 k_v for k_v in vdur["vim_info"].items()
2631 )
2632 self._assign_vim(target_vim)
2633 target_record = "vnfrs:{}:vdur.{}.ssh_keys".format(
2634 vnf["_id"], vdu_index
2635 )
2636 extra_dict = {
2637 "depends_on": [
2638 "vnfrs:{}:vdur.{}".format(vnf["_id"], vdur["id"])
2639 ],
2640 "params": {
2641 "ip_address": vdur.get("ip-address"),
2642 "user": user,
2643 "key": key,
2644 "password": password,
2645 "private_key": db_ro_nsr["private_key"],
2646 "salt": db_ro_nsr["_id"],
2647 "schema_version": db_ro_nsr["_admin"][
2648 "schema_version"
2649 ],
2650 },
2651 }
2652
2653 deployment_info = {
2654 "action_id": action_id,
2655 "nsr_id": nsr_id,
2656 "task_index": task_index,
2657 }
2658
2659 task = Ns._create_task(
2660 deployment_info=deployment_info,
2661 target_id=target_vim,
2662 item="vdu",
2663 action="EXEC",
2664 target_record=target_record,
2665 target_record_id=None,
2666 extra_dict=extra_dict,
2667 )
2668
2669 task_index = deployment_info.get("task_index")
2670
2671 db_new_tasks.append(task)
2672
2673 with self.write_lock:
2674 if indata.get("action"):
2675 _process_action(indata)
2676 else:
2677 # compute network differences
2678 # NS
2679 step = "process NS elements"
2680 changes_list = self.calculate_all_differences_to_deploy(
2681 indata=indata,
2682 nsr_id=nsr_id,
2683 db_nsr=db_nsr,
2684 db_vnfrs=db_vnfrs,
2685 db_ro_nsr=db_ro_nsr,
2686 db_nsr_update=db_nsr_update,
2687 db_vnfrs_update=db_vnfrs_update,
2688 action_id=action_id,
2689 tasks_by_target_record_id=tasks_by_target_record_id,
2690 )
2691 self.define_all_tasks(
2692 changes_list=changes_list,
2693 db_new_tasks=db_new_tasks,
2694 tasks_by_target_record_id=tasks_by_target_record_id,
2695 )
2696
2697 step = "Updating database, Appending tasks to ro_tasks"
2698 self.upload_all_tasks(
2699 db_new_tasks=db_new_tasks,
2700 now=now,
2701 )
2702
2703 step = "Updating database, nsrs"
2704 if db_nsr_update:
2705 self.db.set_one("nsrs", {"_id": nsr_id}, db_nsr_update)
2706
2707 for vnfr_id, db_vnfr_update in db_vnfrs_update.items():
2708 if db_vnfr_update:
2709 step = "Updating database, vnfrs={}".format(vnfr_id)
2710 self.db.set_one("vnfrs", {"_id": vnfr_id}, db_vnfr_update)
2711
2712 self.logger.debug(
2713 logging_text + "Exit. Created {} tasks".format(len(db_new_tasks))
2714 )
2715
2716 return (
2717 {"status": "ok", "nsr_id": nsr_id, "action_id": action_id},
2718 action_id,
2719 True,
2720 )
2721 except Exception as e:
2722 if isinstance(e, (DbException, NsException)):
2723 self.logger.error(
2724 logging_text + "Exit Exception while '{}': {}".format(step, e)
2725 )
2726 else:
2727 e = traceback_format_exc()
2728 self.logger.critical(
2729 logging_text + "Exit Exception while '{}': {}".format(step, e),
2730 exc_info=True,
2731 )
2732
2733 raise NsException(e)
2734
2735 def delete(self, session, indata, version, nsr_id, *args, **kwargs):
2736 self.logger.debug("ns.delete version={} nsr_id={}".format(version, nsr_id))
2737 # self.db.del_list({"_id": ro_task["_id"], "tasks.nsr_id.ne": nsr_id})
2738
2739 with self.write_lock:
2740 try:
2741 NsWorker.delete_db_tasks(self.db, nsr_id, None)
2742 except NsWorkerException as e:
2743 raise NsException(e)
2744
2745 return None, None, True
2746
2747 def status(self, session, indata, version, nsr_id, action_id, *args, **kwargs):
2748 self.logger.debug(
2749 "ns.status version={} nsr_id={}, action_id={} indata={}".format(
2750 version, nsr_id, action_id, indata
2751 )
2752 )
2753 task_list = []
2754 done = 0
2755 total = 0
2756 ro_tasks = self.db.get_list("ro_tasks", {"tasks.action_id": action_id})
2757 global_status = "DONE"
2758 details = []
2759
2760 for ro_task in ro_tasks:
2761 for task in ro_task["tasks"]:
2762 if task and task["action_id"] == action_id:
2763 task_list.append(task)
2764 total += 1
2765
2766 if task["status"] == "FAILED":
2767 global_status = "FAILED"
2768 error_text = "Error at {} {}: {}".format(
2769 task["action"].lower(),
2770 task["item"],
2771 ro_task["vim_info"].get("vim_message") or "unknown",
2772 )
2773 details.append(error_text)
2774 elif task["status"] in ("SCHEDULED", "BUILD"):
2775 if global_status != "FAILED":
2776 global_status = "BUILD"
2777 else:
2778 done += 1
2779
2780 return_data = {
2781 "status": global_status,
2782 "details": ". ".join(details)
2783 if details
2784 else "progress {}/{}".format(done, total),
2785 "nsr_id": nsr_id,
2786 "action_id": action_id,
2787 "tasks": task_list,
2788 }
2789
2790 return return_data, None, True
2791
2792 def recreate_status(
2793 self, session, indata, version, nsr_id, action_id, *args, **kwargs
2794 ):
2795 return self.status(session, indata, version, nsr_id, action_id, *args, **kwargs)
2796
2797 def cancel(self, session, indata, version, nsr_id, action_id, *args, **kwargs):
2798 print(
2799 "ns.cancel session={} indata={} version={} nsr_id={}, action_id={}".format(
2800 session, indata, version, nsr_id, action_id
2801 )
2802 )
2803
2804 return None, None, True
2805
2806 def rebuild_start_stop_task(
2807 self,
2808 vdu_id,
2809 vnf_id,
2810 vdu_index,
2811 action_id,
2812 nsr_id,
2813 task_index,
2814 target_vim,
2815 extra_dict,
2816 ):
2817 self._assign_vim(target_vim)
2818 target_record = "vnfrs:{}:vdur.{}".format(vnf_id, vdu_index)
2819 target_record_id = "vnfrs:{}:vdur.{}".format(vnf_id, vdu_id)
2820 deployment_info = {
2821 "action_id": action_id,
2822 "nsr_id": nsr_id,
2823 "task_index": task_index,
2824 }
2825
2826 task = Ns._create_task(
2827 deployment_info=deployment_info,
2828 target_id=target_vim,
2829 item="update",
2830 action="EXEC",
2831 target_record=target_record,
2832 target_record_id=target_record_id,
2833 extra_dict=extra_dict,
2834 )
2835 return task
2836
2837 def rebuild_start_stop(
2838 self, session, action_dict, version, nsr_id, *args, **kwargs
2839 ):
2840 task_index = 0
2841 extra_dict = {}
2842 now = time()
2843 action_id = action_dict.get("action_id", str(uuid4()))
2844 step = ""
2845 logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id)
2846 self.logger.debug(logging_text + "Enter")
2847
2848 action = list(action_dict.keys())[0]
2849 task_dict = action_dict.get(action)
2850 vim_vm_id = action_dict.get(action).get("vim_vm_id")
2851
2852 if action_dict.get("stop"):
2853 action = "shutoff"
2854 db_new_tasks = []
2855 try:
2856 step = "lock the operation & do task creation"
2857 with self.write_lock:
2858 extra_dict["params"] = {
2859 "vim_vm_id": vim_vm_id,
2860 "action": action,
2861 }
2862 task = self.rebuild_start_stop_task(
2863 task_dict["vdu_id"],
2864 task_dict["vnf_id"],
2865 task_dict["vdu_index"],
2866 action_id,
2867 nsr_id,
2868 task_index,
2869 task_dict["target_vim"],
2870 extra_dict,
2871 )
2872 db_new_tasks.append(task)
2873 step = "upload Task to db"
2874 self.upload_all_tasks(
2875 db_new_tasks=db_new_tasks,
2876 now=now,
2877 )
2878 self.logger.debug(
2879 logging_text + "Exit. Created {} tasks".format(len(db_new_tasks))
2880 )
2881 return (
2882 {"status": "ok", "nsr_id": nsr_id, "action_id": action_id},
2883 action_id,
2884 True,
2885 )
2886 except Exception as e:
2887 if isinstance(e, (DbException, NsException)):
2888 self.logger.error(
2889 logging_text + "Exit Exception while '{}': {}".format(step, e)
2890 )
2891 else:
2892 e = traceback_format_exc()
2893 self.logger.critical(
2894 logging_text + "Exit Exception while '{}': {}".format(step, e),
2895 exc_info=True,
2896 )
2897 raise NsException(e)
2898
2899 def get_deploy(self, session, indata, version, nsr_id, action_id, *args, **kwargs):
2900 nsrs = self.db.get_list("nsrs", {})
2901 return_data = []
2902
2903 for ns in nsrs:
2904 return_data.append({"_id": ns["_id"], "name": ns["name"]})
2905
2906 return return_data, None, True
2907
2908 def get_actions(self, session, indata, version, nsr_id, action_id, *args, **kwargs):
2909 ro_tasks = self.db.get_list("ro_tasks", {"tasks.nsr_id": nsr_id})
2910 return_data = []
2911
2912 for ro_task in ro_tasks:
2913 for task in ro_task["tasks"]:
2914 if task["action_id"] not in return_data:
2915 return_data.append(task["action_id"])
2916
2917 return return_data, None, True
2918
2919 def migrate_task(
2920 self, vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict
2921 ):
2922 target_vim, vim_info = next(k_v for k_v in vdu["vim_info"].items())
2923 self._assign_vim(target_vim)
2924 target_record = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu_index)
2925 target_record_id = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu["id"])
2926 deployment_info = {
2927 "action_id": action_id,
2928 "nsr_id": nsr_id,
2929 "task_index": task_index,
2930 }
2931
2932 task = Ns._create_task(
2933 deployment_info=deployment_info,
2934 target_id=target_vim,
2935 item="migrate",
2936 action="EXEC",
2937 target_record=target_record,
2938 target_record_id=target_record_id,
2939 extra_dict=extra_dict,
2940 )
2941
2942 return task
2943
2944 def migrate(self, session, indata, version, nsr_id, *args, **kwargs):
2945 task_index = 0
2946 extra_dict = {}
2947 now = time()
2948 action_id = indata.get("action_id", str(uuid4()))
2949 step = ""
2950 logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id)
2951 self.logger.debug(logging_text + "Enter")
2952 try:
2953 vnf_instance_id = indata["vnfInstanceId"]
2954 step = "Getting vnfrs from db"
2955 db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_instance_id})
2956 vdu = indata.get("vdu")
2957 migrateToHost = indata.get("migrateToHost")
2958 db_new_tasks = []
2959
2960 with self.write_lock:
2961 if vdu is not None:
2962 vdu_id = indata["vdu"]["vduId"]
2963 vdu_count_index = indata["vdu"].get("vduCountIndex", 0)
2964 for vdu_index, vdu in enumerate(db_vnfr["vdur"]):
2965 if (
2966 vdu["vdu-id-ref"] == vdu_id
2967 and vdu["count-index"] == vdu_count_index
2968 ):
2969 extra_dict["params"] = {
2970 "vim_vm_id": vdu["vim-id"],
2971 "migrate_host": migrateToHost,
2972 "vdu_vim_info": vdu["vim_info"],
2973 }
2974 step = "Creating migration task for vdu:{}".format(vdu)
2975 task = self.migrate_task(
2976 vdu,
2977 db_vnfr,
2978 vdu_index,
2979 action_id,
2980 nsr_id,
2981 task_index,
2982 extra_dict,
2983 )
2984 db_new_tasks.append(task)
2985 task_index += 1
2986 break
2987 else:
2988 for vdu_index, vdu in enumerate(db_vnfr["vdur"]):
2989 extra_dict["params"] = {
2990 "vim_vm_id": vdu["vim-id"],
2991 "migrate_host": migrateToHost,
2992 "vdu_vim_info": vdu["vim_info"],
2993 }
2994 step = "Creating migration task for vdu:{}".format(vdu)
2995 task = self.migrate_task(
2996 vdu,
2997 db_vnfr,
2998 vdu_index,
2999 action_id,
3000 nsr_id,
3001 task_index,
3002 extra_dict,
3003 )
3004 db_new_tasks.append(task)
3005 task_index += 1
3006
3007 self.upload_all_tasks(
3008 db_new_tasks=db_new_tasks,
3009 now=now,
3010 )
3011
3012 self.logger.debug(
3013 logging_text + "Exit. Created {} tasks".format(len(db_new_tasks))
3014 )
3015 return (
3016 {"status": "ok", "nsr_id": nsr_id, "action_id": action_id},
3017 action_id,
3018 True,
3019 )
3020 except Exception as e:
3021 if isinstance(e, (DbException, NsException)):
3022 self.logger.error(
3023 logging_text + "Exit Exception while '{}': {}".format(step, e)
3024 )
3025 else:
3026 e = traceback_format_exc()
3027 self.logger.critical(
3028 logging_text + "Exit Exception while '{}': {}".format(step, e),
3029 exc_info=True,
3030 )
3031 raise NsException(e)
3032
3033 def verticalscale_task(
3034 self, vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict
3035 ):
3036 target_vim, vim_info = next(k_v for k_v in vdu["vim_info"].items())
3037 self._assign_vim(target_vim)
3038 target_record = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu_index)
3039 target_record_id = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu["id"])
3040 deployment_info = {
3041 "action_id": action_id,
3042 "nsr_id": nsr_id,
3043 "task_index": task_index,
3044 }
3045
3046 task = Ns._create_task(
3047 deployment_info=deployment_info,
3048 target_id=target_vim,
3049 item="verticalscale",
3050 action="EXEC",
3051 target_record=target_record,
3052 target_record_id=target_record_id,
3053 extra_dict=extra_dict,
3054 )
3055 return task
3056
3057 def verticalscale(self, session, indata, version, nsr_id, *args, **kwargs):
3058 task_index = 0
3059 extra_dict = {}
3060 now = time()
3061 action_id = indata.get("action_id", str(uuid4()))
3062 step = ""
3063 logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id)
3064 self.logger.debug(logging_text + "Enter")
3065 try:
3066 VnfFlavorData = indata.get("changeVnfFlavorData")
3067 vnf_instance_id = VnfFlavorData["vnfInstanceId"]
3068 step = "Getting vnfrs from db"
3069 db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_instance_id})
3070 vduid = VnfFlavorData["additionalParams"]["vduid"]
3071 vduCountIndex = VnfFlavorData["additionalParams"]["vduCountIndex"]
3072 virtualMemory = VnfFlavorData["additionalParams"]["virtualMemory"]
3073 numVirtualCpu = VnfFlavorData["additionalParams"]["numVirtualCpu"]
3074 sizeOfStorage = VnfFlavorData["additionalParams"]["sizeOfStorage"]
3075 flavor_dict = {
3076 "name": vduid + "-flv",
3077 "ram": virtualMemory,
3078 "vcpus": numVirtualCpu,
3079 "disk": sizeOfStorage,
3080 }
3081 db_new_tasks = []
3082 step = "Creating Tasks for vertical scaling"
3083 with self.write_lock:
3084 for vdu_index, vdu in enumerate(db_vnfr["vdur"]):
3085 if (
3086 vdu["vdu-id-ref"] == vduid
3087 and vdu["count-index"] == vduCountIndex
3088 ):
3089 extra_dict["params"] = {
3090 "vim_vm_id": vdu["vim-id"],
3091 "flavor_dict": flavor_dict,
3092 }
3093 task = self.verticalscale_task(
3094 vdu,
3095 db_vnfr,
3096 vdu_index,
3097 action_id,
3098 nsr_id,
3099 task_index,
3100 extra_dict,
3101 )
3102 db_new_tasks.append(task)
3103 task_index += 1
3104 break
3105 self.upload_all_tasks(
3106 db_new_tasks=db_new_tasks,
3107 now=now,
3108 )
3109 self.logger.debug(
3110 logging_text + "Exit. Created {} tasks".format(len(db_new_tasks))
3111 )
3112 return (
3113 {"status": "ok", "nsr_id": nsr_id, "action_id": action_id},
3114 action_id,
3115 True,
3116 )
3117 except Exception as e:
3118 if isinstance(e, (DbException, NsException)):
3119 self.logger.error(
3120 logging_text + "Exit Exception while '{}': {}".format(step, e)
3121 )
3122 else:
3123 e = traceback_format_exc()
3124 self.logger.critical(
3125 logging_text + "Exit Exception while '{}': {}".format(step, e),
3126 exc_info=True,
3127 )
3128 raise NsException(e)