| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame^] | 3 | from osm_common import dbmongo |
| 4 | from osm_common import dbmemory |
| 5 | from osm_common import fslocal |
| 6 | from osm_common import msglocal |
| 7 | from osm_common import msgkafka |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 8 | import tarfile |
| 9 | import yaml |
| 10 | import json |
| 11 | import logging |
| 12 | from random import choice as random_choice |
| 13 | from uuid import uuid4 |
| 14 | from hashlib import sha256, md5 |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame^] | 15 | from osm_common.dbbase import DbException |
| 16 | from osm_common.fsbase import FsException |
| 17 | from osm_common.msgbase import MsgException |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 18 | from http import HTTPStatus |
| 19 | from time import time |
| 20 | from copy import deepcopy |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 21 | from validation import validate_input, ValidationError |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 22 | |
| 23 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 24 | |
| 25 | |
| 26 | class EngineException(Exception): |
| 27 | |
| 28 | def __init__(self, message, http_code=HTTPStatus.BAD_REQUEST): |
| 29 | self.http_code = http_code |
| 30 | Exception.__init__(self, message) |
| 31 | |
| 32 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 33 | def _deep_update(dict_to_change, dict_reference): |
| 34 | """ |
| 35 | Modifies one dictionary with the information of the other following https://tools.ietf.org/html/rfc7396 |
| 36 | :param dict_to_change: Ends modified |
| 37 | :param dict_reference: reference |
| 38 | :return: none |
| 39 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 40 | for k in dict_reference: |
| 41 | if dict_reference[k] is None: # None->Anything |
| 42 | if k in dict_to_change: |
| 43 | del dict_to_change[k] |
| 44 | elif not isinstance(dict_reference[k], dict): # NotDict->Anything |
| 45 | dict_to_change[k] = dict_reference[k] |
| 46 | elif k not in dict_to_change: # Dict->Empty |
| 47 | dict_to_change[k] = deepcopy(dict_reference[k]) |
| 48 | _deep_update(dict_to_change[k], dict_reference[k]) |
| 49 | elif isinstance(dict_to_change[k], dict): # Dict->Dict |
| 50 | _deep_update(dict_to_change[k], dict_reference[k]) |
| 51 | else: # Dict->NotDict |
| 52 | dict_to_change[k] = deepcopy(dict_reference[k]) |
| 53 | _deep_update(dict_to_change[k], dict_reference[k]) |
| 54 | |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 55 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 56 | class Engine(object): |
| 57 | |
| 58 | def __init__(self): |
| 59 | self.tokens = {} |
| 60 | self.db = None |
| 61 | self.fs = None |
| 62 | self.msg = None |
| 63 | self.config = None |
| 64 | self.logger = logging.getLogger("nbi.engine") |
| 65 | |
| 66 | def start(self, config): |
| 67 | """ |
| 68 | Connect to database, filesystem storage, and messaging |
| 69 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 70 | :return: None |
| 71 | """ |
| 72 | self.config = config |
| 73 | try: |
| 74 | if not self.db: |
| 75 | if config["database"]["driver"] == "mongo": |
| 76 | self.db = dbmongo.DbMongo() |
| 77 | self.db.db_connect(config["database"]) |
| 78 | elif config["database"]["driver"] == "memory": |
| 79 | self.db = dbmemory.DbMemory() |
| 80 | self.db.db_connect(config["database"]) |
| 81 | else: |
| 82 | raise EngineException("Invalid configuration param '{}' at '[database]':'driver'".format( |
| 83 | config["database"]["driver"])) |
| 84 | if not self.fs: |
| 85 | if config["storage"]["driver"] == "local": |
| 86 | self.fs = fslocal.FsLocal() |
| 87 | self.fs.fs_connect(config["storage"]) |
| 88 | else: |
| 89 | raise EngineException("Invalid configuration param '{}' at '[storage]':'driver'".format( |
| 90 | config["storage"]["driver"])) |
| 91 | if not self.msg: |
| 92 | if config["message"]["driver"] == "local": |
| 93 | self.msg = msglocal.MsgLocal() |
| 94 | self.msg.connect(config["message"]) |
| 95 | elif config["message"]["driver"] == "kafka": |
| 96 | self.msg = msgkafka.MsgKafka() |
| 97 | self.msg.connect(config["message"]) |
| 98 | else: |
| 99 | raise EngineException("Invalid configuration param '{}' at '[message]':'driver'".format( |
| 100 | config["storage"]["driver"])) |
| 101 | except (DbException, FsException, MsgException) as e: |
| 102 | raise EngineException(str(e), http_code=e.http_code) |
| 103 | |
| 104 | def stop(self): |
| 105 | try: |
| 106 | if self.db: |
| 107 | self.db.db_disconnect() |
| 108 | if self.fs: |
| 109 | self.fs.fs_disconnect() |
| 110 | if self.fs: |
| 111 | self.fs.fs_disconnect() |
| 112 | except (DbException, FsException, MsgException) as e: |
| 113 | raise EngineException(str(e), http_code=e.http_code) |
| 114 | |
| 115 | def authorize(self, token): |
| 116 | try: |
| 117 | if not token: |
| 118 | raise EngineException("Needed a token or Authorization http header", |
| 119 | http_code=HTTPStatus.UNAUTHORIZED) |
| 120 | if token not in self.tokens: |
| 121 | raise EngineException("Invalid token or Authorization http header", |
| 122 | http_code=HTTPStatus.UNAUTHORIZED) |
| 123 | session = self.tokens[token] |
| 124 | now = time() |
| 125 | if session["expires"] < now: |
| 126 | del self.tokens[token] |
| 127 | raise EngineException("Expired Token or Authorization http header", |
| 128 | http_code=HTTPStatus.UNAUTHORIZED) |
| 129 | return session |
| 130 | except EngineException: |
| 131 | if self.config["global"].get("test.user_not_authorized"): |
| 132 | return {"id": "fake-token-id-for-test", |
| 133 | "project_id": self.config["global"].get("test.project_not_authorized", "admin"), |
| 134 | "username": self.config["global"]["test.user_not_authorized"]} |
| 135 | else: |
| 136 | raise |
| 137 | |
| 138 | def new_token(self, session, indata, remote): |
| 139 | now = time() |
| 140 | user_content = None |
| 141 | |
| 142 | # Try using username/password |
| 143 | if indata.get("username"): |
| 144 | user_rows = self.db.get_list("users", {"username": indata.get("username")}) |
| 145 | user_content = None |
| 146 | if user_rows: |
| 147 | user_content = user_rows[0] |
| 148 | salt = user_content["_admin"]["salt"] |
| 149 | shadow_password = sha256(indata.get("password", "").encode('utf-8') + salt.encode('utf-8')).hexdigest() |
| 150 | if shadow_password != user_content["password"]: |
| 151 | user_content = None |
| 152 | if not user_content: |
| 153 | raise EngineException("Invalid username/password", http_code=HTTPStatus.UNAUTHORIZED) |
| 154 | elif session: |
| 155 | user_rows = self.db.get_list("users", {"username": session["username"]}) |
| 156 | if user_rows: |
| 157 | user_content = user_rows[0] |
| 158 | else: |
| 159 | raise EngineException("Invalid token", http_code=HTTPStatus.UNAUTHORIZED) |
| 160 | else: |
| 161 | raise EngineException("Provide credentials: username/password or Authorization Bearer token", |
| 162 | http_code=HTTPStatus.UNAUTHORIZED) |
| 163 | |
| 164 | token_id = ''.join(random_choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') |
| 165 | for _ in range(0, 32)) |
| 166 | if indata.get("project_id"): |
| 167 | project_id = indata.get("project_id") |
| 168 | if project_id not in user_content["projects"]: |
| 169 | raise EngineException("project {} not allowed for this user".format(project_id), |
| 170 | http_code=HTTPStatus.UNAUTHORIZED) |
| 171 | else: |
| 172 | project_id = user_content["projects"][0] |
| 173 | if project_id == "admin": |
| 174 | session_admin = True |
| 175 | else: |
| 176 | project = self.db.get_one("projects", {"_id": project_id}) |
| 177 | session_admin = project.get("admin", False) |
| 178 | new_session = {"issued_at": now, "expires": now+3600, |
| 179 | "_id": token_id, "id": token_id, "project_id": project_id, "username": user_content["username"], |
| 180 | "remote_port": remote.port, "admin": session_admin} |
| 181 | if remote.name: |
| 182 | new_session["remote_host"] = remote.name |
| 183 | elif remote.ip: |
| 184 | new_session["remote_host"] = remote.ip |
| 185 | |
| 186 | self.tokens[token_id] = new_session |
| 187 | return deepcopy(new_session) |
| 188 | |
| 189 | def get_token_list(self, session): |
| 190 | token_list = [] |
| 191 | for token_id, token_value in self.tokens.items(): |
| 192 | if token_value["username"] == session["username"]: |
| 193 | token_list.append(deepcopy(token_value)) |
| 194 | return token_list |
| 195 | |
| 196 | def get_token(self, session, token_id): |
| 197 | token_value = self.tokens.get(token_id) |
| 198 | if not token_value: |
| 199 | raise EngineException("token not found", http_code=HTTPStatus.NOT_FOUND) |
| 200 | if token_value["username"] != session["username"] and not session["admin"]: |
| 201 | raise EngineException("needed admin privileges", http_code=HTTPStatus.UNAUTHORIZED) |
| 202 | return token_value |
| 203 | |
| 204 | def del_token(self, token_id): |
| 205 | try: |
| 206 | del self.tokens[token_id] |
| 207 | return "token '{}' deleted".format(token_id) |
| 208 | except KeyError: |
| 209 | raise EngineException("Token '{}' not found".format(token_id), http_code=HTTPStatus.NOT_FOUND) |
| 210 | |
| 211 | @staticmethod |
| 212 | def _remove_envelop(item, indata=None): |
| 213 | """ |
| 214 | Obtain the useful data removing the envelop. It goes throw the vnfd or nsd catalog and returns the |
| 215 | vnfd or nsd content |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 216 | :param item: can be vnfds, nsds, users, projects, userDefinedData (initial content of a vnfds, nsds |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 217 | :param indata: Content to be inspected |
| 218 | :return: the useful part of indata |
| 219 | """ |
| 220 | clean_indata = indata |
| 221 | if not indata: |
| 222 | return {} |
| 223 | if item == "vnfds": |
| 224 | if clean_indata.get('vnfd:vnfd-catalog'): |
| 225 | clean_indata = clean_indata['vnfd:vnfd-catalog'] |
| 226 | elif clean_indata.get('vnfd-catalog'): |
| 227 | clean_indata = clean_indata['vnfd-catalog'] |
| 228 | if clean_indata.get('vnfd'): |
| 229 | if not isinstance(clean_indata['vnfd'], list) or len(clean_indata['vnfd']) != 1: |
| 230 | raise EngineException("'vnfd' must be a list only one element") |
| 231 | clean_indata = clean_indata['vnfd'][0] |
| 232 | elif item == "nsds": |
| 233 | if clean_indata.get('nsd:nsd-catalog'): |
| 234 | clean_indata = clean_indata['nsd:nsd-catalog'] |
| 235 | elif clean_indata.get('nsd-catalog'): |
| 236 | clean_indata = clean_indata['nsd-catalog'] |
| 237 | if clean_indata.get('nsd'): |
| 238 | if not isinstance(clean_indata['nsd'], list) or len(clean_indata['nsd']) != 1: |
| 239 | raise EngineException("'nsd' must be a list only one element") |
| 240 | clean_indata = clean_indata['nsd'][0] |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 241 | elif item == "userDefinedData": |
| 242 | if "userDefinedData" in indata: |
| 243 | clean_indata = clean_indata['userDefinedData'] |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 244 | return clean_indata |
| 245 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 246 | def _validate_new_data(self, session, item, indata, id=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 247 | if item == "users": |
| 248 | if not indata.get("username"): |
| 249 | raise EngineException("missing 'username'", HTTPStatus.UNPROCESSABLE_ENTITY) |
| 250 | if not indata.get("password"): |
| 251 | raise EngineException("missing 'password'", HTTPStatus.UNPROCESSABLE_ENTITY) |
| 252 | if not indata.get("projects"): |
| 253 | raise EngineException("missing 'projects'", HTTPStatus.UNPROCESSABLE_ENTITY) |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 254 | # check username not exists |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 255 | if self.db.get_one(item, {"username": indata.get("username")}, fail_on_empty=False, fail_on_more=False): |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 256 | raise EngineException("username '{}' exists".format(indata["username"]), HTTPStatus.CONFLICT) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 257 | elif item == "projects": |
| 258 | if not indata.get("name"): |
| 259 | raise EngineException("missing 'name'") |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 260 | # check name not exists |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 261 | if self.db.get_one(item, {"name": indata.get("name")}, fail_on_empty=False, fail_on_more=False): |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 262 | raise EngineException("name '{}' exists".format(indata["name"]), HTTPStatus.CONFLICT) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 263 | elif item in ("vnfds", "nsds"): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 264 | filter = {"id": indata["id"]} |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 265 | if id: |
| 266 | filter["_id.neq"] = id |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 267 | # TODO add admin to filter, validate rights |
| 268 | self._add_read_filter(session, item, filter) |
| 269 | if self.db.get_one(item, filter, fail_on_empty=False): |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 270 | raise EngineException("{} with id '{}' already exists for this tenant".format(item[:-1], indata["id"]), |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 271 | HTTPStatus.CONFLICT) |
| 272 | |
| 273 | # TODO validate with pyangbind |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 274 | elif item == "userDefinedData": |
| 275 | # TODO validate userDefinedData is a keypair values |
| 276 | pass |
| 277 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 278 | elif item == "nsrs": |
| 279 | pass |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 280 | elif item == "vim_accounts" or item == "sdns": |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 281 | if self.db.get_one(item, {"name": indata.get("name")}, fail_on_empty=False, fail_on_more=False): |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 282 | raise EngineException("name '{}' already exists for {}".format(indata["name"], item), |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 283 | HTTPStatus.CONFLICT) |
| 284 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 285 | def _format_new_data(self, session, item, indata): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 286 | now = time() |
| 287 | if not "_admin" in indata: |
| 288 | indata["_admin"] = {} |
| 289 | indata["_admin"]["created"] = now |
| 290 | indata["_admin"]["modified"] = now |
| 291 | if item == "users": |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 292 | indata["_id"] = indata["username"] |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 293 | salt = uuid4().hex |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 294 | indata["_admin"]["salt"] = salt |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 295 | indata["password"] = sha256(indata["password"].encode('utf-8') + salt.encode('utf-8')).hexdigest() |
| 296 | elif item == "projects": |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 297 | indata["_id"] = indata["name"] |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 298 | else: |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 299 | if not indata.get("_id"): |
| 300 | indata["_id"] = str(uuid4()) |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 301 | if item in ("vnfds", "nsds", "nsrs", "vnfrs"): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 302 | if not indata["_admin"].get("projects_read"): |
| 303 | indata["_admin"]["projects_read"] = [session["project_id"]] |
| 304 | if not indata["_admin"].get("projects_write"): |
| 305 | indata["_admin"]["projects_write"] = [session["project_id"]] |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 306 | if item in ("vnfds", "nsds"): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 307 | indata["_admin"]["onboardingState"] = "CREATED" |
| 308 | indata["_admin"]["operationalState"] = "DISABLED" |
| 309 | indata["_admin"]["usageSate"] = "NOT_IN_USE" |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 310 | if item == "nsrs": |
| 311 | indata["_admin"]["nsState"] = "NOT_INSTANTIATED" |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 312 | if item in ("vim_accounts", "sdns"): |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 313 | indata["_admin"]["operationalState"] = "PROCESSING" |
| 314 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 315 | def upload_content(self, session, item, _id, indata, kwargs, headers): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 316 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 317 | Used for receiving content by chunks (with a transaction_id header and/or gzip file. It will store and extract) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 318 | :param session: session |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 319 | :param item: can be nsds or vnfds |
| 320 | :param _id : the nsd,vnfd is already created, this is the id |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 321 | :param indata: http body request |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 322 | :param kwargs: user query string to override parameters. NOT USED |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 323 | :param headers: http request headers |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 324 | :return: True package has is completely uploaded or False if partial content has been uplodaed. |
| 325 | Raise exception on error |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 326 | """ |
| tierno | 3ace63c | 2018-05-03 17:51:43 +0200 | [diff] [blame] | 327 | # Check that _id exists and it is valid |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 328 | current_desc = self.get_item(session, item, _id) |
| 329 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 330 | content_range_text = headers.get("Content-Range") |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 331 | expected_md5 = headers.get("Content-File-MD5") |
| 332 | compressed = None |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 333 | content_type = headers.get("Content-Type") |
| 334 | if content_type and "application/gzip" in content_type or "application/x-gzip" in content_type or \ |
| 335 | "application/zip" in content_type: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 336 | compressed = "gzip" |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 337 | filename = headers.get("Content-Filename") |
| 338 | if not filename: |
| 339 | filename = "package.tar.gz" if compressed else "package" |
| 340 | # TODO change to Content-Disposition filename https://tools.ietf.org/html/rfc6266 |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 341 | file_pkg = None |
| 342 | error_text = "" |
| 343 | try: |
| 344 | if content_range_text: |
| 345 | content_range = content_range_text.replace("-", " ").replace("/", " ").split() |
| 346 | if content_range[0] != "bytes": # TODO check x<y not negative < total.... |
| 347 | raise IndexError() |
| 348 | start = int(content_range[1]) |
| 349 | end = int(content_range[2]) + 1 |
| 350 | total = int(content_range[3]) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 351 | else: |
| 352 | start = 0 |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 353 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 354 | if start: |
| 355 | if not self.fs.file_exists(_id, 'dir'): |
| 356 | raise EngineException("invalid Transaction-Id header", HTTPStatus.NOT_FOUND) |
| 357 | else: |
| 358 | self.fs.file_delete(_id, ignore_non_exist=True) |
| 359 | self.fs.mkdir(_id) |
| 360 | |
| 361 | storage = self.fs.get_params() |
| 362 | storage["folder"] = _id |
| 363 | |
| 364 | file_path = (_id, filename) |
| 365 | if self.fs.file_exists(file_path, 'file'): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 366 | file_size = self.fs.file_size(file_path) |
| 367 | else: |
| 368 | file_size = 0 |
| 369 | if file_size != start: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 370 | raise EngineException("invalid Content-Range start sequence, expected '{}' but received '{}'".format( |
| 371 | file_size, start), HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 372 | file_pkg = self.fs.file_open(file_path, 'a+b') |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 373 | if isinstance(indata, dict): |
| 374 | indata_text = yaml.safe_dump(indata, indent=4, default_flow_style=False) |
| 375 | file_pkg.write(indata_text.encode(encoding="utf-8")) |
| 376 | else: |
| 377 | indata_len = 0 |
| 378 | while True: |
| 379 | indata_text = indata.read(4096) |
| 380 | indata_len += len(indata_text) |
| 381 | if not indata_text: |
| 382 | break |
| 383 | file_pkg.write(indata_text) |
| 384 | if content_range_text: |
| 385 | if indata_len != end-start: |
| 386 | raise EngineException("Mismatch between Content-Range header {}-{} and body length of {}".format( |
| 387 | start, end-1, indata_len), HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE) |
| 388 | if end != total: |
| 389 | # TODO update to UPLOADING |
| 390 | return False |
| 391 | |
| 392 | # PACKAGE UPLOADED |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 393 | if expected_md5: |
| 394 | file_pkg.seek(0, 0) |
| 395 | file_md5 = md5() |
| 396 | chunk_data = file_pkg.read(1024) |
| 397 | while chunk_data: |
| 398 | file_md5.update(chunk_data) |
| 399 | chunk_data = file_pkg.read(1024) |
| 400 | if expected_md5 != file_md5.hexdigest(): |
| 401 | raise EngineException("Error, MD5 mismatch", HTTPStatus.CONFLICT) |
| 402 | file_pkg.seek(0, 0) |
| 403 | if compressed == "gzip": |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 404 | tar = tarfile.open(mode='r', fileobj=file_pkg) |
| 405 | descriptor_file_name = None |
| 406 | for tarinfo in tar: |
| 407 | tarname = tarinfo.name |
| 408 | tarname_path = tarname.split("/") |
| 409 | if not tarname_path[0] or ".." in tarname_path: # if start with "/" means absolute path |
| 410 | raise EngineException("Absolute path or '..' are not allowed for package descriptor tar.gz") |
| 411 | if len(tarname_path) == 1 and not tarinfo.isdir(): |
| 412 | raise EngineException("All files must be inside a dir for package descriptor tar.gz") |
| 413 | if tarname.endswith(".yaml") or tarname.endswith(".json") or tarname.endswith(".yml"): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 414 | storage["pkg-dir"] = tarname_path[0] |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 415 | if len(tarname_path) == 2: |
| 416 | if descriptor_file_name: |
| 417 | raise EngineException("Found more than one descriptor file at package descriptor tar.gz") |
| 418 | descriptor_file_name = tarname |
| 419 | if not descriptor_file_name: |
| 420 | raise EngineException("Not found any descriptor file at package descriptor tar.gz") |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 421 | storage["descriptor"] = descriptor_file_name |
| 422 | storage["zipfile"] = filename |
| 423 | self.fs.file_extract(tar, _id) |
| 424 | with self.fs.file_open((_id, descriptor_file_name), "r") as descriptor_file: |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 425 | content = descriptor_file.read() |
| 426 | else: |
| 427 | content = file_pkg.read() |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 428 | storage["descriptor"] = descriptor_file_name = filename |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 429 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 430 | if descriptor_file_name.endswith(".json"): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 431 | error_text = "Invalid json format " |
| 432 | indata = json.load(content) |
| 433 | else: |
| 434 | error_text = "Invalid yaml format " |
| 435 | indata = yaml.load(content) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 436 | |
| 437 | current_desc["_admin"]["storage"] = storage |
| 438 | current_desc["_admin"]["onboardingState"] = "ONBOARDED" |
| 439 | current_desc["_admin"]["operationalState"] = "ENABLED" |
| 440 | |
| 441 | self._edit_item(session, item, _id, current_desc, indata, kwargs) |
| 442 | # TODO if descriptor has changed because kwargs update content and remove cached zip |
| 443 | # TODO if zip is not present creates one |
| 444 | return True |
| 445 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 446 | except EngineException: |
| 447 | raise |
| 448 | except IndexError: |
| 449 | raise EngineException("invalid Content-Range header format. Expected 'bytes start-end/total'", |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 450 | HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 451 | except IOError as e: |
| 452 | raise EngineException("invalid upload transaction sequence: '{}'".format(e), HTTPStatus.BAD_REQUEST) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 453 | except tarfile.ReadError as e: |
| 454 | raise EngineException("invalid file content {}".format(e), HTTPStatus.BAD_REQUEST) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 455 | except (ValueError, yaml.YAMLError) as e: |
| 456 | raise EngineException(error_text + str(e)) |
| 457 | finally: |
| 458 | if file_pkg: |
| 459 | file_pkg.close() |
| 460 | |
| 461 | def new_nsr(self, session, ns_request): |
| 462 | """ |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 463 | Creates a new nsr into database. It also creates needed vnfrs |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 464 | :param session: contains the used login username and working project |
| 465 | :param ns_request: params to be used for the nsr |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 466 | :return: the _id of nsr descriptor stored at database |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 467 | """ |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 468 | rollback = [] |
| 469 | step = "" |
| 470 | try: |
| 471 | # look for nsr |
| 472 | step = "getting nsd id='{}' from database".format(ns_request.get("nsdId")) |
| 473 | nsd = self.get_item(session, "nsds", ns_request["nsdId"]) |
| 474 | nsr_id = str(uuid4()) |
| 475 | now = time() |
| 476 | step = "filling nsr from input data" |
| 477 | nsr_descriptor = { |
| 478 | "name": ns_request["nsName"], |
| 479 | "name-ref": ns_request["nsName"], |
| 480 | "short-name": ns_request["nsName"], |
| 481 | "admin-status": "ENABLED", |
| 482 | "nsd": nsd, |
| 483 | "datacenter": ns_request["vimAccountId"], |
| 484 | "resource-orchestrator": "osmopenmano", |
| 485 | "description": ns_request.get("nsDescription", ""), |
| 486 | "constituent-vnfr-ref": [], |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 487 | |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 488 | "operational-status": "init", # typedef ns-operational- |
| 489 | "config-status": "init", # typedef config-states |
| 490 | "detailed-status": "scheduled", |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 491 | |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 492 | "orchestration-progress": {}, # {"networks": {"active": 0, "total": 0}, "vms": {"active": 0, "total": 0}}, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 493 | |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 494 | "crete-time": now, |
| 495 | "nsd-name-ref": nsd["name"], |
| 496 | "operational-events": [], # "id", "timestamp", "description", "event", |
| 497 | "nsd-ref": nsd["id"], |
| 498 | "instantiate_params": ns_request, |
| 499 | "ns-instance-config-ref": nsr_id, |
| 500 | "id": nsr_id, |
| 501 | "_id": nsr_id, |
| 502 | # "input-parameter": xpath, value, |
| 503 | "ssh-authorized-key": ns_request.get("key-pair-ref"), |
| 504 | } |
| 505 | ns_request["nsr_id"] = nsr_id |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 506 | |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 507 | # Create VNFR |
| 508 | needed_vnfds = {} |
| 509 | for member_vnf in nsd["constituent-vnfd"]: |
| 510 | vnfd_id = member_vnf["vnfd-id-ref"] |
| 511 | step = "getting vnfd id='{}' constituent-vnfd='{}' from database".format( |
| 512 | member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"]) |
| 513 | if vnfd_id not in needed_vnfds: |
| 514 | # Obtain vnfd |
| 515 | vnf_filter = {"id": vnfd_id} |
| 516 | self._add_read_filter(session, "vnfds", vnf_filter) |
| 517 | vnfd = self.db.get_one("vnfds", vnf_filter) |
| 518 | vnfd.pop("_admin") |
| 519 | needed_vnfds[vnfd_id] = vnfd |
| 520 | else: |
| 521 | vnfd = needed_vnfds[vnfd_id] |
| 522 | step = "filling vnfr vnfd-id='{}' constituent-vnfd='{}'".format( |
| 523 | member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"]) |
| 524 | vnfr_id = str(uuid4()) |
| 525 | vnfr_descriptor = { |
| 526 | "id": vnfr_id, |
| 527 | "_id": vnfr_id, |
| 528 | "nsr-id-ref": nsr_id, |
| 529 | "member-vnf-index-ref": member_vnf["member-vnf-index"], |
| 530 | "created-time": now, |
| 531 | # "vnfd": vnfd, # at OSM model. TODO can it be removed in the future to avoid data duplication? |
| 532 | "vnfd-ref": vnfd_id, |
| 533 | "vnfd-id": vnfr_id, # not at OSM model, but useful |
| 534 | "vim-account-id": None, |
| 535 | "vdur": [], |
| 536 | "connection-point": [], |
| 537 | "ip-address": None, # mgmt-interface filled by LCM |
| 538 | } |
| 539 | for cp in vnfd.get("connection-point", ()): |
| 540 | vnf_cp = { |
| 541 | "name": cp["name"], |
| 542 | "connection-point-id": cp.get("id"), |
| 543 | "id": cp.get("id"), |
| 544 | # "ip-address", "mac-address" # filled by LCM |
| 545 | # vim-id # TODO it would be nice having a vim port id |
| 546 | } |
| 547 | vnfr_descriptor["connection-point"].append(vnf_cp) |
| 548 | for vdu in vnfd["vdu"]: |
| 549 | vdur_id = str(uuid4()) |
| 550 | vdur = { |
| 551 | "id": vdur_id, |
| 552 | "vdu-id-ref": vdu["id"], |
| 553 | "ip-address": None, # mgmt-interface filled by LCM |
| 554 | # "vim-id", "flavor-id", "image-id", "management-ip" # filled by LCM |
| 555 | "internal-connection-point": [], |
| 556 | } |
| 557 | # TODO volumes: name, volume-id |
| 558 | for icp in vdu.get("internal-connection-point", ()): |
| 559 | vdu_icp = { |
| 560 | "id": icp["id"], |
| 561 | "connection-point-id": icp["id"], |
| 562 | "name": icp.get("name"), |
| 563 | # "ip-address", "mac-address" # filled by LCM |
| 564 | # vim-id # TODO it would be nice having a vim port id |
| 565 | } |
| 566 | vdur["internal-connection-point"].append(vdu_icp) |
| 567 | vnfr_descriptor["vdur"].append(vdur) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 568 | |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 569 | step = "creating vnfr vnfd-id='{}' constituent-vnfd='{}' at database".format( |
| 570 | member_vnf["vnfd-id-ref"], member_vnf["member-vnf-index"]) |
| 571 | self._format_new_data(session, "vnfrs", vnfr_descriptor) |
| 572 | self.db.create("vnfrs", vnfr_descriptor) |
| 573 | rollback.append({"session": session, "item": "vnfrs", "_id": vnfr_id, "force": True}) |
| 574 | nsr_descriptor["constituent-vnfr-ref"].append(vnfr_id) |
| 575 | |
| 576 | step = "creating nsr at database" |
| 577 | self._format_new_data(session, "nsrs", nsr_descriptor) |
| 578 | self.db.create("nsrs", nsr_descriptor) |
| 579 | return nsr_id |
| 580 | except Exception as e: |
| 581 | raise EngineException("Error {}: {}".format(step, e)) |
| 582 | for rollback_item in rollback: |
| 583 | try: |
| 584 | self.engine.del_item(**rollback) |
| 585 | except Exception as e2: |
| 586 | self.logger.error("Rollback Exception {}: {}".format(rollback, e2)) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 587 | |
| 588 | @staticmethod |
| 589 | def _update_descriptor(desc, kwargs): |
| 590 | """ |
| 591 | Update descriptor with the kwargs |
| 592 | :param kwargs: |
| 593 | :return: |
| 594 | """ |
| 595 | if not kwargs: |
| 596 | return |
| 597 | try: |
| 598 | for k, v in kwargs.items(): |
| 599 | update_content = content |
| 600 | kitem_old = None |
| 601 | klist = k.split(".") |
| 602 | for kitem in klist: |
| 603 | if kitem_old is not None: |
| 604 | update_content = update_content[kitem_old] |
| 605 | if isinstance(update_content, dict): |
| 606 | kitem_old = kitem |
| 607 | elif isinstance(update_content, list): |
| 608 | kitem_old = int(kitem) |
| 609 | else: |
| 610 | raise EngineException( |
| 611 | "Invalid query string '{}'. Descriptor is not a list nor dict at '{}'".format(k, kitem)) |
| 612 | update_content[kitem_old] = v |
| 613 | except KeyError: |
| 614 | raise EngineException( |
| 615 | "Invalid query string '{}'. Descriptor does not contain '{}'".format(k, kitem_old)) |
| 616 | except ValueError: |
| 617 | raise EngineException("Invalid query string '{}'. Expected integer index list instead of '{}'".format( |
| 618 | k, kitem)) |
| 619 | except IndexError: |
| 620 | raise EngineException( |
| 621 | "Invalid query string '{}'. Index '{}' out of range".format(k, kitem_old)) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 622 | |
| 623 | def new_item(self, session, item, indata={}, kwargs=None, headers={}): |
| 624 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 625 | Creates a new entry into database. For nsds and vnfds it creates an almost empty DISABLED entry, |
| 626 | that must be completed with a call to method upload_content |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 627 | :param session: contains the used login username and working project |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 628 | :param item: it can be: users, projects, vim_accounts, sdns, nsrs, nsds, vnfds |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 629 | :param indata: data to be inserted |
| 630 | :param kwargs: used to override the indata descriptor |
| 631 | :param headers: http request headers |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 632 | :return: _id: identity of the inserted data. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 633 | """ |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 634 | |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 635 | try: |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 636 | item_envelop = item |
| 637 | if item in ("nsds", "vnfds"): |
| 638 | item_envelop = "userDefinedData" |
| 639 | content = self._remove_envelop(item_envelop, indata) |
| 640 | |
| 641 | # Override descriptor with query string kwargs |
| 642 | self._update_descriptor(content, kwargs) |
| 643 | if not indata and item not in ("nsds", "vnfds"): |
| 644 | raise EngineException("Empty payload") |
| 645 | |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 646 | validate_input(content, item, new=True) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 647 | |
| 648 | if item == "nsrs": |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 649 | # in this case the input descriptor is not the data to be stored |
| 650 | return self.new_nsr(session, ns_request=content) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 651 | |
| 652 | self._validate_new_data(session, item_envelop, content) |
| 653 | if item in ("nsds", "vnfds"): |
| 654 | content = {"_admin": {"userDefinedData": content}} |
| 655 | self._format_new_data(session, item, content) |
| 656 | _id = self.db.create(item, content) |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 657 | |
| 658 | if item == "vim_accounts": |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 659 | msg_data = self.db.get_one(item, {"_id": _id}) |
| 660 | msg_data.pop("_admin", None) |
| 661 | self.msg.write("vim_account", "create", msg_data) |
| 662 | elif item == "sdns": |
| 663 | msg_data = self.db.get_one(item, {"_id": _id}) |
| 664 | msg_data.pop("_admin", None) |
| 665 | self.msg.write("sdn", "create", msg_data) |
| 666 | return _id |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 667 | except ValidationError as e: |
| 668 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 669 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 670 | def new_nslcmop(self, session, nsInstanceId, action, params): |
| 671 | now = time() |
| 672 | _id = str(uuid4()) |
| 673 | nslcmop = { |
| 674 | "id": _id, |
| 675 | "_id": _id, |
| 676 | "operationState": "PROCESSING", # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK |
| 677 | "statusEnteredTime": now, |
| 678 | "nsInstanceId": nsInstanceId, |
| 679 | "lcmOperationType": action, |
| 680 | "startTime": now, |
| 681 | "isAutomaticInvocation": False, |
| 682 | "operationParams": params, |
| 683 | "isCancelPending": False, |
| 684 | "links": { |
| 685 | "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id, |
| 686 | "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsInstanceId, |
| 687 | } |
| 688 | } |
| 689 | return nslcmop |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 690 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 691 | def ns_action(self, session, nsInstanceId, action, indata, kwargs=None): |
| 692 | """ |
| 693 | Performs a new action over a ns |
| 694 | :param session: contains the used login username and working project |
| 695 | :param nsInstanceId: _id of the nsr to perform the action |
| 696 | :param action: it can be: instantiate, terminate, action, TODO: update, heal |
| 697 | :param indata: descriptor with the parameters of the action |
| 698 | :param kwargs: used to override the indata descriptor |
| 699 | :return: id of the nslcmops |
| 700 | """ |
| 701 | try: |
| 702 | # Override descriptor with query string kwargs |
| 703 | self._update_descriptor(indata, kwargs) |
| 704 | validate_input(indata, "ns_" + action, new=True) |
| 705 | # get ns from nsr_id |
| 706 | nsr = self.get_item(session, "nsrs", nsInstanceId) |
| tierno | 2102560 | 2018-04-27 14:36:23 +0200 | [diff] [blame] | 707 | if not nsr["_admin"].get("nsState") or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED": |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 708 | if action == "terminate" and indata.get("autoremove"): |
| 709 | # NSR must be deleted |
| 710 | return self.del_item(session, "nsrs", nsInstanceId) |
| 711 | if action != "instantiate": |
| 712 | raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format( |
| 713 | nsInstanceId, action), HTTPStatus.CONFLICT) |
| 714 | else: |
| 715 | if action == "instantiate" and not indata.get("force"): |
| 716 | raise EngineException("ns_instance '{}' cannot be '{}' because it is already instantiated".format( |
| 717 | nsInstanceId, action), HTTPStatus.CONFLICT) |
| 718 | indata["nsInstanceId"] = nsInstanceId |
| 719 | # TODO |
| 720 | nslcmop = self.new_nslcmop(session, nsInstanceId, action, indata) |
| 721 | self._format_new_data(session, "nslcmops", nslcmop) |
| 722 | _id = self.db.create("nslcmops", nslcmop) |
| 723 | indata["_id"] = _id |
| 724 | self.msg.write("ns", action, nslcmop) |
| 725 | return _id |
| 726 | except ValidationError as e: |
| 727 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 728 | # except DbException as e: |
| 729 | # raise EngineException("Cannot get ns_instance '{}': {}".format(e), HTTPStatus.NOT_FOUND) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 730 | |
| 731 | def _add_read_filter(self, session, item, filter): |
| 732 | if session["project_id"] == "admin": # allows all |
| 733 | return filter |
| 734 | if item == "users": |
| 735 | filter["username"] = session["username"] |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 736 | elif item in ("vnfds", "nsds", "nsrs"): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 737 | filter["_admin.projects_read.cont"] = ["ANY", session["project_id"]] |
| 738 | |
| 739 | def _add_delete_filter(self, session, item, filter): |
| 740 | if session["project_id"] != "admin" and item in ("users", "projects"): |
| 741 | raise EngineException("Only admin users can perform this task", http_code=HTTPStatus.FORBIDDEN) |
| 742 | if item == "users": |
| 743 | if filter.get("_id") == session["username"] or filter.get("username") == session["username"]: |
| 744 | raise EngineException("You cannot delete your own user", http_code=HTTPStatus.CONFLICT) |
| 745 | elif item == "project": |
| 746 | if filter.get("_id") == session["project_id"]: |
| 747 | raise EngineException("You cannot delete your own project", http_code=HTTPStatus.CONFLICT) |
| 748 | elif item in ("vnfds", "nsds") and session["project_id"] != "admin": |
| 749 | filter["_admin.projects_write.cont"] = ["ANY", session["project_id"]] |
| 750 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 751 | def get_file(self, session, item, _id, path=None, accept_header=None): |
| 752 | """ |
| 753 | Return the file content of a vnfd or nsd |
| 754 | :param session: contains the used login username and working project |
| 755 | :param item: it can be vnfds or nsds |
| 756 | :param _id: Identity of the vnfd, ndsd |
| 757 | :param path: artifact path or "$DESCRIPTOR" or None |
| 758 | :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain |
| 759 | :return: opened file or raises an exception |
| 760 | """ |
| 761 | accept_text = accept_zip = False |
| 762 | if accept_header: |
| 763 | if 'text/plain' in accept_header or '*/*' in accept_header: |
| 764 | accept_text = True |
| 765 | if 'application/zip' in accept_header or '*/*' in accept_header: |
| 766 | accept_zip = True |
| 767 | if not accept_text and not accept_zip: |
| 768 | raise EngineException("provide request header 'Accept' with 'application/zip' or 'text/plain'", |
| 769 | http_code=HTTPStatus.NOT_ACCEPTABLE) |
| 770 | |
| 771 | content = self.get_item(session, item, _id) |
| 772 | if content["_admin"]["onboardingState"] != "ONBOARDED": |
| 773 | raise EngineException("Cannot get content because this resource is not at 'ONBOARDED' state. " |
| 774 | "onboardingState is {}".format(content["_admin"]["onboardingState"]), |
| 775 | http_code=HTTPStatus.CONFLICT) |
| 776 | storage = content["_admin"]["storage"] |
| 777 | if path is not None and path != "$DESCRIPTOR": # artifacts |
| 778 | if not storage.get('pkg-dir'): |
| 779 | raise EngineException("Packages does not contains artifacts", http_code=HTTPStatus.BAD_REQUEST) |
| 780 | if self.fs.file_exists((storage['folder'], storage['pkg-dir'], *path), 'dir'): |
| 781 | folder_content = self.fs.dir_ls((storage['folder'], storage['pkg-dir'], *path)) |
| 782 | return folder_content, "text/plain" |
| 783 | # TODO manage folders in http |
| 784 | else: |
| 785 | return self.fs.file_open((storage['folder'], storage['pkg-dir'], *path), "rb"), \ |
| 786 | "application/octet-stream" |
| 787 | |
| 788 | # pkgtype accept ZIP TEXT -> result |
| 789 | # manyfiles yes X -> zip |
| 790 | # no yes -> error |
| 791 | # onefile yes no -> zip |
| 792 | # X yes -> text |
| 793 | |
| 794 | if accept_text and (not storage.get('pkg-dir') or path == "$DESCRIPTOR"): |
| 795 | return self.fs.file_open((storage['folder'], storage['descriptor']), "r"), "text/plain" |
| 796 | elif storage.get('pkg-dir') and not accept_zip: |
| 797 | raise EngineException("Packages that contains several files need to be retrieved with 'application/zip'" |
| 798 | "Accept header", http_code=HTTPStatus.NOT_ACCEPTABLE) |
| 799 | else: |
| 800 | if not storage.get('zipfile'): |
| 801 | # TODO generate zipfile if not present |
| 802 | raise EngineException("Only allowed 'text/plain' Accept header for this descriptor. To be solved in future versions" |
| 803 | "", http_code=HTTPStatus.NOT_ACCEPTABLE) |
| 804 | return self.fs.file_open((storage['folder'], storage['zipfile']), "rb"), "application/zip" |
| 805 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 806 | def get_item_list(self, session, item, filter={}): |
| 807 | """ |
| 808 | Get a list of items |
| 809 | :param session: contains the used login username and working project |
| 810 | :param item: it can be: users, projects, vnfds, nsds, ... |
| 811 | :param filter: filter of data to be applied |
| 812 | :return: The list, it can be empty if no one match the filter. |
| 813 | """ |
| 814 | # TODO add admin to filter, validate rights |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 815 | # TODO transform data for SOL005 URL requests. Transform filtering |
| 816 | # TODO implement "field-type" query string SOL005 |
| 817 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 818 | self._add_read_filter(session, item, filter) |
| 819 | return self.db.get_list(item, filter) |
| 820 | |
| 821 | def get_item(self, session, item, _id): |
| 822 | """ |
| 823 | Get complete information on an items |
| 824 | :param session: contains the used login username and working project |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 825 | :param item: it can be: users, projects, vnfds, nsds, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 826 | :param _id: server id of the item |
| 827 | :return: dictionary, raise exception if not found. |
| 828 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 829 | database_item = item |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 830 | filter = {"_id": _id} |
| 831 | # TODO add admin to filter, validate rights |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 832 | # TODO transform data for SOL005 URL requests |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 833 | self._add_read_filter(session, item, filter) |
| 834 | return self.db.get_one(item, filter) |
| 835 | |
| 836 | def del_item_list(self, session, item, filter={}): |
| 837 | """ |
| 838 | Delete a list of items |
| 839 | :param session: contains the used login username and working project |
| 840 | :param item: it can be: users, projects, vnfds, nsds, ... |
| 841 | :param filter: filter of data to be applied |
| 842 | :return: The deleted list, it can be empty if no one match the filter. |
| 843 | """ |
| 844 | # TODO add admin to filter, validate rights |
| 845 | self._add_read_filter(session, item, filter) |
| 846 | return self.db.del_list(item, filter) |
| 847 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 848 | def del_item(self, session, item, _id, force=False): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 849 | """ |
| 850 | Get complete information on an items |
| 851 | :param session: contains the used login username and working project |
| 852 | :param item: it can be: users, projects, vnfds, nsds, ... |
| 853 | :param _id: server id of the item |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 854 | :param force: indicates if deletion must be forced in case of conflict |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 855 | :return: dictionary with deleted item _id. It raises exception if not found. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 856 | """ |
| 857 | # TODO add admin to filter, validate rights |
| 858 | # data = self.get_item(item, _id) |
| 859 | filter = {"_id": _id} |
| 860 | self._add_delete_filter(session, item, filter) |
| 861 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 862 | if item == "nsrs": |
| 863 | nsr = self.db.get_one(item, filter) |
| 864 | if nsr["_admin"]["nsState"] == "INSTANTIATED" and not force: |
| 865 | raise EngineException("nsr '{}' cannot be deleted because it is in 'INSTANTIATED' state. " |
| 866 | "Launch 'terminate' action first; or force deletion".format(_id), |
| 867 | http_code=HTTPStatus.CONFLICT) |
| 868 | v = self.db.del_one(item, {"_id": _id}) |
| 869 | self.db.del_list("nslcmops", {"nsInstanceId": _id}) |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 870 | self.db.del_list("vnfrs", {"nsr-id-ref": _id}) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 871 | self.msg.write("ns", "deleted", {"_id": _id}) |
| 872 | return v |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 873 | if item in ("vim_accounts", "sdns"): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 874 | desc = self.db.get_one(item, filter) |
| 875 | desc["_admin"]["to_delete"] = True |
| 876 | self.db.replace(item, _id, desc) # TODO change to set_one |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 877 | if item == "vim_accounts": |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 878 | self.msg.write("vim_account", "delete", {"_id": _id}) |
| 879 | elif item == "sdns": |
| 880 | self.msg.write("sdn", "delete", {"_id": _id}) |
| 881 | return {"deleted": 1} # TODO indicate an offline operation to return 202 ACCEPTED |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 882 | |
| 883 | v = self.db.del_one(item, filter) |
| 884 | self.fs.file_delete(_id, ignore_non_exist=True) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 885 | return v |
| 886 | |
| 887 | def prune(self): |
| 888 | """ |
| 889 | Prune database not needed content |
| 890 | :return: None |
| 891 | """ |
| 892 | return self.db.del_list("nsrs", {"_admin.to_delete": True}) |
| 893 | |
| 894 | def create_admin(self): |
| 895 | """ |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 896 | Creates a new user admin/admin into database if database is empty. Useful for initialization |
| 897 | :return: _id identity of the inserted data, or None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 898 | """ |
| 899 | users = self.db.get_one("users", fail_on_empty=False, fail_on_more=False) |
| 900 | if users: |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 901 | return None |
| 902 | # raise EngineException("Unauthorized. Database users is not empty", HTTPStatus.UNAUTHORIZED) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 903 | indata = {"username": "admin", "password": "admin", "projects": ["admin"]} |
| 904 | fake_session = {"project_id": "admin", "username": "admin"} |
| 905 | self._format_new_data(fake_session, "users", indata) |
| 906 | _id = self.db.create("users", indata) |
| 907 | return _id |
| 908 | |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 909 | def init_db(self, target_version='1.0'): |
| 910 | """ |
| 911 | Init database if empty. If not empty it checks that database version is ok. |
| 912 | If empty, it creates a new user admin/admin at 'users' and a new entry at 'version' |
| 913 | :return: None if ok, exception if error or if the version is different. |
| 914 | """ |
| tierno | 56ac245 | 2018-04-17 16:06:26 +0200 | [diff] [blame] | 915 | version = self.db.get_one("version", fail_on_empty=False, fail_on_more=False) |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 916 | if not version: |
| 917 | # create user admin |
| 918 | self.create_admin() |
| 919 | # create database version |
| 920 | version_data = { |
| 921 | "_id": '1.0', # version text |
| 922 | "version": 1000, # version number |
| 923 | "date": "2018-04-12", # version date |
| 924 | "description": "initial design", # changes in this version |
| 925 | 'status': 'ENABLED' # ENABLED, DISABLED (migration in process), ERROR, |
| 926 | } |
| 927 | self.db.create("version", version_data) |
| 928 | elif version["_id"] != target_version: |
| 929 | # TODO implement migration process |
| 930 | raise EngineException("Wrong database version '{}'. Expected '{}'".format( |
| 931 | version["_id"], target_version), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 932 | elif version["status"] != 'ENABLED': |
| 933 | raise EngineException("Wrong database status '{}'".format( |
| 934 | version["status"]), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 935 | return |
| 936 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 937 | def _edit_item(self, session, item, id, content, indata={}, kwargs=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 938 | if indata: |
| 939 | indata = self._remove_envelop(item, indata) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 940 | |
| 941 | # Override descriptor with query string kwargs |
| 942 | if kwargs: |
| 943 | try: |
| 944 | for k, v in kwargs.items(): |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 945 | update_content = indata |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 946 | kitem_old = None |
| 947 | klist = k.split(".") |
| 948 | for kitem in klist: |
| 949 | if kitem_old is not None: |
| 950 | update_content = update_content[kitem_old] |
| 951 | if isinstance(update_content, dict): |
| 952 | kitem_old = kitem |
| 953 | elif isinstance(update_content, list): |
| 954 | kitem_old = int(kitem) |
| 955 | else: |
| 956 | raise EngineException( |
| 957 | "Invalid query string '{}'. Descriptor is not a list nor dict at '{}'".format(k, kitem)) |
| 958 | update_content[kitem_old] = v |
| 959 | except KeyError: |
| 960 | raise EngineException( |
| 961 | "Invalid query string '{}'. Descriptor does not contain '{}'".format(k, kitem_old)) |
| 962 | except ValueError: |
| 963 | raise EngineException("Invalid query string '{}'. Expected integer index list instead of '{}'".format( |
| 964 | k, kitem)) |
| 965 | except IndexError: |
| 966 | raise EngineException( |
| 967 | "Invalid query string '{}'. Index '{}' out of range".format(k, kitem_old)) |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 968 | try: |
| 969 | validate_input(content, item, new=False) |
| 970 | except ValidationError as e: |
| 971 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 972 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 973 | _deep_update(content, indata) |
| 974 | self._validate_new_data(session, item, content, id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 975 | # self._format_new_data(session, item, content) |
| 976 | self.db.replace(item, id, content) |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 977 | if item in ("vim_accounts", "sdns"): |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 978 | indata.pop("_admin", None) |
| 979 | indata["_id"] = id |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 980 | if item == "vim_accounts": |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 981 | self.msg.write("vim_account", "edit", indata) |
| 982 | elif item == "sdns": |
| 983 | self.msg.write("sdn", "edit", indata) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 984 | return id |
| 985 | |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 986 | def edit_item(self, session, item, _id, indata={}, kwargs=None): |
| 987 | """ |
| 988 | Update an existing entry at database |
| 989 | :param session: contains the used login username and working project |
| 990 | :param item: it can be: users, projects, vnfds, nsds, ... |
| 991 | :param _id: identifier to be updated |
| 992 | :param indata: data to be inserted |
| 993 | :param kwargs: used to override the indata descriptor |
| 994 | :return: dictionary, raise exception if not found. |
| 995 | """ |
| 996 | |
| 997 | content = self.get_item(session, item, _id) |
| 998 | return self._edit_item(session, item, _id, content, indata, kwargs) |
| 999 | |