1 # -*- coding: utf-8 -*-
5 from http
import HTTPStatus
8 from validation
import validate_input
, ValidationError
, ns_instantiate
, ns_action
, ns_scale
9 from base_topic
import BaseTopic
, EngineException
, get_iterable
10 from descriptor_topics
import DescriptorTopic
12 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
15 class NsrTopic(BaseTopic
):
19 def __init__(self
, db
, fs
, msg
):
20 BaseTopic
.__init
__(self
, db
, fs
, msg
)
22 def _check_descriptor_dependencies(self
, session
, descriptor
):
24 Check that the dependent descriptors exist on a new descriptor or edition
25 :param session: client session information
26 :param descriptor: descriptor to be inserted or edit
27 :return: None or raises exception
29 if not descriptor
.get("nsdId"):
31 nsd_id
= descriptor
["nsdId"]
32 if not self
.get_item_list(session
, "nsds", {"id": nsd_id
}):
33 raise EngineException("Descriptor error at nsdId='{}' references a non exist nsd".format(nsd_id
),
34 http_code
=HTTPStatus
.CONFLICT
)
37 def format_on_new(content
, project_id
=None, make_public
=False):
38 BaseTopic
.format_on_new(content
, project_id
=project_id
, make_public
=make_public
)
39 content
["_admin"]["nsState"] = "NOT_INSTANTIATED"
41 def check_conflict_on_del(self
, session
, _id
, force
=False):
44 nsr
= self
.db
.get_one("nsrs", {"_id": _id
})
45 if nsr
["_admin"].get("nsState") == "INSTANTIATED":
46 raise EngineException("nsr '{}' cannot be deleted because it is in 'INSTANTIATED' state. "
47 "Launch 'terminate' operation first; or force deletion".format(_id
),
48 http_code
=HTTPStatus
.CONFLICT
)
50 def delete(self
, session
, _id
, force
=False, dry_run
=False):
52 Delete item by its internal _id
53 :param session: contains the used login username, working project, and admin rights
54 :param _id: server internal id
55 :param force: indicates if deletion must be forced in case of conflict
56 :param dry_run: make checking but do not delete
57 :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ...
59 # TODO add admin to filter, validate rights
60 BaseTopic
.delete(self
, session
, _id
, force
, dry_run
=True)
64 v
= self
.db
.del_one("nsrs", {"_id": _id
})
65 self
.db
.del_list("nslcmops", {"nsInstanceId": _id
})
66 self
.db
.del_list("vnfrs", {"nsr-id-ref": _id
})
67 # set all used pdus as free
68 self
.db
.set_list("pdus", {"_admin.usage.nsr_id": _id
},
69 {"_admin.usageSate": "NOT_IN_USE", "_admin.usage": None})
70 self
._send
_msg
("deleted", {"_id": _id
})
73 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False):
75 Creates a new nsr into database. It also creates needed vnfrs
76 :param rollback: list to append the created items at database in case a rollback must be done
77 :param session: contains the used login username and working project
78 :param indata: params to be used for the nsr
79 :param kwargs: used to override the indata descriptor
80 :param headers: http request headers
81 :param force: If True avoid some dependence checks
82 :param make_public: Make the created item public to all projects
83 :return: the _id of nsr descriptor created at database
87 ns_request
= self
._remove
_envelop
(indata
)
88 # Override descriptor with query string kwargs
89 self
._update
_input
_with
_kwargs
(ns_request
, kwargs
)
90 self
._validate
_input
_new
(ns_request
, force
)
94 step
= "getting nsd id='{}' from database".format(ns_request
.get("nsdId"))
95 _filter
= {"_id": ns_request
["nsdId"]}
96 _filter
.update(BaseTopic
._get
_project
_filter
(session
, write
=False, show_all
=True))
97 nsd
= self
.db
.get_one("nsds", _filter
)
101 step
= "filling nsr from input data"
103 "name": ns_request
["nsName"],
104 "name-ref": ns_request
["nsName"],
105 "short-name": ns_request
["nsName"],
106 "admin-status": "ENABLED",
108 "datacenter": ns_request
["vimAccountId"],
109 "resource-orchestrator": "osmopenmano",
110 "description": ns_request
.get("nsDescription", ""),
111 "constituent-vnfr-ref": [],
113 "operational-status": "init", # typedef ns-operational-
114 "config-status": "init", # typedef config-states
115 "detailed-status": "scheduled",
117 "orchestration-progress": {},
118 # {"networks": {"active": 0, "total": 0}, "vms": {"active": 0, "total": 0}},
121 "nsd-name-ref": nsd
["name"],
122 "operational-events": [], # "id", "timestamp", "description", "event",
123 "nsd-ref": nsd
["id"],
124 "instantiate_params": ns_request
,
125 "ns-instance-config-ref": nsr_id
,
128 # "input-parameter": xpath, value,
129 "ssh-authorized-key": ns_request
.get("key-pair-ref"),
131 ns_request
["nsr_id"] = nsr_id
135 for member_vnf
in nsd
["constituent-vnfd"]:
136 vnfd_id
= member_vnf
["vnfd-id-ref"]
137 step
= "getting vnfd id='{}' constituent-vnfd='{}' from database".format(
138 member_vnf
["vnfd-id-ref"], member_vnf
["member-vnf-index"])
139 if vnfd_id
not in needed_vnfds
:
141 vnfd
= DescriptorTopic
.get_one_by_id(self
.db
, session
, "vnfds", vnfd_id
)
143 needed_vnfds
[vnfd_id
] = vnfd
145 vnfd
= needed_vnfds
[vnfd_id
]
146 step
= "filling vnfr vnfd-id='{}' constituent-vnfd='{}'".format(
147 member_vnf
["vnfd-id-ref"], member_vnf
["member-vnf-index"])
148 vnfr_id
= str(uuid4())
152 "nsr-id-ref": nsr_id
,
153 "member-vnf-index-ref": member_vnf
["member-vnf-index"],
155 # "vnfd": vnfd, # at OSM model.but removed to avoid data duplication TODO: revise
157 "vnfd-id": vnfd
["_id"], # not at OSM model, but useful
158 "vim-account-id": None,
160 "connection-point": [],
161 "ip-address": None, # mgmt-interface filled by LCM
163 for cp
in vnfd
.get("connection-point", ()):
166 "connection-point-id": cp
.get("id"),
168 # "ip-address", "mac-address" # filled by LCM
169 # vim-id # TODO it would be nice having a vim port id
171 vnfr_descriptor
["connection-point"].append(vnf_cp
)
172 for vdu
in vnfd
["vdu"]:
173 vdur_id
= str(uuid4())
176 "vdu-id-ref": vdu
["id"],
177 # TODO "name": "" Name of the VDU in the VIM
178 "ip-address": None, # mgmt-interface filled by LCM
179 # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM
180 "internal-connection-point": [],
183 # TODO volumes: name, volume-id
184 for icp
in vdu
.get("internal-connection-point", ()):
187 "connection-point-id": icp
["id"],
188 "name": icp
.get("name"),
189 # "ip-address", "mac-address" # filled by LCM
190 # vim-id # TODO it would be nice having a vim port id
192 vdur
["internal-connection-point"].append(vdu_icp
)
193 for iface
in vdu
.get("interface", ()):
195 "name": iface
.get("name"),
196 # "ip-address", "mac-address" # filled by LCM
197 # vim-id # TODO it would be nice having a vim port id
199 vdur
["interfaces"].append(vdu_iface
)
200 vnfr_descriptor
["vdur"].append(vdur
)
202 step
= "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format(
203 member_vnf
["vnfd-id-ref"], member_vnf
["member-vnf-index"])
206 BaseTopic
.format_on_new(vnfr_descriptor
, session
["project_id"], make_public
=make_public
)
207 self
.db
.create("vnfrs", vnfr_descriptor
)
208 rollback
.append({"topic": "vnfrs", "_id": vnfr_id
})
209 nsr_descriptor
["constituent-vnfr-ref"].append(vnfr_id
)
211 step
= "creating nsr at database"
212 self
.format_on_new(nsr_descriptor
, session
["project_id"], make_public
=make_public
)
213 self
.db
.create("nsrs", nsr_descriptor
)
214 rollback
.append({"topic": "nsrs", "_id": nsr_id
})
216 except Exception as e
:
217 self
.logger
.exception("Exception {} at NsrTopic.new()".format(e
), exc_info
=True)
218 raise EngineException("Error {}: {}".format(step
, e
))
219 except ValidationError
as e
:
220 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
222 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
223 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
226 class VnfrTopic(BaseTopic
):
230 def __init__(self
, db
, fs
, msg
):
231 BaseTopic
.__init
__(self
, db
, fs
, msg
)
233 def delete(self
, session
, _id
, force
=False, dry_run
=False):
234 raise EngineException("Method delete called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
236 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
237 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
239 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False):
240 # Not used because vnfrs are created and deleted by NsrTopic class directly
241 raise EngineException("Method new called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
244 class NsLcmOpTopic(BaseTopic
):
247 operation_schema
= { # mapping between operation and jsonschema to validate
248 "instantiate": ns_instantiate
,
254 def __init__(self
, db
, fs
, msg
):
255 BaseTopic
.__init
__(self
, db
, fs
, msg
)
257 def _validate_input_new(self
, input, force
=False):
259 Validates input user content for a new entry. It uses jsonschema for each type or operation.
260 :param input: user input content for the new topic
261 :param force: may be used for being more tolerant
262 :return: The same input content, or a changed version of it.
265 validate_input(input, self
.schema_new
)
268 def _check_ns_operation(self
, session
, nsr
, operation
, indata
):
270 Check that user has enter right parameters for the operation
272 :param operation: it can be: instantiate, terminate, action, TODO: update, heal
273 :param indata: descriptor with the parameters of the operation
280 def check_valid_vnf_member_index(member_vnf_index
):
281 # TODO change to vnfR
282 for vnf
in nsd
["constituent-vnfd"]:
283 if member_vnf_index
== vnf
["member-vnf-index"]:
284 vnfd_id
= vnf
["vnfd-id-ref"]
285 if vnfd_id
not in vnfds
:
286 vnfds
[vnfd_id
] = self
.db
.get_one("vnfds", {"id": vnfd_id
})
287 return vnfds
[vnfd_id
]
289 raise EngineException("Invalid parameter member_vnf_index='{}' is not one of the "
290 "nsd:constituent-vnfd".format(member_vnf_index
))
292 def _check_vnf_instantiation_params(in_vnfd
, vnfd
):
294 for in_vdu
in get_iterable(in_vnfd
.get("vdu")):
295 for vdu
in get_iterable(vnfd
.get("vdu")):
296 if in_vdu
["id"] == vdu
["id"]:
297 for volume
in get_iterable(in_vdu
.get("volume")):
298 for volumed
in get_iterable(vdu
.get("volumes")):
299 if volumed
["name"] == volume
["name"]:
302 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
303 "volume:name='{}' is not present at vnfd:vdu:volumes list".
304 format(in_vnf
["member-vnf-index"], in_vdu
["id"],
306 for in_iface
in get_iterable(in_vdu
["interface"]):
307 for iface
in get_iterable(vdu
.get("interface")):
308 if in_iface
["name"] == iface
["name"]:
311 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:"
312 "interface[name='{}'] is not present at vnfd:vdu:interface"
313 .format(in_vnf
["member-vnf-index"], in_vdu
["id"],
317 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}'] is is not present "
318 "at vnfd:vdu".format(in_vnf
["member-vnf-index"], in_vdu
["id"]))
320 for in_ivld
in get_iterable(in_vnfd
.get("internal-vld")):
321 for ivld
in get_iterable(vnfd
.get("internal-vld")):
322 if in_ivld
["name"] == ivld
["name"] or in_ivld
["name"] == ivld
["id"]:
323 for in_icp
in get_iterable(in_ivld
["internal-connection-point"]):
324 for icp
in ivld
["internal-connection-point"]:
325 if in_icp
["id-ref"] == icp
["id-ref"]:
328 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld[name"
329 "='{}']:internal-connection-point[id-ref:'{}'] is not present at "
330 "vnfd:internal-vld:name/id:internal-connection-point"
331 .format(in_vnf
["member-vnf-index"], in_ivld
["name"],
332 in_icp
["id-ref"], vnfd
["id"]))
335 raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld:name='{}'"
336 " is not present at vnfd '{}'".format(in_vnf
["member-vnf-index"],
337 in_ivld
["name"], vnfd
["id"]))
339 def check_valid_vim_account(vim_account
):
340 if vim_account
in vim_accounts
:
343 # TODO add _get_project_filter
344 self
.db
.get_one("vim_accounts", {"_id": vim_account
})
346 raise EngineException("Invalid vimAccountId='{}' not present".format(vim_account
))
347 vim_accounts
.append(vim_account
)
349 if operation
== "action":
350 # check vnf_member_index
351 if indata
.get("vnf_member_index"):
352 indata
["member_vnf_index"] = indata
.pop("vnf_member_index") # for backward compatibility
353 if not indata
.get("member_vnf_index"):
354 raise EngineException("Missing 'member_vnf_index' parameter")
355 vnfd
= check_valid_vnf_member_index(indata
["member_vnf_index"])
357 for config_primitive
in get_iterable(vnfd
.get("vnf-configuration", {}).get("config-primitive")):
358 if indata
["primitive"] == config_primitive
["name"]:
359 # check needed primitive_params are provided
360 if indata
.get("primitive_params"):
361 in_primitive_params_copy
= copy(indata
["primitive_params"])
363 in_primitive_params_copy
= {}
364 for paramd
in get_iterable(config_primitive
.get("parameter")):
365 if paramd
["name"] in in_primitive_params_copy
:
366 del in_primitive_params_copy
[paramd
["name"]]
367 elif not paramd
.get("default-value"):
368 raise EngineException("Needed parameter {} not provided for primitive '{}'".format(
369 paramd
["name"], indata
["primitive"]))
370 # check no extra primitive params are provided
371 if in_primitive_params_copy
:
372 raise EngineException("parameter/s '{}' not present at vnfd for primitive '{}'".format(
373 list(in_primitive_params_copy
.keys()), indata
["primitive"]))
376 raise EngineException("Invalid primitive '{}' is not present at vnfd".format(indata
["primitive"]))
377 if operation
== "scale":
378 vnfd
= check_valid_vnf_member_index(indata
["scaleVnfData"]["scaleByStepData"]["member-vnf-index"])
379 for scaling_group
in get_iterable(vnfd
.get("scaling-group-descriptor")):
380 if indata
["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"] == scaling_group
["name"]:
383 raise EngineException("Invalid scaleVnfData:scaleByStepData:scaling-group-descriptor '{}' is not "
384 "present at vnfd:scaling-group-descriptor".format(
385 indata
["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"]))
386 if operation
== "instantiate":
388 check_valid_vim_account(indata
["vimAccountId"])
389 for in_vnf
in get_iterable(indata
.get("vnf")):
390 vnfd
= check_valid_vnf_member_index(in_vnf
["member-vnf-index"])
391 _check_vnf_instantiation_params(in_vnf
, vnfd
)
392 if in_vnf
.get("vimAccountId"):
393 check_valid_vim_account(in_vnf
["vimAccountId"])
395 for in_vld
in get_iterable(indata
.get("vld")):
396 for vldd
in get_iterable(nsd
.get("vld")):
397 if in_vld
["name"] == vldd
["name"] or in_vld
["name"] == vldd
["id"]:
400 raise EngineException("Invalid parameter vld:name='{}' is not present at nsd:vld".format(
403 def _create_nslcmop(self
, session
, nsInstanceId
, operation
, params
):
409 "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
410 "statusEnteredTime": now
,
411 "nsInstanceId": nsInstanceId
,
412 "lcmOperationType": operation
,
414 "isAutomaticInvocation": False,
415 "operationParams": params
,
416 "isCancelPending": False,
418 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
419 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsInstanceId
,
424 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None, force
=False, make_public
=False):
426 Performs a new operation over a ns
427 :param rollback: list to append created items at database in case a rollback must to be done
428 :param session: contains the used login username and working project
429 :param indata: descriptor with the parameters of the operation. It must contains among others
430 nsInstanceId: _id of the nsr to perform the operation
431 operation: it can be: instantiate, terminate, action, TODO: update, heal
432 :param kwargs: used to override the indata descriptor
433 :param headers: http request headers
434 :param force: If True avoid some dependence checks
435 :param make_public: Make the created item public to all projects
436 :return: id of the nslcmops
439 # Override descriptor with query string kwargs
440 self
._update
_input
_with
_kwargs
(indata
, kwargs
)
441 operation
= indata
["lcmOperationType"]
442 nsInstanceId
= indata
["nsInstanceId"]
444 validate_input(indata
, self
.operation_schema
[operation
])
446 _filter
= BaseTopic
._get
_project
_filter
(session
, write
=True, show_all
=False)
447 _filter
["_id"] = nsInstanceId
448 nsr
= self
.db
.get_one("nsrs", _filter
)
451 if not nsr
["_admin"].get("nsState") or nsr
["_admin"]["nsState"] == "NOT_INSTANTIATED":
452 if operation
== "terminate" and indata
.get("autoremove"):
453 # NSR must be deleted
454 return self
.delete(session
, nsInstanceId
)
455 if operation
!= "instantiate":
456 raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format(
457 nsInstanceId
, operation
), HTTPStatus
.CONFLICT
)
459 if operation
== "instantiate" and not indata
.get("force"):
460 raise EngineException("ns_instance '{}' cannot be '{}' because it is already instantiated".format(
461 nsInstanceId
, operation
), HTTPStatus
.CONFLICT
)
462 self
._check
_ns
_operation
(session
, nsr
, operation
, indata
)
463 nslcmop_desc
= self
._create
_nslcmop
(session
, nsInstanceId
, operation
, indata
)
464 self
.format_on_new(nslcmop_desc
, session
["project_id"], make_public
=make_public
)
465 _id
= self
.db
.create("nslcmops", nslcmop_desc
)
466 rollback
.append({"topic": "nslcmops", "_id": _id
})
467 self
.msg
.write("ns", operation
, nslcmop_desc
)
469 except ValidationError
as e
:
470 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
471 # except DbException as e:
472 # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND)
474 def delete(self
, session
, _id
, force
=False, dry_run
=False):
475 raise EngineException("Method delete called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)
477 def edit(self
, session
, _id
, indata
=None, kwargs
=None, force
=False, content
=None):
478 raise EngineException("Method edit called directly", HTTPStatus
.INTERNAL_SERVER_ERROR
)