| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [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 | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 16 | import logging |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 17 | from osm_common import dbmongo, dbmemory, fslocal, msglocal, msgkafka, version as common_version |
| 18 | from osm_common.dbbase import DbException |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame] | 19 | from osm_common.fsbase import FsException |
| 20 | from osm_common.msgbase import MsgException |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 21 | from http import HTTPStatus |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 22 | from base_topic import EngineException, versiontuple |
| 23 | from admin_topics import UserTopic, ProjectTopic, VimAccountTopic, SdnTopic |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 24 | from descriptor_topics import VnfdTopic, NsdTopic, PduTopic, NstTopic |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 25 | from instance_topics import NsrTopic, VnfrTopic, NsLcmOpTopic, NsiTopic, NsiLcmOpTopic |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 26 | from base64 import b64encode |
| 27 | from os import urandom |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 28 | |
| 29 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 30 | min_common_version = "0.1.8" |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 31 | |
| 32 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 33 | class Engine(object): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 34 | map_from_topic_to_class = { |
| 35 | "vnfds": VnfdTopic, |
| 36 | "nsds": NsdTopic, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 37 | "nsts": NstTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 38 | "pdus": PduTopic, |
| 39 | "nsrs": NsrTopic, |
| 40 | "vnfrs": VnfrTopic, |
| 41 | "nslcmops": NsLcmOpTopic, |
| 42 | "vim_accounts": VimAccountTopic, |
| 43 | "sdns": SdnTopic, |
| 44 | "users": UserTopic, |
| 45 | "projects": ProjectTopic, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 46 | "nsis": NsiTopic, |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 47 | "nsilcmops": NsiLcmOpTopic |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 48 | # [NEW_TOPIC]: add an entry here |
| 49 | } |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 50 | |
| 51 | def __init__(self): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 52 | self.db = None |
| 53 | self.fs = None |
| 54 | self.msg = None |
| 55 | self.config = None |
| 56 | self.logger = logging.getLogger("nbi.engine") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 57 | self.map_topic = {} |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 58 | |
| 59 | def start(self, config): |
| 60 | """ |
| 61 | Connect to database, filesystem storage, and messaging |
| 62 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 63 | :return: None |
| 64 | """ |
| 65 | self.config = config |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 66 | # check right version of common |
| 67 | if versiontuple(common_version) < versiontuple(min_common_version): |
| 68 | raise EngineException("Not compatible osm/common version '{}'. Needed '{}' or higher".format( |
| 69 | common_version, min_common_version)) |
| 70 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 71 | try: |
| 72 | if not self.db: |
| 73 | if config["database"]["driver"] == "mongo": |
| 74 | self.db = dbmongo.DbMongo() |
| 75 | self.db.db_connect(config["database"]) |
| 76 | elif config["database"]["driver"] == "memory": |
| 77 | self.db = dbmemory.DbMemory() |
| 78 | self.db.db_connect(config["database"]) |
| 79 | else: |
| 80 | raise EngineException("Invalid configuration param '{}' at '[database]':'driver'".format( |
| 81 | config["database"]["driver"])) |
| 82 | if not self.fs: |
| 83 | if config["storage"]["driver"] == "local": |
| 84 | self.fs = fslocal.FsLocal() |
| 85 | self.fs.fs_connect(config["storage"]) |
| 86 | else: |
| 87 | raise EngineException("Invalid configuration param '{}' at '[storage]':'driver'".format( |
| 88 | config["storage"]["driver"])) |
| 89 | if not self.msg: |
| 90 | if config["message"]["driver"] == "local": |
| 91 | self.msg = msglocal.MsgLocal() |
| 92 | self.msg.connect(config["message"]) |
| 93 | elif config["message"]["driver"] == "kafka": |
| 94 | self.msg = msgkafka.MsgKafka() |
| 95 | self.msg.connect(config["message"]) |
| 96 | else: |
| 97 | raise EngineException("Invalid configuration param '{}' at '[message]':'driver'".format( |
| 98 | config["storage"]["driver"])) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 99 | |
| 100 | # create one class per topic |
| 101 | for topic, topic_class in self.map_from_topic_to_class.items(): |
| 102 | self.map_topic[topic] = topic_class(self.db, self.fs, self.msg) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 103 | except (DbException, FsException, MsgException) as e: |
| 104 | raise EngineException(str(e), http_code=e.http_code) |
| 105 | |
| 106 | def stop(self): |
| 107 | try: |
| 108 | if self.db: |
| 109 | self.db.db_disconnect() |
| 110 | if self.fs: |
| 111 | self.fs.fs_disconnect() |
| 112 | if self.fs: |
| 113 | self.fs.fs_disconnect() |
| 114 | except (DbException, FsException, MsgException) as e: |
| 115 | raise EngineException(str(e), http_code=e.http_code) |
| 116 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 117 | def new_item(self, rollback, session, topic, indata=None, kwargs=None, headers=None, force=False): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 118 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 119 | Creates a new entry into database. For nsds and vnfds it creates an almost empty DISABLED entry, |
| 120 | that must be completed with a call to method upload_content |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 121 | :param rollback: list to append created items at database in case a rollback must to be done |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 122 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 123 | :param topic: it can be: users, projects, vim_accounts, sdns, nsrs, nsds, vnfds |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 124 | :param indata: data to be inserted |
| 125 | :param kwargs: used to override the indata descriptor |
| 126 | :param headers: http request headers |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 127 | :param force: If True avoid some dependence checks |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 128 | :return: _id: identity of the inserted data. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 129 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 130 | if topic not in self.map_topic: |
| 131 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 132 | return self.map_topic[topic].new(rollback, session, indata, kwargs, headers, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 133 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 134 | def upload_content(self, session, topic, _id, indata, kwargs, headers, force=False): |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 135 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 136 | Upload content for an already created entry (_id) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 137 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 138 | :param topic: it can be: users, projects, vnfds, nsds, |
| 139 | :param _id: server id of the item |
| 140 | :param indata: data to be inserted |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 141 | :param kwargs: used to override the indata descriptor |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 142 | :param headers: http request headers |
| 143 | :param force: If True avoid some dependence checks |
| 144 | :return: _id: identity of the inserted data. |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 145 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 146 | if topic not in self.map_topic: |
| 147 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 148 | return self.map_topic[topic].upload_content(session, _id, indata, kwargs, headers, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 149 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 150 | def get_item_list(self, session, topic, filter_q=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 151 | """ |
| 152 | Get a list of items |
| 153 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 154 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 155 | :param filter_q: filter of data to be applied |
| 156 | :return: The list, it can be empty if no one match the filter_q. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 157 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 158 | if topic not in self.map_topic: |
| 159 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 160 | return self.map_topic[topic].list(session, filter_q) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 161 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 162 | def get_item(self, session, topic, _id): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 163 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 164 | Get complete information on an item |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 165 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 166 | :param topic: it can be: users, projects, vnfds, nsds, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 167 | :param _id: server id of the item |
| 168 | :return: dictionary, raise exception if not found. |
| 169 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 170 | if topic not in self.map_topic: |
| 171 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 172 | return self.map_topic[topic].show(session, _id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 173 | |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 174 | def get_file(self, session, topic, _id, path=None, accept_header=None): |
| 175 | """ |
| 176 | Get descriptor package or artifact file content |
| 177 | :param session: contains the used login username and working project |
| 178 | :param topic: it can be: users, projects, vnfds, nsds, |
| 179 | :param _id: server id of the item |
| 180 | :param path: artifact path or "$DESCRIPTOR" or None |
| 181 | :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain |
| 182 | :return: opened file plus Accept format or raises an exception |
| 183 | """ |
| 184 | if topic not in self.map_topic: |
| 185 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 186 | return self.map_topic[topic].get_file(session, _id, path, accept_header) |
| 187 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 188 | def del_item_list(self, session, topic, _filter=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 189 | """ |
| 190 | Delete a list of items |
| 191 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 192 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 193 | :param _filter: filter of data to be applied |
| 194 | :return: The deleted list, it can be empty if no one match the _filter. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 195 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 196 | if topic not in self.map_topic: |
| 197 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 198 | return self.map_topic[topic].delete_list(session, _filter) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 199 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 200 | def del_item(self, session, topic, _id, force=False): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 201 | """ |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 202 | Delete item by its internal id |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 203 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 204 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 205 | :param _id: server id of the item |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 206 | :param force: indicates if deletion must be forced in case of conflict |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 207 | :return: dictionary with deleted item _id. It raises exception if not found. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 208 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 209 | if topic not in self.map_topic: |
| 210 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 211 | return self.map_topic[topic].delete(session, _id, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 212 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 213 | def edit_item(self, session, topic, _id, indata=None, kwargs=None, force=False): |
| 214 | """ |
| 215 | Update an existing entry at database |
| 216 | :param session: contains the used login username and working project |
| 217 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 218 | :param _id: identifier to be updated |
| 219 | :param indata: data to be inserted |
| 220 | :param kwargs: used to override the indata descriptor |
| 221 | :param force: If True avoid some dependence checks |
| 222 | :return: dictionary, raise exception if not found. |
| 223 | """ |
| 224 | if topic not in self.map_topic: |
| 225 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 226 | return self.map_topic[topic].edit(session, _id, indata, kwargs, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 227 | |
| 228 | def prune(self): |
| 229 | """ |
| 230 | Prune database not needed content |
| 231 | :return: None |
| 232 | """ |
| 233 | return self.db.del_list("nsrs", {"_admin.to_delete": True}) |
| 234 | |
| 235 | def create_admin(self): |
| 236 | """ |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 237 | Creates a new user admin/admin into database if database is empty. Useful for initialization |
| 238 | :return: _id identity of the inserted data, or None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 239 | """ |
| 240 | users = self.db.get_one("users", fail_on_empty=False, fail_on_more=False) |
| 241 | if users: |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 242 | return None |
| 243 | # raise EngineException("Unauthorized. Database users is not empty", HTTPStatus.UNAUTHORIZED) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 244 | user_desc = {"username": "admin", "password": "admin", "projects": ["admin"]} |
| 245 | fake_session = {"project_id": "admin", "username": "admin", "admin": True} |
| 246 | roolback_list = [] |
| 247 | _id = self.map_topic["users"].new(roolback_list, fake_session, user_desc, force=True) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 248 | return _id |
| 249 | |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 250 | def upgrade_db(self, current_version, target_version): |
| 251 | if not target_version or current_version == target_version: |
| 252 | return |
| 253 | if target_version == '1.0': |
| 254 | if not current_version: |
| 255 | # create database version |
| 256 | serial = urandom(32) |
| 257 | version_data = { |
| 258 | "_id": 'version', # Always 'version' |
| 259 | "version_int": 1000, # version number |
| 260 | "version": '1.0', # version text |
| 261 | "date": "2018-10-25", # version date |
| 262 | "description": "added serial", # changes in this version |
| 263 | 'status': 'ENABLED', # ENABLED, DISABLED (migration in process), ERROR, |
| 264 | 'serial': b64encode(serial) |
| 265 | } |
| 266 | self.db.create("admin", version_data) |
| 267 | self.db.set_secret_key(serial) |
| 268 | # TODO add future migrations here |
| 269 | |
| 270 | raise EngineException("Wrong database version '{}'. Expected '{}'" |
| 271 | ". It cannot be up/down-grade".format(current_version, target_version), |
| 272 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 273 | |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 274 | def init_db(self, target_version='1.0'): |
| 275 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 276 | Init database if empty. If not empty it checks that database version and migrates if needed |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 277 | If empty, it creates a new user admin/admin at 'users' and a new entry at 'version' |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 278 | :param target_version: check desired database version. Migrate to it if possible or raises exception |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 279 | :return: None if ok, exception if error or if the version is different. |
| 280 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 281 | |
| 282 | version_data = self.db.get_one("admin", {"_id": "version"}, fail_on_empty=False, fail_on_more=True) |
| 283 | # check database status is ok |
| 284 | if version_data and version_data.get("status") != 'ENABLED': |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 285 | raise EngineException("Wrong database status '{}'".format( |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 286 | version_data["status"]), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 287 | |
| 288 | # check version |
| 289 | db_version = None if not version_data else version_data.get("version") |
| 290 | if db_version != target_version: |
| 291 | self.upgrade_db(db_version, target_version) |
| 292 | |
| 293 | # create user admin if not exist |
| 294 | self.create_admin() |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 295 | return |