| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # import logging |
| 4 | from uuid import uuid4 |
| 5 | from http import HTTPStatus |
| 6 | from time import time |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 7 | from copy import copy, deepcopy |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 8 | from validation import validate_input, ValidationError, ns_instantiate, ns_action, ns_scale, nsi_instantiate |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 9 | from base_topic import BaseTopic, EngineException, get_iterable |
| 10 | from descriptor_topics import DescriptorTopic |
| 11 | |
| 12 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 13 | |
| 14 | |
| 15 | class NsrTopic(BaseTopic): |
| 16 | topic = "nsrs" |
| 17 | topic_msg = "ns" |
| 18 | |
| 19 | def __init__(self, db, fs, msg): |
| 20 | BaseTopic.__init__(self, db, fs, msg) |
| 21 | |
| 22 | def _check_descriptor_dependencies(self, session, descriptor): |
| 23 | """ |
| 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 |
| 28 | """ |
| 29 | if not descriptor.get("nsdId"): |
| 30 | return |
| 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) |
| 35 | |
| 36 | @staticmethod |
| 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" |
| 40 | |
| 41 | def check_conflict_on_del(self, session, _id, force=False): |
| 42 | if force: |
| 43 | return |
| 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) |
| 49 | |
| 50 | def delete(self, session, _id, force=False, dry_run=False): |
| 51 | """ |
| 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, ... |
| 58 | """ |
| 59 | # TODO add admin to filter, validate rights |
| 60 | BaseTopic.delete(self, session, _id, force, dry_run=True) |
| 61 | if dry_run: |
| 62 | return |
| 63 | |
| 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}, |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 69 | {"_admin.usageState": "NOT_IN_USE", "_admin.usage": None}) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 70 | self._send_msg("deleted", {"_id": _id}) |
| 71 | return v |
| 72 | |
| 73 | def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False): |
| 74 | """ |
| 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 |
| 84 | """ |
| 85 | |
| 86 | try: |
| 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) |
| 91 | |
| 92 | step = "" |
| 93 | # look for nsr |
| 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) |
| 98 | |
| 99 | nsr_id = str(uuid4()) |
| 100 | now = time() |
| 101 | step = "filling nsr from input data" |
| 102 | nsr_descriptor = { |
| 103 | "name": ns_request["nsName"], |
| 104 | "name-ref": ns_request["nsName"], |
| 105 | "short-name": ns_request["nsName"], |
| 106 | "admin-status": "ENABLED", |
| 107 | "nsd": nsd, |
| 108 | "datacenter": ns_request["vimAccountId"], |
| 109 | "resource-orchestrator": "osmopenmano", |
| 110 | "description": ns_request.get("nsDescription", ""), |
| 111 | "constituent-vnfr-ref": [], |
| 112 | |
| 113 | "operational-status": "init", # typedef ns-operational- |
| 114 | "config-status": "init", # typedef config-states |
| 115 | "detailed-status": "scheduled", |
| 116 | |
| 117 | "orchestration-progress": {}, |
| 118 | # {"networks": {"active": 0, "total": 0}, "vms": {"active": 0, "total": 0}}, |
| 119 | |
| 120 | "crete-time": now, |
| 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, |
| 126 | "id": nsr_id, |
| 127 | "_id": nsr_id, |
| 128 | # "input-parameter": xpath, value, |
| 129 | "ssh-authorized-key": ns_request.get("key-pair-ref"), |
| 130 | } |
| 131 | ns_request["nsr_id"] = nsr_id |
| 132 | |
| 133 | # Create VNFR |
| 134 | needed_vnfds = {} |
| 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: |
| 140 | # Obtain vnfd |
| 141 | vnfd = DescriptorTopic.get_one_by_id(self.db, session, "vnfds", vnfd_id) |
| 142 | vnfd.pop("_admin") |
| 143 | needed_vnfds[vnfd_id] = vnfd |
| 144 | else: |
| 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()) |
| 149 | vnfr_descriptor = { |
| 150 | "id": vnfr_id, |
| 151 | "_id": vnfr_id, |
| 152 | "nsr-id-ref": nsr_id, |
| 153 | "member-vnf-index-ref": member_vnf["member-vnf-index"], |
| 154 | "created-time": now, |
| 155 | # "vnfd": vnfd, # at OSM model.but removed to avoid data duplication TODO: revise |
| 156 | "vnfd-ref": vnfd_id, |
| 157 | "vnfd-id": vnfd["_id"], # not at OSM model, but useful |
| 158 | "vim-account-id": None, |
| 159 | "vdur": [], |
| 160 | "connection-point": [], |
| 161 | "ip-address": None, # mgmt-interface filled by LCM |
| 162 | } |
| 163 | for cp in vnfd.get("connection-point", ()): |
| 164 | vnf_cp = { |
| 165 | "name": cp["name"], |
| 166 | "connection-point-id": cp.get("id"), |
| 167 | "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 |
| 170 | } |
| 171 | vnfr_descriptor["connection-point"].append(vnf_cp) |
| 172 | for vdu in vnfd["vdu"]: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 173 | vdur = { |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 174 | "vdu-id-ref": vdu["id"], |
| 175 | # TODO "name": "" Name of the VDU in the VIM |
| 176 | "ip-address": None, # mgmt-interface filled by LCM |
| 177 | # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM |
| 178 | "internal-connection-point": [], |
| 179 | "interfaces": [], |
| 180 | } |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 181 | if vdu.get("pdu-type"): |
| 182 | vdur["pdu-type"] = vdu["pdu-type"] |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 183 | # TODO volumes: name, volume-id |
| 184 | for icp in vdu.get("internal-connection-point", ()): |
| 185 | vdu_icp = { |
| 186 | "id": icp["id"], |
| 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 |
| 191 | } |
| 192 | vdur["internal-connection-point"].append(vdu_icp) |
| 193 | for iface in vdu.get("interface", ()): |
| 194 | vdu_iface = { |
| 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 |
| 198 | } |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 199 | if iface.get("mgmt-interface"): |
| 200 | vdu_iface["mgmt-interface"] = True |
| 201 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 202 | vdur["interfaces"].append(vdu_iface) |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 203 | count = vdu.get("count", 1) |
| 204 | if count is None: |
| 205 | count = 1 |
| 206 | count = int(count) # TODO remove when descriptor serialized with payngbind |
| 207 | for index in range(0, count): |
| 208 | if index: |
| 209 | vdur = deepcopy(vdur) |
| 210 | vdur["_id"] = str(uuid4()) |
| 211 | vdur["count-index"] = index |
| 212 | vnfr_descriptor["vdur"].append(vdur) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 213 | |
| 214 | step = "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format( |
| 215 | member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"]) |
| 216 | |
| 217 | # add at database |
| 218 | BaseTopic.format_on_new(vnfr_descriptor, session["project_id"], make_public=make_public) |
| 219 | self.db.create("vnfrs", vnfr_descriptor) |
| 220 | rollback.append({"topic": "vnfrs", "_id": vnfr_id}) |
| 221 | nsr_descriptor["constituent-vnfr-ref"].append(vnfr_id) |
| 222 | |
| 223 | step = "creating nsr at database" |
| 224 | self.format_on_new(nsr_descriptor, session["project_id"], make_public=make_public) |
| 225 | self.db.create("nsrs", nsr_descriptor) |
| 226 | rollback.append({"topic": "nsrs", "_id": nsr_id}) |
| 227 | return nsr_id |
| 228 | except Exception as e: |
| 229 | self.logger.exception("Exception {} at NsrTopic.new()".format(e), exc_info=True) |
| 230 | raise EngineException("Error {}: {}".format(step, e)) |
| 231 | except ValidationError as e: |
| 232 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 233 | |
| 234 | def edit(self, session, _id, indata=None, kwargs=None, force=False, content=None): |
| 235 | raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 236 | |
| 237 | |
| 238 | class VnfrTopic(BaseTopic): |
| 239 | topic = "vnfrs" |
| 240 | topic_msg = None |
| 241 | |
| 242 | def __init__(self, db, fs, msg): |
| 243 | BaseTopic.__init__(self, db, fs, msg) |
| 244 | |
| 245 | def delete(self, session, _id, force=False, dry_run=False): |
| 246 | raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 247 | |
| 248 | def edit(self, session, _id, indata=None, kwargs=None, force=False, content=None): |
| 249 | raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 250 | |
| 251 | def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False): |
| 252 | # Not used because vnfrs are created and deleted by NsrTopic class directly |
| 253 | raise EngineException("Method new called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 254 | |
| 255 | |
| 256 | class NsLcmOpTopic(BaseTopic): |
| 257 | topic = "nslcmops" |
| 258 | topic_msg = "ns" |
| 259 | operation_schema = { # mapping between operation and jsonschema to validate |
| 260 | "instantiate": ns_instantiate, |
| 261 | "action": ns_action, |
| 262 | "scale": ns_scale, |
| 263 | "terminate": None, |
| 264 | } |
| 265 | |
| 266 | def __init__(self, db, fs, msg): |
| 267 | BaseTopic.__init__(self, db, fs, msg) |
| 268 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 269 | def _check_ns_operation(self, session, nsr, operation, indata): |
| 270 | """ |
| 271 | Check that user has enter right parameters for the operation |
| 272 | :param session: |
| 273 | :param operation: it can be: instantiate, terminate, action, TODO: update, heal |
| 274 | :param indata: descriptor with the parameters of the operation |
| 275 | :return: None |
| 276 | """ |
| 277 | vnfds = {} |
| 278 | vim_accounts = [] |
| 279 | nsd = nsr["nsd"] |
| 280 | |
| 281 | def check_valid_vnf_member_index(member_vnf_index): |
| 282 | # TODO change to vnfR |
| 283 | for vnf in nsd["constituent-vnfd"]: |
| 284 | if member_vnf_index == vnf["member-vnf-index"]: |
| 285 | vnfd_id = vnf["vnfd-id-ref"] |
| 286 | if vnfd_id not in vnfds: |
| 287 | vnfds[vnfd_id] = self.db.get_one("vnfds", {"id": vnfd_id}) |
| 288 | return vnfds[vnfd_id] |
| 289 | else: |
| 290 | raise EngineException("Invalid parameter member_vnf_index='{}' is not one of the " |
| 291 | "nsd:constituent-vnfd".format(member_vnf_index)) |
| 292 | |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 293 | def _check_vnf_instantiation_params(in_vnfd, vnfd): |
| 294 | |
| tierno | 40fbcad | 2018-10-26 10:58:15 +0200 | [diff] [blame] | 295 | for in_vdu in get_iterable(in_vnfd.get("vdu")): |
| 296 | for vdu in get_iterable(vnfd.get("vdu")): |
| 297 | if in_vdu["id"] == vdu["id"]: |
| 298 | for volume in get_iterable(in_vdu.get("volume")): |
| 299 | for volumed in get_iterable(vdu.get("volumes")): |
| 300 | if volumed["name"] == volume["name"]: |
| 301 | break |
| 302 | else: |
| 303 | raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:" |
| 304 | "volume:name='{}' is not present at vnfd:vdu:volumes list". |
| 305 | format(in_vnf["member-vnf-index"], in_vdu["id"], |
| 306 | volume["name"])) |
| 307 | for in_iface in get_iterable(in_vdu["interface"]): |
| 308 | for iface in get_iterable(vdu.get("interface")): |
| 309 | if in_iface["name"] == iface["name"]: |
| 310 | break |
| 311 | else: |
| 312 | raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}']:" |
| 313 | "interface[name='{}'] is not present at vnfd:vdu:interface" |
| 314 | .format(in_vnf["member-vnf-index"], in_vdu["id"], |
| 315 | in_iface["name"])) |
| 316 | break |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 317 | else: |
| tierno | 40fbcad | 2018-10-26 10:58:15 +0200 | [diff] [blame] | 318 | raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:vdu[id='{}'] is is not present " |
| 319 | "at vnfd:vdu".format(in_vnf["member-vnf-index"], in_vdu["id"])) |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 320 | |
| 321 | for in_ivld in get_iterable(in_vnfd.get("internal-vld")): |
| 322 | for ivld in get_iterable(vnfd.get("internal-vld")): |
| 323 | if in_ivld["name"] == ivld["name"] or in_ivld["name"] == ivld["id"]: |
| 324 | for in_icp in get_iterable(in_ivld["internal-connection-point"]): |
| 325 | for icp in ivld["internal-connection-point"]: |
| 326 | if in_icp["id-ref"] == icp["id-ref"]: |
| 327 | break |
| 328 | else: |
| tierno | 40fbcad | 2018-10-26 10:58:15 +0200 | [diff] [blame] | 329 | raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld[name" |
| 330 | "='{}']:internal-connection-point[id-ref:'{}'] is not present at " |
| 331 | "vnfd:internal-vld:name/id:internal-connection-point" |
| 332 | .format(in_vnf["member-vnf-index"], in_ivld["name"], |
| 333 | in_icp["id-ref"], vnfd["id"])) |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 334 | break |
| 335 | else: |
| 336 | raise EngineException("Invalid parameter vnf[member-vnf-index='{}']:internal-vld:name='{}'" |
| 337 | " is not present at vnfd '{}'".format(in_vnf["member-vnf-index"], |
| 338 | in_ivld["name"], vnfd["id"])) |
| 339 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 340 | def check_valid_vim_account(vim_account): |
| 341 | if vim_account in vim_accounts: |
| 342 | return |
| 343 | try: |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 344 | db_filter = self._get_project_filter(session, write=False, show_all=True) |
| 345 | db_filter["_id"] = vim_account |
| 346 | self.db.get_one("vim_accounts", db_filter) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 347 | except Exception: |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 348 | raise EngineException("Invalid vimAccountId='{}' not present for the project".format(vim_account)) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 349 | vim_accounts.append(vim_account) |
| 350 | |
| 351 | if operation == "action": |
| 352 | # check vnf_member_index |
| 353 | if indata.get("vnf_member_index"): |
| 354 | indata["member_vnf_index"] = indata.pop("vnf_member_index") # for backward compatibility |
| 355 | if not indata.get("member_vnf_index"): |
| 356 | raise EngineException("Missing 'member_vnf_index' parameter") |
| 357 | vnfd = check_valid_vnf_member_index(indata["member_vnf_index"]) |
| 358 | # check primitive |
| 359 | for config_primitive in get_iterable(vnfd.get("vnf-configuration", {}).get("config-primitive")): |
| 360 | if indata["primitive"] == config_primitive["name"]: |
| 361 | # check needed primitive_params are provided |
| 362 | if indata.get("primitive_params"): |
| 363 | in_primitive_params_copy = copy(indata["primitive_params"]) |
| 364 | else: |
| 365 | in_primitive_params_copy = {} |
| 366 | for paramd in get_iterable(config_primitive.get("parameter")): |
| 367 | if paramd["name"] in in_primitive_params_copy: |
| 368 | del in_primitive_params_copy[paramd["name"]] |
| 369 | elif not paramd.get("default-value"): |
| 370 | raise EngineException("Needed parameter {} not provided for primitive '{}'".format( |
| 371 | paramd["name"], indata["primitive"])) |
| 372 | # check no extra primitive params are provided |
| 373 | if in_primitive_params_copy: |
| 374 | raise EngineException("parameter/s '{}' not present at vnfd for primitive '{}'".format( |
| 375 | list(in_primitive_params_copy.keys()), indata["primitive"])) |
| 376 | break |
| 377 | else: |
| 378 | raise EngineException("Invalid primitive '{}' is not present at vnfd".format(indata["primitive"])) |
| 379 | if operation == "scale": |
| 380 | vnfd = check_valid_vnf_member_index(indata["scaleVnfData"]["scaleByStepData"]["member-vnf-index"]) |
| 381 | for scaling_group in get_iterable(vnfd.get("scaling-group-descriptor")): |
| 382 | if indata["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"] == scaling_group["name"]: |
| 383 | break |
| 384 | else: |
| 385 | raise EngineException("Invalid scaleVnfData:scaleByStepData:scaling-group-descriptor '{}' is not " |
| 386 | "present at vnfd:scaling-group-descriptor".format( |
| 387 | indata["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"])) |
| 388 | if operation == "instantiate": |
| 389 | # check vim_account |
| 390 | check_valid_vim_account(indata["vimAccountId"]) |
| 391 | for in_vnf in get_iterable(indata.get("vnf")): |
| 392 | vnfd = check_valid_vnf_member_index(in_vnf["member-vnf-index"]) |
| gcalvino | 5e72d15 | 2018-10-23 11:46:57 +0200 | [diff] [blame] | 393 | _check_vnf_instantiation_params(in_vnf, vnfd) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 394 | if in_vnf.get("vimAccountId"): |
| 395 | check_valid_vim_account(in_vnf["vimAccountId"]) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 396 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 397 | for in_vld in get_iterable(indata.get("vld")): |
| 398 | for vldd in get_iterable(nsd.get("vld")): |
| 399 | if in_vld["name"] == vldd["name"] or in_vld["name"] == vldd["id"]: |
| 400 | break |
| 401 | else: |
| 402 | raise EngineException("Invalid parameter vld:name='{}' is not present at nsd:vld".format( |
| 403 | in_vld["name"])) |
| 404 | |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 405 | def _look_for_pdu(self, session, rollback, vnfr, vim_account): |
| 406 | """ |
| 407 | Look for a free PDU in the catalog matching vdur type and interfaces. Fills vdur with ip_address information |
| 408 | :param vdur: vnfr:vdur descriptor. It is modified with pdu interface info if pdu is found |
| 409 | :param member_vnf_index: used just for logging. Target vnfd of nsd |
| 410 | :param vim_account: |
| 411 | :return: vder_update: dictionary to update vnfr:vdur with pdu info. In addition it modified choosen pdu to set |
| 412 | at status IN_USE |
| 413 | """ |
| 414 | vnfr_update = {} |
| 415 | rollback_vnfr = {} |
| 416 | for vdur_index, vdur in enumerate(get_iterable(vnfr.get("vdur"))): |
| 417 | if not vdur.get("pdu-type"): |
| 418 | continue |
| 419 | pdu_type = vdur.get("pdu-type") |
| 420 | pdu_filter = self._get_project_filter(session, write=True, show_all=True) |
| 421 | pdu_filter["vim.vim_accounts"] = vim_account |
| 422 | pdu_filter["type"] = pdu_type |
| 423 | pdu_filter["_admin.operationalState"] = "ENABLED" |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 424 | pdu_filter["_admin.usageState"] = "NOT_IN_USE", |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 425 | # TODO feature 1417: "shared": True, |
| 426 | |
| 427 | available_pdus = self.db.get_list("pdus", pdu_filter) |
| 428 | for pdu in available_pdus: |
| 429 | # step 1 check if this pdu contains needed interfaces: |
| 430 | match_interfaces = True |
| 431 | for vdur_interface in vdur["interfaces"]: |
| 432 | for pdu_interface in pdu["interfaces"]: |
| 433 | if pdu_interface["name"] == vdur_interface["name"]: |
| 434 | # TODO feature 1417: match per mgmt type |
| 435 | break |
| 436 | else: # no interface found for name |
| 437 | match_interfaces = False |
| 438 | break |
| 439 | if match_interfaces: |
| 440 | break |
| 441 | else: |
| 442 | raise EngineException( |
| 443 | "No PDU of type={} found for member_vnf_index={} at vim_account={} matching interface " |
| 444 | "names".format(vdur["vdu-id-ref"], vnfr["member-vnf-index-ref"], pdu_type)) |
| 445 | |
| 446 | # step 2. Update pdu |
| 447 | rollback_pdu = { |
| 448 | "_admin.usageState": pdu["_admin"]["usageState"], |
| 449 | "_admin.usage.vnfr_id": None, |
| 450 | "_admin.usage.nsr_id": None, |
| 451 | "_admin.usage.vdur": None, |
| 452 | } |
| 453 | self.db.set_one("pdus", {"_id": pdu["_id"]}, |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 454 | {"_admin.usageState": "IN_USE", |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 455 | "_admin.usage.vnfr_id": vnfr["_id"], |
| 456 | "_admin.usage.nsr_id": vnfr["nsr-id-ref"], |
| 457 | "_admin.usage.vdur": vdur["vdu-id-ref"]} |
| 458 | ) |
| 459 | rollback.append({"topic": "pdus", "_id": pdu["_id"], "operation": "set", "content": rollback_pdu}) |
| 460 | |
| 461 | # step 3. Fill vnfr info by filling vdur |
| 462 | vdu_text = "vdur.{}".format(vdur_index) |
| 463 | rollback_vnfr[vdu_text + ".pdu-id"] = None |
| 464 | vnfr_update[vdu_text + ".pdu-id"] = pdu["_id"] |
| 465 | for iface_index, vdur_interface in enumerate(vdur["interfaces"]): |
| 466 | for pdu_interface in pdu["interfaces"]: |
| 467 | if pdu_interface["name"] == vdur_interface["name"]: |
| 468 | iface_text = vdu_text + ".interfaces.{}".format(iface_index) |
| 469 | for k, v in pdu_interface.items(): |
| 470 | vnfr_update[iface_text + ".{}".format(k)] = v |
| 471 | rollback_vnfr[iface_text + ".{}".format(k)] = vdur_interface.get(v) |
| 472 | break |
| 473 | |
| 474 | if vnfr_update: |
| 475 | self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, vnfr_update) |
| 476 | rollback.append({"topic": "vnfrs", "_id": vnfr["_id"], "operation": "set", "content": rollback_vnfr}) |
| 477 | return |
| 478 | |
| 479 | def _update_vnfrs(self, session, rollback, nsr, indata): |
| 480 | vnfrs = None |
| 481 | # get vnfr |
| 482 | nsr_id = nsr["_id"] |
| 483 | vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id}) |
| 484 | |
| 485 | for vnfr in vnfrs: |
| 486 | vnfr_update = {} |
| 487 | vnfr_update_rollback = {} |
| 488 | member_vnf_index = vnfr["member-vnf-index-ref"] |
| 489 | # update vim-account-id |
| 490 | |
| 491 | vim_account = indata["vimAccountId"] |
| 492 | # check instantiate parameters |
| 493 | for vnf_inst_params in get_iterable(indata.get("vnf")): |
| 494 | if vnf_inst_params["member-vnf-index"] != member_vnf_index: |
| 495 | continue |
| 496 | if vnf_inst_params.get("vimAccountId"): |
| 497 | vim_account = vnf_inst_params.get("vimAccountId") |
| 498 | |
| 499 | vnfr_update["vim-account-id"] = vim_account |
| 500 | vnfr_update_rollback["vim-account-id"] = vnfr.get("vim-account-id") |
| 501 | |
| 502 | # get pdu |
| 503 | self._look_for_pdu(session, rollback, vnfr, vim_account) |
| 504 | # TODO change instantiation parameters to set network |
| 505 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 506 | def _create_nslcmop(self, session, nsInstanceId, operation, params): |
| 507 | now = time() |
| 508 | _id = str(uuid4()) |
| 509 | nslcmop = { |
| 510 | "id": _id, |
| 511 | "_id": _id, |
| 512 | "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK |
| 513 | "statusEnteredTime": now, |
| 514 | "nsInstanceId": nsInstanceId, |
| 515 | "lcmOperationType": operation, |
| 516 | "startTime": now, |
| 517 | "isAutomaticInvocation": False, |
| 518 | "operationParams": params, |
| 519 | "isCancelPending": False, |
| 520 | "links": { |
| 521 | "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id, |
| 522 | "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsInstanceId, |
| 523 | } |
| 524 | } |
| 525 | return nslcmop |
| 526 | |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 527 | def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False, |
| 528 | slice_object=False): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 529 | """ |
| 530 | Performs a new operation over a ns |
| 531 | :param rollback: list to append created items at database in case a rollback must to be done |
| 532 | :param session: contains the used login username and working project |
| 533 | :param indata: descriptor with the parameters of the operation. It must contains among others |
| 534 | nsInstanceId: _id of the nsr to perform the operation |
| 535 | operation: it can be: instantiate, terminate, action, TODO: update, heal |
| 536 | :param kwargs: used to override the indata descriptor |
| 537 | :param headers: http request headers |
| 538 | :param force: If True avoid some dependence checks |
| 539 | :param make_public: Make the created item public to all projects |
| 540 | :return: id of the nslcmops |
| 541 | """ |
| 542 | try: |
| 543 | # Override descriptor with query string kwargs |
| 544 | self._update_input_with_kwargs(indata, kwargs) |
| 545 | operation = indata["lcmOperationType"] |
| 546 | nsInstanceId = indata["nsInstanceId"] |
| 547 | |
| 548 | validate_input(indata, self.operation_schema[operation]) |
| 549 | # get ns from nsr_id |
| 550 | _filter = BaseTopic._get_project_filter(session, write=True, show_all=False) |
| 551 | _filter["_id"] = nsInstanceId |
| 552 | nsr = self.db.get_one("nsrs", _filter) |
| 553 | |
| 554 | # initial checking |
| 555 | if not nsr["_admin"].get("nsState") or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED": |
| 556 | if operation == "terminate" and indata.get("autoremove"): |
| 557 | # NSR must be deleted |
| 558 | return self.delete(session, nsInstanceId) |
| 559 | if operation != "instantiate": |
| 560 | raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format( |
| 561 | nsInstanceId, operation), HTTPStatus.CONFLICT) |
| 562 | else: |
| 563 | if operation == "instantiate" and not indata.get("force"): |
| 564 | raise EngineException("ns_instance '{}' cannot be '{}' because it is already instantiated".format( |
| 565 | nsInstanceId, operation), HTTPStatus.CONFLICT) |
| 566 | self._check_ns_operation(session, nsr, operation, indata) |
| tierno | cc10343 | 2018-10-19 14:10:35 +0200 | [diff] [blame] | 567 | if operation == "instantiate": |
| 568 | self._update_vnfrs(session, rollback, nsr, indata) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 569 | nslcmop_desc = self._create_nslcmop(session, nsInstanceId, operation, indata) |
| 570 | self.format_on_new(nslcmop_desc, session["project_id"], make_public=make_public) |
| 571 | _id = self.db.create("nslcmops", nslcmop_desc) |
| 572 | rollback.append({"topic": "nslcmops", "_id": _id}) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 573 | if not slice_object: |
| 574 | self.msg.write("ns", operation, nslcmop_desc) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 575 | return _id |
| 576 | except ValidationError as e: |
| 577 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 578 | # except DbException as e: |
| 579 | # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND) |
| 580 | |
| 581 | def delete(self, session, _id, force=False, dry_run=False): |
| 582 | raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 583 | |
| 584 | def edit(self, session, _id, indata=None, kwargs=None, force=False, content=None): |
| 585 | raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 586 | |
| 587 | |
| 588 | class NsiTopic(BaseTopic): |
| 589 | topic = "nsis" |
| 590 | topic_msg = "nsi" |
| 591 | |
| 592 | def __init__(self, db, fs, msg): |
| 593 | BaseTopic.__init__(self, db, fs, msg) |
| 594 | |
| 595 | def _check_descriptor_dependencies(self, session, descriptor): |
| 596 | """ |
| 597 | Check that the dependent descriptors exist on a new descriptor or edition |
| 598 | :param session: client session information |
| 599 | :param descriptor: descriptor to be inserted or edit |
| 600 | :return: None or raises exception |
| 601 | """ |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 602 | if not descriptor.get("nst-ref"): |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 603 | return |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 604 | nstd_id = descriptor["nst-ref"] |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 605 | if not self.get_item_list(session, "nsts", {"id": nstd_id}): |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 606 | raise EngineException("Descriptor error at nst-ref='{}' references a non exist nstd".format(nstd_id), |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 607 | http_code=HTTPStatus.CONFLICT) |
| 608 | |
| 609 | @staticmethod |
| 610 | def format_on_new(content, project_id=None, make_public=False): |
| 611 | BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 612 | |
| 613 | def check_conflict_on_del(self, session, _id, force=False): |
| 614 | if force: |
| 615 | return |
| 616 | nsi = self.db.get_one("nsis", {"_id": _id}) |
| 617 | if nsi["_admin"].get("nsiState") == "INSTANTIATED": |
| 618 | raise EngineException("nsi '{}' cannot be deleted because it is in 'INSTANTIATED' state. " |
| 619 | "Launch 'terminate' operation first; or force deletion".format(_id), |
| 620 | http_code=HTTPStatus.CONFLICT) |
| 621 | |
| 622 | def delete(self, session, _id, force=False, dry_run=False): |
| 623 | """ |
| 624 | Delete item by its internal _id |
| 625 | :param session: contains the used login username, working project, and admin rights |
| 626 | :param _id: server internal id |
| 627 | :param force: indicates if deletion must be forced in case of conflict |
| 628 | :param dry_run: make checking but do not delete |
| 629 | :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... |
| 630 | """ |
| 631 | # TODO add admin to filter, validate rights |
| 632 | BaseTopic.delete(self, session, _id, force, dry_run=True) |
| 633 | if dry_run: |
| 634 | return |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 635 | |
| 636 | # deletes NetSlice instance object |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 637 | v = self.db.del_one("nsis", {"_id": _id}) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 638 | |
| 639 | # makes a temporal list of nsilcmops objects related to the _id given and deletes them from db |
| 640 | _filter = {"netsliceInstanceId": _id} |
| 641 | self.db.del_list("nsilcmops", _filter) |
| 642 | |
| 643 | _filter = {"operationParams.netsliceInstanceId": _id} |
| 644 | nslcmops_list = self.db.get_list("nslcmops", _filter) |
| 645 | |
| 646 | for id_item in nslcmops_list: |
| 647 | _filter = {"_id": id_item} |
| 648 | nslcmop = self.db.get_one("nslcmops", _filter) |
| 649 | nsr_id = nslcmop["operationParams"]["nsr_id"] |
| 650 | NsrTopic.delete(self, session, nsr_id, force=False, dry_run=False) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 651 | self._send_msg("deleted", {"_id": _id}) |
| 652 | return v |
| 653 | |
| 654 | def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False): |
| 655 | """ |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 656 | Creates a new netslice instance record into database. It also creates needed nsrs and vnfrs |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 657 | :param rollback: list to append the created items at database in case a rollback must be done |
| 658 | :param session: contains the used login username and working project |
| 659 | :param indata: params to be used for the nsir |
| 660 | :param kwargs: used to override the indata descriptor |
| 661 | :param headers: http request headers |
| 662 | :param force: If True avoid some dependence checks |
| 663 | :param make_public: Make the created item public to all projects |
| 664 | :return: the _id of nsi descriptor created at database |
| 665 | """ |
| 666 | |
| 667 | try: |
| 668 | slice_request = self._remove_envelop(indata) |
| 669 | # Override descriptor with query string kwargs |
| 670 | self._update_input_with_kwargs(slice_request, kwargs) |
| 671 | self._validate_input_new(slice_request, force) |
| 672 | |
| 673 | step = "" |
| 674 | # look for nstd |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 675 | self.logger.info(str(slice_request)) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 676 | step = "getting nstd id='{}' from database".format(slice_request.get("nstdId")) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 677 | _filter = {"id": slice_request["nstdId"]} |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 678 | _filter.update(BaseTopic._get_project_filter(session, write=False, show_all=True)) |
| 679 | nstd = self.db.get_one("nsts", _filter) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 680 | nstd.pop("_admin", None) |
| 681 | nstd.pop("_id", None) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 682 | nsi_id = str(uuid4()) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 683 | step = "filling nsi_descriptor with input data" |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 684 | |
| 685 | # "instantiation-parameters.netslice-subnet": [] |
| 686 | # TODO: Equal as template for now |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 687 | nsi_descriptor = { |
| 688 | "id": nsi_id, |
| 689 | "nst-ref": nstd["id"], |
| 690 | "instantiation-parameters": { |
| 691 | "netslice-subnet": [] |
| 692 | }, |
| 693 | "network-slice-template": nstd, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 694 | "_id": nsi_id, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 695 | } |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 696 | |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 697 | # Creating netslice-subnet_record. |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 698 | needed_nsds = {} |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 699 | services = [] |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 700 | for member_ns in nstd["netslice-subnet"]: |
| 701 | nsd_id = member_ns["nsd-ref"] |
| 702 | step = "getting nstd id='{}' constituent-nsd='{}' from database".format( |
| 703 | member_ns["nsd-ref"], member_ns["id"]) |
| 704 | if nsd_id not in needed_nsds: |
| 705 | # Obtain nsd |
| 706 | nsd = DescriptorTopic.get_one_by_id(self.db, session, "nsds", nsd_id) |
| 707 | nsd.pop("_admin") |
| 708 | needed_nsds[nsd_id] = nsd |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 709 | member_ns["_id"] = needed_nsds[nsd_id].get("_id") |
| 710 | services.append(member_ns) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 711 | else: |
| 712 | nsd = needed_nsds[nsd_id] |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 713 | member_ns["_id"] = needed_nsds[nsd_id].get("_id") |
| 714 | services.append(member_ns) |
| 715 | |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 716 | step = "filling nsir nsd-id='{}' constituent-nsd='{}' from database".format( |
| 717 | member_ns["nsd-ref"], member_ns["id"]) |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 718 | |
| 719 | step = "creating nsi at database" |
| 720 | self.format_on_new(nsi_descriptor, session["project_id"], make_public=make_public) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 721 | nsi_descriptor["_admin"]["nsiState"] = "NOT_INSTANTIATED" |
| 722 | |
| 723 | # creates Network Services records (NSRs) |
| 724 | step = "creating nsrs at database using NsrTopic.new()" |
| 725 | nsrs_list = [] |
| 726 | for service in services: |
| 727 | indata_ns = {} |
| 728 | indata_ns["nsdId"] = service["_id"] |
| 729 | indata_ns["nsName"] = service["name"] |
| 730 | indata_ns["vimAccountId"] = indata.get("vimAccountId") |
| 731 | indata_ns["nsDescription"] = service["description"] |
| 732 | indata_ns["key-pair-ref"] = None |
| 733 | # NsrTopic(rollback, session, indata_ns, kwargs, headers, force) |
| 734 | _id_nsr = NsrTopic.new(self, rollback, session, indata_ns, kwargs, headers, force) |
| 735 | nsrs_item = {"nsrId": _id_nsr} |
| 736 | nsrs_list.append(nsrs_item) |
| 737 | |
| 738 | # Adding the nsrs list to the nsi |
| 739 | nsi_descriptor["_admin"]["nsrs-detailed-list"] = nsrs_list |
| 740 | # Creating the entry in the database |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 741 | self.db.create("nsis", nsi_descriptor) |
| 742 | rollback.append({"topic": "nsis", "_id": nsi_id}) |
| 743 | return nsi_id |
| 744 | except Exception as e: |
| 745 | self.logger.exception("Exception {} at NsiTopic.new()".format(e), exc_info=True) |
| 746 | raise EngineException("Error {}: {}".format(step, e)) |
| 747 | except ValidationError as e: |
| 748 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 749 | |
| 750 | def edit(self, session, _id, indata=None, kwargs=None, force=False, content=None): |
| 751 | raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 752 | |
| 753 | |
| 754 | class NsiLcmOpTopic(BaseTopic): |
| 755 | topic = "nsilcmops" |
| 756 | topic_msg = "nsi" |
| 757 | operation_schema = { # mapping between operation and jsonschema to validate |
| 758 | "instantiate": nsi_instantiate, |
| 759 | "terminate": None |
| 760 | } |
| 761 | |
| 762 | def __init__(self, db, fs, msg): |
| 763 | BaseTopic.__init__(self, db, fs, msg) |
| 764 | |
| 765 | def _check_nsi_operation(self, session, nsir, operation, indata): |
| 766 | """ |
| 767 | Check that user has enter right parameters for the operation |
| 768 | :param session: |
| 769 | :param operation: it can be: instantiate, terminate, action, TODO: update, heal |
| 770 | :param indata: descriptor with the parameters of the operation |
| 771 | :return: None |
| 772 | """ |
| 773 | nsds = {} |
| 774 | nstd = nsir["network-slice-template"] |
| 775 | |
| 776 | def check_valid_netslice_subnet_id(nsId): |
| 777 | # TODO change to vnfR (??) |
| 778 | for ns in nstd["netslice-subnet"]: |
| 779 | if nsId == ns["id"]: |
| 780 | nsd_id = ns["nsd-ref"] |
| 781 | if nsd_id not in nsds: |
| 782 | nsds[nsd_id] = self.db.get_one("nsds", {"id": nsd_id}) |
| 783 | return nsds[nsd_id] |
| 784 | else: |
| 785 | raise EngineException("Invalid parameter nsId='{}' is not one of the " |
| 786 | "nst:netslice-subnet".format(nsId)) |
| 787 | if operation == "instantiate": |
| 788 | # check the existance of netslice-subnet items |
| 789 | for in_nst in get_iterable(indata.get("netslice-subnet")): |
| 790 | nstd = check_valid_netslice_subnet_id(in_nst["nsdId"]) |
| 791 | |
| 792 | def _create_nsilcmop(self, session, netsliceInstanceId, operation, params): |
| 793 | now = time() |
| 794 | _id = str(uuid4()) |
| 795 | nsilcmop = { |
| 796 | "id": _id, |
| 797 | "_id": _id, |
| 798 | "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK |
| 799 | "statusEnteredTime": now, |
| 800 | "netsliceInstanceId": netsliceInstanceId, |
| 801 | "lcmOperationType": operation, |
| 802 | "startTime": now, |
| 803 | "isAutomaticInvocation": False, |
| 804 | "operationParams": params, |
| 805 | "isCancelPending": False, |
| 806 | "links": { |
| 807 | "self": "/osm/nsilcm/v1/nsi_lcm_op_occs/" + _id, |
| 808 | "nsInstance": "/osm/nsilcm/v1/netslice_instances/" + netsliceInstanceId, |
| 809 | } |
| 810 | } |
| 811 | return nsilcmop |
| 812 | |
| 813 | def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False): |
| 814 | """ |
| 815 | Performs a new operation over a ns |
| 816 | :param rollback: list to append created items at database in case a rollback must to be done |
| 817 | :param session: contains the used login username and working project |
| 818 | :param indata: descriptor with the parameters of the operation. It must contains among others |
| 819 | nsiInstanceId: _id of the nsir to perform the operation |
| 820 | operation: it can be: instantiate, terminate, action, TODO: update, heal |
| 821 | :param kwargs: used to override the indata descriptor |
| 822 | :param headers: http request headers |
| 823 | :param force: If True avoid some dependence checks |
| 824 | :param make_public: Make the created item public to all projects |
| 825 | :return: id of the nslcmops |
| 826 | """ |
| 827 | try: |
| 828 | # Override descriptor with query string kwargs |
| 829 | self._update_input_with_kwargs(indata, kwargs) |
| 830 | operation = indata["lcmOperationType"] |
| 831 | nsiInstanceId = indata["nsiInstanceId"] |
| 832 | validate_input(indata, self.operation_schema[operation]) |
| 833 | |
| 834 | # get nsi from nsiInstanceId |
| 835 | _filter = BaseTopic._get_project_filter(session, write=True, show_all=False) |
| 836 | _filter["_id"] = nsiInstanceId |
| 837 | nsir = self.db.get_one("nsis", _filter) |
| 838 | |
| 839 | # initial checking |
| 840 | if not nsir["_admin"].get("nsiState") or nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED": |
| 841 | if operation == "terminate" and indata.get("autoremove"): |
| 842 | # NSIR must be deleted |
| 843 | return self.delete(session, nsiInstanceId) |
| 844 | if operation != "instantiate": |
| 845 | raise EngineException("netslice_instance '{}' cannot be '{}' because it is not instantiated".format( |
| 846 | nsiInstanceId, operation), HTTPStatus.CONFLICT) |
| 847 | else: |
| 848 | if operation == "instantiate" and not indata.get("force"): |
| 849 | raise EngineException("netslice_instance '{}' cannot be '{}' because it is already instantiated". |
| 850 | format(nsiInstanceId, operation), HTTPStatus.CONFLICT) |
| 851 | |
| 852 | # Creating all the NS_operation (nslcmop) |
| 853 | # Get service list from db |
| 854 | nsrs_list = nsir["_admin"]["nsrs-detailed-list"] |
| 855 | nslcmops = [] |
| 856 | for nsr_item in nsrs_list: |
| 857 | service = self.db.get_one("nsrs", {"_id": nsr_item["nsrId"]}) |
| 858 | indata_ns = {} |
| 859 | indata_ns = service["instantiate_params"] |
| 860 | indata_ns["lcmOperationType"] = operation |
| 861 | indata_ns["nsInstanceId"] = service["_id"] |
| 862 | # Including netslice_id in the ns instantiate Operation |
| 863 | indata_ns["netsliceInstanceId"] = nsiInstanceId |
| 864 | del indata_ns["key-pair-ref"] |
| 865 | nsi_NsLcmOpTopic = NsLcmOpTopic(self.db, self.fs, self.msg) |
| 866 | # Creating NS_LCM_OP with the flag slice_object=True to not trigger the service instantiation |
| 867 | # message via kafka bus |
| 868 | nslcmop = nsi_NsLcmOpTopic.new(rollback, session, indata_ns, kwargs, headers, force, slice_object=True) |
| 869 | nslcmops.append(nslcmop) |
| 870 | |
| 871 | # Creates nsilcmop |
| 872 | indata["nslcmops_ids"] = nslcmops |
| 873 | self._check_nsi_operation(session, nsir, operation, indata) |
| 874 | nsilcmop_desc = self._create_nsilcmop(session, nsiInstanceId, operation, indata) |
| 875 | self.format_on_new(nsilcmop_desc, session["project_id"], make_public=make_public) |
| 876 | _id = self.db.create("nsilcmops", nsilcmop_desc) |
| 877 | rollback.append({"topic": "nsilcmops", "_id": _id}) |
| 878 | self.msg.write("nsi", operation, nsilcmop_desc) |
| 879 | return _id |
| 880 | except ValidationError as e: |
| 881 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 882 | # except DbException as e: |
| 883 | # raise EngineException("Cannot get nsi_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND) |
| 884 | |
| 885 | def delete(self, session, _id, force=False, dry_run=False): |
| 886 | raise EngineException("Method delete called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 887 | |
| 888 | def edit(self, session, _id, indata=None, kwargs=None, force=False, content=None): |
| 889 | raise EngineException("Method edit called directly", HTTPStatus.INTERNAL_SERVER_ERROR) |