blob: d58fc3dbecae4c18cdcb2018c77d1ba056621d9b [file] [log] [blame]
tiernob24258a2018-10-04 18:39:49 +02001# -*- coding: utf-8 -*-
2
tiernod125caf2018-11-22 16:05:54 +00003# 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.
15
tiernob24258a2018-10-04 18:39:49 +020016# import logging
17from uuid import uuid4
18from http import HTTPStatus
19from time import time
tiernocc103432018-10-19 14:10:35 +020020from copy import copy, deepcopy
tierno1c38f2f2020-03-24 11:51:39 +000021from osm_nbi.validation import validate_input, ValidationError, ns_instantiate, ns_terminate, ns_action, ns_scale,\
22 nsi_instantiate
tierno714954e2019-11-29 13:43:26 +000023from osm_nbi.base_topic import BaseTopic, EngineException, get_iterable, deep_get
tiernob4844ab2019-05-23 08:42:12 +000024# from descriptor_topics import DescriptorTopic
tiernobee085c2018-12-12 17:03:04 +000025from yaml import safe_dump
Felipe Vicens09e65422019-01-22 15:06:46 +010026from osm_common.dbbase import DbException
tierno1bfe4e22019-09-02 16:03:25 +000027from osm_common.msgbase import MsgException
28from osm_common.fsbase import FsException
delacruzramo36ffe552019-05-03 14:52:37 +020029from re import match # For checking that additional parameter names are valid Jinja2 identifiers
tiernob24258a2018-10-04 18:39:49 +020030
31__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
32
33
34class NsrTopic(BaseTopic):
35 topic = "nsrs"
36 topic_msg = "ns"
tierno6b02b052020-06-02 10:07:41 +000037 quota_name = "ns_instances"
tiernod77ba6f2019-06-27 14:31:10 +000038 schema_new = ns_instantiate
tiernob24258a2018-10-04 18:39:49 +020039
delacruzramo32bab472019-09-13 12:24:22 +020040 def __init__(self, db, fs, msg, auth):
41 BaseTopic.__init__(self, db, fs, msg, auth)
tiernob24258a2018-10-04 18:39:49 +020042
43 def _check_descriptor_dependencies(self, session, descriptor):
44 """
45 Check that the dependent descriptors exist on a new descriptor or edition
46 :param session: client session information
47 :param descriptor: descriptor to be inserted or edit
48 :return: None or raises exception
49 """
50 if not descriptor.get("nsdId"):
51 return
52 nsd_id = descriptor["nsdId"]
53 if not self.get_item_list(session, "nsds", {"id": nsd_id}):
54 raise EngineException("Descriptor error at nsdId='{}' references a non exist nsd".format(nsd_id),
55 http_code=HTTPStatus.CONFLICT)
56
57 @staticmethod
58 def format_on_new(content, project_id=None, make_public=False):
59 BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public)
60 content["_admin"]["nsState"] = "NOT_INSTANTIATED"
tiernobdebce92019-07-01 15:36:49 +000061 return None
tiernob24258a2018-10-04 18:39:49 +020062
tiernob4844ab2019-05-23 08:42:12 +000063 def check_conflict_on_del(self, session, _id, db_content):
64 """
65 Check that NSR is not instantiated
66 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
67 :param _id: nsr internal id
68 :param db_content: The database content of the nsr
69 :return: None or raises EngineException with the conflict
70 """
tierno65ca36d2019-02-12 19:27:52 +010071 if session["force"]:
tiernob24258a2018-10-04 18:39:49 +020072 return
tiernob4844ab2019-05-23 08:42:12 +000073 nsr = db_content
tiernob24258a2018-10-04 18:39:49 +020074 if nsr["_admin"].get("nsState") == "INSTANTIATED":
75 raise EngineException("nsr '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
76 "Launch 'terminate' operation first; or force deletion".format(_id),
77 http_code=HTTPStatus.CONFLICT)
78
tiernobee3bad2019-12-05 12:26:01 +000079 def delete_extra(self, session, _id, db_content, not_send_msg=None):
tiernob4844ab2019-05-23 08:42:12 +000080 """
81 Deletes associated nslcmops and vnfrs from database. Deletes associated filesystem.
82 Set usageState of pdu, vnfd, nsd
83 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
84 :param _id: server internal id
85 :param db_content: The database content of the descriptor
tiernobee3bad2019-12-05 12:26:01 +000086 :param not_send_msg: To not send message (False) or store content (list) instead
tiernob4844ab2019-05-23 08:42:12 +000087 :return: None if ok or raises EngineException with the problem
88 """
tiernobee085c2018-12-12 17:03:04 +000089 self.fs.file_delete(_id, ignore_non_exist=True)
tiernob24258a2018-10-04 18:39:49 +020090 self.db.del_list("nslcmops", {"nsInstanceId": _id})
91 self.db.del_list("vnfrs", {"nsr-id-ref": _id})
tiernob4844ab2019-05-23 08:42:12 +000092
tiernob24258a2018-10-04 18:39:49 +020093 # set all used pdus as free
94 self.db.set_list("pdus", {"_admin.usage.nsr_id": _id},
tierno36ec8602018-11-02 17:27:11 +010095 {"_admin.usageState": "NOT_IN_USE", "_admin.usage": None})
tiernob24258a2018-10-04 18:39:49 +020096
tiernob4844ab2019-05-23 08:42:12 +000097 # Set NSD usageState
98 nsr = db_content
99 used_nsd_id = nsr.get("nsd-id")
100 if used_nsd_id:
101 # check if used by another NSR
102 nsrs_list = self.db.get_one("nsrs", {"nsd-id": used_nsd_id},
103 fail_on_empty=False, fail_on_more=False)
104 if not nsrs_list:
105 self.db.set_one("nsds", {"_id": used_nsd_id}, {"_admin.usageState": "NOT_IN_USE"})
106
107 # Set VNFD usageState
108 used_vnfd_id_list = nsr.get("vnfd-id")
109 if used_vnfd_id_list:
110 for used_vnfd_id in used_vnfd_id_list:
111 # check if used by another NSR
112 nsrs_list = self.db.get_one("nsrs", {"vnfd-id": used_vnfd_id},
113 fail_on_empty=False, fail_on_more=False)
114 if not nsrs_list:
115 self.db.set_one("vnfds", {"_id": used_vnfd_id}, {"_admin.usageState": "NOT_IN_USE"})
116
tiernobee085c2018-12-12 17:03:04 +0000117 @staticmethod
118 def _format_ns_request(ns_request):
119 formated_request = copy(ns_request)
120 formated_request.pop("additionalParamsForNs", None)
121 formated_request.pop("additionalParamsForVnf", None)
122 return formated_request
123
124 @staticmethod
tiernoe19707b2020-04-21 13:08:04 +0000125 def _format_additional_params(ns_request, member_vnf_index=None, vdu_id=None, kdu_name=None, descriptor=None):
tiernobee085c2018-12-12 17:03:04 +0000126 """
127 Get and format user additional params for NS or VNF
128 :param ns_request: User instantiation additional parameters
129 :param member_vnf_index: None for extract NS params, or member_vnf_index to extract VNF params
130 :param descriptor: If not None it check that needed parameters of descriptor are supplied
tierno54db2e42020-04-06 15:29:42 +0000131 :return: tuple with a formatted copy of additional params or None if not supplied, plus other parameters
tiernobee085c2018-12-12 17:03:04 +0000132 """
133 additional_params = None
tierno54db2e42020-04-06 15:29:42 +0000134 other_params = None
tiernobee085c2018-12-12 17:03:04 +0000135 if not member_vnf_index:
136 additional_params = copy(ns_request.get("additionalParamsForNs"))
137 where_ = "additionalParamsForNs"
138 elif ns_request.get("additionalParamsForVnf"):
tierno714954e2019-11-29 13:43:26 +0000139 where_ = "additionalParamsForVnf[member-vnf-index={}]".format(member_vnf_index)
140 item = next((x for x in ns_request["additionalParamsForVnf"] if x["member-vnf-index"] == member_vnf_index),
141 None)
142 if item:
tierno54db2e42020-04-06 15:29:42 +0000143 if not vdu_id and not kdu_name:
144 other_params = item
tierno714954e2019-11-29 13:43:26 +0000145 additional_params = copy(item.get("additionalParams")) or {}
146 if vdu_id and item.get("additionalParamsForVdu"):
147 item_vdu = next((x for x in item["additionalParamsForVdu"] if x["vdu_id"] == vdu_id), None)
tiernobce98f02020-04-17 11:27:47 +0000148 other_params = item_vdu
tierno714954e2019-11-29 13:43:26 +0000149 if item_vdu and item_vdu.get("additionalParams"):
150 where_ += ".additionalParamsForVdu[vdu_id={}]".format(vdu_id)
tiernob091dc12019-12-02 15:53:25 +0000151 additional_params = item_vdu["additionalParams"]
152 if kdu_name:
153 additional_params = {}
154 if item.get("additionalParamsForKdu"):
155 item_kdu = next((x for x in item["additionalParamsForKdu"] if x["kdu_name"] == kdu_name), None)
tiernobce98f02020-04-17 11:27:47 +0000156 other_params = item_kdu
tiernob091dc12019-12-02 15:53:25 +0000157 if item_kdu and item_kdu.get("additionalParams"):
158 where_ += ".additionalParamsForKdu[kdu_name={}]".format(kdu_name)
159 additional_params = item_kdu["additionalParams"]
tierno714954e2019-11-29 13:43:26 +0000160
tiernobee085c2018-12-12 17:03:04 +0000161 if additional_params:
162 for k, v in additional_params.items():
tierno714954e2019-11-29 13:43:26 +0000163 # BEGIN Check that additional parameter names are valid Jinja2 identifiers if target is not Kdu
164 if not kdu_name and not match('^[a-zA-Z_][a-zA-Z0-9_]*$', k):
delacruzramo36ffe552019-05-03 14:52:37 +0200165 raise EngineException("Invalid param name at {}:{}. Must contain only alphanumeric characters "
166 "and underscores, and cannot start with a digit"
167 .format(where_, k))
168 # END Check that additional parameter names are valid Jinja2 identifiers
tiernobee085c2018-12-12 17:03:04 +0000169 if not isinstance(k, str):
170 raise EngineException("Invalid param at {}:{}. Only string keys are allowed".format(where_, k))
171 if "." in k or "$" in k:
172 raise EngineException("Invalid param at {}:{}. Keys must not contain dots or $".format(where_, k))
173 if isinstance(v, (dict, tuple, list)):
174 additional_params[k] = "!!yaml " + safe_dump(v)
175
176 if descriptor:
177 # check that enough parameters are supplied for the initial-config-primitive
178 # TODO: check for cloud-init
179 if member_vnf_index:
tierno714954e2019-11-29 13:43:26 +0000180 if kdu_name:
181 initial_primitives = None
182 elif vdu_id:
183 vdud = next(x for x in descriptor["vdu"] if x["id"] == vdu_id)
184 initial_primitives = deep_get(vdud, ("vdu-configuration", "initial-config-primitive"))
185 else:
186 initial_primitives = deep_get(descriptor, ("vnf-configuration", "initial-config-primitive"))
187 else:
188 initial_primitives = deep_get(descriptor, ("ns-configuration", "initial-config-primitive"))
tiernobee085c2018-12-12 17:03:04 +0000189
tierno714954e2019-11-29 13:43:26 +0000190 for initial_primitive in get_iterable(initial_primitives):
191 for param in get_iterable(initial_primitive.get("parameter")):
192 if param["value"].startswith("<") and param["value"].endswith(">"):
193 if param["value"] in ("<rw_mgmt_ip>", "<VDU_SCALE_INFO>", "<ns_config_info>"):
194 continue
195 if not additional_params or param["value"][1:-1] not in additional_params:
196 raise EngineException("Parameter '{}' needed for vnfd[id={}]:vnf-configuration:"
197 "initial-config-primitive[name={}] not supplied".
198 format(param["value"], descriptor["id"],
199 initial_primitive["name"]))
200
tierno54db2e42020-04-06 15:29:42 +0000201 return additional_params or None, other_params or None
tiernobee085c2018-12-12 17:03:04 +0000202
tierno65ca36d2019-02-12 19:27:52 +0100203 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
tiernob24258a2018-10-04 18:39:49 +0200204 """
205 Creates a new nsr into database. It also creates needed vnfrs
206 :param rollback: list to append the created items at database in case a rollback must be done
tierno65ca36d2019-02-12 19:27:52 +0100207 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tiernob24258a2018-10-04 18:39:49 +0200208 :param indata: params to be used for the nsr
209 :param kwargs: used to override the indata descriptor
210 :param headers: http request headers
tierno1bfe4e22019-09-02 16:03:25 +0000211 :return: the _id of nsr descriptor created at database. Or an exception of type
212 EngineException, ValidationError, DbException, FsException, MsgException.
213 Note: Exceptions are not captured on purpose. They should be captured at called
tiernob24258a2018-10-04 18:39:49 +0200214 """
215
216 try:
delacruzramo32bab472019-09-13 12:24:22 +0200217 step = "checking quotas"
218 self.check_quota(session)
219
tierno99d4b172019-07-02 09:28:40 +0000220 step = "validating input parameters"
tiernob24258a2018-10-04 18:39:49 +0200221 ns_request = self._remove_envelop(indata)
222 # Override descriptor with query string kwargs
223 self._update_input_with_kwargs(ns_request, kwargs)
tierno65ca36d2019-02-12 19:27:52 +0100224 self._validate_input_new(ns_request, session["force"])
tiernob24258a2018-10-04 18:39:49 +0200225
tierno54db2e42020-04-06 15:29:42 +0000226 # look for nsd
tiernob24258a2018-10-04 18:39:49 +0200227 step = "getting nsd id='{}' from database".format(ns_request.get("nsdId"))
tiernob4844ab2019-05-23 08:42:12 +0000228 _filter = self._get_project_filter(session)
229 _filter["_id"] = ns_request["nsdId"]
tiernob24258a2018-10-04 18:39:49 +0200230 nsd = self.db.get_one("nsds", _filter)
tiernob4844ab2019-05-23 08:42:12 +0000231 del _filter["_id"]
tiernob24258a2018-10-04 18:39:49 +0200232
233 nsr_id = str(uuid4())
tiernobee085c2018-12-12 17:03:04 +0000234
tiernob24258a2018-10-04 18:39:49 +0200235 now = time()
236 step = "filling nsr from input data"
tiernoe19707b2020-04-21 13:08:04 +0000237 additional_params, _ = self._format_additional_params(ns_request, descriptor=nsd)
tierno54db2e42020-04-06 15:29:42 +0000238
239 # use for k8s-namespace from ns_request or additionalParamsForNs. By default, the project_id
240 ns_k8s_namespace = session["project_id"][0] if session["project_id"] else None
241 if ns_request and ns_request.get("k8s-namespace"):
242 ns_k8s_namespace = ns_request["k8s-namespace"]
243 if additional_params and additional_params.get("k8s-namespace"):
244 ns_k8s_namespace = additional_params["k8s-namespace"]
245
tiernob24258a2018-10-04 18:39:49 +0200246 nsr_descriptor = {
247 "name": ns_request["nsName"],
248 "name-ref": ns_request["nsName"],
249 "short-name": ns_request["nsName"],
250 "admin-status": "ENABLED",
tiernoecf94bd2020-01-09 12:40:45 +0000251 "nsState": "NOT_INSTANTIATED",
252 "currentOperation": "IDLE",
253 "currentOperationID": None,
254 "errorDescription": None,
255 "errorDetail": None,
256 "deploymentStatus": None,
257 "configurationStatus": None,
258 "vcaStatus": None,
tierno340df482020-04-03 10:09:06 +0000259 "nsd": {k: v for k, v in nsd.items() if k in ("vld", "_id", "id", "constituent-vnfd", "name")},
tiernob24258a2018-10-04 18:39:49 +0200260 "datacenter": ns_request["vimAccountId"],
261 "resource-orchestrator": "osmopenmano",
262 "description": ns_request.get("nsDescription", ""),
263 "constituent-vnfr-ref": [],
264
265 "operational-status": "init", # typedef ns-operational-
266 "config-status": "init", # typedef config-states
267 "detailed-status": "scheduled",
268
269 "orchestration-progress": {},
270 # {"networks": {"active": 0, "total": 0}, "vms": {"active": 0, "total": 0}},
271
tierno65ca36d2019-02-12 19:27:52 +0100272 "create-time": now,
tiernob24258a2018-10-04 18:39:49 +0200273 "nsd-name-ref": nsd["name"],
274 "operational-events": [], # "id", "timestamp", "description", "event",
275 "nsd-ref": nsd["id"],
tiernof0637052019-03-07 16:26:47 +0000276 "nsd-id": nsd["_id"],
tiernob4844ab2019-05-23 08:42:12 +0000277 "vnfd-id": [],
tiernobee085c2018-12-12 17:03:04 +0000278 "instantiate_params": self._format_ns_request(ns_request),
tierno54db2e42020-04-06 15:29:42 +0000279 "additionalParamsForNs": additional_params,
tiernob24258a2018-10-04 18:39:49 +0200280 "ns-instance-config-ref": nsr_id,
281 "id": nsr_id,
282 "_id": nsr_id,
283 # "input-parameter": xpath, value,
tierno99d4b172019-07-02 09:28:40 +0000284 "ssh-authorized-key": ns_request.get("ssh_keys"), # TODO remove
tiernob24258a2018-10-04 18:39:49 +0200285 }
286 ns_request["nsr_id"] = nsr_id
tierno36ec8602018-11-02 17:27:11 +0100287 # Create vld
288 if nsd.get("vld"):
tierno340df482020-04-03 10:09:06 +0000289 nsr_descriptor["vld"] = nsd["vld"]
tiernob24258a2018-10-04 18:39:49 +0200290
291 # Create VNFR
292 needed_vnfds = {}
gcalvino4f269dd2018-11-06 13:18:31 +0100293 for member_vnf in nsd.get("constituent-vnfd", ()):
tiernob24258a2018-10-04 18:39:49 +0200294 vnfd_id = member_vnf["vnfd-id-ref"]
295 step = "getting vnfd id='{}' constituent-vnfd='{}' from database".format(
296 member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"])
297 if vnfd_id not in needed_vnfds:
298 # Obtain vnfd
tiernob4844ab2019-05-23 08:42:12 +0000299 _filter["id"] = vnfd_id
300 vnfd = self.db.get_one("vnfds", _filter, fail_on_empty=True, fail_on_more=True)
301 del _filter["id"]
tiernob24258a2018-10-04 18:39:49 +0200302 vnfd.pop("_admin")
303 needed_vnfds[vnfd_id] = vnfd
tiernob4844ab2019-05-23 08:42:12 +0000304 nsr_descriptor["vnfd-id"].append(vnfd["_id"])
tiernob24258a2018-10-04 18:39:49 +0200305 else:
306 vnfd = needed_vnfds[vnfd_id]
307 step = "filling vnfr vnfd-id='{}' constituent-vnfd='{}'".format(
308 member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"])
309 vnfr_id = str(uuid4())
tiernoe19707b2020-04-21 13:08:04 +0000310 additional_params, vnf_params = self._format_additional_params(ns_request,
311 member_vnf["member-vnf-index"],
312 descriptor=vnfd)
tierno54db2e42020-04-06 15:29:42 +0000313 vnf_k8s_namespace = ns_k8s_namespace
314 if vnf_params and vnf_params.get("k8s-namespace"):
315 vnf_k8s_namespace = vnf_params["k8s-namespace"]
tiernob24258a2018-10-04 18:39:49 +0200316 vnfr_descriptor = {
317 "id": vnfr_id,
318 "_id": vnfr_id,
319 "nsr-id-ref": nsr_id,
320 "member-vnf-index-ref": member_vnf["member-vnf-index"],
tierno54db2e42020-04-06 15:29:42 +0000321 "additionalParamsForVnf": additional_params,
tiernob24258a2018-10-04 18:39:49 +0200322 "created-time": now,
323 # "vnfd": vnfd, # at OSM model.but removed to avoid data duplication TODO: revise
324 "vnfd-ref": vnfd_id,
325 "vnfd-id": vnfd["_id"], # not at OSM model, but useful
326 "vim-account-id": None,
327 "vdur": [],
328 "connection-point": [],
329 "ip-address": None, # mgmt-interface filled by LCM
330 }
tierno36ec8602018-11-02 17:27:11 +0100331
332 # Create vld
333 if vnfd.get("internal-vld"):
334 vnfr_descriptor["vld"] = []
335 for vnfd_vld in vnfd.get("internal-vld"):
336 vnfr_descriptor["vld"].append(
gcalvino17d5b732018-12-17 16:26:21 +0100337 {key: vnfd_vld[key] for key in ("id", "vim-network-name", "vim-network-id") if key in
338 vnfd_vld})
tierno36ec8602018-11-02 17:27:11 +0100339
340 vnfd_mgmt_cp = vnfd["mgmt-interface"].get("cp")
tiernob24258a2018-10-04 18:39:49 +0200341 for cp in vnfd.get("connection-point", ()):
342 vnf_cp = {
343 "name": cp["name"],
344 "connection-point-id": cp.get("id"),
345 "id": cp.get("id"),
346 # "ip-address", "mac-address" # filled by LCM
347 # vim-id # TODO it would be nice having a vim port id
348 }
349 vnfr_descriptor["connection-point"].append(vnf_cp)
tierno9cb7d672019-10-30 12:13:48 +0000350
tiernoc67b0e92019-11-05 12:45:29 +0000351 # Create k8s-cluster information
352 if vnfd.get("k8s-cluster"):
353 vnfr_descriptor["k8s-cluster"] = vnfd["k8s-cluster"]
354 for net in get_iterable(vnfr_descriptor["k8s-cluster"].get("nets")):
355 if net.get("external-connection-point-ref"):
356 for nsd_vld in get_iterable(nsd.get("vld")):
357 for nsd_vld_cp in get_iterable(nsd_vld.get("vnfd-connection-point-ref")):
358 if nsd_vld_cp.get("vnfd-connection-point-ref") == \
359 net["external-connection-point-ref"] and \
360 nsd_vld_cp.get("member-vnf-index-ref") == member_vnf["member-vnf-index"]:
361 net["ns-vld-id"] = nsd_vld["id"]
362 break
363 else:
364 continue
365 break
366 elif net.get("internal-connection-point-ref"):
367 for vnfd_ivld in get_iterable(vnfd.get("internal-vld")):
368 for vnfd_ivld_icp in get_iterable(vnfd_ivld.get("internal-connection-point")):
369 if vnfd_ivld_icp.get("id-ref") == net["internal-connection-point-ref"]:
370 net["vnf-vld-id"] = vnfd_ivld["id"]
371 break
372 else:
373 continue
374 break
tierno9cb7d672019-10-30 12:13:48 +0000375 # update kdus
376 for kdu in get_iterable(vnfd.get("kdu")):
tiernoe19707b2020-04-21 13:08:04 +0000377 additional_params, kdu_params = self._format_additional_params(ns_request,
378 member_vnf["member-vnf-index"],
379 kdu_name=kdu["name"],
380 descriptor=vnfd)
tierno54db2e42020-04-06 15:29:42 +0000381 kdu_k8s_namespace = vnf_k8s_namespace
tiernobce98f02020-04-17 11:27:47 +0000382 kdu_model = kdu_params.get("kdu_model") if kdu_params else None
tierno54db2e42020-04-06 15:29:42 +0000383 if kdu_params and kdu_params.get("k8s-namespace"):
384 kdu_k8s_namespace = kdu_params["k8s-namespace"]
385
386 kdur = {
387 "additionalParams": additional_params,
388 "k8s-namespace": kdu_k8s_namespace,
389 "kdu-name": kdu["name"],
390 # TODO "name": "" Name of the VDU in the VIM
391 "ip-address": None, # mgmt-interface filled by LCM
392 "k8s-cluster": {},
393 }
394 for k8s_type in ("helm-chart", "juju-bundle"):
395 if kdu.get(k8s_type):
396 kdur[k8s_type] = kdu_model or kdu[k8s_type]
tierno9cb7d672019-10-30 12:13:48 +0000397 if not vnfr_descriptor.get("kdur"):
398 vnfr_descriptor["kdur"] = []
399 vnfr_descriptor["kdur"].append(kdur)
400
gcalvinoe45aded2018-11-13 17:17:28 +0100401 for vdu in vnfd.get("vdu", ()):
tiernoe19707b2020-04-21 13:08:04 +0000402 additional_params, _ = self._format_additional_params(ns_request, member_vnf["member-vnf-index"],
403 vdu_id=vdu["id"], descriptor=vnfd)
tiernob24258a2018-10-04 18:39:49 +0200404 vdur = {
tiernob24258a2018-10-04 18:39:49 +0200405 "vdu-id-ref": vdu["id"],
406 # TODO "name": "" Name of the VDU in the VIM
407 "ip-address": None, # mgmt-interface filled by LCM
408 # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM
409 "internal-connection-point": [],
410 "interfaces": [],
tierno54db2e42020-04-06 15:29:42 +0000411 "additionalParams": additional_params
tiernob24258a2018-10-04 18:39:49 +0200412 }
tiernocc103432018-10-19 14:10:35 +0200413 if vdu.get("pdu-type"):
414 vdur["pdu-type"] = vdu["pdu-type"]
tiernob24258a2018-10-04 18:39:49 +0200415 # TODO volumes: name, volume-id
416 for icp in vdu.get("internal-connection-point", ()):
417 vdu_icp = {
418 "id": icp["id"],
419 "connection-point-id": icp["id"],
420 "name": icp.get("name"),
421 # "ip-address", "mac-address" # filled by LCM
422 # vim-id # TODO it would be nice having a vim port id
423 }
424 vdur["internal-connection-point"].append(vdu_icp)
425 for iface in vdu.get("interface", ()):
426 vdu_iface = {
427 "name": iface.get("name"),
428 # "ip-address", "mac-address" # filled by LCM
429 # vim-id # TODO it would be nice having a vim port id
430 }
tierno36ec8602018-11-02 17:27:11 +0100431 if vnfd_mgmt_cp and iface.get("external-connection-point-ref") == vnfd_mgmt_cp:
432 vdu_iface["mgmt-vnf"] = True
tiernocc103432018-10-19 14:10:35 +0200433 if iface.get("mgmt-interface"):
tierno36ec8602018-11-02 17:27:11 +0100434 vdu_iface["mgmt-interface"] = True # TODO change to mgmt-vdu
435
436 # look for network where this interface is connected
437 if iface.get("external-connection-point-ref"):
438 for nsd_vld in get_iterable(nsd.get("vld")):
439 for nsd_vld_cp in get_iterable(nsd_vld.get("vnfd-connection-point-ref")):
440 if nsd_vld_cp.get("vnfd-connection-point-ref") == \
441 iface["external-connection-point-ref"] and \
442 nsd_vld_cp.get("member-vnf-index-ref") == member_vnf["member-vnf-index"]:
443 vdu_iface["ns-vld-id"] = nsd_vld["id"]
444 break
445 else:
446 continue
447 break
448 elif iface.get("internal-connection-point-ref"):
449 for vnfd_ivld in get_iterable(vnfd.get("internal-vld")):
450 for vnfd_ivld_icp in get_iterable(vnfd_ivld.get("internal-connection-point")):
451 if vnfd_ivld_icp.get("id-ref") == iface["internal-connection-point-ref"]:
452 vdu_iface["vnf-vld-id"] = vnfd_ivld["id"]
453 break
454 else:
455 continue
456 break
tiernocc103432018-10-19 14:10:35 +0200457
tiernob24258a2018-10-04 18:39:49 +0200458 vdur["interfaces"].append(vdu_iface)
tiernocc103432018-10-19 14:10:35 +0200459 count = vdu.get("count", 1)
460 if count is None:
461 count = 1
462 count = int(count) # TODO remove when descriptor serialized with payngbind
463 for index in range(0, count):
464 if index:
465 vdur = deepcopy(vdur)
466 vdur["_id"] = str(uuid4())
467 vdur["count-index"] = index
468 vnfr_descriptor["vdur"].append(vdur)
tiernob24258a2018-10-04 18:39:49 +0200469
470 step = "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format(
471 member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"])
472
473 # add at database
tiernobdebce92019-07-01 15:36:49 +0000474 self.format_on_new(vnfr_descriptor, session["project_id"], make_public=session["public"])
tiernob24258a2018-10-04 18:39:49 +0200475 self.db.create("vnfrs", vnfr_descriptor)
476 rollback.append({"topic": "vnfrs", "_id": vnfr_id})
477 nsr_descriptor["constituent-vnfr-ref"].append(vnfr_id)
478
479 step = "creating nsr at database"
tierno65ca36d2019-02-12 19:27:52 +0100480 self.format_on_new(nsr_descriptor, session["project_id"], make_public=session["public"])
tiernob24258a2018-10-04 18:39:49 +0200481 self.db.create("nsrs", nsr_descriptor)
482 rollback.append({"topic": "nsrs", "_id": nsr_id})
tiernobee085c2018-12-12 17:03:04 +0000483
484 step = "creating nsr temporal folder"
485 self.fs.mkdir(nsr_id)
486
tiernobdebce92019-07-01 15:36:49 +0000487 return nsr_id, None
tierno1bfe4e22019-09-02 16:03:25 +0000488 except (ValidationError, EngineException, DbException, MsgException, FsException) as e:
489 raise type(e)("{} while '{}".format(e, step), http_code=e.http_code)
tiernob24258a2018-10-04 18:39:49 +0200490
tierno65ca36d2019-02-12 19:27:52 +0100491 def edit(self, session, _id, indata=None, kwargs=None, content=None):
tiernob24258a2018-10-04 18:39:49 +0200492 raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
493
494
495class VnfrTopic(BaseTopic):
496 topic = "vnfrs"
497 topic_msg = None
498
delacruzramo32bab472019-09-13 12:24:22 +0200499 def __init__(self, db, fs, msg, auth):
500 BaseTopic.__init__(self, db, fs, msg, auth)
tiernob24258a2018-10-04 18:39:49 +0200501
tiernobee3bad2019-12-05 12:26:01 +0000502 def delete(self, session, _id, dry_run=False, not_send_msg=None):
tiernob24258a2018-10-04 18:39:49 +0200503 raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
504
tierno65ca36d2019-02-12 19:27:52 +0100505 def edit(self, session, _id, indata=None, kwargs=None, content=None):
tiernob24258a2018-10-04 18:39:49 +0200506 raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
507
tierno65ca36d2019-02-12 19:27:52 +0100508 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
tiernob24258a2018-10-04 18:39:49 +0200509 # Not used because vnfrs are created and deleted by NsrTopic class directly
510 raise EngineException("Method new called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
511
512
513class NsLcmOpTopic(BaseTopic):
514 topic = "nslcmops"
515 topic_msg = "ns"
516 operation_schema = { # mapping between operation and jsonschema to validate
517 "instantiate": ns_instantiate,
518 "action": ns_action,
519 "scale": ns_scale,
tierno1c38f2f2020-03-24 11:51:39 +0000520 "terminate": ns_terminate,
tiernob24258a2018-10-04 18:39:49 +0200521 }
522
delacruzramo32bab472019-09-13 12:24:22 +0200523 def __init__(self, db, fs, msg, auth):
524 BaseTopic.__init__(self, db, fs, msg, auth)
tiernob24258a2018-10-04 18:39:49 +0200525
tiernob24258a2018-10-04 18:39:49 +0200526 def _check_ns_operation(self, session, nsr, operation, indata):
527 """
528 Check that user has enter right parameters for the operation
tierno65ca36d2019-02-12 19:27:52 +0100529 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tiernob24258a2018-10-04 18:39:49 +0200530 :param operation: it can be: instantiate, terminate, action, TODO: update, heal
531 :param indata: descriptor with the parameters of the operation
532 :return: None
533 """
tierno982da4e2019-09-03 11:51:55 +0000534 vnf_member_index_to_vnfd = {} # map between vnf_member_index to vnf descriptor.
tiernob24258a2018-10-04 18:39:49 +0200535 vim_accounts = []
tierno4f9d4ae2019-03-20 17:24:11 +0000536 wim_accounts = []
tiernob24258a2018-10-04 18:39:49 +0200537 nsd = nsr["nsd"]
538
539 def check_valid_vnf_member_index(member_vnf_index):
tierno982da4e2019-09-03 11:51:55 +0000540 # Obtain vnf descriptor. The vnfr is used to get the vnfd._id used for this member_vnf_index
541 if vnf_member_index_to_vnfd.get(member_vnf_index):
542 return vnf_member_index_to_vnfd[member_vnf_index]
543 vnfr = self.db.get_one("vnfrs",
544 {"nsr-id-ref": nsr["_id"], "member-vnf-index-ref": member_vnf_index},
545 fail_on_empty=False)
546 if not vnfr:
tiernob24258a2018-10-04 18:39:49 +0200547 raise EngineException("Invalid parameter member_vnf_index='{}' is not one of the "
548 "nsd:constituent-vnfd".format(member_vnf_index))
tierno982da4e2019-09-03 11:51:55 +0000549 vnfd = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]}, fail_on_empty=False)
550 if not vnfd:
551 raise EngineException("vnfd id={} has been deleted!. Operation cannot be performed".
552 format(vnfr["vnfd-id"]))
553 vnf_member_index_to_vnfd[member_vnf_index] = vnfd # add to cache, avoiding a later look for
554 return vnfd
tiernob24258a2018-10-04 18:39:49 +0200555
tierno260dd6f2019-09-02 10:48:56 +0000556 def check_valid_vdu(vnfd, vdu_id):
557 for vdud in get_iterable(vnfd.get("vdu")):
558 if vdud["id"] == vdu_id:
559 return vdud
560 else:
561 raise EngineException("Invalid parameter vdu_id='{}' not present at vnfd:vdu:id".format(vdu_id))
562
tierno9cb7d672019-10-30 12:13:48 +0000563 def check_valid_kdu(vnfd, kdu_name):
564 for kdud in get_iterable(vnfd.get("kdu")):
565 if kdud["name"] == kdu_name:
566 return kdud
567 else:
568 raise EngineException("Invalid parameter kdu_name='{}' not present at vnfd:kdu:name".format(kdu_name))
569
gcalvino5e72d152018-10-23 11:46:57 +0200570 def _check_vnf_instantiation_params(in_vnfd, vnfd):
571
tierno40fbcad2018-10-26 10:58:15 +0200572 for in_vdu in get_iterable(in_vnfd.get("vdu")):
573 for vdu in get_iterable(vnfd.get("vdu")):
574 if in_vdu["id"] == vdu["id"]:
575 for volume in get_iterable(in_vdu.get("volume")):
576 for volumed in get_iterable(vdu.get("volumes")):
577 if volumed["name"] == volume["name"]:
578 break
579 else:
580 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
581 "volume:name='{}' is not present at vnfd:vdu:volumes list".
582 format(in_vnf["member-vnf-index"], in_vdu["id"],
583 volume["name"]))
584 for in_iface in get_iterable(in_vdu["interface"]):
585 for iface in get_iterable(vdu.get("interface")):
586 if in_iface["name"] == iface["name"]:
587 break
588 else:
589 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
590 "interface[name='{}'] is not present at vnfd:vdu:interface"
591 .format(in_vnf["member-vnf-index"], in_vdu["id"],
592 in_iface["name"]))
593 break
gcalvino5e72d152018-10-23 11:46:57 +0200594 else:
tierno40fbcad2018-10-26 10:58:15 +0200595 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}'] is is not present "
596 "at vnfd:vdu".format(in_vnf["member-vnf-index"], in_vdu["id"]))
gcalvino5e72d152018-10-23 11:46:57 +0200597
598 for in_ivld in get_iterable(in_vnfd.get("internal-vld")):
599 for ivld in get_iterable(vnfd.get("internal-vld")):
tierno75d5a4e2020-05-21 15:09:22 +0000600 if in_ivld["name"] in (ivld["id"], ivld.get("name")):
tierno1bfe4e22019-09-02 16:03:25 +0000601 for in_icp in get_iterable(in_ivld.get("internal-connection-point")):
gcalvino5e72d152018-10-23 11:46:57 +0200602 for icp in ivld["internal-connection-point"]:
603 if in_icp["id-ref"] == icp["id-ref"]:
604 break
605 else:
tierno40fbcad2018-10-26 10:58:15 +0200606 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld[name"
607 "='{}']:internal-connection-point[id-ref:'{}'] is not present at "
608 "vnfd:internal-vld:name/id:internal-connection-point"
609 .format(in_vnf["member-vnf-index"], in_ivld["name"],
tierno670b0c62020-05-12 13:01:19 +0000610 in_icp["id-ref"]))
gcalvino5e72d152018-10-23 11:46:57 +0200611 break
612 else:
613 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld:name='{}'"
614 " is not present at vnfd '{}'".format(in_vnf["member-vnf-index"],
615 in_ivld["name"], vnfd["id"]))
616
tiernob24258a2018-10-04 18:39:49 +0200617 def check_valid_vim_account(vim_account):
618 if vim_account in vim_accounts:
619 return
620 try:
tierno65ca36d2019-02-12 19:27:52 +0100621 db_filter = self._get_project_filter(session)
tiernocc103432018-10-19 14:10:35 +0200622 db_filter["_id"] = vim_account
623 self.db.get_one("vim_accounts", db_filter)
tiernob24258a2018-10-04 18:39:49 +0200624 except Exception:
tiernocc103432018-10-19 14:10:35 +0200625 raise EngineException("Invalid vimAccountId='{}' not present for the project".format(vim_account))
tiernob24258a2018-10-04 18:39:49 +0200626 vim_accounts.append(vim_account)
627
tierno4f9d4ae2019-03-20 17:24:11 +0000628 def check_valid_wim_account(wim_account):
629 if not isinstance(wim_account, str):
630 return
631 elif wim_account in wim_accounts:
632 return
633 try:
634 db_filter = self._get_project_filter(session, write=False, show_all=True)
635 db_filter["_id"] = wim_account
636 self.db.get_one("wim_accounts", db_filter)
637 except Exception:
638 raise EngineException("Invalid wimAccountId='{}' not present for the project".format(wim_account))
639 wim_accounts.append(wim_account)
640
tiernob24258a2018-10-04 18:39:49 +0200641 if operation == "action":
642 # check vnf_member_index
643 if indata.get("vnf_member_index"):
644 indata["member_vnf_index"] = indata.pop("vnf_member_index") # for backward compatibility
tierno1ac7f462019-06-03 17:22:12 +0000645 if indata.get("member_vnf_index"):
646 vnfd = check_valid_vnf_member_index(indata["member_vnf_index"])
tierno260dd6f2019-09-02 10:48:56 +0000647 if indata.get("vdu_id"):
648 vdud = check_valid_vdu(vnfd, indata["vdu_id"])
649 descriptor_configuration = vdud.get("vdu-configuration", {}).get("config-primitive")
tierno9cb7d672019-10-30 12:13:48 +0000650 elif indata.get("kdu_name"):
tiernoc67b0e92019-11-05 12:45:29 +0000651 kdud = check_valid_kdu(vnfd, indata["kdu_name"])
tierno9cb7d672019-10-30 12:13:48 +0000652 descriptor_configuration = kdud.get("kdu-configuration", {}).get("config-primitive")
tierno260dd6f2019-09-02 10:48:56 +0000653 else:
654 descriptor_configuration = vnfd.get("vnf-configuration", {}).get("config-primitive")
tierno1ac7f462019-06-03 17:22:12 +0000655 else: # use a NSD
656 descriptor_configuration = nsd.get("ns-configuration", {}).get("config-primitive")
tierno9cb7d672019-10-30 12:13:48 +0000657
658 # For k8s allows default primitives without validating the parameters
delacruzramo6ddff2e2019-11-28 11:24:09 +0100659 if indata.get("kdu_name") and indata["primitive"] in ("upgrade", "rollback", "status", "inspect", "readme"):
tierno9cb7d672019-10-30 12:13:48 +0000660 # TODO should be checked that rollback only can contains revsision_numbe????
delacruzramo6ddff2e2019-11-28 11:24:09 +0100661 if not indata.get("member_vnf_index"):
662 raise EngineException("Missing action parameter 'member_vnf_index' for default KDU primitive '{}'"
663 .format(indata["primitive"]))
tierno9cb7d672019-10-30 12:13:48 +0000664 return
665 # if not, check primitive
tierno1ac7f462019-06-03 17:22:12 +0000666 for config_primitive in get_iterable(descriptor_configuration):
tiernob24258a2018-10-04 18:39:49 +0200667 if indata["primitive"] == config_primitive["name"]:
668 # check needed primitive_params are provided
669 if indata.get("primitive_params"):
670 in_primitive_params_copy = copy(indata["primitive_params"])
671 else:
672 in_primitive_params_copy = {}
673 for paramd in get_iterable(config_primitive.get("parameter")):
674 if paramd["name"] in in_primitive_params_copy:
675 del in_primitive_params_copy[paramd["name"]]
676 elif not paramd.get("default-value"):
677 raise EngineException("Needed parameter {} not provided for primitive '{}'".format(
678 paramd["name"], indata["primitive"]))
679 # check no extra primitive params are provided
680 if in_primitive_params_copy:
tierno1ac7f462019-06-03 17:22:12 +0000681 raise EngineException("parameter/s '{}' not present at vnfd /nsd for primitive '{}'".format(
tiernob24258a2018-10-04 18:39:49 +0200682 list(in_primitive_params_copy.keys()), indata["primitive"]))
683 break
684 else:
tierno1ac7f462019-06-03 17:22:12 +0000685 raise EngineException("Invalid primitive '{}' is not present at vnfd/nsd".format(indata["primitive"]))
tiernob24258a2018-10-04 18:39:49 +0200686 if operation == "scale":
687 vnfd = check_valid_vnf_member_index(indata["scaleVnfData"]["scaleByStepData"]["member-vnf-index"])
688 for scaling_group in get_iterable(vnfd.get("scaling-group-descriptor")):
689 if indata["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"] == scaling_group["name"]:
690 break
691 else:
692 raise EngineException("Invalid scaleVnfData:scaleByStepData:scaling-group-descriptor '{}' is not "
693 "present at vnfd:scaling-group-descriptor".format(
694 indata["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"]))
695 if operation == "instantiate":
696 # check vim_account
697 check_valid_vim_account(indata["vimAccountId"])
tierno4f9d4ae2019-03-20 17:24:11 +0000698 check_valid_wim_account(indata.get("wimAccountId"))
tiernob24258a2018-10-04 18:39:49 +0200699 for in_vnf in get_iterable(indata.get("vnf")):
700 vnfd = check_valid_vnf_member_index(in_vnf["member-vnf-index"])
gcalvino5e72d152018-10-23 11:46:57 +0200701 _check_vnf_instantiation_params(in_vnf, vnfd)
tiernob24258a2018-10-04 18:39:49 +0200702 if in_vnf.get("vimAccountId"):
703 check_valid_vim_account(in_vnf["vimAccountId"])
tiernob24258a2018-10-04 18:39:49 +0200704
tiernob24258a2018-10-04 18:39:49 +0200705 for in_vld in get_iterable(indata.get("vld")):
tierno4f9d4ae2019-03-20 17:24:11 +0000706 check_valid_wim_account(in_vld.get("wimAccountId"))
tiernob24258a2018-10-04 18:39:49 +0200707 for vldd in get_iterable(nsd.get("vld")):
708 if in_vld["name"] == vldd["name"] or in_vld["name"] == vldd["id"]:
709 break
710 else:
711 raise EngineException("Invalid parameter vld:name='{}' is not present at nsd:vld".format(
712 in_vld["name"]))
713
tierno36ec8602018-11-02 17:27:11 +0100714 def _look_for_pdu(self, session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback):
tiernocc103432018-10-19 14:10:35 +0200715 """
tierno36ec8602018-11-02 17:27:11 +0100716 Look for a free PDU in the catalog matching vdur type and interfaces. Fills vnfr.vdur with the interface
717 (ip_address, ...) information.
718 Modifies PDU _admin.usageState to 'IN_USE'
tierno65ca36d2019-02-12 19:27:52 +0100719 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tierno36ec8602018-11-02 17:27:11 +0100720 :param rollback: list with the database modifications to rollback if needed
721 :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found
722 :param vim_account: vim_account where this vnfr should be deployed
723 :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr
724 :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback
725 of the changed vnfr is needed
726
727 :return: List of PDU interfaces that are connected to an existing VIM network. Each item contains:
728 "vim-network-name": used at VIM
729 "name": interface name
730 "vnf-vld-id": internal VNFD vld where this interface is connected, or
731 "ns-vld-id": NSD vld where this interface is connected.
732 NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None
tiernocc103432018-10-19 14:10:35 +0200733 """
tierno36ec8602018-11-02 17:27:11 +0100734
735 ifaces_forcing_vim_network = []
tiernocc103432018-10-19 14:10:35 +0200736 for vdur_index, vdur in enumerate(get_iterable(vnfr.get("vdur"))):
737 if not vdur.get("pdu-type"):
738 continue
739 pdu_type = vdur.get("pdu-type")
tierno65ca36d2019-02-12 19:27:52 +0100740 pdu_filter = self._get_project_filter(session)
tierno36ec8602018-11-02 17:27:11 +0100741 pdu_filter["vim_accounts"] = vim_account
tiernocc103432018-10-19 14:10:35 +0200742 pdu_filter["type"] = pdu_type
743 pdu_filter["_admin.operationalState"] = "ENABLED"
tierno36ec8602018-11-02 17:27:11 +0100744 pdu_filter["_admin.usageState"] = "NOT_IN_USE"
tiernocc103432018-10-19 14:10:35 +0200745 # TODO feature 1417: "shared": True,
746
747 available_pdus = self.db.get_list("pdus", pdu_filter)
748 for pdu in available_pdus:
749 # step 1 check if this pdu contains needed interfaces:
750 match_interfaces = True
751 for vdur_interface in vdur["interfaces"]:
752 for pdu_interface in pdu["interfaces"]:
753 if pdu_interface["name"] == vdur_interface["name"]:
754 # TODO feature 1417: match per mgmt type
755 break
756 else: # no interface found for name
757 match_interfaces = False
758 break
759 if match_interfaces:
760 break
761 else:
762 raise EngineException(
tierno36ec8602018-11-02 17:27:11 +0100763 "No PDU of type={} at vim_account={} found for member_vnf_index={}, vdu={} matching interface "
764 "names".format(pdu_type, vim_account, vnfr["member-vnf-index-ref"], vdur["vdu-id-ref"]))
tiernocc103432018-10-19 14:10:35 +0200765
766 # step 2. Update pdu
767 rollback_pdu = {
768 "_admin.usageState": pdu["_admin"]["usageState"],
769 "_admin.usage.vnfr_id": None,
770 "_admin.usage.nsr_id": None,
771 "_admin.usage.vdur": None,
772 }
773 self.db.set_one("pdus", {"_id": pdu["_id"]},
tierno36ec8602018-11-02 17:27:11 +0100774 {"_admin.usageState": "IN_USE",
tiernoe8631782018-12-21 13:31:52 +0000775 "_admin.usage": {"vnfr_id": vnfr["_id"],
776 "nsr_id": vnfr["nsr-id-ref"],
777 "vdur": vdur["vdu-id-ref"]}
778 })
tiernocc103432018-10-19 14:10:35 +0200779 rollback.append({"topic": "pdus", "_id": pdu["_id"], "operation": "set", "content": rollback_pdu})
780
781 # step 3. Fill vnfr info by filling vdur
782 vdu_text = "vdur.{}".format(vdur_index)
tierno36ec8602018-11-02 17:27:11 +0100783 vnfr_update_rollback[vdu_text + ".pdu-id"] = None
tiernocc103432018-10-19 14:10:35 +0200784 vnfr_update[vdu_text + ".pdu-id"] = pdu["_id"]
785 for iface_index, vdur_interface in enumerate(vdur["interfaces"]):
786 for pdu_interface in pdu["interfaces"]:
787 if pdu_interface["name"] == vdur_interface["name"]:
788 iface_text = vdu_text + ".interfaces.{}".format(iface_index)
789 for k, v in pdu_interface.items():
tierno36ec8602018-11-02 17:27:11 +0100790 if k in ("ip-address", "mac-address"): # TODO: switch-xxxxx must be inserted
791 vnfr_update[iface_text + ".{}".format(k)] = v
792 vnfr_update_rollback[iface_text + ".{}".format(k)] = vdur_interface.get(v)
793 if pdu_interface.get("ip-address"):
tiernoc88003e2020-03-12 17:31:42 +0000794 if vdur_interface.get("mgmt-interface") or vdur_interface.get("mgmt-vnf"):
tierno36ec8602018-11-02 17:27:11 +0100795 vnfr_update_rollback[vdu_text + ".ip-address"] = vdur.get("ip-address")
796 vnfr_update[vdu_text + ".ip-address"] = pdu_interface["ip-address"]
797 if vdur_interface.get("mgmt-vnf"):
798 vnfr_update_rollback["ip-address"] = vnfr.get("ip-address")
799 vnfr_update["ip-address"] = pdu_interface["ip-address"]
tierno72b16e12020-03-18 09:49:43 +0000800 vnfr_update[vdu_text + ".ip-address"] = pdu_interface["ip-address"]
gcalvino17d5b732018-12-17 16:26:21 +0100801 if pdu_interface.get("vim-network-name") or pdu_interface.get("vim-network-id"):
tierno36ec8602018-11-02 17:27:11 +0100802 ifaces_forcing_vim_network.append({
tierno36ec8602018-11-02 17:27:11 +0100803 "name": vdur_interface.get("vnf-vld-id") or vdur_interface.get("ns-vld-id"),
804 "vnf-vld-id": vdur_interface.get("vnf-vld-id"),
805 "ns-vld-id": vdur_interface.get("ns-vld-id")})
gcalvino17d5b732018-12-17 16:26:21 +0100806 if pdu_interface.get("vim-network-id"):
tiernoc67b0e92019-11-05 12:45:29 +0000807 ifaces_forcing_vim_network[-1]["vim-network-id"] = pdu_interface["vim-network-id"]
gcalvino17d5b732018-12-17 16:26:21 +0100808 if pdu_interface.get("vim-network-name"):
tiernoc67b0e92019-11-05 12:45:29 +0000809 ifaces_forcing_vim_network[-1]["vim-network-name"] = pdu_interface["vim-network-name"]
tiernocc103432018-10-19 14:10:35 +0200810 break
811
tierno36ec8602018-11-02 17:27:11 +0100812 return ifaces_forcing_vim_network
tiernocc103432018-10-19 14:10:35 +0200813
tierno9cb7d672019-10-30 12:13:48 +0000814 def _look_for_k8scluster(self, session, rollback, vnfr, vim_account, vnfr_update, vnfr_update_rollback):
815 """
816 Look for an available k8scluster for all the kuds in the vnfd matching version and cni requirements.
817 Fills vnfr.kdur with the selected k8scluster
818
819 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
820 :param rollback: list with the database modifications to rollback if needed
821 :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found
822 :param vim_account: vim_account where this vnfr should be deployed
823 :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr
824 :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback
825 of the changed vnfr is needed
826
827 :return: List of KDU interfaces that are connected to an existing VIM network. Each item contains:
828 "vim-network-name": used at VIM
829 "name": interface name
830 "vnf-vld-id": internal VNFD vld where this interface is connected, or
831 "ns-vld-id": NSD vld where this interface is connected.
832 NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None
833 """
834
835 ifaces_forcing_vim_network = []
tiernoc67b0e92019-11-05 12:45:29 +0000836 if not vnfr.get("kdur"):
837 return ifaces_forcing_vim_network
tierno9cb7d672019-10-30 12:13:48 +0000838
tiernoc67b0e92019-11-05 12:45:29 +0000839 kdu_filter = self._get_project_filter(session)
840 kdu_filter["vim_account"] = vim_account
841 # TODO kdu_filter["_admin.operationalState"] = "ENABLED"
842 available_k8sclusters = self.db.get_list("k8sclusters", kdu_filter)
843
844 k8s_requirements = {} # just for logging
845 for k8scluster in available_k8sclusters:
846 if not vnfr.get("k8s-cluster"):
tierno9cb7d672019-10-30 12:13:48 +0000847 break
tiernoc67b0e92019-11-05 12:45:29 +0000848 # restrict by cni
849 if vnfr["k8s-cluster"].get("cni"):
850 k8s_requirements["cni"] = vnfr["k8s-cluster"]["cni"]
851 if not set(vnfr["k8s-cluster"]["cni"]).intersection(k8scluster.get("cni", ())):
852 continue
853 # restrict by version
854 if vnfr["k8s-cluster"].get("version"):
855 k8s_requirements["version"] = vnfr["k8s-cluster"]["version"]
856 if k8scluster.get("k8s_version") not in vnfr["k8s-cluster"]["version"]:
857 continue
858 # restrict by number of networks
859 if vnfr["k8s-cluster"].get("nets"):
860 k8s_requirements["networks"] = len(vnfr["k8s-cluster"]["nets"])
861 if not k8scluster.get("nets") or len(k8scluster["nets"]) < len(vnfr["k8s-cluster"]["nets"]):
862 continue
863 break
864 else:
865 raise EngineException("No k8scluster with requirements='{}' at vim_account={} found for member_vnf_index={}"
866 .format(k8s_requirements, vim_account, vnfr["member-vnf-index-ref"]))
tierno9cb7d672019-10-30 12:13:48 +0000867
tiernoc67b0e92019-11-05 12:45:29 +0000868 for kdur_index, kdur in enumerate(get_iterable(vnfr.get("kdur"))):
tierno9cb7d672019-10-30 12:13:48 +0000869 # step 3. Fill vnfr info by filling kdur
870 kdu_text = "kdur.{}.".format(kdur_index)
871 vnfr_update_rollback[kdu_text + "k8s-cluster.id"] = None
872 vnfr_update[kdu_text + "k8s-cluster.id"] = k8scluster["_id"]
873
tiernoc67b0e92019-11-05 12:45:29 +0000874 # step 4. Check VIM networks that forces the selected k8s_cluster
875 if vnfr.get("k8s-cluster") and vnfr["k8s-cluster"].get("nets"):
876 k8scluster_net_list = list(k8scluster.get("nets").keys())
877 for net_index, kdur_net in enumerate(vnfr["k8s-cluster"]["nets"]):
878 # get a network from k8s_cluster nets. If name matches use this, if not use other
879 if kdur_net["id"] in k8scluster_net_list: # name matches
880 vim_net = k8scluster["nets"][kdur_net["id"]]
881 k8scluster_net_list.remove(kdur_net["id"])
882 else:
883 vim_net = k8scluster["nets"][k8scluster_net_list[0]]
884 k8scluster_net_list.pop(0)
885 vnfr_update_rollback["k8s-cluster.nets.{}.vim_net".format(net_index)] = None
886 vnfr_update["k8s-cluster.nets.{}.vim_net".format(net_index)] = vim_net
887 if vim_net and (kdur_net.get("vnf-vld-id") or kdur_net.get("ns-vld-id")):
888 ifaces_forcing_vim_network.append({
889 "name": kdur_net.get("vnf-vld-id") or kdur_net.get("ns-vld-id"),
890 "vnf-vld-id": kdur_net.get("vnf-vld-id"),
891 "ns-vld-id": kdur_net.get("ns-vld-id"),
892 "vim-network-name": vim_net, # TODO can it be vim-network-id ???
893 })
894 # TODO check that this forcing is not incompatible with other forcing
tierno9cb7d672019-10-30 12:13:48 +0000895 return ifaces_forcing_vim_network
896
tiernocc103432018-10-19 14:10:35 +0200897 def _update_vnfrs(self, session, rollback, nsr, indata):
tiernocc103432018-10-19 14:10:35 +0200898 # get vnfr
899 nsr_id = nsr["_id"]
900 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
901
902 for vnfr in vnfrs:
903 vnfr_update = {}
904 vnfr_update_rollback = {}
905 member_vnf_index = vnfr["member-vnf-index-ref"]
906 # update vim-account-id
907
908 vim_account = indata["vimAccountId"]
909 # check instantiate parameters
910 for vnf_inst_params in get_iterable(indata.get("vnf")):
911 if vnf_inst_params["member-vnf-index"] != member_vnf_index:
912 continue
913 if vnf_inst_params.get("vimAccountId"):
914 vim_account = vnf_inst_params.get("vimAccountId")
915
916 vnfr_update["vim-account-id"] = vim_account
917 vnfr_update_rollback["vim-account-id"] = vnfr.get("vim-account-id")
918
919 # get pdu
tierno36ec8602018-11-02 17:27:11 +0100920 ifaces_forcing_vim_network = self._look_for_pdu(session, rollback, vnfr, vim_account, vnfr_update,
921 vnfr_update_rollback)
tiernocc103432018-10-19 14:10:35 +0200922
tierno9cb7d672019-10-30 12:13:48 +0000923 # get kdus
924 ifaces_forcing_vim_network += self._look_for_k8scluster(session, rollback, vnfr, vim_account, vnfr_update,
925 vnfr_update_rollback)
926 # update database vnfr
tierno36ec8602018-11-02 17:27:11 +0100927 self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, vnfr_update)
928 rollback.append({"topic": "vnfrs", "_id": vnfr["_id"], "operation": "set", "content": vnfr_update_rollback})
929
930 # Update indada in case pdu forces to use a concrete vim-network-name
931 # TODO check if user has already insert a vim-network-name and raises an error
932 if not ifaces_forcing_vim_network:
933 continue
934 for iface_info in ifaces_forcing_vim_network:
935 if iface_info.get("ns-vld-id"):
936 if "vld" not in indata:
937 indata["vld"] = []
938 indata["vld"].append({key: iface_info[key] for key in
939 ("name", "vim-network-name", "vim-network-id") if iface_info.get(key)})
940
941 elif iface_info.get("vnf-vld-id"):
942 if "vnf" not in indata:
943 indata["vnf"] = []
944 indata["vnf"].append({
945 "member-vnf-index": member_vnf_index,
946 "internal-vld": [{key: iface_info[key] for key in
947 ("name", "vim-network-name", "vim-network-id") if iface_info.get(key)}]
948 })
949
950 @staticmethod
951 def _create_nslcmop(nsr_id, operation, params):
952 """
953 Creates a ns-lcm-opp content to be stored at database.
954 :param nsr_id: internal id of the instance
955 :param operation: instantiate, terminate, scale, action, ...
956 :param params: user parameters for the operation
957 :return: dictionary following SOL005 format
958 """
tiernob24258a2018-10-04 18:39:49 +0200959 now = time()
960 _id = str(uuid4())
961 nslcmop = {
962 "id": _id,
963 "_id": _id,
964 "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
tiernoecf94bd2020-01-09 12:40:45 +0000965 "queuePosition": None,
966 "stage": None,
967 "errorMessage": None,
968 "detailedStatus": None,
tiernob24258a2018-10-04 18:39:49 +0200969 "statusEnteredTime": now,
tierno36ec8602018-11-02 17:27:11 +0100970 "nsInstanceId": nsr_id,
tiernob24258a2018-10-04 18:39:49 +0200971 "lcmOperationType": operation,
972 "startTime": now,
973 "isAutomaticInvocation": False,
974 "operationParams": params,
975 "isCancelPending": False,
976 "links": {
977 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
tierno36ec8602018-11-02 17:27:11 +0100978 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
tiernob24258a2018-10-04 18:39:49 +0200979 }
980 }
981 return nslcmop
982
magnussonlf318b302020-01-20 18:38:18 +0100983 def _get_enabled_vims(self, session):
984 """
985 Retrieve and return VIM accounts that are accessible by current user and has state ENABLE
986 :param session: current session with user information
987 """
988 db_filter = self._get_project_filter(session)
989 db_filter["_admin.operationalState"] = "ENABLED"
990 vims = self.db.get_list("vim_accounts", db_filter)
991 vimAccounts = []
992 for vim in vims:
993 vimAccounts.append(vim['_id'])
994 return vimAccounts
995
tierno65ca36d2019-02-12 19:27:52 +0100996 def new(self, rollback, session, indata=None, kwargs=None, headers=None, slice_object=False):
tiernob24258a2018-10-04 18:39:49 +0200997 """
998 Performs a new operation over a ns
999 :param rollback: list to append created items at database in case a rollback must to be done
tierno65ca36d2019-02-12 19:27:52 +01001000 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
tiernob24258a2018-10-04 18:39:49 +02001001 :param indata: descriptor with the parameters of the operation. It must contains among others
1002 nsInstanceId: _id of the nsr to perform the operation
1003 operation: it can be: instantiate, terminate, action, TODO: update, heal
1004 :param kwargs: used to override the indata descriptor
1005 :param headers: http request headers
tiernob24258a2018-10-04 18:39:49 +02001006 :return: id of the nslcmops
1007 """
Felipe Vicens90fbc9c2019-06-06 01:03:00 +02001008 def check_if_nsr_is_not_slice_member(session, nsr_id):
1009 nsis = None
1010 db_filter = self._get_project_filter(session)
1011 db_filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_id
1012 nsis = self.db.get_one("nsis", db_filter, fail_on_empty=False, fail_on_more=False)
1013 if nsis:
1014 raise EngineException("The NS instance {} cannot be terminate because is used by the slice {}".format(
1015 nsr_id, nsis["_id"]), http_code=HTTPStatus.CONFLICT)
1016
tiernob24258a2018-10-04 18:39:49 +02001017 try:
1018 # Override descriptor with query string kwargs
tierno1c38f2f2020-03-24 11:51:39 +00001019 self._update_input_with_kwargs(indata, kwargs, yaml_format=True)
tiernob24258a2018-10-04 18:39:49 +02001020 operation = indata["lcmOperationType"]
1021 nsInstanceId = indata["nsInstanceId"]
1022
1023 validate_input(indata, self.operation_schema[operation])
1024 # get ns from nsr_id
tierno65ca36d2019-02-12 19:27:52 +01001025 _filter = BaseTopic._get_project_filter(session)
tiernob24258a2018-10-04 18:39:49 +02001026 _filter["_id"] = nsInstanceId
1027 nsr = self.db.get_one("nsrs", _filter)
1028
1029 # initial checking
Felipe Vicens90fbc9c2019-06-06 01:03:00 +02001030 if operation == "terminate" and slice_object is False:
1031 check_if_nsr_is_not_slice_member(session, nsr["_id"])
tiernob24258a2018-10-04 18:39:49 +02001032 if not nsr["_admin"].get("nsState") or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
1033 if operation == "terminate" and indata.get("autoremove"):
1034 # NSR must be deleted
tierno586ae812019-10-17 13:56:53 +00001035 return None, None # a none in this case is used to indicate not instantiated. It can be removed
tiernob24258a2018-10-04 18:39:49 +02001036 if operation != "instantiate":
1037 raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format(
1038 nsInstanceId, operation), HTTPStatus.CONFLICT)
1039 else:
tierno65ca36d2019-02-12 19:27:52 +01001040 if operation == "instantiate" and not session["force"]:
tiernob24258a2018-10-04 18:39:49 +02001041 raise EngineException("ns_instance '{}' cannot be '{}' because it is already instantiated".format(
1042 nsInstanceId, operation), HTTPStatus.CONFLICT)
1043 self._check_ns_operation(session, nsr, operation, indata)
tierno36ec8602018-11-02 17:27:11 +01001044
tiernocc103432018-10-19 14:10:35 +02001045 if operation == "instantiate":
1046 self._update_vnfrs(session, rollback, nsr, indata)
tierno36ec8602018-11-02 17:27:11 +01001047
1048 nslcmop_desc = self._create_nslcmop(nsInstanceId, operation, indata)
tierno1bfe4e22019-09-02 16:03:25 +00001049 _id = nslcmop_desc["_id"]
tierno65ca36d2019-02-12 19:27:52 +01001050 self.format_on_new(nslcmop_desc, session["project_id"], make_public=session["public"])
magnussonlf318b302020-01-20 18:38:18 +01001051 if indata.get("placement-engine"):
1052 # Save valid vim accounts in lcm operation descriptor
1053 nslcmop_desc['operationParams']['validVimAccounts'] = self._get_enabled_vims(session)
tierno1bfe4e22019-09-02 16:03:25 +00001054 self.db.create("nslcmops", nslcmop_desc)
tiernob24258a2018-10-04 18:39:49 +02001055 rollback.append({"topic": "nslcmops", "_id": _id})
Felipe Vicens07f31722018-10-29 15:16:44 +01001056 if not slice_object:
1057 self.msg.write("ns", operation, nslcmop_desc)
tiernobdebce92019-07-01 15:36:49 +00001058 return _id, None
1059 except ValidationError as e: # TODO remove try Except, it is captured at nbi.py
tiernob24258a2018-10-04 18:39:49 +02001060 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1061 # except DbException as e:
1062 # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND)
1063
tiernobee3bad2019-12-05 12:26:01 +00001064 def delete(self, session, _id, dry_run=False, not_send_msg=None):
tiernob24258a2018-10-04 18:39:49 +02001065 raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
1066
tierno65ca36d2019-02-12 19:27:52 +01001067 def edit(self, session, _id, indata=None, kwargs=None, content=None):
tiernob24258a2018-10-04 18:39:49 +02001068 raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
Felipe Vicensb57758d2018-10-16 16:00:20 +02001069
1070
1071class NsiTopic(BaseTopic):
1072 topic = "nsis"
1073 topic_msg = "nsi"
tierno6b02b052020-06-02 10:07:41 +00001074 quota_name = "slice_instances"
Felipe Vicensb57758d2018-10-16 16:00:20 +02001075
delacruzramo32bab472019-09-13 12:24:22 +02001076 def __init__(self, db, fs, msg, auth):
1077 BaseTopic.__init__(self, db, fs, msg, auth)
1078 self.nsrTopic = NsrTopic(db, fs, msg, auth)
Felipe Vicensb57758d2018-10-16 16:00:20 +02001079
Felipe Vicensc37b3842019-01-12 12:24:42 +01001080 @staticmethod
1081 def _format_ns_request(ns_request):
1082 formated_request = copy(ns_request)
1083 # TODO: Add request params
1084 return formated_request
1085
1086 @staticmethod
tiernofd160572019-01-21 10:41:37 +00001087 def _format_addional_params(slice_request):
Felipe Vicensc37b3842019-01-12 12:24:42 +01001088 """
1089 Get and format user additional params for NS or VNF
tiernofd160572019-01-21 10:41:37 +00001090 :param slice_request: User instantiation additional parameters
1091 :return: a formatted copy of additional params or None if not supplied
Felipe Vicensc37b3842019-01-12 12:24:42 +01001092 """
tiernofd160572019-01-21 10:41:37 +00001093 additional_params = copy(slice_request.get("additionalParamsForNsi"))
1094 if additional_params:
1095 for k, v in additional_params.items():
1096 if not isinstance(k, str):
1097 raise EngineException("Invalid param at additionalParamsForNsi:{}. Only string keys are allowed".
1098 format(k))
1099 if "." in k or "$" in k:
1100 raise EngineException("Invalid param at additionalParamsForNsi:{}. Keys must not contain dots or $".
1101 format(k))
1102 if isinstance(v, (dict, tuple, list)):
1103 additional_params[k] = "!!yaml " + safe_dump(v)
Felipe Vicensc37b3842019-01-12 12:24:42 +01001104 return additional_params
1105
Felipe Vicensb57758d2018-10-16 16:00:20 +02001106 def _check_descriptor_dependencies(self, session, descriptor):
1107 """
1108 Check that the dependent descriptors exist on a new descriptor or edition
tierno65ca36d2019-02-12 19:27:52 +01001109 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Felipe Vicensb57758d2018-10-16 16:00:20 +02001110 :param descriptor: descriptor to be inserted or edit
1111 :return: None or raises exception
1112 """
Felipe Vicens07f31722018-10-29 15:16:44 +01001113 if not descriptor.get("nst-ref"):
Felipe Vicensb57758d2018-10-16 16:00:20 +02001114 return
Felipe Vicens07f31722018-10-29 15:16:44 +01001115 nstd_id = descriptor["nst-ref"]
Felipe Vicensb57758d2018-10-16 16:00:20 +02001116 if not self.get_item_list(session, "nsts", {"id": nstd_id}):
Felipe Vicens07f31722018-10-29 15:16:44 +01001117 raise EngineException("Descriptor error at nst-ref='{}' references a non exist nstd".format(nstd_id),
Felipe Vicensb57758d2018-10-16 16:00:20 +02001118 http_code=HTTPStatus.CONFLICT)
1119
tiernob4844ab2019-05-23 08:42:12 +00001120 def check_conflict_on_del(self, session, _id, db_content):
1121 """
1122 Check that NSI is not instantiated
1123 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1124 :param _id: nsi internal id
1125 :param db_content: The database content of the _id
1126 :return: None or raises EngineException with the conflict
1127 """
tierno65ca36d2019-02-12 19:27:52 +01001128 if session["force"]:
Felipe Vicensb57758d2018-10-16 16:00:20 +02001129 return
tiernob4844ab2019-05-23 08:42:12 +00001130 nsi = db_content
Felipe Vicensb57758d2018-10-16 16:00:20 +02001131 if nsi["_admin"].get("nsiState") == "INSTANTIATED":
1132 raise EngineException("nsi '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
1133 "Launch 'terminate' operation first; or force deletion".format(_id),
1134 http_code=HTTPStatus.CONFLICT)
1135
tiernobee3bad2019-12-05 12:26:01 +00001136 def delete_extra(self, session, _id, db_content, not_send_msg=None):
Felipe Vicensb57758d2018-10-16 16:00:20 +02001137 """
tiernob4844ab2019-05-23 08:42:12 +00001138 Deletes associated nsilcmops from database. Deletes associated filesystem.
1139 Set usageState of nst
tierno65ca36d2019-02-12 19:27:52 +01001140 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Felipe Vicensb57758d2018-10-16 16:00:20 +02001141 :param _id: server internal id
tiernob4844ab2019-05-23 08:42:12 +00001142 :param db_content: The database content of the descriptor
tiernobee3bad2019-12-05 12:26:01 +00001143 :param not_send_msg: To not send message (False) or store content (list) instead
tiernob4844ab2019-05-23 08:42:12 +00001144 :return: None if ok or raises EngineException with the problem
Felipe Vicensb57758d2018-10-16 16:00:20 +02001145 """
Felipe Vicens07f31722018-10-29 15:16:44 +01001146
Felipe Vicens09e65422019-01-22 15:06:46 +01001147 # Deleting the nsrs belonging to nsir
tiernob4844ab2019-05-23 08:42:12 +00001148 nsir = db_content
Felipe Vicens09e65422019-01-22 15:06:46 +01001149 for nsrs_detailed_item in nsir["_admin"]["nsrs-detailed-list"]:
1150 nsr_id = nsrs_detailed_item["nsrId"]
1151 if nsrs_detailed_item.get("shared"):
1152 _filter = {"_admin.nsrs-detailed-list.ANYINDEX.shared": True,
1153 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nsr_id,
1154 "_id.ne": nsir["_id"]}
1155 nsi = self.db.get_one("nsis", _filter, fail_on_empty=False, fail_on_more=False)
1156 if nsi: # last one using nsr
1157 continue
1158 try:
tiernobee3bad2019-12-05 12:26:01 +00001159 self.nsrTopic.delete(session, nsr_id, dry_run=False, not_send_msg=not_send_msg)
Felipe Vicens09e65422019-01-22 15:06:46 +01001160 except (DbException, EngineException) as e:
1161 if e.http_code == HTTPStatus.NOT_FOUND:
1162 pass
1163 else:
1164 raise
Felipe Vicens07f31722018-10-29 15:16:44 +01001165
tiernob4844ab2019-05-23 08:42:12 +00001166 # delete related nsilcmops database entries
1167 self.db.del_list("nsilcmops", {"netsliceInstanceId": _id})
Felipe Vicens07f31722018-10-29 15:16:44 +01001168
tiernob4844ab2019-05-23 08:42:12 +00001169 # Check and set used NST usage state
Felipe Vicens09e65422019-01-22 15:06:46 +01001170 nsir_admin = nsir.get("_admin")
tiernob4844ab2019-05-23 08:42:12 +00001171 if nsir_admin and nsir_admin.get("nst-id"):
1172 # check if used by another NSI
1173 nsis_list = self.db.get_one("nsis", {"nst-id": nsir_admin["nst-id"]},
1174 fail_on_empty=False, fail_on_more=False)
1175 if not nsis_list:
1176 self.db.set_one("nsts", {"_id": nsir_admin["nst-id"]}, {"_admin.usageState": "NOT_IN_USE"})
1177
1178 # def delete(self, session, _id, dry_run=False):
1179 # """
1180 # Delete item by its internal _id
1181 # :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
1182 # :param _id: server internal id
1183 # :param dry_run: make checking but do not delete
1184 # :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
1185 # """
1186 # # TODO add admin to filter, validate rights
1187 # BaseTopic.delete(self, session, _id, dry_run=True)
1188 # if dry_run:
1189 # return
1190 #
1191 # # Deleting the nsrs belonging to nsir
1192 # nsir = self.db.get_one("nsis", {"_id": _id})
1193 # for nsrs_detailed_item in nsir["_admin"]["nsrs-detailed-list"]:
1194 # nsr_id = nsrs_detailed_item["nsrId"]
1195 # if nsrs_detailed_item.get("shared"):
1196 # _filter = {"_admin.nsrs-detailed-list.ANYINDEX.shared": True,
1197 # "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nsr_id,
1198 # "_id.ne": nsir["_id"]}
1199 # nsi = self.db.get_one("nsis", _filter, fail_on_empty=False, fail_on_more=False)
1200 # if nsi: # last one using nsr
1201 # continue
1202 # try:
1203 # self.nsrTopic.delete(session, nsr_id, dry_run=False)
1204 # except (DbException, EngineException) as e:
1205 # if e.http_code == HTTPStatus.NOT_FOUND:
1206 # pass
1207 # else:
1208 # raise
1209 # # deletes NetSlice instance object
1210 # v = self.db.del_one("nsis", {"_id": _id})
1211 #
1212 # # makes a temporal list of nsilcmops objects related to the _id given and deletes them from db
1213 # _filter = {"netsliceInstanceId": _id}
1214 # self.db.del_list("nsilcmops", _filter)
1215 #
1216 # # Search if nst is being used by other nsi
1217 # nsir_admin = nsir.get("_admin")
1218 # if nsir_admin:
1219 # if nsir_admin.get("nst-id"):
1220 # nsis_list = self.db.get_one("nsis", {"nst-id": nsir_admin["nst-id"]},
1221 # fail_on_empty=False, fail_on_more=False)
1222 # if not nsis_list:
1223 # self.db.set_one("nsts", {"_id": nsir_admin["nst-id"]}, {"_admin.usageState": "NOT_IN_USE"})
1224 # return v
Felipe Vicensb57758d2018-10-16 16:00:20 +02001225
tierno65ca36d2019-02-12 19:27:52 +01001226 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Felipe Vicensb57758d2018-10-16 16:00:20 +02001227 """
Felipe Vicens07f31722018-10-29 15:16:44 +01001228 Creates a new netslice instance record into database. It also creates needed nsrs and vnfrs
Felipe Vicensb57758d2018-10-16 16:00:20 +02001229 :param rollback: list to append the created items at database in case a rollback must be done
tierno65ca36d2019-02-12 19:27:52 +01001230 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Felipe Vicensb57758d2018-10-16 16:00:20 +02001231 :param indata: params to be used for the nsir
1232 :param kwargs: used to override the indata descriptor
1233 :param headers: http request headers
Felipe Vicensb57758d2018-10-16 16:00:20 +02001234 :return: the _id of nsi descriptor created at database
1235 """
1236
1237 try:
delacruzramo32bab472019-09-13 12:24:22 +02001238 step = "checking quotas"
1239 self.check_quota(session)
1240
tierno99d4b172019-07-02 09:28:40 +00001241 step = ""
Felipe Vicensb57758d2018-10-16 16:00:20 +02001242 slice_request = self._remove_envelop(indata)
1243 # Override descriptor with query string kwargs
1244 self._update_input_with_kwargs(slice_request, kwargs)
tierno65ca36d2019-02-12 19:27:52 +01001245 self._validate_input_new(slice_request, session["force"])
Felipe Vicensb57758d2018-10-16 16:00:20 +02001246
Felipe Vicensb57758d2018-10-16 16:00:20 +02001247 # look for nstd
tierno9e5eea32018-11-29 09:42:09 +00001248 step = "getting nstd id='{}' from database".format(slice_request.get("nstId"))
tiernob4844ab2019-05-23 08:42:12 +00001249 _filter = self._get_project_filter(session)
1250 _filter["_id"] = slice_request["nstId"]
Felipe Vicensb57758d2018-10-16 16:00:20 +02001251 nstd = self.db.get_one("nsts", _filter)
tiernob4844ab2019-05-23 08:42:12 +00001252 del _filter["_id"]
1253
Felipe Vicens07f31722018-10-29 15:16:44 +01001254 nstd.pop("_admin", None)
Felipe Vicens09e65422019-01-22 15:06:46 +01001255 nstd_id = nstd.pop("_id", None)
Felipe Vicensb57758d2018-10-16 16:00:20 +02001256 nsi_id = str(uuid4())
Felipe Vicensb57758d2018-10-16 16:00:20 +02001257 step = "filling nsi_descriptor with input data"
Felipe Vicens07f31722018-10-29 15:16:44 +01001258
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001259 # Creating the NSIR
Felipe Vicensb57758d2018-10-16 16:00:20 +02001260 nsi_descriptor = {
1261 "id": nsi_id,
garciadeblasc54d4202018-11-29 23:41:37 +01001262 "name": slice_request["nsiName"],
1263 "description": slice_request.get("nsiDescription", ""),
1264 "datacenter": slice_request["vimAccountId"],
Felipe Vicensb57758d2018-10-16 16:00:20 +02001265 "nst-ref": nstd["id"],
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001266 "instantiation_parameters": slice_request,
Felipe Vicensb57758d2018-10-16 16:00:20 +02001267 "network-slice-template": nstd,
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001268 "nsr-ref-list": [],
1269 "vlr-list": [],
Felipe Vicensb57758d2018-10-16 16:00:20 +02001270 "_id": nsi_id,
tiernofd160572019-01-21 10:41:37 +00001271 "additionalParamsForNsi": self._format_addional_params(slice_request)
Felipe Vicensb57758d2018-10-16 16:00:20 +02001272 }
Felipe Vicensb57758d2018-10-16 16:00:20 +02001273
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001274 step = "creating nsi at database"
tierno65ca36d2019-02-12 19:27:52 +01001275 self.format_on_new(nsi_descriptor, session["project_id"], make_public=session["public"])
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001276 nsi_descriptor["_admin"]["nsiState"] = "NOT_INSTANTIATED"
1277 nsi_descriptor["_admin"]["netslice-subnet"] = None
Felipe Vicens09e65422019-01-22 15:06:46 +01001278 nsi_descriptor["_admin"]["deployed"] = {}
1279 nsi_descriptor["_admin"]["deployed"]["RO"] = []
1280 nsi_descriptor["_admin"]["nst-id"] = nstd_id
1281
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001282 # Creating netslice-vld for the RO.
1283 step = "creating netslice-vld at database"
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001284
1285 # Building the vlds list to be deployed
1286 # From netslice descriptors, creating the initial list
Felipe Vicens09e65422019-01-22 15:06:46 +01001287 nsi_vlds = []
1288
1289 for netslice_vlds in get_iterable(nstd.get("netslice-vld")):
1290 # Getting template Instantiation parameters from NST
1291 nsi_vld = deepcopy(netslice_vlds)
1292 nsi_vld["shared-nsrs-list"] = []
1293 nsi_vld["vimAccountId"] = slice_request["vimAccountId"]
1294 nsi_vlds.append(nsi_vld)
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001295
1296 nsi_descriptor["_admin"]["netslice-vld"] = nsi_vlds
tierno3ffc7a42019-12-03 09:39:40 +00001297 # Creating netslice-subnet_record.
Felipe Vicensb57758d2018-10-16 16:00:20 +02001298 needed_nsds = {}
Felipe Vicens07f31722018-10-29 15:16:44 +01001299 services = []
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001300
Felipe Vicens09e65422019-01-22 15:06:46 +01001301 # Updating the nstd with the nsd["_id"] associated to the nss -> services list
Felipe Vicensb57758d2018-10-16 16:00:20 +02001302 for member_ns in nstd["netslice-subnet"]:
1303 nsd_id = member_ns["nsd-ref"]
1304 step = "getting nstd id='{}' constituent-nsd='{}' from database".format(
1305 member_ns["nsd-ref"], member_ns["id"])
1306 if nsd_id not in needed_nsds:
1307 # Obtain nsd
tiernob4844ab2019-05-23 08:42:12 +00001308 _filter["id"] = nsd_id
1309 nsd = self.db.get_one("nsds", _filter, fail_on_empty=True, fail_on_more=True)
1310 del _filter["id"]
Felipe Vicensb57758d2018-10-16 16:00:20 +02001311 nsd.pop("_admin")
1312 needed_nsds[nsd_id] = nsd
1313 else:
1314 nsd = needed_nsds[nsd_id]
Felipe Vicens09e65422019-01-22 15:06:46 +01001315 member_ns["_id"] = needed_nsds[nsd_id].get("_id")
1316 services.append(member_ns)
Felipe Vicens07f31722018-10-29 15:16:44 +01001317
Felipe Vicensb57758d2018-10-16 16:00:20 +02001318 step = "filling nsir nsd-id='{}' constituent-nsd='{}' from database".format(
1319 member_ns["nsd-ref"], member_ns["id"])
Felipe Vicensb57758d2018-10-16 16:00:20 +02001320
Felipe Vicens07f31722018-10-29 15:16:44 +01001321 # creates Network Services records (NSRs)
1322 step = "creating nsrs at database using NsrTopic.new()"
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001323 ns_params = slice_request.get("netslice-subnet")
Felipe Vicens07f31722018-10-29 15:16:44 +01001324 nsrs_list = []
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001325 nsi_netslice_subnet = []
Felipe Vicens07f31722018-10-29 15:16:44 +01001326 for service in services:
Felipe Vicens09e65422019-01-22 15:06:46 +01001327 # Check if the netslice-subnet is shared and if it is share if the nss exists
1328 _id_nsr = None
Felipe Vicens07f31722018-10-29 15:16:44 +01001329 indata_ns = {}
Felipe Vicens09e65422019-01-22 15:06:46 +01001330 # Is the nss shared and instantiated?
tiernob4844ab2019-05-23 08:42:12 +00001331 _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True
1332 _filter["_admin.nsrs-detailed-list.ANYINDEX.nsd-id"] = service["nsd-ref"]
Felipe Vicens08ddb142019-08-09 15:52:40 +02001333 _filter["_admin.nsrs-detailed-list.ANYINDEX.nss-id"] = service["id"]
Felipe Vicens09e65422019-01-22 15:06:46 +01001334 nsi = self.db.get_one("nsis", _filter, fail_on_empty=False, fail_on_more=False)
Felipe Vicens09e65422019-01-22 15:06:46 +01001335 if nsi and service.get("is-shared-nss"):
1336 nsrs_detailed_list = nsi["_admin"]["nsrs-detailed-list"]
1337 for nsrs_detailed_item in nsrs_detailed_list:
1338 if nsrs_detailed_item["nsd-id"] == service["nsd-ref"]:
Felipe Vicens08ddb142019-08-09 15:52:40 +02001339 if nsrs_detailed_item["nss-id"] == service["id"]:
1340 _id_nsr = nsrs_detailed_item["nsrId"]
1341 break
Felipe Vicens09e65422019-01-22 15:06:46 +01001342 for netslice_subnet in nsi["_admin"]["netslice-subnet"]:
1343 if netslice_subnet["nss-id"] == service["id"]:
1344 indata_ns = netslice_subnet
1345 break
1346 else:
1347 indata_ns = {}
1348 if service.get("instantiation-parameters"):
1349 indata_ns = deepcopy(service["instantiation-parameters"])
1350 # del service["instantiation-parameters"]
1351
1352 indata_ns["nsdId"] = service["_id"]
1353 indata_ns["nsName"] = slice_request.get("nsiName") + "." + service["id"]
1354 indata_ns["vimAccountId"] = slice_request.get("vimAccountId")
1355 indata_ns["nsDescription"] = service["description"]
tierno99d4b172019-07-02 09:28:40 +00001356 if slice_request.get("ssh_keys"):
1357 indata_ns["ssh_keys"] = slice_request.get("ssh_keys")
Felipe Vicensc37b3842019-01-12 12:24:42 +01001358
Felipe Vicens09e65422019-01-22 15:06:46 +01001359 if ns_params:
1360 for ns_param in ns_params:
1361 if ns_param.get("id") == service["id"]:
1362 copy_ns_param = deepcopy(ns_param)
1363 del copy_ns_param["id"]
1364 indata_ns.update(copy_ns_param)
1365 break
1366
1367 # Creates Nsr objects
tiernobdebce92019-07-01 15:36:49 +00001368 _id_nsr, _ = self.nsrTopic.new(rollback, session, indata_ns, kwargs, headers)
Felipe Vicens09e65422019-01-22 15:06:46 +01001369 nsrs_item = {"nsrId": _id_nsr, "shared": service.get("is-shared-nss"), "nsd-id": service["nsd-ref"],
Felipe Vicens08ddb142019-08-09 15:52:40 +02001370 "nss-id": service["id"], "nslcmop_instantiate": None}
Felipe Vicens09e65422019-01-22 15:06:46 +01001371 indata_ns["nss-id"] = service["id"]
Felipe Vicens07f31722018-10-29 15:16:44 +01001372 nsrs_list.append(nsrs_item)
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001373 nsi_netslice_subnet.append(indata_ns)
1374 nsr_ref = {"nsr-ref": _id_nsr}
1375 nsi_descriptor["nsr-ref-list"].append(nsr_ref)
Felipe Vicens07f31722018-10-29 15:16:44 +01001376
1377 # Adding the nsrs list to the nsi
1378 nsi_descriptor["_admin"]["nsrs-detailed-list"] = nsrs_list
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001379 nsi_descriptor["_admin"]["netslice-subnet"] = nsi_netslice_subnet
Felipe Vicens09e65422019-01-22 15:06:46 +01001380 self.db.set_one("nsts", {"_id": slice_request["nstId"]}, {"_admin.usageState": "IN_USE"})
1381
Felipe Vicens07f31722018-10-29 15:16:44 +01001382 # Creating the entry in the database
Felipe Vicensb57758d2018-10-16 16:00:20 +02001383 self.db.create("nsis", nsi_descriptor)
1384 rollback.append({"topic": "nsis", "_id": nsi_id})
tiernobdebce92019-07-01 15:36:49 +00001385 return nsi_id, None
1386 except Exception as e: # TODO remove try Except, it is captured at nbi.py
Felipe Vicensb57758d2018-10-16 16:00:20 +02001387 self.logger.exception("Exception {} at NsiTopic.new()".format(e), exc_info=True)
1388 raise EngineException("Error {}: {}".format(step, e))
1389 except ValidationError as e:
1390 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
1391
tierno65ca36d2019-02-12 19:27:52 +01001392 def edit(self, session, _id, indata=None, kwargs=None, content=None):
Felipe Vicensb57758d2018-10-16 16:00:20 +02001393 raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
Felipe Vicens07f31722018-10-29 15:16:44 +01001394
1395
1396class NsiLcmOpTopic(BaseTopic):
1397 topic = "nsilcmops"
1398 topic_msg = "nsi"
1399 operation_schema = { # mapping between operation and jsonschema to validate
1400 "instantiate": nsi_instantiate,
1401 "terminate": None
1402 }
Felipe Vicens09e65422019-01-22 15:06:46 +01001403
delacruzramo32bab472019-09-13 12:24:22 +02001404 def __init__(self, db, fs, msg, auth):
1405 BaseTopic.__init__(self, db, fs, msg, auth)
1406 self.nsi_NsLcmOpTopic = NsLcmOpTopic(self.db, self.fs, self.msg, self.auth)
Felipe Vicens07f31722018-10-29 15:16:44 +01001407
1408 def _check_nsi_operation(self, session, nsir, operation, indata):
1409 """
1410 Check that user has enter right parameters for the operation
tierno65ca36d2019-02-12 19:27:52 +01001411 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Felipe Vicens07f31722018-10-29 15:16:44 +01001412 :param operation: it can be: instantiate, terminate, action, TODO: update, heal
1413 :param indata: descriptor with the parameters of the operation
1414 :return: None
1415 """
1416 nsds = {}
1417 nstd = nsir["network-slice-template"]
1418
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001419 def check_valid_netslice_subnet_id(nstId):
Felipe Vicens07f31722018-10-29 15:16:44 +01001420 # TODO change to vnfR (??)
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001421 for netslice_subnet in nstd["netslice-subnet"]:
1422 if nstId == netslice_subnet["id"]:
1423 nsd_id = netslice_subnet["nsd-ref"]
Felipe Vicens07f31722018-10-29 15:16:44 +01001424 if nsd_id not in nsds:
Felipe Vicens5403f542020-05-22 16:37:39 +02001425 _filter = self._get_project_filter(session)
1426 _filter["id"] = nsd_id
1427 nsds[nsd_id] = self.db.get_one("nsds", _filter)
Felipe Vicens07f31722018-10-29 15:16:44 +01001428 return nsds[nsd_id]
1429 else:
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001430 raise EngineException("Invalid parameter nstId='{}' is not one of the "
1431 "nst:netslice-subnet".format(nstId))
Felipe Vicens07f31722018-10-29 15:16:44 +01001432 if operation == "instantiate":
1433 # check the existance of netslice-subnet items
Felipe Vicensc8bbaaa2018-12-01 04:42:40 +01001434 for in_nst in get_iterable(indata.get("netslice-subnet")):
1435 check_valid_netslice_subnet_id(in_nst["id"])
Felipe Vicens07f31722018-10-29 15:16:44 +01001436
1437 def _create_nsilcmop(self, session, netsliceInstanceId, operation, params):
1438 now = time()
1439 _id = str(uuid4())
1440 nsilcmop = {
1441 "id": _id,
1442 "_id": _id,
1443 "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
1444 "statusEnteredTime": now,
1445 "netsliceInstanceId": netsliceInstanceId,
1446 "lcmOperationType": operation,
1447 "startTime": now,
1448 "isAutomaticInvocation": False,
1449 "operationParams": params,
1450 "isCancelPending": False,
1451 "links": {
1452 "self": "/osm/nsilcm/v1/nsi_lcm_op_occs/" + _id,
Felipe Vicens126af572019-06-05 19:13:04 +02001453 "netsliceInstanceId": "/osm/nsilcm/v1/netslice_instances/" + netsliceInstanceId,
Felipe Vicens07f31722018-10-29 15:16:44 +01001454 }
1455 }
1456 return nsilcmop
1457
Felipe Vicens09e65422019-01-22 15:06:46 +01001458 def add_shared_nsr_2vld(self, nsir, nsr_item):
1459 for nst_sb_item in nsir["network-slice-template"].get("netslice-subnet"):
1460 if nst_sb_item.get("is-shared-nss"):
1461 for admin_subnet_item in nsir["_admin"].get("netslice-subnet"):
1462 if admin_subnet_item["nss-id"] == nst_sb_item["id"]:
1463 for admin_vld_item in nsir["_admin"].get("netslice-vld"):
1464 for admin_vld_nss_cp_ref_item in admin_vld_item["nss-connection-point-ref"]:
1465 if admin_subnet_item["nss-id"] == admin_vld_nss_cp_ref_item["nss-ref"]:
1466 if not nsr_item["nsrId"] in admin_vld_item["shared-nsrs-list"]:
1467 admin_vld_item["shared-nsrs-list"].append(nsr_item["nsrId"])
1468 break
1469 # self.db.set_one("nsis", {"_id": nsir["_id"]}, nsir)
1470 self.db.set_one("nsis", {"_id": nsir["_id"]}, {"_admin.netslice-vld": nsir["_admin"].get("netslice-vld")})
1471
tierno65ca36d2019-02-12 19:27:52 +01001472 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
Felipe Vicens07f31722018-10-29 15:16:44 +01001473 """
1474 Performs a new operation over a ns
1475 :param rollback: list to append created items at database in case a rollback must to be done
tierno65ca36d2019-02-12 19:27:52 +01001476 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
Felipe Vicens07f31722018-10-29 15:16:44 +01001477 :param indata: descriptor with the parameters of the operation. It must contains among others
Felipe Vicens126af572019-06-05 19:13:04 +02001478 netsliceInstanceId: _id of the nsir to perform the operation
Felipe Vicens07f31722018-10-29 15:16:44 +01001479 operation: it can be: instantiate, terminate, action, TODO: update, heal
1480 :param kwargs: used to override the indata descriptor
1481 :param headers: http request headers
Felipe Vicens07f31722018-10-29 15:16:44 +01001482 :return: id of the nslcmops
1483 """
1484 try:
1485 # Override descriptor with query string kwargs
1486 self._update_input_with_kwargs(indata, kwargs)
1487 operation = indata["lcmOperationType"]
Felipe Vicens126af572019-06-05 19:13:04 +02001488 netsliceInstanceId = indata["netsliceInstanceId"]
Felipe Vicens07f31722018-10-29 15:16:44 +01001489 validate_input(indata, self.operation_schema[operation])
1490
Felipe Vicens126af572019-06-05 19:13:04 +02001491 # get nsi from netsliceInstanceId
tiernob4844ab2019-05-23 08:42:12 +00001492 _filter = self._get_project_filter(session)
Felipe Vicens126af572019-06-05 19:13:04 +02001493 _filter["_id"] = netsliceInstanceId
Felipe Vicens07f31722018-10-29 15:16:44 +01001494 nsir = self.db.get_one("nsis", _filter)
tiernob4844ab2019-05-23 08:42:12 +00001495 del _filter["_id"]
Felipe Vicens07f31722018-10-29 15:16:44 +01001496
1497 # initial checking
1498 if not nsir["_admin"].get("nsiState") or nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
1499 if operation == "terminate" and indata.get("autoremove"):
1500 # NSIR must be deleted
tierno586ae812019-10-17 13:56:53 +00001501 return None, None # a none in this case is used to indicate not instantiated. It can be removed
Felipe Vicens07f31722018-10-29 15:16:44 +01001502 if operation != "instantiate":
1503 raise EngineException("netslice_instance '{}' cannot be '{}' because it is not instantiated".format(
Felipe Vicens126af572019-06-05 19:13:04 +02001504 netsliceInstanceId, operation), HTTPStatus.CONFLICT)
Felipe Vicens07f31722018-10-29 15:16:44 +01001505 else:
tierno65ca36d2019-02-12 19:27:52 +01001506 if operation == "instantiate" and not session["force"]:
Felipe Vicens07f31722018-10-29 15:16:44 +01001507 raise EngineException("netslice_instance '{}' cannot be '{}' because it is already instantiated".
Felipe Vicens126af572019-06-05 19:13:04 +02001508 format(netsliceInstanceId, operation), HTTPStatus.CONFLICT)
Felipe Vicens07f31722018-10-29 15:16:44 +01001509
1510 # Creating all the NS_operation (nslcmop)
1511 # Get service list from db
1512 nsrs_list = nsir["_admin"]["nsrs-detailed-list"]
1513 nslcmops = []
Felipe Vicens09e65422019-01-22 15:06:46 +01001514 # nslcmops_item = None
1515 for index, nsr_item in enumerate(nsrs_list):
1516 nsi = None
1517 if nsr_item.get("shared"):
Felipe Vicens58e2d2f2019-05-30 13:01:20 +02001518 _filter["_admin.nsrs-detailed-list.ANYINDEX.shared"] = True
tiernob4844ab2019-05-23 08:42:12 +00001519 _filter["_admin.nsrs-detailed-list.ANYINDEX.nsrId"] = nsr_item["nsrId"]
1520 _filter["_admin.nsrs-detailed-list.ANYINDEX.nslcmop_instantiate.ne"] = None
Felipe Vicens126af572019-06-05 19:13:04 +02001521 _filter["_id.ne"] = netsliceInstanceId
Felipe Vicens09e65422019-01-22 15:06:46 +01001522 nsi = self.db.get_one("nsis", _filter, fail_on_empty=False, fail_on_more=False)
Felipe Vicens58e2d2f2019-05-30 13:01:20 +02001523 if operation == "terminate":
1524 _update = {"_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format(index): None}
1525 self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
1526
Felipe Vicens09e65422019-01-22 15:06:46 +01001527 # looks the first nsi fulfilling the conditions but not being the current NSIR
1528 if nsi:
1529 nsi_admin_shared = nsi["_admin"]["nsrs-detailed-list"]
1530 for nsi_nsr_item in nsi_admin_shared:
1531 if nsi_nsr_item["nsd-id"] == nsr_item["nsd-id"] and nsi_nsr_item["shared"]:
1532 self.add_shared_nsr_2vld(nsir, nsr_item)
1533 nslcmops.append(nsi_nsr_item["nslcmop_instantiate"])
1534 _update = {"_admin.nsrs-detailed-list.{}".format(index): nsi_nsr_item}
1535 self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
1536 break
1537 # continue to not create nslcmop since nsrs is shared and nsrs was created
1538 continue
1539 else:
1540 self.add_shared_nsr_2vld(nsir, nsr_item)
1541
1542 try:
1543 service = self.db.get_one("nsrs", {"_id": nsr_item["nsrId"]})
tierno0b8752f2020-05-12 09:42:02 +00001544 indata_ns = {
1545 "lcmOperationType": operation,
1546 "nsInstanceId": service["_id"],
1547 # Including netslice_id in the ns instantiate Operation
1548 "netsliceInstanceId": netsliceInstanceId,
1549 }
1550 if operation == "instantiate":
1551 indata_ns.update(service["instantiate_params"])
1552
tierno99d4b172019-07-02 09:28:40 +00001553 # Creating NS_LCM_OP with the flag slice_object=True to not trigger the service instantiation
Felipe Vicens09e65422019-01-22 15:06:46 +01001554 # message via kafka bus
tiernobdebce92019-07-01 15:36:49 +00001555 nslcmop, _ = self.nsi_NsLcmOpTopic.new(rollback, session, indata_ns, kwargs, headers,
1556 slice_object=True)
Felipe Vicens09e65422019-01-22 15:06:46 +01001557 nslcmops.append(nslcmop)
1558 if operation == "terminate":
1559 nslcmop = None
1560 _update = {"_admin.nsrs-detailed-list.{}.nslcmop_instantiate".format(index): nslcmop}
1561 self.db.set_one("nsis", {"_id": nsir["_id"]}, _update)
1562 except (DbException, EngineException) as e:
1563 if e.http_code == HTTPStatus.NOT_FOUND:
1564 self.logger.info("HTTPStatus.NOT_FOUND")
1565 pass
1566 else:
1567 raise
Felipe Vicens07f31722018-10-29 15:16:44 +01001568
1569 # Creates nsilcmop
1570 indata["nslcmops_ids"] = nslcmops
1571 self._check_nsi_operation(session, nsir, operation, indata)
Felipe Vicens09e65422019-01-22 15:06:46 +01001572
Felipe Vicens126af572019-06-05 19:13:04 +02001573 nsilcmop_desc = self._create_nsilcmop(session, netsliceInstanceId, operation, indata)
tierno65ca36d2019-02-12 19:27:52 +01001574 self.format_on_new(nsilcmop_desc, session["project_id"], make_public=session["public"])
Felipe Vicens07f31722018-10-29 15:16:44 +01001575 _id = self.db.create("nsilcmops", nsilcmop_desc)
1576 rollback.append({"topic": "nsilcmops", "_id": _id})
1577 self.msg.write("nsi", operation, nsilcmop_desc)
tiernobdebce92019-07-01 15:36:49 +00001578 return _id, None
Felipe Vicens07f31722018-10-29 15:16:44 +01001579 except ValidationError as e:
1580 raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
Felipe Vicens07f31722018-10-29 15:16:44 +01001581
tiernobee3bad2019-12-05 12:26:01 +00001582 def delete(self, session, _id, dry_run=False, not_send_msg=None):
Felipe Vicens07f31722018-10-29 15:16:44 +01001583 raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR)
1584
tierno65ca36d2019-02-12 19:27:52 +01001585 def edit(self, session, _id, indata=None, kwargs=None, content=None):
Felipe Vicens07f31722018-10-29 15:16:44 +01001586 raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR)