Coverage for osm_nbi/instance_topics.py: 47%
1393 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-10 20:04 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-10 20:04 +0000
1# -*- coding: utf-8 -*-
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
16# import logging
17import json
18from uuid import uuid4
19from http import HTTPStatus
20from time import time
21from copy import copy, deepcopy
22from osm_nbi.validation import (
23 validate_input,
24 ValidationError,
25 ns_instantiate,
26 ns_terminate,
27 ns_action,
28 ns_scale,
29 ns_update,
30 ns_heal,
31 nsi_instantiate,
32 ns_migrate,
33 nslcmop_cancel,
34)
35from osm_nbi.base_topic import (
36 BaseTopic,
37 EngineException,
38 get_iterable,
39 deep_get,
40 increment_ip_mac,
41 update_descriptor_usage_state,
42)
43from yaml import safe_dump
44from osm_common.dbbase import DbException
45from osm_common.msgbase import MsgException
46from osm_common.fsbase import FsException
47from osm_nbi import utils
48from re import (
49 match,
50) # For checking that additional parameter names are valid Jinja2 identifiers
52__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
55class NsrTopic(BaseTopic):
56 topic = "nsrs"
57 topic_msg = "ns"
58 quota_name = "ns_instances"
59 schema_new = ns_instantiate
61 def __init__(self, db, fs, msg, auth):
62 BaseTopic.__init__(self, db, fs, msg, auth)
64 @staticmethod
65 def format_on_new(content, project_id=None, make_public=False):
66 BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public)
67 content["_admin"]["nsState"] = "NOT_INSTANTIATED"
68 return None
70 def check_conflict_on_del(self, session, _id, db_content):
71 """
72 Check that NSR is not instantiated
73 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
74 :param _id: nsr internal id
75 :param db_content: The database content of the nsr
76 :return: None or raises EngineException with the conflict
77 """
78 if session["force"]:
79 return
80 nsr = db_content
81 if nsr["_admin"].get("nsState") == "INSTANTIATED":
82 raise EngineException(
83 "nsr '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
84 "Launch 'terminate' operation first; or force deletion".format(_id),
85 http_code=HTTPStatus.CONFLICT,
86 )
88 def delete_extra(self, session, _id, db_content, not_send_msg=None):
89 """
90 Deletes associated nslcmops and vnfrs from database. Deletes associated filesystem.
91 Set usageState of pdu, vnfd, nsd
92 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
93 :param _id: server internal id
94 :param db_content: The database content of the descriptor
95 :param not_send_msg: To not send message (False) or store content (list) instead
96 :return: None if ok or raises EngineException with the problem
97 """
98 self.fs.file_delete(_id, ignore_non_exist=True)
99 self.db.del_list("nslcmops", {"nsInstanceId": _id})
100 self.db.del_list("vnfrs", {"nsr-id-ref": _id})
102 # set all used pdus as free
103 self.db.set_list(
104 "pdus",
105 {"_admin.usage.nsr_id": _id},
106 {"_admin.usageState": "NOT_IN_USE", "_admin.usage": None},
107 )
109 # Set NSD usageState
110 nsr = db_content
111 used_nsd_id = nsr.get("nsd-id")
112 if used_nsd_id:
113 # check if used by another NSR
114 nsrs_list = self.db.get_one(
115 "nsrs", {"nsd-id": used_nsd_id}, fail_on_empty=False, fail_on_more=False
116 )
117 if not nsrs_list:
118 self.db.set_one(
119 "nsds", {"_id": used_nsd_id}, {"_admin.usageState": "NOT_IN_USE"}
120 )
122 # Set NS CONFIG TEMPLATE usageState
123 if nsr.get("instantiate_params", {}).get("nsConfigTemplateId"):
124 nsconfigtemplate_id = nsr.get("instantiate_params", {}).get(
125 "nsConfigTemplateId"
126 )
127 nsconfigtemplate_list = self.db.get_one(
128 "nsrs",
129 {"instantiate_params.nsConfigTemplateId": nsconfigtemplate_id},
130 fail_on_empty=False,
131 fail_on_more=False,
132 )
133 if not nsconfigtemplate_list:
134 self.db.set_one(
135 "ns_config_template",
136 {"_id": nsconfigtemplate_id},
137 {"_admin.usageState": "NOT_IN_USE"},
138 )
140 # Set VNFD usageState
141 used_vnfd_id_list = nsr.get("vnfd-id")
142 if used_vnfd_id_list:
143 for used_vnfd_id in used_vnfd_id_list:
144 # check if used by another NSR
145 nsrs_list = self.db.get_one(
146 "nsrs",
147 {"vnfd-id": used_vnfd_id},
148 fail_on_empty=False,
149 fail_on_more=False,
150 )
151 if not nsrs_list:
152 self.db.set_one(
153 "vnfds",
154 {"_id": used_vnfd_id},
155 {"_admin.usageState": "NOT_IN_USE"},
156 )
158 # delete extra ro_nsrs used for internal RO module
159 self.db.del_one("ro_nsrs", q_filter={"_id": _id}, fail_on_empty=False)
161 @staticmethod
162 def _format_ns_request(ns_request):
163 formated_request = copy(ns_request)
164 formated_request.pop("additionalParamsForNs", None)
165 formated_request.pop("additionalParamsForVnf", None)
166 return formated_request
168 @staticmethod
169 def _format_additional_params(
170 ns_request, member_vnf_index=None, vdu_id=None, kdu_name=None, descriptor=None
171 ):
172 """
173 Get and format user additional params for NS or VNF.
174 The vdu_id and kdu_name params are mutually exclusive! If none of them are given, then the method will
175 exclusively search for the VNF/NS LCM additional params.
177 :param ns_request: User instantiation additional parameters
178 :param member_vnf_index: None for extract NS params, or member_vnf_index to extract VNF params
179 :vdu_id: VDU's ID against which we want to format the additional params
180 :kdu_name: KDU's name against which we want to format the additional params
181 :param descriptor: If not None it check that needed parameters of descriptor are supplied
182 :return: tuple with a formatted copy of additional params or None if not supplied, plus other parameters
183 """
184 additional_params = None
185 other_params = None
186 if not member_vnf_index:
187 additional_params = copy(ns_request.get("additionalParamsForNs"))
188 where_ = "additionalParamsForNs"
189 elif ns_request.get("additionalParamsForVnf"):
190 where_ = "additionalParamsForVnf[member-vnf-index={}]".format(
191 member_vnf_index
192 )
193 item = next(
194 (
195 x
196 for x in ns_request["additionalParamsForVnf"]
197 if x["member-vnf-index"] == member_vnf_index
198 ),
199 None,
200 )
201 if item:
202 if not vdu_id and not kdu_name:
203 other_params = item
204 additional_params = copy(item.get("additionalParams")) or {}
205 if vdu_id and item.get("additionalParamsForVdu"):
206 item_vdu = next(
207 (
208 x
209 for x in item["additionalParamsForVdu"]
210 if x["vdu_id"] == vdu_id
211 ),
212 None,
213 )
214 other_params = item_vdu
215 if item_vdu and item_vdu.get("additionalParams"):
216 where_ += ".additionalParamsForVdu[vdu_id={}]".format(vdu_id)
217 additional_params = item_vdu["additionalParams"]
218 if kdu_name:
219 additional_params = {}
220 if item.get("additionalParamsForKdu"):
221 item_kdu = next(
222 (
223 x
224 for x in item["additionalParamsForKdu"]
225 if x["kdu_name"] == kdu_name
226 ),
227 None,
228 )
229 other_params = item_kdu
230 if item_kdu and item_kdu.get("additionalParams"):
231 where_ += ".additionalParamsForKdu[kdu_name={}]".format(
232 kdu_name
233 )
234 additional_params = item_kdu["additionalParams"]
236 if additional_params:
237 for k, v in additional_params.items():
238 # BEGIN Check that additional parameter names are valid Jinja2 identifiers if target is not Kdu
239 if not kdu_name and not match("^[a-zA-Z_][a-zA-Z0-9_]*$", k):
240 raise EngineException(
241 "Invalid param name at {}:{}. Must contain only alphanumeric characters "
242 "and underscores, and cannot start with a digit".format(
243 where_, k
244 )
245 )
246 # END Check that additional parameter names are valid Jinja2 identifiers
247 if not isinstance(k, str):
248 raise EngineException(
249 "Invalid param at {}:{}. Only string keys are allowed".format(
250 where_, k
251 )
252 )
253 if "$" in k:
254 raise EngineException(
255 "Invalid param at {}:{}. Keys must not contain $ symbol".format(
256 where_, k
257 )
258 )
259 if isinstance(v, (dict, tuple, list)):
260 additional_params[k] = "!!yaml " + safe_dump(v)
261 if kdu_name:
262 additional_params = json.dumps(additional_params)
264 # Select the VDU ID, KDU name or NS/VNF ID, depending on the method's call intent
265 selector = vdu_id if vdu_id else kdu_name if kdu_name else descriptor.get("id")
267 if descriptor:
268 for df in descriptor.get("df", []):
269 # check that enough parameters are supplied for the initial-config-primitive
270 # TODO: check for cloud-init
271 if member_vnf_index:
272 initial_primitives = []
273 if (
274 "lcm-operations-configuration" in df
275 and "operate-vnf-op-config"
276 in df["lcm-operations-configuration"]
277 ):
278 for config in df["lcm-operations-configuration"][
279 "operate-vnf-op-config"
280 ].get("day1-2", []):
281 # Verify the target object (VNF|NS|VDU|KDU) where we need to populate
282 # the params with the additional ones given by the user
283 if config.get("id") == selector:
284 for primitive in get_iterable(
285 config.get("initial-config-primitive")
286 ):
287 initial_primitives.append(primitive)
288 else:
289 initial_primitives = deep_get(
290 descriptor, ("ns-configuration", "initial-config-primitive")
291 )
293 for initial_primitive in get_iterable(initial_primitives):
294 for param in get_iterable(initial_primitive.get("parameter")):
295 if param["value"].startswith("<") and param["value"].endswith(
296 ">"
297 ):
298 if param["value"] in (
299 "<rw_mgmt_ip>",
300 "<VDU_SCALE_INFO>",
301 "<ns_config_info>",
302 "<OSM>",
303 ):
304 continue
305 if (
306 not additional_params
307 or param["value"][1:-1] not in additional_params
308 ):
309 raise EngineException(
310 "Parameter '{}' needed for vnfd[id={}]:day1-2 configuration:"
311 "initial-config-primitive[name={}] not supplied".format(
312 param["value"],
313 descriptor["id"],
314 initial_primitive["name"],
315 )
316 )
318 return additional_params or None, other_params or None
320 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
321 """
322 Creates a new nsr into database. It also creates needed vnfrs
323 :param rollback: list to append the created items at database in case a rollback must be done
324 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
325 :param indata: params to be used for the nsr
326 :param kwargs: used to override the indata descriptor
327 :param headers: http request headers
328 :return: the _id of nsr descriptor created at database. Or an exception of type
329 EngineException, ValidationError, DbException, FsException, MsgException.
330 Note: Exceptions are not captured on purpose. They should be captured at called
331 """
332 step = "checking quotas" # first step must be defined outside try
333 try:
334 self.check_quota(session)
336 step = "validating input parameters"
337 ns_request = self._remove_envelop(indata)
338 self._update_input_with_kwargs(ns_request, kwargs)
339 ns_request = self._validate_input_new(ns_request, session["force"])
341 step = "getting nsd id='{}' from database".format(ns_request.get("nsdId"))
342 nsd = self._get_nsd_from_db(ns_request["nsdId"], session)
343 ns_k8s_namespace = self._get_ns_k8s_namespace(nsd, ns_request, session)
345 # Uploading the instantiation parameters to ns_request from ns config template
346 if ns_request.get("nsConfigTemplateId"):
347 step = "getting ns_config_template is='{}' from database".format(
348 ns_request.get("nsConfigTemplateId")
349 )
350 ns_config_template_db = self._get_nsConfigTemplate_from_db(
351 ns_request.get("nsConfigTemplateId"), session
352 )
353 ns_config_params = ns_config_template_db.get("config")
354 for key, value in ns_config_params.items():
355 if key == "vnf":
356 ns_request["vnf"] = ns_config_params.get("vnf")
357 elif key == "additionalParamsForVnf":
358 ns_request["additionalParamsForVnf"] = ns_config_params.get(
359 "additionalParamsForVnf"
360 )
361 elif key == "additionalParamsForNs":
362 ns_request["additionalParamsForNs"] = ns_config_params.get(
363 "additionalParamsForNs"
364 )
365 elif key == "vld":
366 ns_request["vld"] = ns_config_params.get("vld")
367 step = "checking ns_config_templateOperationalState"
368 self._check_ns_config_template_operational_state(
369 ns_config_template_db, ns_request
370 )
372 step = "Updating NSCONFIG TEMPLATE usageState"
373 update_descriptor_usage_state(
374 ns_config_template_db, "ns_config_template", self.db
375 )
377 elif ns_request.get("vnf"):
378 vnf_data = ns_request.get("vnf")
379 for vnf in vnf_data:
380 for vdu in vnf.get("vdu"):
381 if vdu.get("vim-flavor-name") and vdu.get("vim-flavor-id"):
382 raise EngineException(
383 "Instantiation parameters vim-flavor-name and vim-flavor-id are mutually exclusive"
384 )
386 step = "checking nsdOperationalState"
387 self._check_nsd_operational_state(nsd, ns_request)
389 step = "filling nsr from input data"
390 nsr_id = str(uuid4())
391 nsr_descriptor = self._create_nsr_descriptor_from_nsd(
392 nsd, ns_request, nsr_id, session
393 )
395 # Create VNFRs
396 needed_vnfds = {}
397 # TODO: Change for multiple df support
398 vnf_profiles = nsd.get("df", [{}])[0].get("vnf-profile", ())
399 for vnfp in vnf_profiles:
400 vnfd_id = vnfp.get("vnfd-id")
401 vnf_index = vnfp.get("id")
402 step = (
403 "getting vnfd id='{}' constituent-vnfd='{}' from database".format(
404 vnfd_id, vnf_index
405 )
406 )
407 if vnfd_id not in needed_vnfds:
408 vnfd = self._get_vnfd_from_db(vnfd_id, session)
409 if "revision" in vnfd["_admin"]:
410 vnfd["revision"] = vnfd["_admin"]["revision"]
411 vnfd.pop("_admin")
412 needed_vnfds[vnfd_id] = vnfd
413 nsr_descriptor["vnfd-id"].append(vnfd["_id"])
414 else:
415 vnfd = needed_vnfds[vnfd_id]
417 step = "filling vnfr vnfd-id='{}' constituent-vnfd='{}'".format(
418 vnfd_id, vnf_index
419 )
420 vnfr_descriptor = self._create_vnfr_descriptor_from_vnfd(
421 nsd,
422 vnfd,
423 vnfd_id,
424 vnf_index,
425 nsr_descriptor,
426 ns_request,
427 ns_k8s_namespace,
428 )
430 step = "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format(
431 vnfd_id, vnf_index
432 )
433 self._add_vnfr_to_db(vnfr_descriptor, rollback, session)
434 nsr_descriptor["constituent-vnfr-ref"].append(vnfr_descriptor["id"])
435 step = "Updating VNFD usageState"
436 update_descriptor_usage_state(vnfd, "vnfds", self.db)
438 step = "creating nsr at database"
439 self._add_nsr_to_db(nsr_descriptor, rollback, session)
440 step = "Updating NSD usageState"
441 update_descriptor_usage_state(nsd, "nsds", self.db)
443 step = "creating nsr temporal folder"
444 self.fs.mkdir(nsr_id)
446 return nsr_id, None
447 except (
448 ValidationError,
449 EngineException,
450 DbException,
451 MsgException,
452 FsException,
453 ) as e:
454 raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code)
456 def _get_nsd_from_db(self, nsd_id, session):
457 _filter = self._get_project_filter(session)
458 _filter["_id"] = nsd_id
459 return self.db.get_one("nsds", _filter)
461 def _get_nsConfigTemplate_from_db(self, nsConfigTemplate_id, session):
462 _filter = self._get_project_filter(session)
463 _filter["_id"] = nsConfigTemplate_id
464 ns_config_template_db = self.db.get_one(
465 "ns_config_template", _filter, fail_on_empty=False
466 )
467 return ns_config_template_db
469 def _get_vnfd_from_db(self, vnfd_id, session):
470 _filter = self._get_project_filter(session)
471 _filter["id"] = vnfd_id
472 vnfd = self.db.get_one("vnfds", _filter, fail_on_empty=True, fail_on_more=True)
473 return vnfd
475 def _add_nsr_to_db(self, nsr_descriptor, rollback, session):
476 self.format_on_new(
477 nsr_descriptor, session["project_id"], make_public=session["public"]
478 )
479 self.db.create("nsrs", nsr_descriptor)
480 rollback.append({"topic": "nsrs", "_id": nsr_descriptor["id"]})
482 def _add_vnfr_to_db(self, vnfr_descriptor, rollback, session):
483 self.format_on_new(
484 vnfr_descriptor, session["project_id"], make_public=session["public"]
485 )
486 self.db.create("vnfrs", vnfr_descriptor)
487 rollback.append({"topic": "vnfrs", "_id": vnfr_descriptor["id"]})
489 def _check_nsd_operational_state(self, nsd, ns_request):
490 if nsd["_admin"]["operationalState"] == "DISABLED":
491 raise EngineException(
492 "nsd with id '{}' is DISABLED, and thus cannot be used to create "
493 "a network service".format(ns_request["nsdId"]),
494 http_code=HTTPStatus.CONFLICT,
495 )
497 def _check_ns_config_template_operational_state(
498 self, ns_config_template_db, ns_request
499 ):
500 if ns_config_template_db["_admin"]["operationalState"] == "DISABLED":
501 raise EngineException(
502 "ns_config_template with id '{}' is DISABLED, and thus cannot be used to create "
503 "a network service".format(ns_request["nsConfigTemplateId"]),
504 http_code=HTTPStatus.CONFLICT,
505 )
507 def _get_ns_k8s_namespace(self, nsd, ns_request, session):
508 additional_params, _ = self._format_additional_params(
509 ns_request, descriptor=nsd
510 )
511 # use for k8s-namespace from ns_request or additionalParamsForNs. By default, the project_id
512 ns_k8s_namespace = session["project_id"][0] if session["project_id"] else None
513 if ns_request and ns_request.get("k8s-namespace"):
514 ns_k8s_namespace = ns_request["k8s-namespace"]
515 if additional_params and additional_params.get("k8s-namespace"):
516 ns_k8s_namespace = additional_params["k8s-namespace"]
518 return ns_k8s_namespace
520 def _add_shared_volumes_to_nsr(
521 self, vdu, vnfd, nsr_descriptor, member_vnf_index, revision=None
522 ):
523 svsd = []
524 for vsd in vnfd.get("virtual-storage-desc", ()):
525 if vsd.get("vdu-storage-requirements"):
526 if (
527 vsd.get("vdu-storage-requirements")[0].get("key") == "multiattach"
528 and vsd.get("vdu-storage-requirements")[0].get("value") == "True"
529 ):
530 # Avoid setting the volume name multiple times
531 if not match(f"shared-.*-{vnfd['id']}", vsd["id"]):
532 vsd["id"] = f"shared-{vsd['id']}-{vnfd['id']}"
533 svsd.append(vsd)
534 if svsd:
535 nsr_descriptor["shared-volumes"] = svsd
537 def _add_flavor_to_nsr(
538 self, vdu, vnfd, nsr_descriptor, member_vnf_index, revision=None
539 ):
540 flavor_data = {}
541 guest_epa = {}
542 # Find this vdu compute and storage descriptors
543 vdu_virtual_compute = {}
544 vdu_virtual_storage = {}
545 for vcd in vnfd.get("virtual-compute-desc", ()):
546 if vcd.get("id") == vdu.get("virtual-compute-desc"):
547 vdu_virtual_compute = vcd
548 for vsd in vnfd.get("virtual-storage-desc", ()):
549 if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]:
550 vdu_virtual_storage = vsd
551 # Get this vdu vcpus, memory and storage info for flavor_data
552 if vdu_virtual_compute.get("virtual-cpu", {}).get("num-virtual-cpu"):
553 flavor_data["vcpu-count"] = vdu_virtual_compute["virtual-cpu"][
554 "num-virtual-cpu"
555 ]
556 if vdu_virtual_compute.get("virtual-memory", {}).get("size"):
557 flavor_data["memory-mb"] = (
558 float(vdu_virtual_compute["virtual-memory"]["size"]) * 1024.0
559 )
560 if vdu_virtual_storage.get("size-of-storage"):
561 flavor_data["storage-gb"] = vdu_virtual_storage["size-of-storage"]
562 # Get this vdu EPA info for guest_epa
563 if vdu_virtual_compute.get("virtual-cpu", {}).get("cpu-quota"):
564 guest_epa["cpu-quota"] = vdu_virtual_compute["virtual-cpu"]["cpu-quota"]
565 if vdu_virtual_compute.get("virtual-cpu", {}).get("pinning"):
566 vcpu_pinning = vdu_virtual_compute["virtual-cpu"]["pinning"]
567 if vcpu_pinning.get("thread-policy"):
568 guest_epa["cpu-thread-pinning-policy"] = vcpu_pinning["thread-policy"]
569 if vcpu_pinning.get("policy"):
570 cpu_policy = (
571 "SHARED" if vcpu_pinning["policy"] == "dynamic" else "DEDICATED"
572 )
573 guest_epa["cpu-pinning-policy"] = cpu_policy
574 if vdu_virtual_compute.get("virtual-memory", {}).get("mem-quota"):
575 guest_epa["mem-quota"] = vdu_virtual_compute["virtual-memory"]["mem-quota"]
576 if vdu_virtual_compute.get("virtual-memory", {}).get("mempage-size"):
577 guest_epa["mempage-size"] = vdu_virtual_compute["virtual-memory"][
578 "mempage-size"
579 ]
580 if vdu_virtual_compute.get("virtual-memory", {}).get("numa-node-policy"):
581 guest_epa["numa-node-policy"] = vdu_virtual_compute["virtual-memory"][
582 "numa-node-policy"
583 ]
584 if vdu_virtual_storage.get("disk-io-quota"):
585 guest_epa["disk-io-quota"] = vdu_virtual_storage["disk-io-quota"]
587 if guest_epa:
588 flavor_data["guest-epa"] = guest_epa
590 revision = revision if revision is not None else 1
591 flavor_data["name"] = (
592 vdu["id"][:56] + "-" + member_vnf_index + "-" + str(revision) + "-flv"
593 )
594 flavor_data["id"] = str(len(nsr_descriptor["flavor"]))
595 nsr_descriptor["flavor"].append(flavor_data)
597 def _create_nsr_descriptor_from_nsd(self, nsd, ns_request, nsr_id, session):
598 now = time()
599 additional_params, _ = self._format_additional_params(
600 ns_request, descriptor=nsd
601 )
603 nsr_descriptor = {
604 "name": ns_request["nsName"],
605 "name-ref": ns_request["nsName"],
606 "short-name": ns_request["nsName"],
607 "admin-status": "ENABLED",
608 "nsState": "NOT_INSTANTIATED",
609 "currentOperation": "IDLE",
610 "currentOperationID": None,
611 "errorDescription": None,
612 "errorDetail": None,
613 "deploymentStatus": None,
614 "configurationStatus": None,
615 "vcaStatus": None,
616 "nsd": {k: v for k, v in nsd.items()},
617 "datacenter": ns_request["vimAccountId"],
618 "resource-orchestrator": "osmopenmano",
619 "description": ns_request.get("nsDescription", ""),
620 "constituent-vnfr-ref": [],
621 "operational-status": "init", # typedef ns-operational-
622 "config-status": "init", # typedef config-states
623 "detailed-status": "scheduled",
624 "orchestration-progress": {},
625 "create-time": now,
626 "nsd-name-ref": nsd["name"],
627 "operational-events": [], # "id", "timestamp", "description", "event",
628 "nsd-ref": nsd["id"],
629 "nsd-id": nsd["_id"],
630 "vnfd-id": [],
631 "instantiate_params": self._format_ns_request(ns_request),
632 "additionalParamsForNs": additional_params,
633 "ns-instance-config-ref": nsr_id,
634 "id": nsr_id,
635 "_id": nsr_id,
636 "ssh-authorized-key": ns_request.get("ssh_keys"), # TODO remove
637 "flavor": [],
638 "image": [],
639 "affinity-or-anti-affinity-group": [],
640 "shared-volumes": [],
641 "vnffgd": [],
642 }
643 if "revision" in nsd["_admin"]:
644 nsr_descriptor["revision"] = nsd["_admin"]["revision"]
646 ns_request["nsr_id"] = nsr_id
647 if ns_request and ns_request.get("config-units"):
648 nsr_descriptor["config-units"] = ns_request["config-units"]
649 # Create vld
650 if nsd.get("virtual-link-desc"):
651 nsr_vld = deepcopy(nsd.get("virtual-link-desc", []))
652 # Fill each vld with vnfd-connection-point-ref data
653 # TODO: Change for multiple df support
654 all_vld_connection_point_data = {vld.get("id"): [] for vld in nsr_vld}
655 vnf_profiles = nsd.get("df", [[]])[0].get("vnf-profile", ())
656 for vnf_profile in vnf_profiles:
657 for vlc in vnf_profile.get("virtual-link-connectivity", ()):
658 for cpd in vlc.get("constituent-cpd-id", ()):
659 all_vld_connection_point_data[
660 vlc.get("virtual-link-profile-id")
661 ].append(
662 {
663 "member-vnf-index-ref": cpd.get(
664 "constituent-base-element-id"
665 ),
666 "vnfd-connection-point-ref": cpd.get(
667 "constituent-cpd-id"
668 ),
669 "vnfd-id-ref": vnf_profile.get("vnfd-id"),
670 }
671 )
673 vnfd = self._get_vnfd_from_db(vnf_profile.get("vnfd-id"), session)
674 vnfd.pop("_admin")
676 for vdu in vnfd.get("vdu", ()):
677 member_vnf_index = vnf_profile.get("id")
678 self._add_flavor_to_nsr(vdu, vnfd, nsr_descriptor, member_vnf_index)
679 self._add_shared_volumes_to_nsr(
680 vdu, vnfd, nsr_descriptor, member_vnf_index
681 )
682 sw_image_id = vdu.get("sw-image-desc")
683 if sw_image_id:
684 image_data = self._get_image_data_from_vnfd(vnfd, sw_image_id)
685 self._add_image_to_nsr(nsr_descriptor, image_data)
687 # also add alternative images to the list of images
688 for alt_image in vdu.get("alternative-sw-image-desc", ()):
689 image_data = self._get_image_data_from_vnfd(vnfd, alt_image)
690 self._add_image_to_nsr(nsr_descriptor, image_data)
692 # Add Affinity or Anti-affinity group information to NSR
693 vdu_profiles = vnfd.get("df", [[]])[0].get("vdu-profile", ())
694 affinity_group_prefix_name = "{}-{}".format(
695 nsr_descriptor["name"][:16], vnf_profile.get("id")[:16]
696 )
698 for vdu_profile in vdu_profiles:
699 affinity_group_data = {}
700 for affinity_group in vdu_profile.get(
701 "affinity-or-anti-affinity-group", ()
702 ):
703 affinity_group_data = (
704 self._get_affinity_or_anti_affinity_group_data_from_vnfd(
705 vnfd, affinity_group["id"]
706 )
707 )
708 affinity_group_data["member-vnf-index"] = vnf_profile.get("id")
709 self._add_affinity_or_anti_affinity_group_to_nsr(
710 nsr_descriptor,
711 affinity_group_data,
712 affinity_group_prefix_name,
713 )
715 for vld in nsr_vld:
716 vld["vnfd-connection-point-ref"] = all_vld_connection_point_data.get(
717 vld.get("id"), []
718 )
719 vld["name"] = vld["id"]
720 nsr_descriptor["vld"] = nsr_vld
721 if nsd.get("vnffgd"):
722 vnffgd = nsd.get("vnffgd")
723 for vnffg in vnffgd:
724 info = {}
725 for k, v in vnffg.items():
726 if k == "id":
727 info.update({k: v})
728 if k == "nfpd":
729 info.update({k: v})
730 nsr_descriptor["vnffgd"].append(info)
732 return nsr_descriptor
734 def _get_affinity_or_anti_affinity_group_data_from_vnfd(
735 self, vnfd, affinity_group_id
736 ):
737 """
738 Gets affinity-or-anti-affinity-group info from df and returns the desired affinity group
739 """
740 affinity_group = utils.find_in_list(
741 vnfd.get("df", [[]])[0].get("affinity-or-anti-affinity-group", ()),
742 lambda ag: ag["id"] == affinity_group_id,
743 )
744 affinity_group_data = {}
745 if affinity_group:
746 if affinity_group.get("id"):
747 affinity_group_data["ag-id"] = affinity_group["id"]
748 if affinity_group.get("type"):
749 affinity_group_data["type"] = affinity_group["type"]
750 if affinity_group.get("scope"):
751 affinity_group_data["scope"] = affinity_group["scope"]
752 return affinity_group_data
754 def _add_affinity_or_anti_affinity_group_to_nsr(
755 self, nsr_descriptor, affinity_group_data, affinity_group_prefix_name
756 ):
757 """
758 Adds affinity-or-anti-affinity-group to nsr checking first it is not already added
759 """
760 affinity_group = next(
761 (
762 f
763 for f in nsr_descriptor["affinity-or-anti-affinity-group"]
764 if all(f.get(k) == affinity_group_data[k] for k in affinity_group_data)
765 ),
766 None,
767 )
768 if not affinity_group:
769 affinity_group_data["id"] = str(
770 len(nsr_descriptor["affinity-or-anti-affinity-group"])
771 )
772 affinity_group_data["name"] = "{}-{}".format(
773 affinity_group_prefix_name, affinity_group_data["ag-id"][:32]
774 )
775 nsr_descriptor["affinity-or-anti-affinity-group"].append(
776 affinity_group_data
777 )
779 def _get_image_data_from_vnfd(self, vnfd, sw_image_id):
780 sw_image_desc = utils.find_in_list(
781 vnfd.get("sw-image-desc", ()), lambda sw: sw["id"] == sw_image_id
782 )
783 image_data = {}
784 if sw_image_desc.get("image"):
785 image_data["image"] = sw_image_desc["image"]
786 if sw_image_desc.get("checksum"):
787 image_data["image_checksum"] = sw_image_desc["checksum"]["hash"]
788 if sw_image_desc.get("vim-type"):
789 image_data["vim-type"] = sw_image_desc["vim-type"]
790 return image_data
792 def _add_image_to_nsr(self, nsr_descriptor, image_data):
793 """
794 Adds image to nsr checking first it is not already added
795 """
796 img = next(
797 (
798 f
799 for f in nsr_descriptor["image"]
800 if all(f.get(k) == image_data[k] for k in image_data)
801 ),
802 None,
803 )
804 if not img:
805 image_data["id"] = str(len(nsr_descriptor["image"]))
806 nsr_descriptor["image"].append(image_data)
808 def _create_vnfr_descriptor_from_vnfd(
809 self,
810 nsd,
811 vnfd,
812 vnfd_id,
813 vnf_index,
814 nsr_descriptor,
815 ns_request,
816 ns_k8s_namespace,
817 revision=None,
818 ):
819 vnfr_id = str(uuid4())
820 nsr_id = nsr_descriptor["id"]
821 now = time()
822 additional_params, vnf_params = self._format_additional_params(
823 ns_request, vnf_index, descriptor=vnfd
824 )
826 vnfr_descriptor = {
827 "id": vnfr_id,
828 "_id": vnfr_id,
829 "nsr-id-ref": nsr_id,
830 "member-vnf-index-ref": vnf_index,
831 "additionalParamsForVnf": additional_params,
832 "created-time": now,
833 # "vnfd": vnfd, # at OSM model.but removed to avoid data duplication TODO: revise
834 "vnfd-ref": vnfd_id,
835 "vnfd-id": vnfd["_id"], # not at OSM model, but useful
836 "vim-account-id": None,
837 "vca-id": None,
838 "vdur": [],
839 "connection-point": [],
840 "ip-address": None, # mgmt-interface filled by LCM
841 }
843 # Revision backwards compatility. Only specify the revision in the record if
844 # the original VNFD has a revision.
845 if "revision" in vnfd:
846 vnfr_descriptor["revision"] = vnfd["revision"]
848 vnf_k8s_namespace = ns_k8s_namespace
849 if vnf_params:
850 if vnf_params.get("k8s-namespace"):
851 vnf_k8s_namespace = vnf_params["k8s-namespace"]
852 if vnf_params.get("config-units"):
853 vnfr_descriptor["config-units"] = vnf_params["config-units"]
855 # Create vld
856 if vnfd.get("int-virtual-link-desc"):
857 vnfr_descriptor["vld"] = []
858 for vnfd_vld in vnfd.get("int-virtual-link-desc"):
859 vnfr_descriptor["vld"].append({key: vnfd_vld[key] for key in vnfd_vld})
861 for cp in vnfd.get("ext-cpd", ()):
862 vnf_cp = {
863 "name": cp.get("id"),
864 "connection-point-id": cp.get("int-cpd", {}).get("cpd"),
865 "connection-point-vdu-id": cp.get("int-cpd", {}).get("vdu-id"),
866 "id": cp.get("id"),
867 # "ip-address", "mac-address" # filled by LCM
868 # vim-id # TODO it would be nice having a vim port id
869 }
870 vnfr_descriptor["connection-point"].append(vnf_cp)
872 # Create k8s-cluster information
873 # TODO: Validate if a k8s-cluster net can have more than one ext-cpd ?
874 if vnfd.get("k8s-cluster"):
875 vnfr_descriptor["k8s-cluster"] = vnfd["k8s-cluster"]
876 all_k8s_cluster_nets_cpds = {}
877 for cpd in get_iterable(vnfd.get("ext-cpd")):
878 if cpd.get("k8s-cluster-net"):
879 all_k8s_cluster_nets_cpds[cpd.get("k8s-cluster-net")] = cpd.get(
880 "id"
881 )
882 for net in get_iterable(vnfr_descriptor["k8s-cluster"].get("nets")):
883 if net.get("id") in all_k8s_cluster_nets_cpds:
884 net["external-connection-point-ref"] = all_k8s_cluster_nets_cpds[
885 net.get("id")
886 ]
888 # update kdus
889 for kdu in get_iterable(vnfd.get("kdu")):
890 additional_params, kdu_params = self._format_additional_params(
891 ns_request, vnf_index, kdu_name=kdu["name"], descriptor=vnfd
892 )
893 kdu_k8s_namespace = vnf_k8s_namespace
894 kdu_model = kdu_params.get("kdu_model") if kdu_params else None
895 if kdu_params and kdu_params.get("k8s-namespace"):
896 kdu_k8s_namespace = kdu_params["k8s-namespace"]
898 kdu_deployment_name = ""
899 if kdu_params and kdu_params.get("kdu-deployment-name"):
900 kdu_deployment_name = kdu_params.get("kdu-deployment-name")
902 kdur = {
903 "additionalParams": additional_params,
904 "k8s-namespace": kdu_k8s_namespace,
905 "kdu-deployment-name": kdu_deployment_name,
906 "kdu-name": kdu["name"],
907 # TODO "name": "" Name of the VDU in the VIM
908 "ip-address": None, # mgmt-interface filled by LCM
909 "k8s-cluster": {},
910 }
911 if kdu_params and kdu_params.get("config-units"):
912 kdur["config-units"] = kdu_params["config-units"]
913 if kdu.get("helm-version"):
914 kdur["helm-version"] = kdu["helm-version"]
915 for k8s_type in ("helm-chart", "juju-bundle"):
916 if kdu.get(k8s_type):
917 kdur[k8s_type] = kdu_model or kdu[k8s_type]
918 if not vnfr_descriptor.get("kdur"):
919 vnfr_descriptor["kdur"] = []
920 vnfr_descriptor["kdur"].append(kdur)
922 vnfd_mgmt_cp = vnfd.get("mgmt-cp")
924 for vdu in vnfd.get("vdu", ()):
925 vdu_mgmt_cp = []
926 try:
927 configs = vnfd.get("df")[0]["lcm-operations-configuration"][
928 "operate-vnf-op-config"
929 ]["day1-2"]
930 vdu_config = utils.find_in_list(
931 configs, lambda config: config["id"] == vdu["id"]
932 )
933 except Exception:
934 vdu_config = None
936 try:
937 vdu_instantiation_level = utils.find_in_list(
938 vnfd.get("df")[0]["instantiation-level"][0]["vdu-level"],
939 lambda a_vdu_profile: a_vdu_profile["vdu-id"] == vdu["id"],
940 )
941 except Exception:
942 vdu_instantiation_level = None
944 if vdu_config:
945 external_connection_ee = utils.filter_in_list(
946 vdu_config.get("execution-environment-list", []),
947 lambda ee: "external-connection-point-ref" in ee,
948 )
949 for ee in external_connection_ee:
950 vdu_mgmt_cp.append(ee["external-connection-point-ref"])
952 additional_params, vdu_params = self._format_additional_params(
953 ns_request, vnf_index, vdu_id=vdu["id"], descriptor=vnfd
954 )
956 try:
957 vdu_virtual_storage_descriptors = utils.filter_in_list(
958 vnfd.get("virtual-storage-desc", []),
959 lambda stg_desc: stg_desc["id"] in vdu["virtual-storage-desc"],
960 )
961 except Exception:
962 vdu_virtual_storage_descriptors = []
963 vdur = {
964 "vdu-id-ref": vdu["id"],
965 # TODO "name": "" Name of the VDU in the VIM
966 "ip-address": None, # mgmt-interface filled by LCM
967 # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM
968 "internal-connection-point": [],
969 "interfaces": [],
970 "additionalParams": additional_params,
971 "vdu-name": vdu["name"],
972 "virtual-storages": vdu_virtual_storage_descriptors,
973 }
974 if vdu_params and vdu_params.get("config-units"):
975 vdur["config-units"] = vdu_params["config-units"]
976 if deep_get(vdu, ("supplemental-boot-data", "boot-data-drive")):
977 vdur["boot-data-drive"] = vdu["supplemental-boot-data"][
978 "boot-data-drive"
979 ]
980 if vdu.get("pdu-type"):
981 vdur["pdu-type"] = vdu["pdu-type"]
982 vdur["name"] = vdu["pdu-type"]
983 # TODO volumes: name, volume-id
984 for icp in vdu.get("int-cpd", ()):
985 vdu_icp = {
986 "id": icp["id"],
987 "connection-point-id": icp["id"],
988 "name": icp.get("id"),
989 }
991 vdur["internal-connection-point"].append(vdu_icp)
993 for iface in icp.get("virtual-network-interface-requirement", ()):
994 # Name, mac-address and interface position is taken from VNFD
995 # and included into VNFR. By this way RO can process this information
996 # while creating the VDU.
997 iface_fields = ("name", "mac-address", "position", "ip-address")
998 vdu_iface = {
999 x: iface[x] for x in iface_fields if iface.get(x) is not None
1000 }
1002 vdu_iface["internal-connection-point-ref"] = vdu_icp["id"]
1003 if "port-security-enabled" in icp:
1004 vdu_iface["port-security-enabled"] = icp[
1005 "port-security-enabled"
1006 ]
1008 if "port-security-disable-strategy" in icp:
1009 vdu_iface["port-security-disable-strategy"] = icp[
1010 "port-security-disable-strategy"
1011 ]
1013 for ext_cp in vnfd.get("ext-cpd", ()):
1014 if not ext_cp.get("int-cpd"):
1015 continue
1016 if ext_cp["int-cpd"].get("vdu-id") != vdu["id"]:
1017 continue
1018 if icp["id"] == ext_cp["int-cpd"].get("cpd"):
1019 vdu_iface["external-connection-point-ref"] = ext_cp.get(
1020 "id"
1021 )
1023 if "port-security-enabled" in ext_cp:
1024 vdu_iface["port-security-enabled"] = ext_cp[
1025 "port-security-enabled"
1026 ]
1028 if "port-security-disable-strategy" in ext_cp:
1029 vdu_iface["port-security-disable-strategy"] = ext_cp[
1030 "port-security-disable-strategy"
1031 ]
1033 break
1035 if (
1036 vnfd_mgmt_cp
1037 and vdu_iface.get("external-connection-point-ref")
1038 == vnfd_mgmt_cp
1039 ):
1040 vdu_iface["mgmt-vnf"] = True
1041 vdu_iface["mgmt-interface"] = True
1043 for ecp in vdu_mgmt_cp:
1044 if vdu_iface.get("external-connection-point-ref") == ecp:
1045 vdu_iface["mgmt-interface"] = True
1047 if iface.get("virtual-interface"):
1048 vdu_iface.update(deepcopy(iface["virtual-interface"]))
1050 # look for network where this interface is connected
1051 iface_ext_cp = vdu_iface.get("external-connection-point-ref")
1052 if iface_ext_cp:
1053 # TODO: Change for multiple df support
1054 for df in get_iterable(nsd.get("df")):
1055 for vnf_profile in get_iterable(df.get("vnf-profile")):
1056 for vlc_index, vlc in enumerate(
1057 get_iterable(
1058 vnf_profile.get("virtual-link-connectivity")
1059 )
1060 ):
1061 for cpd in get_iterable(
1062 vlc.get("constituent-cpd-id")
1063 ):
1064 if (
1065 cpd.get("constituent-cpd-id")
1066 == iface_ext_cp
1067 ) and vnf_profile.get("id") == vnf_index:
1068 vdu_iface["ns-vld-id"] = vlc.get(
1069 "virtual-link-profile-id"
1070 )
1071 # if iface type is SRIOV or PASSTHROUGH, set pci-interfaces flag to True
1072 if vdu_iface.get("type") in (
1073 "SR-IOV",
1074 "PCI-PASSTHROUGH",
1075 ):
1076 nsr_descriptor["vld"][vlc_index][
1077 "pci-interfaces"
1078 ] = True
1079 break
1080 elif vdu_iface.get("internal-connection-point-ref"):
1081 vdu_iface["vnf-vld-id"] = icp.get("int-virtual-link-desc")
1082 # TODO: store fixed IP address in the record (if it exists in the ICP)
1083 # if iface type is SRIOV or PASSTHROUGH, set pci-interfaces flag to True
1084 if vdu_iface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1085 ivld_index = utils.find_index_in_list(
1086 vnfd.get("int-virtual-link-desc", ()),
1087 lambda ivld: ivld["id"]
1088 == icp.get("int-virtual-link-desc"),
1089 )
1090 vnfr_descriptor["vld"][ivld_index]["pci-interfaces"] = True
1092 vdur["interfaces"].append(vdu_iface)
1094 if vdu.get("sw-image-desc"):
1095 sw_image = utils.find_in_list(
1096 vnfd.get("sw-image-desc", ()),
1097 lambda image: image["id"] == vdu.get("sw-image-desc"),
1098 )
1099 nsr_sw_image_data = utils.find_in_list(
1100 nsr_descriptor["image"],
1101 lambda nsr_image: (nsr_image.get("image") == sw_image.get("image")),
1102 )
1103 vdur["ns-image-id"] = nsr_sw_image_data["id"]
1105 if vdu.get("alternative-sw-image-desc"):
1106 alt_image_ids = []
1107 for alt_image_id in vdu.get("alternative-sw-image-desc", ()):
1108 sw_image = utils.find_in_list(
1109 vnfd.get("sw-image-desc", ()),
1110 lambda image: image["id"] == alt_image_id,
1111 )
1112 nsr_sw_image_data = utils.find_in_list(
1113 nsr_descriptor["image"],
1114 lambda nsr_image: (
1115 nsr_image.get("image") == sw_image.get("image")
1116 ),
1117 )
1118 alt_image_ids.append(nsr_sw_image_data["id"])
1119 vdur["alt-image-ids"] = alt_image_ids
1121 revision = revision if revision is not None else 1
1122 flavor_data_name = (
1123 vdu["id"][:56] + "-" + vnf_index + "-" + str(revision) + "-flv"
1124 )
1125 nsr_flavor_desc = utils.find_in_list(
1126 nsr_descriptor["flavor"],
1127 lambda flavor: flavor["name"] == flavor_data_name,
1128 )
1130 if nsr_flavor_desc:
1131 vdur["ns-flavor-id"] = nsr_flavor_desc["id"]
1133 # Adding Shared Volume information to vdur
1134 if vdur.get("virtual-storages"):
1135 nsr_sv = []
1136 for vsd in vdur["virtual-storages"]:
1137 if vsd.get("vdu-storage-requirements"):
1138 if (
1139 vsd["vdu-storage-requirements"][0].get("key")
1140 == "multiattach"
1141 and vsd["vdu-storage-requirements"][0].get("value")
1142 == "True"
1143 ):
1144 nsr_sv.append(vsd["id"])
1145 if nsr_sv:
1146 vdur["shared-volumes-id"] = nsr_sv
1148 # Adding Affinity groups information to vdur
1149 try:
1150 vdu_profile_affinity_group = utils.find_in_list(
1151 vnfd.get("df")[0]["vdu-profile"],
1152 lambda a_vdu: a_vdu["id"] == vdu["id"],
1153 )
1154 except Exception:
1155 vdu_profile_affinity_group = None
1157 if vdu_profile_affinity_group:
1158 affinity_group_ids = []
1159 for affinity_group in vdu_profile_affinity_group.get(
1160 "affinity-or-anti-affinity-group", ()
1161 ):
1162 vdu_affinity_group = utils.find_in_list(
1163 vdu_profile_affinity_group.get(
1164 "affinity-or-anti-affinity-group", ()
1165 ),
1166 lambda ag_fp: ag_fp["id"] == affinity_group["id"],
1167 )
1168 nsr_affinity_group = utils.find_in_list(
1169 nsr_descriptor["affinity-or-anti-affinity-group"],
1170 lambda nsr_ag: (
1171 nsr_ag.get("ag-id") == vdu_affinity_group.get("id")
1172 and nsr_ag.get("member-vnf-index")
1173 == vnfr_descriptor.get("member-vnf-index-ref")
1174 ),
1175 )
1176 # Update Affinity Group VIM name if VDU instantiation parameter is present
1177 if vnf_params and vnf_params.get("affinity-or-anti-affinity-group"):
1178 vnf_params_affinity_group = utils.find_in_list(
1179 vnf_params["affinity-or-anti-affinity-group"],
1180 lambda vnfp_ag: (
1181 vnfp_ag.get("id") == vdu_affinity_group.get("id")
1182 ),
1183 )
1184 if vnf_params_affinity_group.get("vim-affinity-group-id"):
1185 nsr_affinity_group[
1186 "vim-affinity-group-id"
1187 ] = vnf_params_affinity_group["vim-affinity-group-id"]
1188 affinity_group_ids.append(nsr_affinity_group["id"])
1189 vdur["affinity-or-anti-affinity-group-id"] = affinity_group_ids
1191 if vdu_instantiation_level:
1192 count = vdu_instantiation_level.get("number-of-instances")
1193 else:
1194 count = 1
1196 for index in range(0, count):
1197 vdur = deepcopy(vdur)
1198 for iface in vdur["interfaces"]:
1199 if iface.get("ip-address") and index != 0:
1200 iface["ip-address"] = increment_ip_mac(iface["ip-address"])
1201 if iface.get("mac-address") and index != 0:
1202 iface["mac-address"] = increment_ip_mac(iface["mac-address"])
1204 vdur["_id"] = str(uuid4())
1205 vdur["id"] = vdur["_id"]
1206 vdur["count-index"] = index
1207 vnfr_descriptor["vdur"].append(vdur)
1208 return vnfr_descriptor
1210 def vca_status_refresh(self, session, ns_instance_content, filter_q):
1211 """
1212 vcaStatus in ns_instance_content maybe stale, check if it is stale and create lcm op
1213 to refresh vca status by sending message to LCM when it is stale. Ignore otherwise.
1214 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1215 :param ns_instance_content: ns instance content
1216 :param filter_q: dict: query parameter containing vcaStatus-refresh as true or false
1217 :return: None
1218 """
1219 time_now, time_delta = (
1220 time(),
1221 time() - ns_instance_content["_admin"]["modified"],
1222 )
1223 force_refresh = (
1224 isinstance(filter_q, dict) and filter_q.get("vcaStatusRefresh") == "true"
1225 )
1226 threshold_reached = time_delta > 120
1227 if force_refresh or threshold_reached:
1228 operation, _id = "vca_status_refresh", ns_instance_content["_id"]
1229 ns_instance_content["_admin"]["modified"] = time_now
1230 self.db.set_one(self.topic, {"_id": _id}, ns_instance_content)
1231 nslcmop_desc = NsLcmOpTopic._create_nslcmop(_id, operation, None)
1232 self.format_on_new(
1233 nslcmop_desc, session["project_id"], make_public=session["public"]
1234 )
1235 nslcmop_desc["_admin"].pop("nsState")
1236 self.msg.write("ns", operation, nslcmop_desc)
1237 return
1239 def show(self, session, _id, filter_q=None, api_req=False):
1240 """
1241 Get complete information on an ns instance.
1242 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1243 :param _id: string, ns instance id
1244 :param filter_q: dict: query parameter containing vcaStatusRefresh as true or false
1245 :param api_req: True if this call is serving an external API request. False if serving internal request.
1246 :return: dictionary, raise exception if not found.
1247 """
1248 ns_instance_content = super().show(session, _id, api_req)
1249 self.vca_status_refresh(session, ns_instance_content, filter_q)
1250 return ns_instance_content
1252 def edit(self, session, _id, indata=None, kwargs=None, content=None):
1253 raise EngineException(
1254 "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR
1255 )
1258class VnfrTopic(BaseTopic):
1259 topic = "vnfrs"
1260 topic_msg = None
1262 def __init__(self, db, fs, msg, auth):
1263 BaseTopic.__init__(self, db, fs, msg, auth)
1265 def delete(self, session, _id, dry_run=False, not_send_msg=None):
1266 raise EngineException(
1267 "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR
1268 )
1270 def edit(self, session, _id, indata=None, kwargs=None, content=None):
1271 raise EngineException(
1272 "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR
1273 )
1275 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
1276 # Not used because vnfrs are created and deleted by NsrTopic class directly
1277 raise EngineException(
1278 "Method new called directly", HTTPStatus.INTERNAL_SERVER_ERROR
1279 )
1282class NsLcmOpTopic(BaseTopic):
1283 topic = "nslcmops"
1284 topic_msg = "ns"
1285 operation_schema = { # mapping between operation and jsonschema to validate
1286 "instantiate": ns_instantiate,
1287 "action": ns_action,
1288 "update": ns_update,
1289 "scale": ns_scale,
1290 "heal": ns_heal,
1291 "terminate": ns_terminate,
1292 "migrate": ns_migrate,
1293 "cancel": nslcmop_cancel,
1294 }
1296 def __init__(self, db, fs, msg, auth):
1297 BaseTopic.__init__(self, db, fs, msg, auth)
1298 self.nsrtopic = NsrTopic(db, fs, msg, auth)
1300 def _check_ns_operation(self, session, nsr, operation, indata):
1301 """
1302 Check that user has enter right parameters for the operation
1303 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1304 :param operation: it can be: instantiate, terminate, action, update, heal
1305 :param indata: descriptor with the parameters of the operation
1306 :return: None
1307 """
1308 if operation == "action":
1309 self._check_action_ns_operation(indata, nsr)
1310 elif operation == "scale":
1311 self._check_scale_ns_operation(indata, nsr)
1312 elif operation == "update":
1313 self._check_update_ns_operation(indata, nsr)
1314 elif operation == "heal":
1315 self._check_heal_ns_operation(indata, nsr)
1316 elif operation == "instantiate":
1317 self._check_instantiate_ns_operation(indata, nsr, session)
1319 def _check_action_ns_operation(self, indata, nsr):
1320 nsd = nsr["nsd"]
1321 # check vnf_member_index
1322 if indata.get("vnf_member_index"):
1323 indata["member_vnf_index"] = indata.pop(
1324 "vnf_member_index"
1325 ) # for backward compatibility
1326 if indata.get("member_vnf_index"):
1327 vnfd = self._get_vnfd_from_vnf_member_index(
1328 indata["member_vnf_index"], nsr["_id"]
1329 )
1330 try:
1331 configs = vnfd.get("df")[0]["lcm-operations-configuration"][
1332 "operate-vnf-op-config"
1333 ]["day1-2"]
1334 except Exception:
1335 configs = []
1337 if indata.get("vdu_id"):
1338 self._check_valid_vdu(vnfd, indata["vdu_id"])
1339 descriptor_configuration = utils.find_in_list(
1340 configs, lambda config: config["id"] == indata["vdu_id"]
1341 )
1342 elif indata.get("kdu_name"):
1343 self._check_valid_kdu(vnfd, indata["kdu_name"])
1344 descriptor_configuration = utils.find_in_list(
1345 configs, lambda config: config["id"] == indata.get("kdu_name")
1346 )
1347 else:
1348 descriptor_configuration = utils.find_in_list(
1349 configs, lambda config: config["id"] == vnfd["id"]
1350 )
1351 if descriptor_configuration is not None:
1352 descriptor_configuration = descriptor_configuration.get(
1353 "config-primitive"
1354 )
1355 else: # use a NSD
1356 descriptor_configuration = nsd.get("ns-configuration", {}).get(
1357 "config-primitive"
1358 )
1360 # For k8s allows default primitives without validating the parameters
1361 if indata.get("kdu_name") and indata["primitive"] in (
1362 "upgrade",
1363 "rollback",
1364 "status",
1365 "inspect",
1366 "readme",
1367 ):
1368 # TODO should be checked that rollback only can contains revsision_numbe????
1369 if not indata.get("member_vnf_index"):
1370 raise EngineException(
1371 "Missing action parameter 'member_vnf_index' for default KDU primitive '{}'".format(
1372 indata["primitive"]
1373 )
1374 )
1375 return
1376 # if not, check primitive
1377 for config_primitive in get_iterable(descriptor_configuration):
1378 if indata["primitive"] == config_primitive["name"]:
1379 # check needed primitive_params are provided
1380 if indata.get("primitive_params"):
1381 in_primitive_params_copy = copy(indata["primitive_params"])
1382 else:
1383 in_primitive_params_copy = {}
1384 for paramd in get_iterable(config_primitive.get("parameter")):
1385 if paramd["name"] in in_primitive_params_copy:
1386 del in_primitive_params_copy[paramd["name"]]
1387 elif not paramd.get("default-value"):
1388 raise EngineException(
1389 "Needed parameter {} not provided for primitive '{}'".format(
1390 paramd["name"], indata["primitive"]
1391 )
1392 )
1393 # check no extra primitive params are provided
1394 if in_primitive_params_copy:
1395 raise EngineException(
1396 "parameter/s '{}' not present at vnfd /nsd for primitive '{}'".format(
1397 list(in_primitive_params_copy.keys()), indata["primitive"]
1398 )
1399 )
1400 break
1401 else:
1402 raise EngineException(
1403 "Invalid primitive '{}' is not present at vnfd/nsd".format(
1404 indata["primitive"]
1405 )
1406 )
1408 def _check_update_ns_operation(self, indata, nsr) -> None:
1409 """Validates the ns-update request according to updateType
1411 If updateType is CHANGE_VNFPKG:
1412 - it checks the vnfInstanceId, whether it's available under ns instance
1413 - it checks the vnfdId whether it matches with the vnfd-id in the vnf-record of specified VNF.
1414 Otherwise exception will be raised.
1415 If updateType is REMOVE_VNF:
1416 - it checks if the vnfInstanceId is available in the ns instance
1417 - Otherwise exception will be raised.
1418 If updateType is OPERATE_VNF
1419 - it checks if the vdu-id is persent in the descriptor or not
1420 - it checks if the changeStateTo is either start, stop or rebuild
1421 If updateType is VERTICAL_SCALE
1422 - it checks if the vdu-id is persent in the descriptor or not
1424 Args:
1425 indata: includes updateType such as CHANGE_VNFPKG,
1426 nsr: network service record
1428 Raises:
1429 EngineException:
1430 a meaningful error if given update parameters are not proper such as
1431 "Error in validating ns-update request: <ID> does not match
1432 with the vnfd-id of vnfinstance
1433 http_code=HTTPStatus.UNPROCESSABLE_ENTITY"
1435 """
1436 try:
1437 if indata["updateType"] == "CHANGE_VNFPKG":
1438 # vnfInstanceId, nsInstanceId, vnfdId are mandatory
1439 vnf_instance_id = indata["changeVnfPackageData"]["vnfInstanceId"]
1440 ns_instance_id = indata["nsInstanceId"]
1441 vnfd_id_2update = indata["changeVnfPackageData"]["vnfdId"]
1443 if vnf_instance_id not in nsr["constituent-vnfr-ref"]:
1444 raise EngineException(
1445 f"Error in validating ns-update request: vnf {vnf_instance_id} does not "
1446 f"belong to NS {ns_instance_id}",
1447 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
1448 )
1450 # Getting vnfrs through the ns_instance_id
1451 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_instance_id})
1452 constituent_vnfd_id = next(
1453 (
1454 vnfr["vnfd-id"]
1455 for vnfr in vnfrs
1456 if vnfr["id"] == vnf_instance_id
1457 ),
1458 None,
1459 )
1461 # Check the given vnfd-id belongs to given vnf instance
1462 if constituent_vnfd_id and (vnfd_id_2update != constituent_vnfd_id):
1463 raise EngineException(
1464 f"Error in validating ns-update request: vnfd-id {vnfd_id_2update} does not "
1465 f"match with the vnfd-id: {constituent_vnfd_id} of VNF instance: {vnf_instance_id}",
1466 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
1467 )
1469 # Validating the ns update timeout
1470 if (
1471 indata.get("timeout_ns_update")
1472 and indata["timeout_ns_update"] < 300
1473 ):
1474 raise EngineException(
1475 "Error in validating ns-update request: {} second is not enough "
1476 "to upgrade the VNF instance: {}".format(
1477 indata["timeout_ns_update"], vnf_instance_id
1478 ),
1479 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
1480 )
1481 elif indata["updateType"] == "REMOVE_VNF":
1482 vnf_instance_id = indata["removeVnfInstanceId"]
1483 ns_instance_id = indata["nsInstanceId"]
1484 if vnf_instance_id not in nsr["constituent-vnfr-ref"]:
1485 raise EngineException(
1486 "Invalid VNF Instance Id. '{}' is not "
1487 "present in the NS '{}'".format(vnf_instance_id, ns_instance_id)
1488 )
1489 elif indata["updateType"] == "OPERATE_VNF":
1490 if indata.get("operateVnfData"):
1491 if indata["operateVnfData"]["changeStateTo"] not in (
1492 "start",
1493 "stop",
1494 "rebuild",
1495 ):
1496 raise EngineException(
1497 f"The operate type should be either start, stop or rebuild not {indata['operateVnfData']['changeStateTo']}",
1498 http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
1499 )
1500 if indata["operateVnfData"].get("additionalParam"):
1501 vdu_id = indata["operateVnfData"]["additionalParam"]["vdu_id"]
1502 vnfinstance_id = indata["operateVnfData"]["vnfInstanceId"]
1503 vnf = self.db.get_one("vnfrs", {"_id": vnfinstance_id})
1504 vnfd_member_vnf_index = vnf.get("member-vnf-index-ref")
1505 vnfd = self._get_vnfd_from_vnf_member_index(
1506 vnfd_member_vnf_index, nsr["_id"]
1507 )
1508 self._check_valid_vdu(vnfd, vdu_id)
1509 elif indata["updateType"] == "VERTICAL_SCALE":
1510 if indata.get("verticalScaleVnf"):
1511 vdu_id = indata["verticalScaleVnf"]["vduId"]
1512 vnfinstance_id = indata["verticalScaleVnf"]["vnfInstanceId"]
1513 vnf = self.db.get_one("vnfrs", {"_id": vnfinstance_id})
1514 vnfd_member_vnf_index = vnf.get("member-vnf-index-ref")
1515 vnfd = self._get_vnfd_from_vnf_member_index(
1516 vnfd_member_vnf_index, nsr["_id"]
1517 )
1518 self._check_valid_vdu(vnfd, vdu_id)
1520 except (
1521 DbException,
1522 AttributeError,
1523 IndexError,
1524 KeyError,
1525 ValueError,
1526 ) as e:
1527 raise type(e)(
1528 "Ns update request could not be processed with error: {}.".format(e)
1529 )
1531 def _check_scale_ns_operation(self, indata, nsr):
1532 vnfd = self._get_vnfd_from_vnf_member_index(
1533 indata["scaleVnfData"]["scaleByStepData"]["member-vnf-index"], nsr["_id"]
1534 )
1535 for scaling_aspect in get_iterable(vnfd.get("df", ())[0]["scaling-aspect"]):
1536 if (
1537 indata["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"]
1538 == scaling_aspect["id"]
1539 ):
1540 break
1541 else:
1542 raise EngineException(
1543 "Invalid scaleVnfData:scaleByStepData:scaling-group-descriptor '{}' is not "
1544 "present at vnfd:scaling-aspect".format(
1545 indata["scaleVnfData"]["scaleByStepData"][
1546 "scaling-group-descriptor"
1547 ]
1548 )
1549 )
1551 def _check_heal_ns_operation(self, indata, nsr):
1552 try:
1553 for data in indata.get("healVnfData"):
1554 vnf_id = data.get("vnfInstanceId")
1555 vnf = self.db.get_one("vnfrs", {"_id": vnf_id})
1556 vnfd_member_vnf_index = vnf.get("member-vnf-index-ref")
1557 vnfd = self._get_vnfd_from_vnf_member_index(
1558 vnfd_member_vnf_index, nsr["_id"]
1559 )
1560 if data.get("additionalParams"):
1561 vdu_id = data["additionalParams"].get("vdu")
1562 if vdu_id:
1563 for index in range(len(vdu_id)):
1564 vdu = vdu_id[index].get("vdu-id")
1565 self._check_valid_vdu(vnfd, vdu)
1566 except (DbException, AttributeError, IndexError, KeyError, ValueError) as e:
1567 raise type(e)(
1568 "Ns healing request could not be processed with error: {}.".format(e)
1569 )
1571 def _check_instantiate_ns_operation(self, indata, nsr, session):
1572 vnf_member_index_to_vnfd = {} # map between vnf_member_index to vnf descriptor.
1573 vim_accounts = []
1574 wim_accounts = []
1575 nsd = nsr["nsd"]
1576 self._check_valid_vim_account(indata["vimAccountId"], vim_accounts, session)
1577 self._check_valid_wim_account(indata.get("wimAccountId"), wim_accounts, session)
1578 for in_vnf in get_iterable(indata.get("vnf")):
1579 member_vnf_index = in_vnf["member-vnf-index"]
1580 if vnf_member_index_to_vnfd.get(member_vnf_index):
1581 vnfd = vnf_member_index_to_vnfd[member_vnf_index]
1582 else:
1583 vnfd = self._get_vnfd_from_vnf_member_index(
1584 member_vnf_index, nsr["_id"]
1585 )
1586 vnf_member_index_to_vnfd[
1587 member_vnf_index
1588 ] = vnfd # add to cache, avoiding a later look for
1589 self._check_vnf_instantiation_params(in_vnf, vnfd)
1590 if in_vnf.get("vimAccountId"):
1591 self._check_valid_vim_account(
1592 in_vnf["vimAccountId"], vim_accounts, session
1593 )
1595 for in_vld in get_iterable(indata.get("vld")):
1596 self._check_valid_wim_account(
1597 in_vld.get("wimAccountId"), wim_accounts, session
1598 )
1599 for vldd in get_iterable(nsd.get("virtual-link-desc")):
1600 if in_vld["name"] == vldd["id"]:
1601 break
1602 else:
1603 raise EngineException(
1604 "Invalid parameter vld:name='{}' is not present at nsd:vld".format(
1605 in_vld["name"]
1606 )
1607 )
1609 def _get_vnfd_from_vnf_member_index(self, member_vnf_index, nsr_id):
1610 # Obtain vnf descriptor. The vnfr is used to get the vnfd._id used for this member_vnf_index
1611 vnfr = self.db.get_one(
1612 "vnfrs",
1613 {"nsr-id-ref": nsr_id, "member-vnf-index-ref": member_vnf_index},
1614 fail_on_empty=False,
1615 )
1616 if not vnfr:
1617 raise EngineException(
1618 "Invalid parameter member_vnf_index='{}' is not one of the "
1619 "nsd:constituent-vnfd".format(member_vnf_index)
1620 )
1622 # Backwards compatibility: if there is no revision, get it from the one and only VNFD entry
1623 if "revision" in vnfr:
1624 vnfd_revision = vnfr["vnfd-id"] + ":" + str(vnfr["revision"])
1625 vnfd = self.db.get_one(
1626 "vnfds_revisions", {"_id": vnfd_revision}, fail_on_empty=False
1627 )
1628 else:
1629 vnfd = self.db.get_one(
1630 "vnfds", {"_id": vnfr["vnfd-id"]}, fail_on_empty=False
1631 )
1633 if not vnfd:
1634 raise EngineException(
1635 "vnfd id={} has been deleted!. Operation cannot be performed".format(
1636 vnfr["vnfd-id"]
1637 )
1638 )
1639 return vnfd
1641 def _check_valid_vdu(self, vnfd, vdu_id):
1642 for vdud in get_iterable(vnfd.get("vdu")):
1643 if vdud["id"] == vdu_id:
1644 return vdud
1645 else:
1646 raise EngineException(
1647 "Invalid parameter vdu_id='{}' not present at vnfd:vdu:id".format(
1648 vdu_id
1649 )
1650 )
1652 def _check_valid_kdu(self, vnfd, kdu_name):
1653 for kdud in get_iterable(vnfd.get("kdu")):
1654 if kdud["name"] == kdu_name:
1655 return kdud
1656 else:
1657 raise EngineException(
1658 "Invalid parameter kdu_name='{}' not present at vnfd:kdu:name".format(
1659 kdu_name
1660 )
1661 )
1663 def _check_vnf_instantiation_params(self, in_vnf, vnfd):
1664 for in_vdu in get_iterable(in_vnf.get("vdu")):
1665 for vdu in get_iterable(vnfd.get("vdu")):
1666 if in_vdu["id"] == vdu["id"]:
1667 for volume in get_iterable(in_vdu.get("volume")):
1668 for volumed in get_iterable(vdu.get("virtual-storage-desc")):
1669 if volumed == volume["name"]:
1670 break
1671 else:
1672 raise EngineException(
1673 "Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
1674 "volume:name='{}' is not present at "
1675 "vnfd:vdu:virtual-storage-desc list".format(
1676 in_vnf["member-vnf-index"],
1677 in_vdu["id"],
1678 volume["id"],
1679 )
1680 )
1682 vdu_if_names = set()
1683 for cpd in get_iterable(vdu.get("int-cpd")):
1684 for iface in get_iterable(
1685 cpd.get("virtual-network-interface-requirement")
1686 ):
1687 vdu_if_names.add(iface.get("name"))
1689 for in_iface in get_iterable(in_vdu.get("interface")):
1690 if in_iface["name"] in vdu_if_names:
1691 break
1692 else:
1693 raise EngineException(
1694 "Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
1695 "int-cpd[id='{}'] is not present at vnfd:vdu:int-cpd".format(
1696 in_vnf["member-vnf-index"],
1697 in_vdu["id"],
1698 in_iface["name"],
1699 )
1700 )
1701 break
1703 else:
1704 raise EngineException(
1705 "Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}'] is not present "
1706 "at vnfd:vdu".format(in_vnf["member-vnf-index"], in_vdu["id"])
1707 )
1709 vnfd_ivlds_cpds = {
1710 ivld.get("id"): set()
1711 for ivld in get_iterable(vnfd.get("int-virtual-link-desc"))
1712 }
1713 for vdu in vnfd.get("vdu", {}):
1714 for cpd in vdu.get("int-cpd", {}):
1715 if cpd.get("int-virtual-link-desc"):
1716 vnfd_ivlds_cpds[cpd.get("int-virtual-link-desc")] = cpd.get("id")
1718 for in_ivld in get_iterable(in_vnf.get("internal-vld")):
1719 if in_ivld.get("name") in vnfd_ivlds_cpds:
1720 for in_icp in get_iterable(in_ivld.get("internal-connection-point")):
1721 if in_icp["id-ref"] in vnfd_ivlds_cpds[in_ivld.get("name")]:
1722 break
1723 else:
1724 raise EngineException(
1725 "Invalid parameter vnf[member-vnf-index='{}']:internal-vld[name"
1726 "='{}']:internal-connection-point[id-ref:'{}'] is not present at "
1727 "vnfd:internal-vld:name/id:internal-connection-point".format(
1728 in_vnf["member-vnf-index"],
1729 in_ivld["name"],
1730 in_icp["id-ref"],
1731 )
1732 )
1733 else:
1734 raise EngineException(
1735 "Invalid parameter vnf[member-vnf-index='{}']:internal-vld:name='{}'"
1736 " is not present at vnfd '{}'".format(
1737 in_vnf["member-vnf-index"], in_ivld["name"], vnfd["id"]
1738 )
1739 )
1741 def _check_valid_vim_account(self, vim_account, vim_accounts, session):
1742 if vim_account in vim_accounts:
1743 return
1744 try:
1745 db_filter = self._get_project_filter(session)
1746 db_filter["_id"] = vim_account
1747 self.db.get_one("vim_accounts", db_filter)
1748 except Exception:
1749 raise EngineException(
1750 "Invalid vimAccountId='{}' not present for the project".format(
1751 vim_account
1752 )
1753 )
1754 vim_accounts.append(vim_account)
1756 def _get_vim_account(self, vim_id: str, session):
1757 try:
1758 db_filter = self._get_project_filter(session)
1759 db_filter["_id"] = vim_id
1760 return self.db.get_one("vim_accounts", db_filter)
1761 except Exception:
1762 raise EngineException(
1763 "Invalid vimAccountId='{}' not present for the project".format(vim_id)
1764 )
1766 def _check_valid_wim_account(self, wim_account, wim_accounts, session):
1767 if not isinstance(wim_account, str):
1768 return
1769 if wim_account in wim_accounts:
1770 return
1771 try:
1772 db_filter = self._get_project_filter(session)
1773 db_filter["_id"] = wim_account
1774 self.db.get_one("wim_accounts", db_filter)
1775 except Exception:
1776 raise EngineException(
1777 "Invalid wimAccountId='{}' not present for the project".format(
1778 wim_account
1779 )
1780 )
1781 wim_accounts.append(wim_account)
1783 def _look_for_pdu(
1784 self, session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback
1785 ):
1786 """
1787 Look for a free PDU in the catalog matching vdur type and interfaces. Fills vnfr.vdur with the interface
1788 (ip_address, ...) information.
1789 Modifies PDU _admin.usageState to 'IN_USE'
1790 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1791 :param rollback: list with the database modifications to rollback if needed
1792 :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found
1793 :param vim_account: vim_account where this vnfr should be deployed
1794 :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr
1795 :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback
1796 of the changed vnfr is needed
1798 :return: List of PDU interfaces that are connected to an existing VIM network. Each item contains:
1799 "vim-network-name": used at VIM
1800 "name": interface name
1801 "vnf-vld-id": internal VNFD vld where this interface is connected, or
1802 "ns-vld-id": NSD vld where this interface is connected.
1803 NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None
1804 """
1806 ifaces_forcing_vim_network = []
1807 for vdur_index, vdur in enumerate(get_iterable(vnfr.get("vdur"))):
1808 if not vdur.get("pdu-type"):
1809 continue
1810 pdu_type = vdur.get("pdu-type")
1811 pdu_filter = self._get_project_filter(session)
1812 pdu_filter["vim_accounts"] = vim_account
1813 pdu_filter["type"] = pdu_type
1814 pdu_filter["_admin.operationalState"] = "ENABLED"
1815 pdu_filter["_admin.usageState"] = "NOT_IN_USE"
1816 # TODO feature 1417: "shared": True,
1818 available_pdus = self.db.get_list("pdus", pdu_filter)
1819 for pdu in available_pdus:
1820 # step 1 check if this pdu contains needed interfaces:
1821 match_interfaces = True
1822 for vdur_interface in vdur["interfaces"]:
1823 for pdu_interface in pdu["interfaces"]:
1824 if pdu_interface["name"] == vdur_interface["name"]:
1825 # TODO feature 1417: match per mgmt type
1826 break
1827 else: # no interface found for name
1828 match_interfaces = False
1829 break
1830 if match_interfaces:
1831 break
1832 else:
1833 raise EngineException(
1834 "No PDU of type={} at vim_account={} found for member_vnf_index={}, vdu={} matching interface "
1835 "names".format(
1836 pdu_type,
1837 vim_account,
1838 vnfr["member-vnf-index-ref"],
1839 vdur["vdu-id-ref"],
1840 )
1841 )
1843 # step 2. Update pdu
1844 rollback_pdu = {
1845 "_admin.usageState": pdu["_admin"]["usageState"],
1846 "_admin.usage.vnfr_id": None,
1847 "_admin.usage.nsr_id": None,
1848 "_admin.usage.vdur": None,
1849 }
1850 self.db.set_one(
1851 "pdus",
1852 {"_id": pdu["_id"]},
1853 {
1854 "_admin.usageState": "IN_USE",
1855 "_admin.usage": {
1856 "vnfr_id": vnfr["_id"],
1857 "nsr_id": vnfr["nsr-id-ref"],
1858 "vdur": vdur["vdu-id-ref"],
1859 },
1860 },
1861 )
1862 rollback.append(
1863 {
1864 "topic": "pdus",
1865 "_id": pdu["_id"],
1866 "operation": "set",
1867 "content": rollback_pdu,
1868 }
1869 )
1871 # step 3. Fill vnfr info by filling vdur
1872 vdu_text = "vdur.{}".format(vdur_index)
1873 vnfr_update_rollback[vdu_text + ".pdu-id"] = None
1874 vnfr_update[vdu_text + ".pdu-id"] = pdu["_id"]
1875 for iface_index, vdur_interface in enumerate(vdur["interfaces"]):
1876 for pdu_interface in pdu["interfaces"]:
1877 if pdu_interface["name"] == vdur_interface["name"]:
1878 iface_text = vdu_text + ".interfaces.{}".format(iface_index)
1879 for k, v in pdu_interface.items():
1880 if k in (
1881 "ip-address",
1882 "mac-address",
1883 ): # TODO: switch-xxxxx must be inserted
1884 vnfr_update[iface_text + ".{}".format(k)] = v
1885 vnfr_update_rollback[
1886 iface_text + ".{}".format(k)
1887 ] = vdur_interface.get(v)
1888 if pdu_interface.get("ip-address"):
1889 if vdur_interface.get(
1890 "mgmt-interface"
1891 ) or vdur_interface.get("mgmt-vnf"):
1892 vnfr_update_rollback[
1893 vdu_text + ".ip-address"
1894 ] = vdur.get("ip-address")
1895 vnfr_update[vdu_text + ".ip-address"] = pdu_interface[
1896 "ip-address"
1897 ]
1898 if vdur_interface.get("mgmt-vnf"):
1899 vnfr_update_rollback["ip-address"] = vnfr.get(
1900 "ip-address"
1901 )
1902 vnfr_update["ip-address"] = pdu_interface["ip-address"]
1903 vnfr_update[vdu_text + ".ip-address"] = pdu_interface[
1904 "ip-address"
1905 ]
1906 if pdu_interface.get("vim-network-name") or pdu_interface.get(
1907 "vim-network-id"
1908 ):
1909 ifaces_forcing_vim_network.append(
1910 {
1911 "name": vdur_interface.get("vnf-vld-id")
1912 or vdur_interface.get("ns-vld-id"),
1913 "vnf-vld-id": vdur_interface.get("vnf-vld-id"),
1914 "ns-vld-id": vdur_interface.get("ns-vld-id"),
1915 }
1916 )
1917 if pdu_interface.get("vim-network-id"):
1918 ifaces_forcing_vim_network[-1][
1919 "vim-network-id"
1920 ] = pdu_interface["vim-network-id"]
1921 if pdu_interface.get("vim-network-name"):
1922 ifaces_forcing_vim_network[-1][
1923 "vim-network-name"
1924 ] = pdu_interface["vim-network-name"]
1925 break
1927 return ifaces_forcing_vim_network
1929 def _look_for_k8scluster(
1930 self, session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback
1931 ):
1932 """
1933 Look for an available k8scluster for all the kuds in the vnfd matching version and cni requirements.
1934 Fills vnfr.kdur with the selected k8scluster
1936 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1937 :param rollback: list with the database modifications to rollback if needed
1938 :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found
1939 :param vim_account: vim_account where this vnfr should be deployed
1940 :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr
1941 :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback
1942 of the changed vnfr is needed
1944 :return: List of KDU interfaces that are connected to an existing VIM network. Each item contains:
1945 "vim-network-name": used at VIM
1946 "name": interface name
1947 "vnf-vld-id": internal VNFD vld where this interface is connected, or
1948 "ns-vld-id": NSD vld where this interface is connected.
1949 NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None
1950 """
1952 ifaces_forcing_vim_network = []
1953 if not vnfr.get("kdur"):
1954 return ifaces_forcing_vim_network
1956 kdu_filter = self._get_project_filter(session)
1957 kdu_filter["vim_account"] = vim_account
1958 # TODO kdu_filter["_admin.operationalState"] = "ENABLED"
1959 available_k8sclusters = self.db.get_list("k8sclusters", kdu_filter)
1961 k8s_requirements = {} # just for logging
1962 for k8scluster in available_k8sclusters:
1963 if not vnfr.get("k8s-cluster"):
1964 break
1965 # restrict by cni
1966 if vnfr["k8s-cluster"].get("cni"):
1967 k8s_requirements["cni"] = vnfr["k8s-cluster"]["cni"]
1968 if not set(vnfr["k8s-cluster"]["cni"]).intersection(
1969 k8scluster.get("cni", ())
1970 ):
1971 continue
1972 # restrict by version
1973 if vnfr["k8s-cluster"].get("version"):
1974 k8s_requirements["version"] = vnfr["k8s-cluster"]["version"]
1975 if k8scluster.get("k8s_version") not in vnfr["k8s-cluster"]["version"]:
1976 continue
1977 # restrict by number of networks
1978 if vnfr["k8s-cluster"].get("nets"):
1979 k8s_requirements["networks"] = len(vnfr["k8s-cluster"]["nets"])
1980 if not k8scluster.get("nets") or len(k8scluster["nets"]) < len(
1981 vnfr["k8s-cluster"]["nets"]
1982 ):
1983 continue
1984 break
1985 else:
1986 raise EngineException(
1987 "No k8scluster with requirements='{}' at vim_account={} found for member_vnf_index={}".format(
1988 k8s_requirements, vim_account, vnfr["member-vnf-index-ref"]
1989 )
1990 )
1992 for kdur_index, kdur in enumerate(get_iterable(vnfr.get("kdur"))):
1993 # step 3. Fill vnfr info by filling kdur
1994 kdu_text = "kdur.{}.".format(kdur_index)
1995 vnfr_update_rollback[kdu_text + "k8s-cluster.id"] = None
1996 vnfr_update[kdu_text + "k8s-cluster.id"] = k8scluster["_id"]
1998 # step 4. Check VIM networks that forces the selected k8s_cluster
1999 if vnfr.get("k8s-cluster") and vnfr["k8s-cluster"].get("nets"):
2000 k8scluster_net_list = list(k8scluster.get("nets").keys())
2001 for net_index, kdur_net in enumerate(vnfr["k8s-cluster"]["nets"]):
2002 # get a network from k8s_cluster nets. If name matches use this, if not use other
2003 if kdur_net["id"] in k8scluster_net_list: # name matches
2004 vim_net = k8scluster["nets"][kdur_net["id"]]
2005 k8scluster_net_list.remove(kdur_net["id"])
2006 else:
2007 vim_net = k8scluster["nets"][k8scluster_net_list[0]]
2008 k8scluster_net_list.pop(0)
2009 vnfr_update_rollback[
2010 "k8s-cluster.nets.{}.vim_net".format(net_index)
2011 ] = None
2012 vnfr_update["k8s-cluster.nets.{}.vim_net".format(net_index)] = vim_net
2013 if vim_net and (
2014 kdur_net.get("vnf-vld-id") or kdur_net.get("ns-vld-id")
2015 ):
2016 ifaces_forcing_vim_network.append(
2017 {
2018 "name": kdur_net.get("vnf-vld-id")
2019 or kdur_net.get("ns-vld-id"),
2020 "vnf-vld-id": kdur_net.get("vnf-vld-id"),
2021 "ns-vld-id": kdur_net.get("ns-vld-id"),
2022 "vim-network-name": vim_net, # TODO can it be vim-network-id ???
2023 }
2024 )
2025 # TODO check that this forcing is not incompatible with other forcing
2026 return ifaces_forcing_vim_network
2028 def _update_vnfrs_from_nsd(self, nsr):
2029 step = "Getting vnf_profiles from nsd" # first step must be defined outside try
2030 try:
2031 nsr_id = nsr["_id"]
2032 nsd = nsr["nsd"]
2034 vnf_profiles = nsd.get("df", [{}])[0].get("vnf-profile", ())
2035 vld_fixed_ip_connection_point_data = {}
2037 step = "Getting ip-address info from vnf_profile if it exists"
2038 for vnfp in vnf_profiles:
2039 # Checking ip-address info from nsd.vnf_profile and storing
2040 for vlc in vnfp.get("virtual-link-connectivity", ()):
2041 for cpd in vlc.get("constituent-cpd-id", ()):
2042 if cpd.get("ip-address"):
2043 step = "Storing ip-address info"
2044 vld_fixed_ip_connection_point_data.update(
2045 {
2046 vlc.get("virtual-link-profile-id")
2047 + "."
2048 + cpd.get("constituent-base-element-id"): {
2049 "vnfd-connection-point-ref": cpd.get(
2050 "constituent-cpd-id"
2051 ),
2052 "ip-address": cpd.get("ip-address"),
2053 }
2054 }
2055 )
2057 # Inserting ip address to vnfr
2058 if len(vld_fixed_ip_connection_point_data) > 0:
2059 step = "Getting vnfrs"
2060 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
2061 for item in vld_fixed_ip_connection_point_data.keys():
2062 step = "Filtering vnfrs"
2063 vnfr = next(
2064 filter(
2065 lambda vnfr: vnfr["member-vnf-index-ref"]
2066 == item.split(".")[1],
2067 vnfrs,
2068 ),
2069 None,
2070 )
2071 if vnfr:
2072 vnfr_update = {}
2073 for vdur_index, vdur in enumerate(vnfr["vdur"]):
2074 for iface_index, iface in enumerate(vdur["interfaces"]):
2075 step = "Looking for matched interface"
2076 if (
2077 iface.get("external-connection-point-ref")
2078 == vld_fixed_ip_connection_point_data[item].get(
2079 "vnfd-connection-point-ref"
2080 )
2081 and iface.get("ns-vld-id") == item.split(".")[0]
2082 ):
2083 vnfr_update_text = "vdur.{}.interfaces.{}".format(
2084 vdur_index, iface_index
2085 )
2086 step = "Storing info in order to update vnfr"
2087 vnfr_update[
2088 vnfr_update_text + ".ip-address"
2089 ] = increment_ip_mac(
2090 vld_fixed_ip_connection_point_data[item].get(
2091 "ip-address"
2092 ),
2093 vdur.get("count-index", 0),
2094 )
2095 vnfr_update[vnfr_update_text + ".fixed-ip"] = True
2097 step = "updating vnfr at database"
2098 self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, vnfr_update)
2099 except (
2100 ValidationError,
2101 EngineException,
2102 DbException,
2103 MsgException,
2104 FsException,
2105 ) as e:
2106 raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code)
2108 def _update_vnfrs(self, session, rollback, nsr, indata):
2109 # get vnfr
2110 nsr_id = nsr["_id"]
2111 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
2113 for vnfr in vnfrs:
2114 vnfr_update = {}
2115 vnfr_update_rollback = {}
2116 member_vnf_index = vnfr["member-vnf-index-ref"]
2117 # update vim-account-id
2119 vim_account = indata["vimAccountId"]
2120 vca_id = self._get_vim_account(vim_account, session).get("vca")
2121 # check instantiate parameters
2122 for vnf_inst_params in get_iterable(indata.get("vnf")):
2123 if vnf_inst_params["member-vnf-index"] != member_vnf_index:
2124 continue
2125 if vnf_inst_params.get("vimAccountId"):
2126 vim_account = vnf_inst_params.get("vimAccountId")
2127 vca_id = self._get_vim_account(vim_account, session).get("vca")
2129 # get vnf.vdu.interface instantiation params to update vnfr.vdur.interfaces ip, mac
2130 for vdu_inst_param in get_iterable(vnf_inst_params.get("vdu")):
2131 for vdur_index, vdur in enumerate(vnfr["vdur"]):
2132 if vdu_inst_param["id"] != vdur["vdu-id-ref"]:
2133 continue
2134 for iface_inst_param in get_iterable(
2135 vdu_inst_param.get("interface")
2136 ):
2137 iface_index, _ = next(
2138 i
2139 for i in enumerate(vdur["interfaces"])
2140 if i[1]["name"] == iface_inst_param["name"]
2141 )
2142 vnfr_update_text = "vdur.{}.interfaces.{}".format(
2143 vdur_index, iface_index
2144 )
2145 if iface_inst_param.get("ip-address"):
2146 vnfr_update[
2147 vnfr_update_text + ".ip-address"
2148 ] = increment_ip_mac(
2149 iface_inst_param.get("ip-address"),
2150 vdur.get("count-index", 0),
2151 )
2152 vnfr_update[vnfr_update_text + ".fixed-ip"] = True
2153 if iface_inst_param.get("mac-address"):
2154 vnfr_update[
2155 vnfr_update_text + ".mac-address"
2156 ] = increment_ip_mac(
2157 iface_inst_param.get("mac-address"),
2158 vdur.get("count-index", 0),
2159 )
2160 vnfr_update[vnfr_update_text + ".fixed-mac"] = True
2161 if iface_inst_param.get("floating-ip-required"):
2162 vnfr_update[
2163 vnfr_update_text + ".floating-ip-required"
2164 ] = True
2165 # get vnf.internal-vld.internal-conection-point instantiation params to update vnfr.vdur.interfaces
2166 # TODO update vld with the ip-profile
2167 for ivld_inst_param in get_iterable(
2168 vnf_inst_params.get("internal-vld")
2169 ):
2170 for icp_inst_param in get_iterable(
2171 ivld_inst_param.get("internal-connection-point")
2172 ):
2173 # look for iface
2174 for vdur_index, vdur in enumerate(vnfr["vdur"]):
2175 for iface_index, iface in enumerate(vdur["interfaces"]):
2176 if (
2177 iface.get("internal-connection-point-ref")
2178 == icp_inst_param["id-ref"]
2179 ):
2180 vnfr_update_text = "vdur.{}.interfaces.{}".format(
2181 vdur_index, iface_index
2182 )
2183 if icp_inst_param.get("ip-address"):
2184 vnfr_update[
2185 vnfr_update_text + ".ip-address"
2186 ] = increment_ip_mac(
2187 icp_inst_param.get("ip-address"),
2188 vdur.get("count-index", 0),
2189 )
2190 vnfr_update[
2191 vnfr_update_text + ".fixed-ip"
2192 ] = True
2193 if icp_inst_param.get("mac-address"):
2194 vnfr_update[
2195 vnfr_update_text + ".mac-address"
2196 ] = increment_ip_mac(
2197 icp_inst_param.get("mac-address"),
2198 vdur.get("count-index", 0),
2199 )
2200 vnfr_update[
2201 vnfr_update_text + ".fixed-mac"
2202 ] = True
2203 break
2204 # get ip address from instantiation parameters.vld.vnfd-connection-point-ref
2205 for vld_inst_param in get_iterable(indata.get("vld")):
2206 for vnfcp_inst_param in get_iterable(
2207 vld_inst_param.get("vnfd-connection-point-ref")
2208 ):
2209 if vnfcp_inst_param["member-vnf-index-ref"] != member_vnf_index:
2210 continue
2211 # look for iface
2212 for vdur_index, vdur in enumerate(vnfr["vdur"]):
2213 for iface_index, iface in enumerate(vdur["interfaces"]):
2214 if (
2215 iface.get("external-connection-point-ref")
2216 == vnfcp_inst_param["vnfd-connection-point-ref"]
2217 ):
2218 vnfr_update_text = "vdur.{}.interfaces.{}".format(
2219 vdur_index, iface_index
2220 )
2221 if vnfcp_inst_param.get("ip-address"):
2222 vnfr_update[
2223 vnfr_update_text + ".ip-address"
2224 ] = increment_ip_mac(
2225 vnfcp_inst_param.get("ip-address"),
2226 vdur.get("count-index", 0),
2227 )
2228 vnfr_update[vnfr_update_text + ".fixed-ip"] = True
2229 if vnfcp_inst_param.get("mac-address"):
2230 vnfr_update[
2231 vnfr_update_text + ".mac-address"
2232 ] = increment_ip_mac(
2233 vnfcp_inst_param.get("mac-address"),
2234 vdur.get("count-index", 0),
2235 )
2236 vnfr_update[vnfr_update_text + ".fixed-mac"] = True
2237 break
2239 vnfr_update["vim-account-id"] = vim_account
2240 vnfr_update_rollback["vim-account-id"] = vnfr.get("vim-account-id")
2242 if vca_id:
2243 vnfr_update["vca-id"] = vca_id
2244 vnfr_update_rollback["vca-id"] = vnfr.get("vca-id")
2246 # get pdu
2247 ifaces_forcing_vim_network = self._look_for_pdu(
2248 session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback
2249 )
2251 # get kdus
2252 ifaces_forcing_vim_network += self._look_for_k8scluster(
2253 session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback
2254 )
2255 # update database vnfr
2256 self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, vnfr_update)
2257 rollback.append(
2258 {
2259 "topic": "vnfrs",
2260 "_id": vnfr["_id"],
2261 "operation": "set",
2262 "content": vnfr_update_rollback,
2263 }
2264 )
2266 # Update indada in case pdu forces to use a concrete vim-network-name
2267 # TODO check if user has already insert a vim-network-name and raises an error
2268 if not ifaces_forcing_vim_network:
2269 continue
2270 for iface_info in ifaces_forcing_vim_network:
2271 if iface_info.get("ns-vld-id"):
2272 if "vld" not in indata:
2273 indata["vld"] = []
2274 indata["vld"].append(
2275 {
2276 key: iface_info[key]
2277 for key in ("name", "vim-network-name", "vim-network-id")
2278 if iface_info.get(key)
2279 }
2280 )
2282 elif iface_info.get("vnf-vld-id"):
2283 if "vnf" not in indata:
2284 indata["vnf"] = []
2285 indata["vnf"].append(
2286 {
2287 "member-vnf-index": member_vnf_index,
2288 "internal-vld": [
2289 {
2290 key: iface_info[key]
2291 for key in (
2292 "name",
2293 "vim-network-name",
2294 "vim-network-id",
2295 )
2296 if iface_info.get(key)
2297 }
2298 ],
2299 }
2300 )
2302 @staticmethod
2303 def _create_nslcmop(nsr_id, operation, params):
2304 """
2305 Creates a ns-lcm-opp content to be stored at database.
2306 :param nsr_id: internal id of the instance
2307 :param operation: instantiate, terminate, scale, action, update ...
2308 :param params: user parameters for the operation
2309 :return: dictionary following SOL005 format
2310 """
2311 now = time()
2312 _id = str(uuid4())
2313 nslcmop = {
2314 "id": _id,
2315 "_id": _id,
2316 "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
2317 "queuePosition": None,
2318 "stage": None,
2319 "errorMessage": None,
2320 "detailedStatus": None,
2321 "statusEnteredTime": now,
2322 "nsInstanceId": nsr_id,
2323 "lcmOperationType": operation,
2324 "startTime": now,
2325 "isAutomaticInvocation": False,
2326 "operationParams": params,
2327 "isCancelPending": False,
2328 "links": {
2329 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
2330 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
2331 },
2332 }
2333 return nslcmop
2335 def _get_enabled_vims(self, session):
2336 """
2337 Retrieve and return VIM accounts that are accessible by current user and has state ENABLE
2338 :param session: current session with user information
2339 """
2340 db_filter = self._get_project_filter(session)
2341 db_filter["_admin.operationalState"] = "ENABLED"
2342 vims = self.db.get_list("vim_accounts", db_filter)
2343 vimAccounts = []
2344 for vim in vims:
2345 vimAccounts.append(vim["_id"])
2346 return vimAccounts
2348 def new(
2349 self,
2350 rollback,
2351 session,
2352 indata=None,
2353 kwargs=None,
2354 headers=None,
2355 slice_object=False,
2356 ):
2357 """
2358 Performs a new operation over a ns
2359 :param rollback: list to append created items at database in case a rollback must to be done
2360 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
2361 :param indata: descriptor with the parameters of the operation. It must contains among others
2362 nsInstanceId: _id of the nsr to perform the operation
2363 operation: it can be: instantiate, terminate, action, update TODO: heal
2364 :param kwargs: used to override the indata descriptor
2365 :param headers: http request headers
2366 :return: id of the nslcmops
2367 """
2369 def check_if_nsr_is_not_slice_member(session, nsr_id):
2370 nsis = None
2371 db_filter = self._get_project_filter(session)
2372 db_filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_id
2373 nsis = self.db.get_one(
2374 "nsis", db_filter, fail_on_empty=False, fail_on_more=False
2375 )
2376 if nsis:
2377 raise EngineException(
2378 "The NS instance {} cannot be terminated because is used by the slice {}".format(
2379 nsr_id, nsis["_id"]
2380 ),
2381 http_code=HTTPStatus.CONFLICT,
2382 )
2384 try:
2385 # Override descriptor with query string kwargs
2386 self._update_input_with_kwargs(indata, kwargs, yaml_format=True)
2387 operation = indata["lcmOperationType"]
2388 nsInstanceId = indata["nsInstanceId"]
2390 validate_input(indata, self.operation_schema[operation])
2391 # get ns from nsr_id
2392 _filter = BaseTopic._get_project_filter(session)
2393 _filter["_id"] = nsInstanceId
2394 nsr = self.db.get_one("nsrs", _filter)
2396 # initial checking
2397 if operation == "terminate" and slice_object is False:
2398 check_if_nsr_is_not_slice_member(session, nsr["_id"])
2399 if (
2400 not nsr["_admin"].get("nsState")
2401 or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED"
2402 ):
2403 if operation == "terminate" and indata.get("autoremove"):
2404 # NSR must be deleted
2405 return (
2406 None,
2407 None,
2408 None,
2409 ) # a none in this case is used to indicate not instantiated. It can be removed
2410 if operation != "instantiate":
2411 raise EngineException(
2412 "ns_instance '{}' cannot be '{}' because it is not instantiated".format(
2413 nsInstanceId, operation
2414 ),
2415 HTTPStatus.CONFLICT,
2416 )
2417 else:
2418 if operation == "instantiate" and not session["force"]:
2419 raise EngineException(
2420 "ns_instance '{}' cannot be '{}' because it is already instantiated".format(
2421 nsInstanceId, operation
2422 ),
2423 HTTPStatus.CONFLICT,
2424 )
2425 self._check_ns_operation(session, nsr, operation, indata)
2426 if indata.get("primitive_params"):
2427 indata["primitive_params"] = json.dumps(indata["primitive_params"])
2428 elif indata.get("additionalParamsForVnf"):
2429 indata["additionalParamsForVnf"] = json.dumps(
2430 indata["additionalParamsForVnf"]
2431 )
2433 if operation == "instantiate":
2434 self._update_vnfrs_from_nsd(nsr)
2435 self._update_vnfrs(session, rollback, nsr, indata)
2436 if (operation == "update") and (indata["updateType"] == "CHANGE_VNFPKG"):
2437 nsr_update = {}
2438 vnfd_id = indata["changeVnfPackageData"]["vnfdId"]
2439 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
2440 nsd = self.db.get_one("nsds", {"_id": nsr["nsd-id"]})
2441 ns_request = nsr["instantiate_params"]
2442 vnfr = self.db.get_one(
2443 "vnfrs", {"_id": indata["changeVnfPackageData"]["vnfInstanceId"]}
2444 )
2445 latest_vnfd_revision = vnfd["_admin"].get("revision", 1)
2446 vnfr_vnfd_revision = vnfr.get("revision", 1)
2447 if latest_vnfd_revision != vnfr_vnfd_revision:
2448 old_vnfd_id = vnfd_id + ":" + str(vnfr_vnfd_revision)
2449 old_db_vnfd = self.db.get_one(
2450 "vnfds_revisions", {"_id": old_vnfd_id}
2451 )
2452 old_sw_version = old_db_vnfd.get("software-version", "1.0")
2453 new_sw_version = vnfd.get("software-version", "1.0")
2454 if new_sw_version != old_sw_version:
2455 vnf_index = vnfr["member-vnf-index-ref"]
2456 for vdu in vnfd["vdu"]:
2457 self.nsrtopic._add_shared_volumes_to_nsr(
2458 vdu, vnfd, nsr, vnf_index, latest_vnfd_revision
2459 )
2460 self.nsrtopic._add_flavor_to_nsr(
2461 vdu, vnfd, nsr, vnf_index, latest_vnfd_revision
2462 )
2463 sw_image_id = vdu.get("sw-image-desc")
2464 if sw_image_id:
2465 image_data = self.nsrtopic._get_image_data_from_vnfd(
2466 vnfd, sw_image_id
2467 )
2468 self.nsrtopic._add_image_to_nsr(nsr, image_data)
2469 for alt_image in vdu.get("alternative-sw-image-desc", ()):
2470 image_data = self.nsrtopic._get_image_data_from_vnfd(
2471 vnfd, alt_image
2472 )
2473 self.nsrtopic._add_image_to_nsr(nsr, image_data)
2474 nsr_update["image"] = nsr["image"]
2475 nsr_update["flavor"] = nsr["flavor"]
2476 nsr_update["shared-volumes"] = nsr["shared-volumes"]
2477 self.db.set_one("nsrs", {"_id": nsr["_id"]}, nsr_update)
2478 ns_k8s_namespace = self.nsrtopic._get_ns_k8s_namespace(
2479 nsd, ns_request, session
2480 )
2481 vnfr_descriptor = (
2482 self.nsrtopic._create_vnfr_descriptor_from_vnfd(
2483 nsd,
2484 vnfd,
2485 vnfd_id,
2486 vnf_index,
2487 nsr,
2488 ns_request,
2489 ns_k8s_namespace,
2490 latest_vnfd_revision,
2491 )
2492 )
2493 self._update_vnfrs_from_nsd(nsr)
2494 vnfr_new = self.db.get_one(
2495 "vnfrs",
2496 {"_id": indata["changeVnfPackageData"]["vnfInstanceId"]},
2497 )
2498 fixed_ip_dict = {}
2499 for vdu_record in vnfr_new.get("vdur"):
2500 if vdu_record.get("count-index") == 0:
2501 for interface in vdu_record.get("interfaces"):
2502 if (
2503 interface.get("external-connection-point-ref")
2504 and interface.get("fixed-ip") is True
2505 ):
2506 fixed_ip_dict[
2507 vdu_record.get("vdu-id-ref")
2508 ] = interface.get("ip-address")
2509 for new_vdu in vnfr_descriptor.get("vdur"):
2510 if fixed_ip_dict.get(new_vdu.get("vdu-id-ref")):
2511 for new_interface in new_vdu.get("interfaces"):
2512 if new_interface.get(
2513 "external-connection-point-ref"
2514 ):
2515 new_interface["ip-address"] = fixed_ip_dict.get(
2516 new_vdu.get("vdu-id-ref")
2517 )
2518 new_interface["fixed-ip"] = True
2519 indata["newVdur"] = vnfr_descriptor["vdur"]
2520 nslcmop_desc = self._create_nslcmop(nsInstanceId, operation, indata)
2521 _id = nslcmop_desc["_id"]
2522 nsName = nsr.get("name")
2523 self.format_on_new(
2524 nslcmop_desc, session["project_id"], make_public=session["public"]
2525 )
2526 if indata.get("placement-engine"):
2527 # Save valid vim accounts in lcm operation descriptor
2528 nslcmop_desc["operationParams"][
2529 "validVimAccounts"
2530 ] = self._get_enabled_vims(session)
2531 self.db.create("nslcmops", nslcmop_desc)
2532 rollback.append({"topic": "nslcmops", "_id": _id})
2533 if not slice_object:
2534 self.msg.write("ns", operation, nslcmop_desc)
2535 return _id, nsName, None
2536 except ValidationError as e: # TODO remove try Except, it is captured at nbi.py
2537 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
2538 # except DbException as e:
2539 # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND)
2541 def cancel(self, rollback, session, indata=None, kwargs=None, headers=None):
2542 validate_input(indata, self.operation_schema["cancel"])
2543 # Override descriptor with query string kwargs
2544 self._update_input_with_kwargs(indata, kwargs, yaml_format=True)
2545 nsLcmOpOccId = indata["nsLcmOpOccId"]
2546 cancelMode = indata["cancelMode"]
2547 # get nslcmop from nsLcmOpOccId
2548 _filter = BaseTopic._get_project_filter(session)
2549 _filter["_id"] = nsLcmOpOccId
2550 nslcmop = self.db.get_one("nslcmops", _filter)
2551 # Fail is this is not an ongoing nslcmop
2552 if nslcmop.get("operationState") not in [
2553 "STARTING",
2554 "PROCESSING",
2555 "ROLLING_BACK",
2556 ]:
2557 raise EngineException(
2558 "Operation is not in STARTING, PROCESSING or ROLLING_BACK state",
2559 http_code=HTTPStatus.CONFLICT,
2560 )
2561 nsInstanceId = nslcmop["nsInstanceId"]
2562 update_dict = {
2563 "isCancelPending": True,
2564 "cancelMode": cancelMode,
2565 }
2566 self.db.set_one(
2567 "nslcmops", q_filter=_filter, update_dict=update_dict, fail_on_empty=False
2568 )
2569 data = {
2570 "_id": nsLcmOpOccId,
2571 "nsInstanceId": nsInstanceId,
2572 "cancelMode": cancelMode,
2573 }
2574 self.msg.write("nslcmops", "cancel", data)
2576 def delete(self, session, _id, dry_run=False, not_send_msg=None):
2577 raise EngineException(
2578 "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR
2579 )
2581 def edit(self, session, _id, indata=None, kwargs=None, content=None):
2582 raise EngineException(
2583 "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR
2584 )
2587class NsiTopic(BaseTopic):
2588 topic = "nsis"
2589 topic_msg = "nsi"
2590 quota_name = "slice_instances"
2592 def __init__(self, db, fs, msg, auth):
2593 BaseTopic.__init__(self, db, fs, msg, auth)
2594 self.nsrTopic = NsrTopic(db, fs, msg, auth)
2596 @staticmethod
2597 def _format_ns_request(ns_request):
2598 formated_request = copy(ns_request)
2599 # TODO: Add request params
2600 return formated_request
2602 @staticmethod
2603 def _format_addional_params(slice_request):
2604 """
2605 Get and format user additional params for NS or VNF
2606 :param slice_request: User instantiation additional parameters
2607 :return: a formatted copy of additional params or None if not supplied
2608 """
2609 additional_params = copy(slice_request.get("additionalParamsForNsi"))
2610 if additional_params:
2611 for k, v in additional_params.items():
2612 if not isinstance(k, str):
2613 raise EngineException(
2614 "Invalid param at additionalParamsForNsi:{}. Only string keys are allowed".format(
2615 k
2616 )
2617 )
2618 if "." in k or "$" in k:
2619 raise EngineException(
2620 "Invalid param at additionalParamsForNsi:{}. Keys must not contain dots or $".format(
2621 k
2622 )
2623 )
2624 if isinstance(v, (dict, tuple, list)):
2625 additional_params[k] = "!!yaml " + safe_dump(v)
2626 return additional_params
2628 def check_conflict_on_del(self, session, _id, db_content):
2629 """
2630 Check that NSI is not instantiated
2631 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
2632 :param _id: nsi internal id
2633 :param db_content: The database content of the _id
2634 :return: None or raises EngineException with the conflict
2635 """
2636 if session["force"]:
2637 return
2638 nsi = db_content
2639 if nsi["_admin"].get("nsiState") == "INSTANTIATED":
2640 raise EngineException(
2641 "nsi '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
2642 "Launch 'terminate' operation first; or force deletion".format(_id),
2643 http_code=HTTPStatus.CONFLICT,
2644 )
2646 def delete_extra(self, session, _id, db_content, not_send_msg=None):
2647 """
2648 Deletes associated nsilcmops from database. Deletes associated filesystem.
2649 Set usageState of nst
2650 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
2651 :param _id: server internal id
2652 :param db_content: The database content of the descriptor
2653 :param not_send_msg: To not send message (False) or store content (list) instead
2654 :return: None if ok or raises EngineException with the problem
2655 """
2657 # Deleting the nsrs belonging to nsir
2658 nsir = db_content
2659 for nsrs_detailed_item in nsir["_admin"]["nsrs-detailed-list"]:
2660 nsr_id = nsrs_detailed_item["nsrId"]
2661 if nsrs_detailed_item.get("shared"):
2662 _filter = {
2663 "_admin.nsrs-detailed-list.ANYINDEX.shared": True,
2664 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nsr_id,
2665 "_id.ne": nsir["_id"],
2666 }
2667 nsi = self.db.get_one(
2668 "nsis", _filter, fail_on_empty=False, fail_on_more=False
2669 )
2670 if nsi: # last one using nsr
2671 continue
2672 try:
2673 self.nsrTopic.delete(
2674 session, nsr_id, dry_run=False, not_send_msg=not_send_msg
2675 )
2676 except (DbException, EngineException) as e:
2677 if e.http_code == HTTPStatus.NOT_FOUND:
2678 pass
2679 else:
2680 raise
2682 # delete related nsilcmops database entries
2683 self.db.del_list("nsilcmops", {"netsliceInstanceId": _id})
2685 # Check and set used NST usage state
2686 nsir_admin = nsir.get("_admin")
2687 if nsir_admin and nsir_admin.get("nst-id"):
2688 # check if used by another NSI
2689 nsis_list = self.db.get_one(
2690 "nsis",
2691 {"nst-id": nsir_admin["nst-id"]},
2692 fail_on_empty=False,
2693 fail_on_more=False,
2694 )
2695 if not nsis_list:
2696 self.db.set_one(
2697 "nsts",
2698 {"_id": nsir_admin["nst-id"]},
2699 {"_admin.usageState": "NOT_IN_USE"},
2700 )
2702 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
2703 """
2704 Creates a new netslice instance record into database. It also creates needed nsrs and vnfrs
2705 :param rollback: list to append the created items at database in case a rollback must be done
2706 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
2707 :param indata: params to be used for the nsir
2708 :param kwargs: used to override the indata descriptor
2709 :param headers: http request headers
2710 :return: the _id of nsi descriptor created at database
2711 """
2713 step = "checking quotas" # first step must be defined outside try
2714 try:
2715 self.check_quota(session)
2717 step = ""
2718 slice_request = self._remove_envelop(indata)
2719 # Override descriptor with query string kwargs
2720 self._update_input_with_kwargs(slice_request, kwargs)
2721 slice_request = self._validate_input_new(slice_request, session["force"])
2723 # look for nstd
2724 step = "getting nstd id='{}' from database".format(
2725 slice_request.get("nstId")
2726 )
2727 _filter = self._get_project_filter(session)
2728 _filter["_id"] = slice_request["nstId"]
2729 nstd = self.db.get_one("nsts", _filter)
2730 # check NST is not disabled
2731 step = "checking NST operationalState"
2732 if nstd["_admin"]["operationalState"] == "DISABLED":
2733 raise EngineException(
2734 "nst with id '{}' is DISABLED, and thus cannot be used to create a netslice "
2735 "instance".format(slice_request["nstId"]),
2736 http_code=HTTPStatus.CONFLICT,
2737 )
2738 del _filter["_id"]
2740 # check NSD is not disabled
2741 step = "checking operationalState"
2742 if nstd["_admin"]["operationalState"] == "DISABLED":
2743 raise EngineException(
2744 "nst with id '{}' is DISABLED, and thus cannot be used to create "
2745 "a network slice".format(slice_request["nstId"]),
2746 http_code=HTTPStatus.CONFLICT,
2747 )
2749 nstd.pop("_admin", None)
2750 nstd_id = nstd.pop("_id", None)
2751 nsi_id = str(uuid4())
2752 step = "filling nsi_descriptor with input data"
2754 # Creating the NSIR
2755 nsi_descriptor = {
2756 "id": nsi_id,
2757 "name": slice_request["nsiName"],
2758 "description": slice_request.get("nsiDescription", ""),
2759 "datacenter": slice_request["vimAccountId"],
2760 "nst-ref": nstd["id"],
2761 "instantiation_parameters": slice_request,
2762 "network-slice-template": nstd,
2763 "nsr-ref-list": [],
2764 "vlr-list": [],
2765 "_id": nsi_id,
2766 "additionalParamsForNsi": self._format_addional_params(slice_request),
2767 }
2769 step = "creating nsi at database"
2770 self.format_on_new(
2771 nsi_descriptor, session["project_id"], make_public=session["public"]
2772 )
2773 nsi_descriptor["_admin"]["nsiState"] = "NOT_INSTANTIATED"
2774 nsi_descriptor["_admin"]["netslice-subnet"] = None
2775 nsi_descriptor["_admin"]["deployed"] = {}
2776 nsi_descriptor["_admin"]["deployed"]["RO"] = []
2777 nsi_descriptor["_admin"]["nst-id"] = nstd_id
2779 # Creating netslice-vld for the RO.
2780 step = "creating netslice-vld at database"
2782 # Building the vlds list to be deployed
2783 # From netslice descriptors, creating the initial list
2784 nsi_vlds = []
2786 for netslice_vlds in get_iterable(nstd.get("netslice-vld")):
2787 # Getting template Instantiation parameters from NST
2788 nsi_vld = deepcopy(netslice_vlds)
2789 nsi_vld["shared-nsrs-list"] = []
2790 nsi_vld["vimAccountId"] = slice_request["vimAccountId"]
2791 nsi_vlds.append(nsi_vld)
2793 nsi_descriptor["_admin"]["netslice-vld"] = nsi_vlds
2794 # Creating netslice-subnet_record.
2795 needed_nsds = {}
2796 services = []
2798 # Updating the nstd with the nsd["_id"] associated to the nss -> services list
2799 for member_ns in nstd["netslice-subnet"]:
2800 nsd_id = member_ns["nsd-ref"]
2801 step = "getting nstd id='{}' constituent-nsd='{}' from database".format(
2802 member_ns["nsd-ref"], member_ns["id"]
2803 )
2804 if nsd_id not in needed_nsds:
2805 # Obtain nsd
2806 _filter["id"] = nsd_id
2807 nsd = self.db.get_one(
2808 "nsds", _filter, fail_on_empty=True, fail_on_more=True
2809 )
2810 del _filter["id"]
2811 nsd.pop("_admin")
2812 needed_nsds[nsd_id] = nsd
2813 else:
2814 nsd = needed_nsds[nsd_id]
2815 member_ns["_id"] = needed_nsds[nsd_id].get("_id")
2816 services.append(member_ns)
2818 step = "filling nsir nsd-id='{}' constituent-nsd='{}' from database".format(
2819 member_ns["nsd-ref"], member_ns["id"]
2820 )
2822 # creates Network Services records (NSRs)
2823 step = "creating nsrs at database using NsrTopic.new()"
2824 ns_params = slice_request.get("netslice-subnet")
2825 nsrs_list = []
2826 nsi_netslice_subnet = []
2827 for service in services:
2828 # Check if the netslice-subnet is shared and if it is share if the nss exists
2829 _id_nsr = None
2830 indata_ns = {}
2831 # Is the nss shared and instantiated?
2832 _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True
2833 _filter["_admin.nsrs-detailed-list.ANYINDEX.nsd-id"] = service[
2834 "nsd-ref"
2835 ]
2836 _filter["_admin.nsrs-detailed-list.ANYINDEX.nss-id"] = service["id"]
2837 nsi = self.db.get_one(
2838 "nsis", _filter, fail_on_empty=False, fail_on_more=False
2839 )
2840 if nsi and service.get("is-shared-nss"):
2841 nsrs_detailed_list = nsi["_admin"]["nsrs-detailed-list"]
2842 for nsrs_detailed_item in nsrs_detailed_list:
2843 if nsrs_detailed_item["nsd-id"] == service["nsd-ref"]:
2844 if nsrs_detailed_item["nss-id"] == service["id"]:
2845 _id_nsr = nsrs_detailed_item["nsrId"]
2846 break
2847 for netslice_subnet in nsi["_admin"]["netslice-subnet"]:
2848 if netslice_subnet["nss-id"] == service["id"]:
2849 indata_ns = netslice_subnet
2850 break
2851 else:
2852 indata_ns = {}
2853 if service.get("instantiation-parameters"):
2854 indata_ns = deepcopy(service["instantiation-parameters"])
2855 # del service["instantiation-parameters"]
2857 indata_ns["nsdId"] = service["_id"]
2858 indata_ns["nsName"] = (
2859 slice_request.get("nsiName") + "." + service["id"]
2860 )
2861 indata_ns["vimAccountId"] = slice_request.get("vimAccountId")
2862 indata_ns["nsDescription"] = service["description"]
2863 if slice_request.get("ssh_keys"):
2864 indata_ns["ssh_keys"] = slice_request.get("ssh_keys")
2866 if ns_params:
2867 for ns_param in ns_params:
2868 if ns_param.get("id") == service["id"]:
2869 copy_ns_param = deepcopy(ns_param)
2870 del copy_ns_param["id"]
2871 indata_ns.update(copy_ns_param)
2872 break
2874 # Creates Nsr objects
2875 _id_nsr, _ = self.nsrTopic.new(
2876 rollback, session, indata_ns, kwargs, headers
2877 )
2878 nsrs_item = {
2879 "nsrId": _id_nsr,
2880 "shared": service.get("is-shared-nss"),
2881 "nsd-id": service["nsd-ref"],
2882 "nss-id": service["id"],
2883 "nslcmop_instantiate": None,
2884 }
2885 indata_ns["nss-id"] = service["id"]
2886 nsrs_list.append(nsrs_item)
2887 nsi_netslice_subnet.append(indata_ns)
2888 nsr_ref = {"nsr-ref": _id_nsr}
2889 nsi_descriptor["nsr-ref-list"].append(nsr_ref)
2891 # Adding the nsrs list to the nsi
2892 nsi_descriptor["_admin"]["nsrs-detailed-list"] = nsrs_list
2893 nsi_descriptor["_admin"]["netslice-subnet"] = nsi_netslice_subnet
2894 self.db.set_one(
2895 "nsts", {"_id": slice_request["nstId"]}, {"_admin.usageState": "IN_USE"}
2896 )
2898 # Creating the entry in the database
2899 self.db.create("nsis", nsi_descriptor)
2900 rollback.append({"topic": "nsis", "_id": nsi_id})
2901 return nsi_id, None
2902 except ValidationError as e:
2903 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
2904 except Exception as e: # TODO remove try Except, it is captured at nbi.py
2905 # self.logger.exception(
2906 # "Exception {} at NsiTopic.new()".format(e), exc_info=True
2907 # )
2908 raise EngineException("Error {}: {}".format(step, e))
2910 def edit(self, session, _id, indata=None, kwargs=None, content=None):
2911 raise EngineException(
2912 "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR
2913 )
2916class NsiLcmOpTopic(BaseTopic):
2917 topic = "nsilcmops"
2918 topic_msg = "nsi"
2919 operation_schema = { # mapping between operation and jsonschema to validate
2920 "instantiate": nsi_instantiate,
2921 "terminate": None,
2922 }
2924 def __init__(self, db, fs, msg, auth):
2925 BaseTopic.__init__(self, db, fs, msg, auth)
2926 self.nsi_NsLcmOpTopic = NsLcmOpTopic(self.db, self.fs, self.msg, self.auth)
2928 def _check_nsi_operation(self, session, nsir, operation, indata):
2929 """
2930 Check that user has enter right parameters for the operation
2931 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
2932 :param operation: it can be: instantiate, terminate, action, TODO: update, heal
2933 :param indata: descriptor with the parameters of the operation
2934 :return: None
2935 """
2936 nsds = {}
2937 nstd = nsir["network-slice-template"]
2939 def check_valid_netslice_subnet_id(nstId):
2940 # TODO change to vnfR (??)
2941 for netslice_subnet in nstd["netslice-subnet"]:
2942 if nstId == netslice_subnet["id"]:
2943 nsd_id = netslice_subnet["nsd-ref"]
2944 if nsd_id not in nsds:
2945 _filter = self._get_project_filter(session)
2946 _filter["id"] = nsd_id
2947 nsds[nsd_id] = self.db.get_one("nsds", _filter)
2948 return nsds[nsd_id]
2949 else:
2950 raise EngineException(
2951 "Invalid parameter nstId='{}' is not one of the "
2952 "nst:netslice-subnet".format(nstId)
2953 )
2955 if operation == "instantiate":
2956 # check the existance of netslice-subnet items
2957 for in_nst in get_iterable(indata.get("netslice-subnet")):
2958 check_valid_netslice_subnet_id(in_nst["id"])
2960 def _create_nsilcmop(self, session, netsliceInstanceId, operation, params):
2961 now = time()
2962 _id = str(uuid4())
2963 nsilcmop = {
2964 "id": _id,
2965 "_id": _id,
2966 "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
2967 "statusEnteredTime": now,
2968 "netsliceInstanceId": netsliceInstanceId,
2969 "lcmOperationType": operation,
2970 "startTime": now,
2971 "isAutomaticInvocation": False,
2972 "operationParams": params,
2973 "isCancelPending": False,
2974 "links": {
2975 "self": "/osm/nsilcm/v1/nsi_lcm_op_occs/" + _id,
2976 "netsliceInstanceId": "/osm/nsilcm/v1/netslice_instances/"
2977 + netsliceInstanceId,
2978 },
2979 }
2980 return nsilcmop
2982 def add_shared_nsr_2vld(self, nsir, nsr_item):
2983 for nst_sb_item in nsir["network-slice-template"].get("netslice-subnet"):
2984 if nst_sb_item.get("is-shared-nss"):
2985 for admin_subnet_item in nsir["_admin"].get("netslice-subnet"):
2986 if admin_subnet_item["nss-id"] == nst_sb_item["id"]:
2987 for admin_vld_item in nsir["_admin"].get("netslice-vld"):
2988 for admin_vld_nss_cp_ref_item in admin_vld_item[
2989 "nss-connection-point-ref"
2990 ]:
2991 if (
2992 admin_subnet_item["nss-id"]
2993 == admin_vld_nss_cp_ref_item["nss-ref"]
2994 ):
2995 if (
2996 not nsr_item["nsrId"]
2997 in admin_vld_item["shared-nsrs-list"]
2998 ):
2999 admin_vld_item["shared-nsrs-list"].append(
3000 nsr_item["nsrId"]
3001 )
3002 break
3003 # self.db.set_one("nsis", {"_id": nsir["_id"]}, nsir)
3004 self.db.set_one(
3005 "nsis",
3006 {"_id": nsir["_id"]},
3007 {"_admin.netslice-vld": nsir["_admin"].get("netslice-vld")},
3008 )
3010 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
3011 """
3012 Performs a new operation over a ns
3013 :param rollback: list to append created items at database in case a rollback must to be done
3014 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
3015 :param indata: descriptor with the parameters of the operation. It must contains among others
3016 netsliceInstanceId: _id of the nsir to perform the operation
3017 operation: it can be: instantiate, terminate, action, TODO: update, heal
3018 :param kwargs: used to override the indata descriptor
3019 :param headers: http request headers
3020 :return: id of the nslcmops
3021 """
3022 try:
3023 # Override descriptor with query string kwargs
3024 self._update_input_with_kwargs(indata, kwargs)
3025 operation = indata["lcmOperationType"]
3026 netsliceInstanceId = indata["netsliceInstanceId"]
3027 validate_input(indata, self.operation_schema[operation])
3029 # get nsi from netsliceInstanceId
3030 _filter = self._get_project_filter(session)
3031 _filter["_id"] = netsliceInstanceId
3032 nsir = self.db.get_one("nsis", _filter)
3033 # logging_prefix = "nsi={} {} ".format(netsliceInstanceId, operation)
3034 del _filter["_id"]
3036 # initial checking
3037 if (
3038 not nsir["_admin"].get("nsiState")
3039 or nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED"
3040 ):
3041 if operation == "terminate" and indata.get("autoremove"):
3042 # NSIR must be deleted
3043 return (
3044 None,
3045 None,
3046 ) # a none in this case is used to indicate not instantiated. It can be removed
3047 if operation != "instantiate":
3048 raise EngineException(
3049 "netslice_instance '{}' cannot be '{}' because it is not instantiated".format(
3050 netsliceInstanceId, operation
3051 ),
3052 HTTPStatus.CONFLICT,
3053 )
3054 else:
3055 if operation == "instantiate" and not session["force"]:
3056 raise EngineException(
3057 "netslice_instance '{}' cannot be '{}' because it is already instantiated".format(
3058 netsliceInstanceId, operation
3059 ),
3060 HTTPStatus.CONFLICT,
3061 )
3063 # Creating all the NS_operation (nslcmop)
3064 # Get service list from db
3065 nsrs_list = nsir["_admin"]["nsrs-detailed-list"]
3066 nslcmops = []
3067 # nslcmops_item = None
3068 for index, nsr_item in enumerate(nsrs_list):
3069 nsr_id = nsr_item["nsrId"]
3070 if nsr_item.get("shared"):
3071 _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True
3072 _filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_id
3073 _filter[
3074 "_admin.nsrs-detailed-list.ANYINDEX.nslcmop_instantiate.ne"
3075 ] = None
3076 _filter["_id.ne"] = netsliceInstanceId
3077 nsi = self.db.get_one(
3078 "nsis", _filter, fail_on_empty=False, fail_on_more=False
3079 )
3080 if operation == "terminate":
3081 _update = {
3082 "_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format(
3083 index
3084 ): None
3085 }
3086 self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
3087 if (
3088 nsi
3089 ): # other nsi is using this nsr and it needs this nsr instantiated
3090 continue # do not create nsilcmop
3091 else: # instantiate
3092 # looks the first nsi fulfilling the conditions but not being the current NSIR
3093 if nsi:
3094 nsi_nsr_item = next(
3095 n
3096 for n in nsi["_admin"]["nsrs-detailed-list"]
3097 if n["nsrId"] == nsr_id
3098 and n["shared"]
3099 and n["nslcmop_instantiate"]
3100 )
3101 self.add_shared_nsr_2vld(nsir, nsr_item)
3102 nslcmops.append(nsi_nsr_item["nslcmop_instantiate"])
3103 _update = {
3104 "_admin.nsrs-detailed-list.{}".format(
3105 index
3106 ): nsi_nsr_item
3107 }
3108 self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
3109 # continue to not create nslcmop since nsrs is shared and nsrs was created
3110 continue
3111 else:
3112 self.add_shared_nsr_2vld(nsir, nsr_item)
3114 # create operation
3115 try:
3116 indata_ns = {
3117 "lcmOperationType": operation,
3118 "nsInstanceId": nsr_id,
3119 # Including netslice_id in the ns instantiate Operation
3120 "netsliceInstanceId": netsliceInstanceId,
3121 }
3122 if operation == "instantiate":
3123 service = self.db.get_one("nsrs", {"_id": nsr_id})
3124 indata_ns.update(service["instantiate_params"])
3126 # Creating NS_LCM_OP with the flag slice_object=True to not trigger the service instantiation
3127 # message via kafka bus
3128 nslcmop, _, _ = self.nsi_NsLcmOpTopic.new(
3129 rollback, session, indata_ns, None, headers, slice_object=True
3130 )
3131 nslcmops.append(nslcmop)
3132 if operation == "instantiate":
3133 _update = {
3134 "_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format(
3135 index
3136 ): nslcmop
3137 }
3138 self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
3139 except (DbException, EngineException) as e:
3140 if e.http_code == HTTPStatus.NOT_FOUND:
3141 pass
3142 else:
3143 raise
3145 # Creates nsilcmop
3146 indata["nslcmops_ids"] = nslcmops
3147 self._check_nsi_operation(session, nsir, operation, indata)
3149 nsilcmop_desc = self._create_nsilcmop(
3150 session, netsliceInstanceId, operation, indata
3151 )
3152 self.format_on_new(
3153 nsilcmop_desc, session["project_id"], make_public=session["public"]
3154 )
3155 _id = self.db.create("nsilcmops", nsilcmop_desc)
3156 rollback.append({"topic": "nsilcmops", "_id": _id})
3157 self.msg.write("nsi", operation, nsilcmop_desc)
3158 return _id, None
3159 except ValidationError as e:
3160 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
3162 def delete(self, session, _id, dry_run=False, not_send_msg=None):
3163 raise EngineException(
3164 "Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR
3165 )
3167 def edit(self, session, _id, indata=None, kwargs=None, content=None):
3168 raise EngineException(
3169 "Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR
3170 )