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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from uuid
import uuid4
18 from http
import HTTPStatus
20 from copy
import copy
, deepcopy
21 from validation
import validate_input
, ValidationError
, ns_instantiate
, ns_action
, ns_scale
, nsi_instantiate
22 from base_topic
import BaseTopic
, EngineException
, get_iterable
23 from descriptor_topics
import DescriptorTopic
25 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
28 class NsrTopic(BaseTopic
):
32 def __init__(self
, db
, fs
, msg
):
33 BaseTopic
.__init
__(self
, db
, fs
, msg
)
35 def _check_descriptor_dependencies(self
, session
, descriptor
):
37 Check that the dependent descriptors exist on a new descriptor or edition
38 :param session: client session information
39 :param descriptor: descriptor to be inserted or edit
40 :return: None or raises exception
42 if not descriptor
.get("nsdId"):
44 nsd_id
= descriptor
["nsdId"]
45 if not self
.get_item_list(session
, "nsds", {"id": nsd_id
}):
46 raise EngineException("Descriptor error at nsdId='{}' references a non exist nsd".format(nsd_id
),
47 http_code
=HTTPStatus
.CONFLICT
)
50 def format_on_new(content
, project_id
=None, make_public
=False):
51 BaseTopic
.format_on_new(content
, project_id
=project_id
, make_public
=make_public
)
52 content
["_admin"]["nsState"] = "NOT_INSTANTIATED"
54 def check_conflict_on_del(self
, session
, _id
, force
=False):
57 nsr
= self
.db
.get_one("nsrs", {"_id": _id
})
58 if nsr
["_admin"].get("nsState") == "INSTANTIATED":
59 raise EngineException("nsr '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
60 "Launch 'terminate' operation first; or force deletion".format(_id
),
61 http_code
=HTTPStatus
.CONFLICT
)
63 def delete(self
, session
, _id
, force
=False, dry_run
=False):
65 Delete item by its internal _id
66 :param session: contains the used login username, working project, and admin rights
67 :param _id: server internal id
68 :param force: indicates if deletion must be forced in case of conflict
69 :param dry_run: make checking but do not delete
70 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
72 # TODO add admin to filter, validate rights
73 BaseTopic
.delete(self
, session
, _id
, force
, dry_run
=True)
77 v
= self
.db
.del_one("nsrs", {"_id": _id
})
78 self
.db
.del_list("nslcmops", {"nsInstanceId": _id
})
79 self
.db
.del_list("vnfrs", {"nsr-id-ref": _id
})
80 # set all used pdus as free
81 self
.db
.set_list("pdus", {"_admin.usage.nsr_id": _id
},
82 {"_admin.usageState": "NOT_IN_USE", "_admin.usage": None})
83 self
._send
_msg
("deleted", {"_id": _id
})
86 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False):
88 Creates a new nsr into database. It also creates needed vnfrs
89 :param rollback: list to append the created items at database in case a rollback must be done
90 :param session: contains the used login username and working project
91 :param indata: params to be used for the nsr
92 :param kwargs: used to override the indata descriptor
93 :param headers: http request headers
94 :param force: If True avoid some dependence checks
95 :param make_public: Make the created item public to all projects
96 :return: the _id of nsr descriptor created at database
100 ns_request
= self
._remove
_envelop
(indata
)
101 # Override descriptor with query string kwargs
102 self
._update
_input
_with
_kwargs
(ns_request
, kwargs
)
103 self
._validate
_input
_new
(ns_request
, force
)
107 step
= "getting nsd id='{}' from database".format(ns_request
.get("nsdId"))
108 _filter
= {"_id": ns_request
["nsdId"]}
109 _filter
.update(BaseTopic
._get
_project
_filter
(session
, write
=False, show_all
=True))
110 nsd
= self
.db
.get_one("nsds", _filter
)
112 nsr_id
= str(uuid4())
114 step
= "filling nsr from input data"
116 "name": ns_request
["nsName"],
117 "name-ref": ns_request
["nsName"],
118 "short-name": ns_request
["nsName"],
119 "admin-status": "ENABLED",
121 "datacenter": ns_request
["vimAccountId"],
122 "resource-orchestrator": "osmopenmano",
123 "description": ns_request
.get("nsDescription", ""),
124 "constituent-vnfr-ref": [],
126 "operational-status": "init", # typedef ns-operational-
127 "config-status": "init", # typedef config-states
128 "detailed-status": "scheduled",
130 "orchestration-progress": {},
131 # {"networks": {"active": 0, "total": 0}, "vms": {"active": 0, "total": 0}},
134 "nsd-name-ref": nsd
["name"],
135 "operational-events": [], # "id", "timestamp", "description", "event",
136 "nsd-ref": nsd
["id"],
137 "instantiate_params": ns_request
,
138 "ns-instance-config-ref": nsr_id
,
141 # "input-parameter": xpath, value,
142 "ssh-authorized-key": ns_request
.get("key-pair-ref"), # TODO remove
144 ns_request
["nsr_id"] = nsr_id
147 nsr_descriptor
["vld"] = []
148 for nsd_vld
in nsd
.get("vld"):
149 nsr_descriptor
["vld"].append(
150 {key
: nsd_vld
[key
] for key
in ("id", "vim-network-name", "vim-network-id") if key
in nsd_vld
})
154 for member_vnf
in nsd
.get("constituent-vnfd", ()):
155 vnfd_id
= member_vnf
["vnfd-id-ref"]
156 step
= "getting vnfd id='{}' constituent-vnfd='{}' from database".format(
157 member_vnf
["vnfd-id-ref"], member_vnf
["member-vnf-index"])
158 if vnfd_id
not in needed_vnfds
:
160 vnfd
= DescriptorTopic
.get_one_by_id(self
.db
, session
, "vnfds", vnfd_id
)
162 needed_vnfds
[vnfd_id
] = vnfd
164 vnfd
= needed_vnfds
[vnfd_id
]
165 step
= "filling vnfr vnfd-id='{}' constituent-vnfd='{}'".format(
166 member_vnf
["vnfd-id-ref"], member_vnf
["member-vnf-index"])
167 vnfr_id
= str(uuid4())
171 "nsr-id-ref": nsr_id
,
172 "member-vnf-index-ref": member_vnf
["member-vnf-index"],
174 # "vnfd": vnfd, # at OSM model.but removed to avoid data duplication TODO: revise
176 "vnfd-id": vnfd
["_id"], # not at OSM model, but useful
177 "vim-account-id": None,
179 "connection-point": [],
180 "ip-address": None, # mgmt-interface filled by LCM
184 if vnfd
.get("internal-vld"):
185 vnfr_descriptor
["vld"] = []
186 for vnfd_vld
in vnfd
.get("internal-vld"):
187 vnfr_descriptor
["vld"].append(
188 {key
: vnfd_vld
[key
] for key
in ("id", "vim-network-name", "vim-network-id") if key
in
191 vnfd_mgmt_cp
= vnfd
["mgmt-interface"].get("cp")
192 for cp
in vnfd
.get("connection-point", ()):
195 "connection-point-id": cp
.get("id"),
197 # "ip-address", "mac-address" # filled by LCM
198 # vim-id # TODO it would be nice having a vim port id
200 vnfr_descriptor
["connection-point"].append(vnf_cp
)
201 for vdu
in vnfd
.get("vdu", ()):
203 "vdu-id-ref": vdu
["id"],
204 # TODO "name": "" Name of the VDU in the VIM
205 "ip-address": None, # mgmt-interface filled by LCM
206 # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM
207 "internal-connection-point": [],
210 if vdu
.get("pdu-type"):
211 vdur
["pdu-type"] = vdu
["pdu-type"]
212 # TODO volumes: name, volume-id
213 for icp
in vdu
.get("internal-connection-point", ()):
216 "connection-point-id": icp
["id"],
217 "name": icp
.get("name"),
218 # "ip-address", "mac-address" # filled by LCM
219 # vim-id # TODO it would be nice having a vim port id
221 vdur
["internal-connection-point"].append(vdu_icp
)
222 for iface
in vdu
.get("interface", ()):
224 "name": iface
.get("name"),
225 # "ip-address", "mac-address" # filled by LCM
226 # vim-id # TODO it would be nice having a vim port id
228 if vnfd_mgmt_cp
and iface
.get("external-connection-point-ref") == vnfd_mgmt_cp
:
229 vdu_iface
["mgmt-vnf"] = True
230 if iface
.get("mgmt-interface"):
231 vdu_iface
["mgmt-interface"] = True # TODO change to mgmt-vdu
233 # look for network where this interface is connected
234 if iface
.get("external-connection-point-ref"):
235 for nsd_vld
in get_iterable(nsd
.get("vld")):
236 for nsd_vld_cp
in get_iterable(nsd_vld
.get("vnfd-connection-point-ref")):
237 if nsd_vld_cp
.get("vnfd-connection-point-ref") == \
238 iface
["external-connection-point-ref"] and \
239 nsd_vld_cp
.get("member-vnf-index-ref") == member_vnf
["member-vnf-index"]:
240 vdu_iface
["ns-vld-id"] = nsd_vld
["id"]
245 elif iface
.get("internal-connection-point-ref"):
246 for vnfd_ivld
in get_iterable(vnfd
.get("internal-vld")):
247 for vnfd_ivld_icp
in get_iterable(vnfd_ivld
.get("internal-connection-point")):
248 if vnfd_ivld_icp
.get("id-ref") == iface
["internal-connection-point-ref"]:
249 vdu_iface
["vnf-vld-id"] = vnfd_ivld
["id"]
255 vdur
["interfaces"].append(vdu_iface
)
256 count
= vdu
.get("count", 1)
259 count
= int(count
) # TODO remove when descriptor serialized with payngbind
260 for index
in range(0, count
):
262 vdur
= deepcopy(vdur
)
263 vdur
["_id"] = str(uuid4())
264 vdur
["count-index"] = index
265 vnfr_descriptor
["vdur"].append(vdur
)
267 step
= "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format(
268 member_vnf
["vnfd-id-ref"], member_vnf
["member-vnf-index"])
271 BaseTopic
.format_on_new(vnfr_descriptor
, session
["project_id"], make_public
=make_public
)
272 self
.db
.create("vnfrs", vnfr_descriptor
)
273 rollback
.append({"topic": "vnfrs", "_id": vnfr_id
})
274 nsr_descriptor
["constituent-vnfr-ref"].append(vnfr_id
)
276 step
= "creating nsr at database"
277 self
.format_on_new(nsr_descriptor
, session
["project_id"], make_public
=make_public
)
278 self
.db
.create("nsrs", nsr_descriptor
)
279 rollback
.append({"topic": "nsrs", "_id": nsr_id
})
281 except Exception as e
:
282 self
.logger
.exception("Exception {} at NsrTopic.new()".format(e
), exc_info
=True)
283 raise EngineException("Error {}: {}".format(step
, e
))
284 except ValidationError
as e
:
285 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
287 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
288 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
291 class VnfrTopic(BaseTopic
):
295 def __init__(self
, db
, fs
, msg
):
296 BaseTopic
.__init
__(self
, db
, fs
, msg
)
298 def delete(self
, session
, _id
, force
=False, dry_run
=False):
299 raise EngineException("Method delete called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
301 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
302 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
304 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False):
305 # Not used because vnfrs are created and deleted by NsrTopic class directly
306 raise EngineException("Method new called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
309 class NsLcmOpTopic(BaseTopic
):
312 operation_schema
= { # mapping between operation and jsonschema to validate
313 "instantiate": ns_instantiate
,
319 def __init__(self
, db
, fs
, msg
):
320 BaseTopic
.__init
__(self
, db
, fs
, msg
)
322 def _check_ns_operation(self
, session
, nsr
, operation
, indata
):
324 Check that user has enter right parameters for the operation
326 :param operation: it can be: instantiate, terminate, action, TODO: update, heal
327 :param indata: descriptor with the parameters of the operation
334 def check_valid_vnf_member_index(member_vnf_index
):
335 # TODO change to vnfR
336 for vnf
in nsd
["constituent-vnfd"]:
337 if member_vnf_index
== vnf
["member-vnf-index"]:
338 vnfd_id
= vnf
["vnfd-id-ref"]
339 if vnfd_id
not in vnfds
:
340 vnfds
[vnfd_id
] = self
.db
.get_one("vnfds", {"id": vnfd_id
})
341 return vnfds
[vnfd_id
]
343 raise EngineException("Invalid parameter member_vnf_index='{}' is not one of the "
344 "nsd:constituent-vnfd".format(member_vnf_index
))
346 def _check_vnf_instantiation_params(in_vnfd
, vnfd
):
348 for in_vdu
in get_iterable(in_vnfd
.get("vdu")):
349 for vdu
in get_iterable(vnfd
.get("vdu")):
350 if in_vdu
["id"] == vdu
["id"]:
351 for volume
in get_iterable(in_vdu
.get("volume")):
352 for volumed
in get_iterable(vdu
.get("volumes")):
353 if volumed
["name"] == volume
["name"]:
356 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
357 "volume:name='{}' is not present at vnfd:vdu:volumes list".
358 format(in_vnf
["member-vnf-index"], in_vdu
["id"],
360 for in_iface
in get_iterable(in_vdu
["interface"]):
361 for iface
in get_iterable(vdu
.get("interface")):
362 if in_iface
["name"] == iface
["name"]:
365 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
366 "interface[name='{}'] is not present at vnfd:vdu:interface"
367 .format(in_vnf
["member-vnf-index"], in_vdu
["id"],
371 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}'] is is not present "
372 "at vnfd:vdu".format(in_vnf
["member-vnf-index"], in_vdu
["id"]))
374 for in_ivld
in get_iterable(in_vnfd
.get("internal-vld")):
375 for ivld
in get_iterable(vnfd
.get("internal-vld")):
376 if in_ivld
["name"] == ivld
["name"] or in_ivld
["name"] == ivld
["id"]:
377 for in_icp
in get_iterable(in_ivld
["internal-connection-point"]):
378 for icp
in ivld
["internal-connection-point"]:
379 if in_icp
["id-ref"] == icp
["id-ref"]:
382 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld[name"
383 "='{}']:internal-connection-point[id-ref:'{}'] is not present at "
384 "vnfd:internal-vld:name/id:internal-connection-point"
385 .format(in_vnf
["member-vnf-index"], in_ivld
["name"],
386 in_icp
["id-ref"], vnfd
["id"]))
389 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld:name='{}'"
390 " is not present at vnfd '{}'".format(in_vnf
["member-vnf-index"],
391 in_ivld
["name"], vnfd
["id"]))
393 def check_valid_vim_account(vim_account
):
394 if vim_account
in vim_accounts
:
397 db_filter
= self
._get
_project
_filter
(session
, write
=False, show_all
=True)
398 db_filter
["_id"] = vim_account
399 self
.db
.get_one("vim_accounts", db_filter
)
401 raise EngineException("Invalid vimAccountId='{}' not present for the project".format(vim_account
))
402 vim_accounts
.append(vim_account
)
404 if operation
== "action":
405 # check vnf_member_index
406 if indata
.get("vnf_member_index"):
407 indata
["member_vnf_index"] = indata
.pop("vnf_member_index") # for backward compatibility
408 if not indata
.get("member_vnf_index"):
409 raise EngineException("Missing 'member_vnf_index' parameter")
410 vnfd
= check_valid_vnf_member_index(indata
["member_vnf_index"])
412 for config_primitive
in get_iterable(vnfd
.get("vnf-configuration", {}).get("config-primitive")):
413 if indata
["primitive"] == config_primitive
["name"]:
414 # check needed primitive_params are provided
415 if indata
.get("primitive_params"):
416 in_primitive_params_copy
= copy(indata
["primitive_params"])
418 in_primitive_params_copy
= {}
419 for paramd
in get_iterable(config_primitive
.get("parameter")):
420 if paramd
["name"] in in_primitive_params_copy
:
421 del in_primitive_params_copy
[paramd
["name"]]
422 elif not paramd
.get("default-value"):
423 raise EngineException("Needed parameter {} not provided for primitive '{}'".format(
424 paramd
["name"], indata
["primitive"]))
425 # check no extra primitive params are provided
426 if in_primitive_params_copy
:
427 raise EngineException("parameter/s '{}' not present at vnfd for primitive '{}'".format(
428 list(in_primitive_params_copy
.keys()), indata
["primitive"]))
431 raise EngineException("Invalid primitive '{}' is not present at vnfd".format(indata
["primitive"]))
432 if operation
== "scale":
433 vnfd
= check_valid_vnf_member_index(indata
["scaleVnfData"]["scaleByStepData"]["member-vnf-index"])
434 for scaling_group
in get_iterable(vnfd
.get("scaling-group-descriptor")):
435 if indata
["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"] == scaling_group
["name"]:
438 raise EngineException("Invalid scaleVnfData:scaleByStepData:scaling-group-descriptor '{}' is not "
439 "present at vnfd:scaling-group-descriptor".format(
440 indata
["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"]))
441 if operation
== "instantiate":
443 check_valid_vim_account(indata
["vimAccountId"])
444 for in_vnf
in get_iterable(indata
.get("vnf")):
445 vnfd
= check_valid_vnf_member_index(in_vnf
["member-vnf-index"])
446 _check_vnf_instantiation_params(in_vnf
, vnfd
)
447 if in_vnf
.get("vimAccountId"):
448 check_valid_vim_account(in_vnf
["vimAccountId"])
450 for in_vld
in get_iterable(indata
.get("vld")):
451 for vldd
in get_iterable(nsd
.get("vld")):
452 if in_vld
["name"] == vldd
["name"] or in_vld
["name"] == vldd
["id"]:
455 raise EngineException("Invalid parameter vld:name='{}' is not present at nsd:vld".format(
458 def _look_for_pdu(self
, session
, rollback
, vnfr
, vim_account
, vnfr_update
, vnfr_update_rollback
):
460 Look for a free PDU in the catalog matching vdur type and interfaces. Fills vnfr.vdur with the interface
461 (ip_address, ...) information.
462 Modifies PDU _admin.usageState to 'IN_USE'
464 :param session: client session information
465 :param rollback: list with the database modifications to rollback if needed
466 :param vnfr: vnfr to be updated. It is modified with pdu interface info if pdu is found
467 :param vim_account: vim_account where this vnfr should be deployed
468 :param vnfr_update: dictionary filled by this method with changes to be done at database vnfr
469 :param vnfr_update_rollback: dictionary filled by this method with original content of vnfr in case a rollback
470 of the changed vnfr is needed
472 :return: List of PDU interfaces that are connected to an existing VIM network. Each item contains:
473 "vim-network-name": used at VIM
474 "name": interface name
475 "vnf-vld-id": internal VNFD vld where this interface is connected, or
476 "ns-vld-id": NSD vld where this interface is connected.
477 NOTE: One, and only one between 'vnf-vld-id' and 'ns-vld-id' contains a value. The other will be None
480 ifaces_forcing_vim_network
= []
481 for vdur_index
, vdur
in enumerate(get_iterable(vnfr
.get("vdur"))):
482 if not vdur
.get("pdu-type"):
484 pdu_type
= vdur
.get("pdu-type")
485 pdu_filter
= self
._get
_project
_filter
(session
, write
=True, show_all
=True)
486 pdu_filter
["vim_accounts"] = vim_account
487 pdu_filter
["type"] = pdu_type
488 pdu_filter
["_admin.operationalState"] = "ENABLED"
489 pdu_filter
["_admin.usageState"] = "NOT_IN_USE"
490 # TODO feature 1417: "shared": True,
492 available_pdus
= self
.db
.get_list("pdus", pdu_filter
)
493 for pdu
in available_pdus
:
494 # step 1 check if this pdu contains needed interfaces:
495 match_interfaces
= True
496 for vdur_interface
in vdur
["interfaces"]:
497 for pdu_interface
in pdu
["interfaces"]:
498 if pdu_interface
["name"] == vdur_interface
["name"]:
499 # TODO feature 1417: match per mgmt type
501 else: # no interface found for name
502 match_interfaces
= False
507 raise EngineException(
508 "No PDU of type={} at vim_account={} found for member_vnf_index={}, vdu={} matching interface "
509 "names".format(pdu_type
, vim_account
, vnfr
["member-vnf-index-ref"], vdur
["vdu-id-ref"]))
513 "_admin.usageState": pdu
["_admin"]["usageState"],
514 "_admin.usage.vnfr_id": None,
515 "_admin.usage.nsr_id": None,
516 "_admin.usage.vdur": None,
518 self
.db
.set_one("pdus", {"_id": pdu
["_id"]},
519 {"_admin.usageState": "IN_USE",
520 "_admin.usage": {"vnfr_id": vnfr
["_id"],
521 "nsr_id": vnfr
["nsr-id-ref"],
522 "vdur": vdur
["vdu-id-ref"]}
524 rollback
.append({"topic": "pdus", "_id": pdu
["_id"], "operation": "set", "content": rollback_pdu
})
526 # step 3. Fill vnfr info by filling vdur
527 vdu_text
= "vdur.{}".format(vdur_index
)
528 vnfr_update_rollback
[vdu_text
+ ".pdu-id"] = None
529 vnfr_update
[vdu_text
+ ".pdu-id"] = pdu
["_id"]
530 for iface_index
, vdur_interface
in enumerate(vdur
["interfaces"]):
531 for pdu_interface
in pdu
["interfaces"]:
532 if pdu_interface
["name"] == vdur_interface
["name"]:
533 iface_text
= vdu_text
+ ".interfaces.{}".format(iface_index
)
534 for k
, v
in pdu_interface
.items():
535 if k
in ("ip-address", "mac-address"): # TODO: switch-xxxxx must be inserted
536 vnfr_update
[iface_text
+ ".{}".format(k
)] = v
537 vnfr_update_rollback
[iface_text
+ ".{}".format(k
)] = vdur_interface
.get(v
)
538 if pdu_interface
.get("ip-address"):
539 if vdur_interface
.get("mgmt-interface"):
540 vnfr_update_rollback
[vdu_text
+ ".ip-address"] = vdur
.get("ip-address")
541 vnfr_update
[vdu_text
+ ".ip-address"] = pdu_interface
["ip-address"]
542 if vdur_interface
.get("mgmt-vnf"):
543 vnfr_update_rollback
["ip-address"] = vnfr
.get("ip-address")
544 vnfr_update
["ip-address"] = pdu_interface
["ip-address"]
545 if pdu_interface
.get("vim-network-name") or pdu_interface
.get("vim-network-id"):
546 ifaces_forcing_vim_network
.append({
547 "name": vdur_interface
.get("vnf-vld-id") or vdur_interface
.get("ns-vld-id"),
548 "vnf-vld-id": vdur_interface
.get("vnf-vld-id"),
549 "ns-vld-id": vdur_interface
.get("ns-vld-id")})
550 if pdu_interface
.get("vim-network-id"):
551 ifaces_forcing_vim_network
.append({
552 "vim-network-id": pdu_interface
.get("vim-network-id")})
553 if pdu_interface
.get("vim-network-name"):
554 ifaces_forcing_vim_network
.append({
555 "vim-network-name": pdu_interface
.get("vim-network-name")})
558 return ifaces_forcing_vim_network
560 def _update_vnfrs(self
, session
, rollback
, nsr
, indata
):
564 vnfrs
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
568 vnfr_update_rollback
= {}
569 member_vnf_index
= vnfr
["member-vnf-index-ref"]
570 # update vim-account-id
572 vim_account
= indata
["vimAccountId"]
573 # check instantiate parameters
574 for vnf_inst_params
in get_iterable(indata
.get("vnf")):
575 if vnf_inst_params
["member-vnf-index"] != member_vnf_index
:
577 if vnf_inst_params
.get("vimAccountId"):
578 vim_account
= vnf_inst_params
.get("vimAccountId")
580 vnfr_update
["vim-account-id"] = vim_account
581 vnfr_update_rollback
["vim-account-id"] = vnfr
.get("vim-account-id")
584 ifaces_forcing_vim_network
= self
._look
_for
_pdu
(session
, rollback
, vnfr
, vim_account
, vnfr_update
,
585 vnfr_update_rollback
)
587 # updata database vnfr
588 self
.db
.set_one("vnfrs", {"_id": vnfr
["_id"]}, vnfr_update
)
589 rollback
.append({"topic": "vnfrs", "_id": vnfr
["_id"], "operation": "set", "content": vnfr_update_rollback
})
591 # Update indada in case pdu forces to use a concrete vim-network-name
592 # TODO check if user has already insert a vim-network-name and raises an error
593 if not ifaces_forcing_vim_network
:
595 for iface_info
in ifaces_forcing_vim_network
:
596 if iface_info
.get("ns-vld-id"):
597 if "vld" not in indata
:
599 indata
["vld"].append({key
: iface_info
[key
] for key
in
600 ("name", "vim-network-name", "vim-network-id") if iface_info
.get(key
)})
602 elif iface_info
.get("vnf-vld-id"):
603 if "vnf" not in indata
:
605 indata
["vnf"].append({
606 "member-vnf-index": member_vnf_index
,
607 "internal-vld": [{key
: iface_info
[key
] for key
in
608 ("name", "vim-network-name", "vim-network-id") if iface_info
.get(key
)}]
612 def _create_nslcmop(nsr_id
, operation
, params
):
614 Creates a ns-lcm-opp content to be stored at database.
615 :param nsr_id: internal id of the instance
616 :param operation: instantiate, terminate, scale, action, ...
617 :param params: user parameters for the operation
618 :return: dictionary following SOL005 format
625 "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
626 "statusEnteredTime": now
,
627 "nsInstanceId": nsr_id
,
628 "lcmOperationType": operation
,
630 "isAutomaticInvocation": False,
631 "operationParams": params
,
632 "isCancelPending": False,
634 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
635 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,
640 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False,
643 Performs a new operation over a ns
644 :param rollback: list to append created items at database in case a rollback must to be done
645 :param session: contains the used login username and working project
646 :param indata: descriptor with the parameters of the operation. It must contains among others
647 nsInstanceId: _id of the nsr to perform the operation
648 operation: it can be: instantiate, terminate, action, TODO: update, heal
649 :param kwargs: used to override the indata descriptor
650 :param headers: http request headers
651 :param force: If True avoid some dependence checks
652 :param make_public: Make the created item public to all projects
653 :return: id of the nslcmops
656 # Override descriptor with query string kwargs
657 self
._update
_input
_with
_kwargs
(indata
, kwargs
)
658 operation
= indata
["lcmOperationType"]
659 nsInstanceId
= indata
["nsInstanceId"]
661 validate_input(indata
, self
.operation_schema
[operation
])
663 _filter
= BaseTopic
._get
_project
_filter
(session
, write
=True, show_all
=False)
664 _filter
["_id"] = nsInstanceId
665 nsr
= self
.db
.get_one("nsrs", _filter
)
668 if not nsr
["_admin"].get("nsState") or nsr
["_admin"]["nsState"] == "NOT_INSTANTIATED":
669 if operation
== "terminate" and indata
.get("autoremove"):
670 # NSR must be deleted
671 return None # a none in this case is used to indicate not instantiated. It can be removed
672 if operation
!= "instantiate":
673 raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format(
674 nsInstanceId
, operation
), HTTPStatus
.CONFLICT
)
676 if operation
== "instantiate" and not indata
.get("force"):
677 raise EngineException("ns_instance '{}' cannot be '{}' because it is already instantiated".format(
678 nsInstanceId
, operation
), HTTPStatus
.CONFLICT
)
679 self
._check
_ns
_operation
(session
, nsr
, operation
, indata
)
681 if operation
== "instantiate":
682 self
._update
_vnfrs
(session
, rollback
, nsr
, indata
)
684 nslcmop_desc
= self
._create
_nslcmop
(nsInstanceId
, operation
, indata
)
685 self
.format_on_new(nslcmop_desc
, session
["project_id"], make_public
=make_public
)
686 _id
= self
.db
.create("nslcmops", nslcmop_desc
)
687 rollback
.append({"topic": "nslcmops", "_id": _id
})
689 self
.msg
.write("ns", operation
, nslcmop_desc
)
691 except ValidationError
as e
:
692 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
693 # except DbException as e:
694 # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND)
696 def delete(self
, session
, _id
, force
=False, dry_run
=False):
697 raise EngineException("Method delete called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
699 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
700 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
703 class NsiTopic(BaseTopic
):
707 def __init__(self
, db
, fs
, msg
):
708 BaseTopic
.__init
__(self
, db
, fs
, msg
)
710 def _check_descriptor_dependencies(self
, session
, descriptor
):
712 Check that the dependent descriptors exist on a new descriptor or edition
713 :param session: client session information
714 :param descriptor: descriptor to be inserted or edit
715 :return: None or raises exception
717 if not descriptor
.get("nst-ref"):
719 nstd_id
= descriptor
["nst-ref"]
720 if not self
.get_item_list(session
, "nsts", {"id": nstd_id
}):
721 raise EngineException("Descriptor error at nst-ref='{}' references a non exist nstd".format(nstd_id
),
722 http_code
=HTTPStatus
.CONFLICT
)
725 def format_on_new(content
, project_id
=None, make_public
=False):
726 BaseTopic
.format_on_new(content
, project_id
=project_id
, make_public
=make_public
)
728 def check_conflict_on_del(self
, session
, _id
, force
=False):
731 nsi
= self
.db
.get_one("nsis", {"_id": _id
})
732 if nsi
["_admin"].get("nsiState") == "INSTANTIATED":
733 raise EngineException("nsi '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
734 "Launch 'terminate' operation first; or force deletion".format(_id
),
735 http_code
=HTTPStatus
.CONFLICT
)
737 def delete(self
, session
, _id
, force
=False, dry_run
=False):
739 Delete item by its internal _id
740 :param session: contains the used login username, working project, and admin rights
741 :param _id: server internal id
742 :param force: indicates if deletion must be forced in case of conflict
743 :param dry_run: make checking but do not delete
744 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
746 # TODO add admin to filter, validate rights
747 BaseTopic
.delete(self
, session
, _id
, force
, dry_run
=True)
751 # deletes NetSlice instance object
752 v
= self
.db
.del_one("nsis", {"_id": _id
})
754 # makes a temporal list of nsilcmops objects related to the _id given and deletes them from db
755 _filter
= {"netsliceInstanceId": _id
}
756 self
.db
.del_list("nsilcmops", _filter
)
758 _filter
= {"operationParams.netsliceInstanceId": _id
}
759 nslcmops_list
= self
.db
.get_list("nslcmops", _filter
)
761 for id_item
in nslcmops_list
:
762 _filter
= {"_id": id_item
}
763 nslcmop
= self
.db
.get_one("nslcmops", _filter
)
764 nsr_id
= nslcmop
["operationParams"]["nsr_id"]
765 NsrTopic
.delete(self
, session
, nsr_id
, force
=False, dry_run
=False)
766 self
._send
_msg
("deleted", {"_id": _id
})
769 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False):
771 Creates a new netslice instance record into database. It also creates needed nsrs and vnfrs
772 :param rollback: list to append the created items at database in case a rollback must be done
773 :param session: contains the used login username and working project
774 :param indata: params to be used for the nsir
775 :param kwargs: used to override the indata descriptor
776 :param headers: http request headers
777 :param force: If True avoid some dependence checks
778 :param make_public: Make the created item public to all projects
779 :return: the _id of nsi descriptor created at database
783 slice_request
= self
._remove
_envelop
(indata
)
784 # Override descriptor with query string kwargs
785 self
._update
_input
_with
_kwargs
(slice_request
, kwargs
)
786 self
._validate
_input
_new
(slice_request
, force
)
790 step
= "getting nstd id='{}' from database".format(slice_request
.get("nstId"))
791 _filter
= {"_id": slice_request
["nstId"]}
792 _filter
.update(BaseTopic
._get
_project
_filter
(session
, write
=False, show_all
=True))
793 nstd
= self
.db
.get_one("nsts", _filter
)
794 nstd
.pop("_admin", None)
795 nstd
.pop("_id", None)
796 nsi_id
= str(uuid4())
797 step
= "filling nsi_descriptor with input data"
802 "name": slice_request
["nsiName"],
803 "description": slice_request
.get("nsiDescription", ""),
804 "datacenter": slice_request
["vimAccountId"],
805 "nst-ref": nstd
["id"],
806 "instantiation_parameters": slice_request
,
807 "network-slice-template": nstd
,
813 step
= "creating nsi at database"
814 self
.format_on_new(nsi_descriptor
, session
["project_id"], make_public
=make_public
)
815 nsi_descriptor
["_admin"]["nsiState"] = "NOT_INSTANTIATED"
816 nsi_descriptor
["_admin"]["netslice-subnet"] = None
818 # Creating netslice-vld for the RO.
819 step
= "creating netslice-vld at database"
820 instantiation_parameters
= slice_request
822 # Building the vlds list to be deployed
823 # From netslice descriptors, creating the initial list
825 if nstd
.get("netslice-vld"):
826 # Building VLDs from NST
827 for netslice_vlds
in nstd
["netslice-vld"]:
829 # Adding nst vld name and global vimAccountId for netslice vld creation
830 nsi_vld
["name"] = netslice_vlds
["name"]
831 nsi_vld
["vimAccountId"] = slice_request
["vimAccountId"]
832 # Getting template Instantiation parameters from NST
833 for netslice_vld
in netslice_vlds
["nss-connection-point-ref"]:
834 for netslice_subnet
in nstd
["netslice-subnet"]:
835 nsi_vld
["nsd-ref"] = netslice_subnet
["nsd-ref"]
836 nsi_vld
["nsd-connection-point-ref"] = netslice_vld
["nsd-connection-point-ref"]
837 # Obtaining the vimAccountId from template instantiation parameter
838 if netslice_subnet
.get("instantiation-parameters"):
839 # Taking the vimAccountId from NST netslice-subnet instantiation parameters
840 if netslice_subnet
["instantiation-parameters"].get("vimAccountId"):
841 netsn
= netslice_subnet
["instantiation-parameters"]["vimAccountId"]
842 nsi_vld
["vimAccountId"] = netsn
843 # Obtaining the vimAccountId from user instantiation parameter
844 if instantiation_parameters
.get("netslice-subnet"):
845 for ins_param
in instantiation_parameters
["netslice-subnet"]:
846 if ins_param
.get("id") == netslice_vld
["nss-ref"]:
847 if ins_param
.get("vimAccountId"):
848 nsi_vld
["vimAccountId"] = ins_param
["vimAccountId"]
849 # Adding vim-network-name defined by the user to vld
850 if instantiation_parameters
.get("netslice-vld"):
851 for ins_param
in instantiation_parameters
["netslice-vld"]:
852 if ins_param
["name"] == netslice_vlds
["name"]:
853 if ins_param
.get("vim-network-name"):
854 nsi_vld
["vim-network-name"] = ins_param
.get("vim-network-name")
855 if ins_param
.get("vim-network-id"):
856 nsi_vld
["vim-network-id"] = ins_param
.get("vim-network-id")
857 nsi_vlds
.append(nsi_vld
)
859 nsi_descriptor
["_admin"]["netslice-vld"] = nsi_vlds
860 # Creating netslice-subnet_record.
864 for member_ns
in nstd
["netslice-subnet"]:
865 nsd_id
= member_ns
["nsd-ref"]
866 step
= "getting nstd id='{}' constituent-nsd='{}' from database".format(
867 member_ns
["nsd-ref"], member_ns
["id"])
868 if nsd_id
not in needed_nsds
:
870 nsd
= DescriptorTopic
.get_one_by_id(self
.db
, session
, "nsds", nsd_id
)
872 needed_nsds
[nsd_id
] = nsd
873 member_ns
["_id"] = needed_nsds
[nsd_id
].get("_id")
874 services
.append(member_ns
)
876 nsd
= needed_nsds
[nsd_id
]
877 member_ns
["_id"] = needed_nsds
[nsd_id
].get("_id")
878 services
.append(member_ns
)
880 step
= "filling nsir nsd-id='{}' constituent-nsd='{}' from database".format(
881 member_ns
["nsd-ref"], member_ns
["id"])
883 # creates Network Services records (NSRs)
884 step
= "creating nsrs at database using NsrTopic.new()"
885 ns_params
= slice_request
.get("netslice-subnet")
888 nsi_netslice_subnet
= []
889 for service
in services
:
891 indata_ns
["nsdId"] = service
["_id"]
892 indata_ns
["nsName"] = slice_request
.get("nsiName") + "." + service
["id"]
893 indata_ns
["vimAccountId"] = slice_request
.get("vimAccountId")
894 indata_ns
["nsDescription"] = service
["description"]
895 indata_ns
["key-pair-ref"] = None
897 # NsrTopic(rollback, session, indata_ns, kwargs, headers, force)
898 # Overwriting ns_params filtering by nsName == netslice-subnet.id
901 for ns_param
in ns_params
:
902 if ns_param
.get("id") == service
["id"]:
903 copy_ns_param
= deepcopy(ns_param
)
904 del copy_ns_param
["id"]
905 indata_ns
.update(copy_ns_param
)
906 # TODO: Improve network selection via networkID
909 for nsi_vld
in nsi_vlds
:
910 nsd
= self
.db
.get_one("nsds", {"id": nsi_vld
["nsd-ref"]})
911 if nsd
["connection-point"]:
912 for vld_id_ref
in nsd
["connection-point"]:
913 name
= vld_id_ref
["vld-id-ref"]
914 vnm
= nsi_vld
.get("vim-network-name")
915 if type(vnm
) is not dict:
916 vnm
= {slice_request
.get("vimAccountId"): vnm
}
917 if vld_id_ref
["name"] == nsi_vld
["nsd-connection-point-ref"]:
918 if list(vnm
.values())[0] is None:
919 networkName
= "default"
921 networkName
= str(list(vnm
.values())[0])
922 # VIMNetworkName = NetSliceName-NetworkName
923 vimnetname
= str(slice_request
["nsiName"]) + "-" + networkName
924 indata_ns_list
.append({"name": name
, "vim-network-name": vimnetname
})
925 indata_ns
["vld"] = indata_ns_list
927 _id_nsr
= NsrTopic
.new(self
, rollback
, session
, indata_ns
, kwargs
, headers
, force
)
928 nsrs_item
= {"nsrId": _id_nsr
}
929 nsrs_list
.append(nsrs_item
)
930 nsi_netslice_subnet
.append(indata_ns
)
931 nsr_ref
= {"nsr-ref": _id_nsr
}
932 nsi_descriptor
["nsr-ref-list"].append(nsr_ref
)
934 # Adding the nsrs list to the nsi
935 nsi_descriptor
["_admin"]["nsrs-detailed-list"] = nsrs_list
936 nsi_descriptor
["_admin"]["netslice-subnet"] = nsi_netslice_subnet
937 # Creating the entry in the database
938 self
.db
.create("nsis", nsi_descriptor
)
939 rollback
.append({"topic": "nsis", "_id": nsi_id
})
941 except Exception as e
:
942 self
.logger
.exception("Exception {} at NsiTopic.new()".format(e
), exc_info
=True)
943 raise EngineException("Error {}: {}".format(step
, e
))
944 except ValidationError
as e
:
945 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
947 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
948 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
951 class NsiLcmOpTopic(BaseTopic
):
954 operation_schema
= { # mapping between operation and jsonschema to validate
955 "instantiate": nsi_instantiate
,
959 def __init__(self
, db
, fs
, msg
):
960 BaseTopic
.__init
__(self
, db
, fs
, msg
)
962 def _check_nsi_operation(self
, session
, nsir
, operation
, indata
):
964 Check that user has enter right parameters for the operation
966 :param operation: it can be: instantiate, terminate, action, TODO: update, heal
967 :param indata: descriptor with the parameters of the operation
971 nstd
= nsir
["network-slice-template"]
973 def check_valid_netslice_subnet_id(nstId
):
974 # TODO change to vnfR (??)
975 for netslice_subnet
in nstd
["netslice-subnet"]:
976 if nstId
== netslice_subnet
["id"]:
977 nsd_id
= netslice_subnet
["nsd-ref"]
978 if nsd_id
not in nsds
:
979 nsds
[nsd_id
] = self
.db
.get_one("nsds", {"id": nsd_id
})
982 raise EngineException("Invalid parameter nstId='{}' is not one of the "
983 "nst:netslice-subnet".format(nstId
))
984 if operation
== "instantiate":
985 # check the existance of netslice-subnet items
986 for in_nst
in get_iterable(indata
.get("netslice-subnet")):
987 check_valid_netslice_subnet_id(in_nst
["id"])
989 def _create_nsilcmop(self
, session
, netsliceInstanceId
, operation
, params
):
995 "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
996 "statusEnteredTime": now
,
997 "netsliceInstanceId": netsliceInstanceId
,
998 "lcmOperationType": operation
,
1000 "isAutomaticInvocation": False,
1001 "operationParams": params
,
1002 "isCancelPending": False,
1004 "self": "/osm/nsilcm/v1/nsi_lcm_op_occs/" + _id
,
1005 "nsInstance": "/osm/nsilcm/v1/netslice_instances/" + netsliceInstanceId
,
1010 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False):
1012 Performs a new operation over a ns
1013 :param rollback: list to append created items at database in case a rollback must to be done
1014 :param session: contains the used login username and working project
1015 :param indata: descriptor with the parameters of the operation. It must contains among others
1016 nsiInstanceId: _id of the nsir to perform the operation
1017 operation: it can be: instantiate, terminate, action, TODO: update, heal
1018 :param kwargs: used to override the indata descriptor
1019 :param headers: http request headers
1020 :param force: If True avoid some dependence checks
1021 :param make_public: Make the created item public to all projects
1022 :return: id of the nslcmops
1025 # Override descriptor with query string kwargs
1026 self
._update
_input
_with
_kwargs
(indata
, kwargs
)
1027 operation
= indata
["lcmOperationType"]
1028 nsiInstanceId
= indata
["nsiInstanceId"]
1029 validate_input(indata
, self
.operation_schema
[operation
])
1031 # get nsi from nsiInstanceId
1032 _filter
= BaseTopic
._get
_project
_filter
(session
, write
=True, show_all
=False)
1033 _filter
["_id"] = nsiInstanceId
1034 nsir
= self
.db
.get_one("nsis", _filter
)
1037 if not nsir
["_admin"].get("nsiState") or nsir
["_admin"]["nsiState"] == "NOT_INSTANTIATED":
1038 if operation
== "terminate" and indata
.get("autoremove"):
1039 # NSIR must be deleted
1040 return None # a none in this case is used to indicate not instantiated. It can be removed
1041 if operation
!= "instantiate":
1042 raise EngineException("netslice_instance '{}' cannot be '{}' because it is not instantiated".format(
1043 nsiInstanceId
, operation
), HTTPStatus
.CONFLICT
)
1045 if operation
== "instantiate" and not indata
.get("force"):
1046 raise EngineException("netslice_instance '{}' cannot be '{}' because it is already instantiated".
1047 format(nsiInstanceId
, operation
), HTTPStatus
.CONFLICT
)
1049 # Creating all the NS_operation (nslcmop)
1050 # Get service list from db
1051 nsrs_list
= nsir
["_admin"]["nsrs-detailed-list"]
1053 for nsr_item
in nsrs_list
:
1054 service
= self
.db
.get_one("nsrs", {"_id": nsr_item
["nsrId"]})
1056 indata_ns
= service
["instantiate_params"]
1057 indata_ns
["lcmOperationType"] = operation
1058 indata_ns
["nsInstanceId"] = service
["_id"]
1059 # Including netslice_id in the ns instantiate Operation
1060 indata_ns
["netsliceInstanceId"] = nsiInstanceId
1061 del indata_ns
["key-pair-ref"]
1062 nsi_NsLcmOpTopic
= NsLcmOpTopic(self
.db
, self
.fs
, self
.msg
)
1063 # Creating NS_LCM_OP with the flag slice_object=True to not trigger the service instantiation
1064 # message via kafka bus
1065 nslcmop
= nsi_NsLcmOpTopic
.new(rollback
, session
, indata_ns
, kwargs
, headers
, force
, slice_object
=True)
1066 nslcmops
.append(nslcmop
)
1069 indata
["nslcmops_ids"] = nslcmops
1070 self
._check
_nsi
_operation
(session
, nsir
, operation
, indata
)
1071 nsilcmop_desc
= self
._create
_nsilcmop
(session
, nsiInstanceId
, operation
, indata
)
1072 self
.format_on_new(nsilcmop_desc
, session
["project_id"], make_public
=make_public
)
1073 _id
= self
.db
.create("nsilcmops", nsilcmop_desc
)
1074 rollback
.append({"topic": "nsilcmops", "_id": _id
})
1075 self
.msg
.write("nsi", operation
, nsilcmop_desc
)
1077 except ValidationError
as e
:
1078 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
1079 # except DbException as e:
1080 # raise EngineException("Cannot get nsi_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND)
1082 def delete(self
, session
, _id
, force
=False, dry_run
=False):
1083 raise EngineException("Method delete called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
1085 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
1086 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)