| 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 |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 17 | import yaml |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 18 | from osm_common import dbmongo, dbmemory, fslocal, msglocal, msgkafka, version as common_version |
| 19 | from osm_common.dbbase import DbException |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame] | 20 | from osm_common.fsbase import FsException |
| 21 | from osm_common.msgbase import MsgException |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 22 | from http import HTTPStatus |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 23 | |
| 24 | from authconn_keystone import AuthconnKeystone |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 25 | from authconn_internal import AuthconnInternal |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 26 | from base_topic import EngineException, versiontuple |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 27 | from admin_topics import VimAccountTopic, WimAccountTopic, SdnTopic |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 28 | from admin_topics import UserTopicAuth, ProjectTopicAuth, RoleTopicAuth |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 29 | from descriptor_topics import VnfdTopic, NsdTopic, PduTopic, NstTopic |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 30 | from instance_topics import NsrTopic, VnfrTopic, NsLcmOpTopic, NsiTopic, NsiLcmOpTopic |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 31 | from pmjobs_topics import PmJobsTopic |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 32 | from base64 import b64encode |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 33 | from os import urandom, path |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 34 | from threading import Lock |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 35 | |
| 36 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 37 | min_common_version = "0.1.16" |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 38 | |
| 39 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 40 | class Engine(object): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 41 | map_from_topic_to_class = { |
| 42 | "vnfds": VnfdTopic, |
| 43 | "nsds": NsdTopic, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 44 | "nsts": NstTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 45 | "pdus": PduTopic, |
| 46 | "nsrs": NsrTopic, |
| 47 | "vnfrs": VnfrTopic, |
| 48 | "nslcmops": NsLcmOpTopic, |
| 49 | "vim_accounts": VimAccountTopic, |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 50 | "wim_accounts": WimAccountTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 51 | "sdns": SdnTopic, |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 52 | "users": UserTopicAuth, # Valid for both internal and keystone authentication backends |
| 53 | "projects": ProjectTopicAuth, # Valid for both internal and keystone authentication backends |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 54 | "roles": RoleTopicAuth, # Valid for both internal and keystone authentication backends |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 55 | "nsis": NsiTopic, |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 56 | "nsilcmops": NsiLcmOpTopic |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 57 | # [NEW_TOPIC]: add an entry here |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 58 | # "pm_jobs": PmJobsTopic will be added manually because it needs other parameters |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 59 | } |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 60 | |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 61 | map_target_version_to_int = { |
| 62 | "1.0": 1000, |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 63 | "1.1": 1001, |
| 64 | "1.2": 1002, |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 65 | # Add new versions here |
| 66 | } |
| 67 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 68 | def __init__(self): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 69 | self.db = None |
| 70 | self.fs = None |
| 71 | self.msg = None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 72 | self.auth = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 73 | self.config = None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 74 | self.operations = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 75 | self.logger = logging.getLogger("nbi.engine") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 76 | self.map_topic = {} |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 77 | self.write_lock = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 78 | |
| 79 | def start(self, config): |
| 80 | """ |
| 81 | Connect to database, filesystem storage, and messaging |
| 82 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 83 | :return: None |
| 84 | """ |
| 85 | self.config = config |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 86 | # check right version of common |
| 87 | if versiontuple(common_version) < versiontuple(min_common_version): |
| 88 | raise EngineException("Not compatible osm/common version '{}'. Needed '{}' or higher".format( |
| 89 | common_version, min_common_version)) |
| 90 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 91 | try: |
| 92 | if not self.db: |
| 93 | if config["database"]["driver"] == "mongo": |
| 94 | self.db = dbmongo.DbMongo() |
| 95 | self.db.db_connect(config["database"]) |
| 96 | elif config["database"]["driver"] == "memory": |
| 97 | self.db = dbmemory.DbMemory() |
| 98 | self.db.db_connect(config["database"]) |
| 99 | else: |
| 100 | raise EngineException("Invalid configuration param '{}' at '[database]':'driver'".format( |
| 101 | config["database"]["driver"])) |
| 102 | if not self.fs: |
| 103 | if config["storage"]["driver"] == "local": |
| 104 | self.fs = fslocal.FsLocal() |
| 105 | self.fs.fs_connect(config["storage"]) |
| 106 | else: |
| 107 | raise EngineException("Invalid configuration param '{}' at '[storage]':'driver'".format( |
| 108 | config["storage"]["driver"])) |
| 109 | if not self.msg: |
| 110 | if config["message"]["driver"] == "local": |
| 111 | self.msg = msglocal.MsgLocal() |
| 112 | self.msg.connect(config["message"]) |
| 113 | elif config["message"]["driver"] == "kafka": |
| 114 | self.msg = msgkafka.MsgKafka() |
| 115 | self.msg.connect(config["message"]) |
| 116 | else: |
| 117 | raise EngineException("Invalid configuration param '{}' at '[message]':'driver'".format( |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 118 | config["message"]["driver"])) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 119 | if not self.auth: |
| 120 | if config["authentication"]["backend"] == "keystone": |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 121 | self.auth = AuthconnKeystone(config["authentication"], self.db, None) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 122 | else: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 123 | self.auth = AuthconnInternal(config["authentication"], self.db, dict()) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 124 | if not self.operations: |
| 125 | if "resources_to_operations" in config["rbac"]: |
| 126 | resources_to_operations_file = config["rbac"]["resources_to_operations"] |
| 127 | else: |
| 128 | possible_paths = ( |
| 129 | __file__[:__file__.rfind("engine.py")] + "resources_to_operations.yml", |
| 130 | "./resources_to_operations.yml" |
| 131 | ) |
| 132 | for config_file in possible_paths: |
| 133 | if path.isfile(config_file): |
| 134 | resources_to_operations_file = config_file |
| 135 | break |
| Eduardo Sousa | a519a96 | 2019-06-06 15:00:50 +0100 | [diff] [blame] | 136 | if not resources_to_operations_file: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 137 | raise EngineException("Invalid permission configuration: resources_to_operations file missing") |
| Eduardo Sousa | a519a96 | 2019-06-06 15:00:50 +0100 | [diff] [blame] | 138 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 139 | with open(resources_to_operations_file, 'r') as f: |
| 140 | resources_to_operations = yaml.load(f) |
| Eduardo Sousa | a519a96 | 2019-06-06 15:00:50 +0100 | [diff] [blame] | 141 | |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 142 | self.operations = [] |
| 143 | |
| 144 | for _, value in resources_to_operations["resources_to_operations"].items(): |
| 145 | if value not in self.operations: |
| Eduardo Sousa | c5a1889 | 2019-06-06 14:51:23 +0100 | [diff] [blame] | 146 | self.operations += [value] |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 147 | |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 148 | self.write_lock = Lock() |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 149 | # create one class per topic |
| 150 | for topic, topic_class in self.map_from_topic_to_class.items(): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 151 | if self.auth and topic_class in (UserTopicAuth, ProjectTopicAuth): |
| 152 | self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth) |
| 153 | elif self.auth and topic_class == RoleTopicAuth: |
| 154 | self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth, |
| 155 | self.operations) |
| 156 | else: |
| 157 | self.map_topic[topic] = topic_class(self.db, self.fs, self.msg) |
| Eduardo Sousa | 225200d | 2019-05-22 15:57:17 +0100 | [diff] [blame] | 158 | |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 159 | self.map_topic["pm_jobs"] = PmJobsTopic(config["prometheus"].get("host"), config["prometheus"].get("port")) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 160 | except (DbException, FsException, MsgException) as e: |
| 161 | raise EngineException(str(e), http_code=e.http_code) |
| 162 | |
| 163 | def stop(self): |
| 164 | try: |
| 165 | if self.db: |
| 166 | self.db.db_disconnect() |
| 167 | if self.fs: |
| 168 | self.fs.fs_disconnect() |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 169 | if self.msg: |
| 170 | self.msg.disconnect() |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 171 | self.write_lock = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 172 | except (DbException, FsException, MsgException) as e: |
| 173 | raise EngineException(str(e), http_code=e.http_code) |
| 174 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 175 | def new_item(self, rollback, session, topic, indata=None, kwargs=None, headers=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 176 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 177 | Creates a new entry into database. For nsds and vnfds it creates an almost empty DISABLED entry, |
| 178 | that must be completed with a call to method upload_content |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 179 | :param rollback: list to append created items at database in case a rollback must to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 180 | :param session: contains the used login username and working project, force to avoid checkins, public |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 181 | :param topic: it can be: users, projects, vim_accounts, sdns, nsrs, nsds, vnfds |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 182 | :param indata: data to be inserted |
| 183 | :param kwargs: used to override the indata descriptor |
| 184 | :param headers: http request headers |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 185 | :return: _id: identity of the inserted data. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 186 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 187 | if topic not in self.map_topic: |
| 188 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 189 | with self.write_lock: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 190 | return self.map_topic[topic].new(rollback, session, indata, kwargs, headers) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 191 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 192 | def upload_content(self, session, topic, _id, indata, kwargs, headers): |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 193 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 194 | Upload content for an already created entry (_id) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 195 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 196 | :param topic: it can be: users, projects, vnfds, nsds, |
| 197 | :param _id: server id of the item |
| 198 | :param indata: data to be inserted |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 199 | :param kwargs: used to override the indata descriptor |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 200 | :param headers: http request headers |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 201 | :return: _id: identity of the inserted data. |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 202 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 203 | if topic not in self.map_topic: |
| 204 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 205 | with self.write_lock: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 206 | return self.map_topic[topic].upload_content(session, _id, indata, kwargs, headers) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 207 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 208 | def get_item_list(self, session, topic, filter_q=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 209 | """ |
| 210 | Get a list of items |
| 211 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 212 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 213 | :param filter_q: filter of data to be applied |
| 214 | :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] | 215 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 216 | if topic not in self.map_topic: |
| 217 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 218 | return self.map_topic[topic].list(session, filter_q) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 219 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 220 | def get_item(self, session, topic, _id): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 221 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 222 | Get complete information on an item |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 223 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 224 | :param topic: it can be: users, projects, vnfds, nsds, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 225 | :param _id: server id of the item |
| 226 | :return: dictionary, raise exception if not found. |
| 227 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 228 | if topic not in self.map_topic: |
| 229 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 230 | return self.map_topic[topic].show(session, _id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 231 | |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 232 | def get_file(self, session, topic, _id, path=None, accept_header=None): |
| 233 | """ |
| 234 | Get descriptor package or artifact file content |
| 235 | :param session: contains the used login username and working project |
| 236 | :param topic: it can be: users, projects, vnfds, nsds, |
| 237 | :param _id: server id of the item |
| 238 | :param path: artifact path or "$DESCRIPTOR" or None |
| 239 | :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain |
| 240 | :return: opened file plus Accept format or raises an exception |
| 241 | """ |
| 242 | if topic not in self.map_topic: |
| 243 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 244 | return self.map_topic[topic].get_file(session, _id, path, accept_header) |
| 245 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 246 | def del_item_list(self, session, topic, _filter=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 247 | """ |
| 248 | Delete a list of items |
| 249 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 250 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 251 | :param _filter: filter of data to be applied |
| 252 | :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] | 253 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 254 | if topic not in self.map_topic: |
| 255 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 256 | with self.write_lock: |
| 257 | return self.map_topic[topic].delete_list(session, _filter) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 258 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 259 | def del_item(self, session, topic, _id): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 260 | """ |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 261 | Delete item by its internal id |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 262 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 263 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 264 | :param _id: server id of the item |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 265 | :return: dictionary with deleted item _id. It raises exception if not found. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 266 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 267 | if topic not in self.map_topic: |
| 268 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 269 | with self.write_lock: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 270 | return self.map_topic[topic].delete(session, _id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 271 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 272 | def edit_item(self, session, topic, _id, indata=None, kwargs=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 273 | """ |
| 274 | Update an existing entry at database |
| 275 | :param session: contains the used login username and working project |
| 276 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 277 | :param _id: identifier to be updated |
| 278 | :param indata: data to be inserted |
| 279 | :param kwargs: used to override the indata descriptor |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 280 | :return: dictionary with edited item _id, raise exception if not found. |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 281 | """ |
| 282 | if topic not in self.map_topic: |
| 283 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 284 | with self.write_lock: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 285 | return self.map_topic[topic].edit(session, _id, indata, kwargs) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 286 | |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 287 | def upgrade_db(self, current_version, target_version): |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 288 | if target_version not in self.map_target_version_to_int.keys(): |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 289 | raise EngineException("Cannot upgrade to version '{}' with this version of code".format(target_version), |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 290 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 291 | |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 292 | if current_version == target_version: |
| 293 | return |
| 294 | |
| 295 | target_version_int = self.map_target_version_to_int[target_version] |
| 296 | |
| 297 | if not current_version: |
| 298 | # create database version |
| 299 | serial = urandom(32) |
| 300 | version_data = { |
| 301 | "_id": "version", # Always "version" |
| 302 | "version_int": 1000, # version number |
| 303 | "version": "1.0", # version text |
| 304 | "date": "2018-10-25", # version date |
| 305 | "description": "added serial", # changes in this version |
| 306 | 'status': "ENABLED", # ENABLED, DISABLED (migration in process), ERROR, |
| 307 | 'serial': b64encode(serial) |
| 308 | } |
| 309 | self.db.create("admin", version_data) |
| 310 | self.db.set_secret_key(serial) |
| 311 | current_version = "1.0" |
| 312 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 313 | if current_version in ("1.0", "1.1") and target_version_int >= self.map_target_version_to_int["1.2"]: |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame^] | 314 | if self.config['authentication']['backend'] == "internal": |
| 315 | self.db.del_list("roles") |
| 316 | |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 317 | version_data = { |
| 318 | "_id": "version", |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 319 | "version_int": 1002, |
| 320 | "version": "1.2", |
| 321 | "date": "2019-06-11", |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 322 | "description": "set new format for roles_operations" |
| 323 | } |
| 324 | |
| 325 | self.db.set_one("admin", {"_id": "version"}, version_data) |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 326 | current_version = "1.2" |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 327 | # TODO add future migrations here |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 328 | |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 329 | def init_db(self, target_version='1.0'): |
| 330 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 331 | 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] | 332 | 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] | 333 | :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] | 334 | :return: None if ok, exception if error or if the version is different. |
| 335 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 336 | |
| 337 | version_data = self.db.get_one("admin", {"_id": "version"}, fail_on_empty=False, fail_on_more=True) |
| 338 | # check database status is ok |
| 339 | if version_data and version_data.get("status") != 'ENABLED': |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 340 | raise EngineException("Wrong database status '{}'".format( |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 341 | version_data["status"]), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 342 | |
| 343 | # check version |
| 344 | db_version = None if not version_data else version_data.get("version") |
| 345 | if db_version != target_version: |
| 346 | self.upgrade_db(db_version, target_version) |
| 347 | |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 348 | return |