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