| 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 |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 17 | |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 18 | # import yaml |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 19 | from osm_common import ( |
| 20 | dbmongo, |
| 21 | dbmemory, |
| 22 | fslocal, |
| 23 | fsmongo, |
| 24 | msglocal, |
| 25 | msgkafka, |
| 26 | version as common_version, |
| 27 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 28 | from osm_common.dbbase import DbException |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame] | 29 | from osm_common.fsbase import FsException |
| 30 | from osm_common.msgbase import MsgException |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 31 | from http import HTTPStatus |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 32 | |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 33 | from osm_nbi.authconn_keystone import AuthconnKeystone |
| 34 | from osm_nbi.authconn_internal import AuthconnInternal |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 35 | from osm_nbi.authconn_tacacs import AuthconnTacacs |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 36 | from osm_nbi.base_topic import EngineException, versiontuple |
| 37 | from osm_nbi.admin_topics import VimAccountTopic, WimAccountTopic, SdnTopic |
| Felipe Vicens | b66b041 | 2020-05-06 10:11:00 +0200 | [diff] [blame] | 38 | from osm_nbi.admin_topics import K8sClusterTopic, K8sRepoTopic, OsmRepoTopic |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 39 | from osm_nbi.admin_topics import VcaTopic |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 40 | from osm_nbi.admin_topics import UserTopicAuth, ProjectTopicAuth, RoleTopicAuth |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 41 | from osm_nbi.descriptor_topics import ( |
| 42 | VnfdTopic, |
| 43 | NsdTopic, |
| 44 | PduTopic, |
| 45 | NstTopic, |
| 46 | VnfPkgOpTopic, |
| kayal2001 | f71c2e8 | 2024-06-25 15:26:24 +0530 | [diff] [blame] | 47 | NsConfigTemplateTopic, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 48 | ) |
| 49 | from osm_nbi.instance_topics import ( |
| 50 | NsrTopic, |
| 51 | VnfrTopic, |
| 52 | NsLcmOpTopic, |
| 53 | NsiTopic, |
| 54 | NsiLcmOpTopic, |
| 55 | ) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 56 | from osm_nbi.k8s_topics import ( |
| shrinithi | 28d887f | 2025-01-08 05:27:19 +0000 | [diff] [blame] | 57 | ClusterTopic, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 58 | InfraContTopic, |
| 59 | InfraConfTopic, |
| garciadeblas | b798f45 | 2025-08-05 18:21:26 +0200 | [diff] [blame] | 60 | AppProfileTopic, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 61 | ResourceTopic, |
| shrinithi | 28d887f | 2025-01-08 05:27:19 +0000 | [diff] [blame] | 62 | ClusterOpsTopic, |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 63 | KsusTopic, |
| garciadeblas | b798f45 | 2025-08-05 18:21:26 +0200 | [diff] [blame] | 64 | AppInstanceTopic, |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 65 | OkaTopic, |
| yshah | d23c6a5 | 2025-06-13 05:49:31 +0000 | [diff] [blame] | 66 | NodeGroupTopic, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 67 | ) |
| almagia | e47b913 | 2022-05-17 14:12:22 +0200 | [diff] [blame] | 68 | from osm_nbi.vnf_instance_topics import VnfInstances, VnfLcmOpTopic |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 69 | from osm_nbi.pmjobs_topics import PmJobsTopic |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 70 | from osm_nbi.subscription_topics import NslcmSubscriptionsTopic |
| selvi.j | f100459 | 2022-04-29 05:42:35 +0000 | [diff] [blame] | 71 | from osm_nbi.osm_vnfm.vnf_subscription import VnflcmSubscriptionsTopic |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 72 | from base64 import b64encode |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 73 | from os import urandom # , path |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 74 | from threading import Lock |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 75 | |
| 76 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 77 | min_common_version = "0.1.16" |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 78 | |
| 79 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 80 | class Engine(object): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 81 | map_from_topic_to_class = { |
| 82 | "vnfds": VnfdTopic, |
| 83 | "nsds": NsdTopic, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 84 | "nsts": NstTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 85 | "pdus": PduTopic, |
| 86 | "nsrs": NsrTopic, |
| 87 | "vnfrs": VnfrTopic, |
| 88 | "nslcmops": NsLcmOpTopic, |
| 89 | "vim_accounts": VimAccountTopic, |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 90 | "wim_accounts": WimAccountTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 91 | "sdns": SdnTopic, |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 92 | "k8sclusters": K8sClusterTopic, |
| David Garcia | ecb4132 | 2021-03-31 19:10:46 +0200 | [diff] [blame] | 93 | "vca": VcaTopic, |
| delacruzramo | fe598fe | 2019-10-23 18:25:11 +0200 | [diff] [blame] | 94 | "k8srepos": K8sRepoTopic, |
| Felipe Vicens | b66b041 | 2020-05-06 10:11:00 +0200 | [diff] [blame] | 95 | "osmrepos": OsmRepoTopic, |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 96 | "users": UserTopicAuth, # Valid for both internal and keystone authentication backends |
| 97 | "projects": ProjectTopicAuth, # Valid for both internal and keystone authentication backends |
| 98 | "roles": RoleTopicAuth, # Valid for both internal and keystone authentication backends |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 99 | "nsis": NsiTopic, |
| delacruzramo | 271d200 | 2019-12-02 21:00:37 +0100 | [diff] [blame] | 100 | "nsilcmops": NsiLcmOpTopic, |
| 101 | "vnfpkgops": VnfPkgOpTopic, |
| preethika.p | 329b818 | 2020-04-22 12:25:39 +0530 | [diff] [blame] | 102 | "nslcm_subscriptions": NslcmSubscriptionsTopic, |
| almagia | e47b913 | 2022-05-17 14:12:22 +0200 | [diff] [blame] | 103 | "vnf_instances": VnfInstances, |
| 104 | "vnflcmops": VnfLcmOpTopic, |
| selvi.j | f100459 | 2022-04-29 05:42:35 +0000 | [diff] [blame] | 105 | "vnflcm_subscriptions": VnflcmSubscriptionsTopic, |
| kayal2001 | f71c2e8 | 2024-06-25 15:26:24 +0530 | [diff] [blame] | 106 | "nsconfigtemps": NsConfigTemplateTopic, |
| shrinithi | 28d887f | 2025-01-08 05:27:19 +0000 | [diff] [blame] | 107 | "cluster": ClusterTopic, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 108 | "infras_cont": InfraContTopic, |
| 109 | "infras_conf": InfraConfTopic, |
| garciadeblas | b798f45 | 2025-08-05 18:21:26 +0200 | [diff] [blame] | 110 | "apps": AppProfileTopic, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 111 | "resources": ResourceTopic, |
| shrinithi | 28d887f | 2025-01-08 05:27:19 +0000 | [diff] [blame] | 112 | "clusterops": ClusterOpsTopic, |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 113 | "ksus": KsusTopic, |
| garciadeblas | b798f45 | 2025-08-05 18:21:26 +0200 | [diff] [blame] | 114 | "appinstances": AppInstanceTopic, |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 115 | "oka_packages": OkaTopic, |
| yshah | d23c6a5 | 2025-06-13 05:49:31 +0000 | [diff] [blame] | 116 | "node_groups": NodeGroupTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 117 | # [NEW_TOPIC]: add an entry here |
| vijay.r | 35ef2f7 | 2019-04-30 17:55:49 +0530 | [diff] [blame] | 118 | # "pm_jobs": PmJobsTopic will be added manually because it needs other parameters |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 119 | } |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 120 | |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 121 | map_target_version_to_int = { |
| 122 | "1.0": 1000, |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 123 | "1.1": 1001, |
| 124 | "1.2": 1002, |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 125 | # Add new versions here |
| 126 | } |
| 127 | |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 128 | def __init__(self, authenticator): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 129 | self.db = None |
| 130 | self.fs = None |
| 131 | self.msg = None |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 132 | self.authconn = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 133 | self.config = None |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 134 | # self.operations = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 135 | self.logger = logging.getLogger("nbi.engine") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 136 | self.map_topic = {} |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 137 | self.write_lock = None |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 138 | # self.token_cache = token_cache |
| 139 | self.authenticator = authenticator |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 140 | |
| 141 | def start(self, config): |
| 142 | """ |
| 143 | Connect to database, filesystem storage, and messaging |
| 144 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 145 | :return: None |
| 146 | """ |
| 147 | self.config = config |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 148 | # check right version of common |
| 149 | if versiontuple(common_version) < versiontuple(min_common_version): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 150 | raise EngineException( |
| 151 | "Not compatible osm/common version '{}'. Needed '{}' or higher".format( |
| 152 | common_version, min_common_version |
| 153 | ) |
| 154 | ) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 155 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 156 | try: |
| 157 | if not self.db: |
| 158 | if config["database"]["driver"] == "mongo": |
| 159 | self.db = dbmongo.DbMongo() |
| 160 | self.db.db_connect(config["database"]) |
| 161 | elif config["database"]["driver"] == "memory": |
| 162 | self.db = dbmemory.DbMemory() |
| 163 | self.db.db_connect(config["database"]) |
| 164 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 165 | raise EngineException( |
| 166 | "Invalid configuration param '{}' at '[database]':'driver'".format( |
| 167 | config["database"]["driver"] |
| 168 | ) |
| 169 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 170 | if not self.fs: |
| 171 | if config["storage"]["driver"] == "local": |
| 172 | self.fs = fslocal.FsLocal() |
| 173 | self.fs.fs_connect(config["storage"]) |
| Eduardo Sousa | 7e0eb13 | 2019-06-21 11:50:21 +0100 | [diff] [blame] | 174 | elif config["storage"]["driver"] == "mongo": |
| 175 | self.fs = fsmongo.FsMongo() |
| 176 | self.fs.fs_connect(config["storage"]) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 177 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 178 | raise EngineException( |
| 179 | "Invalid configuration param '{}' at '[storage]':'driver'".format( |
| 180 | config["storage"]["driver"] |
| 181 | ) |
| 182 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 183 | if not self.msg: |
| 184 | if config["message"]["driver"] == "local": |
| 185 | self.msg = msglocal.MsgLocal() |
| 186 | self.msg.connect(config["message"]) |
| 187 | elif config["message"]["driver"] == "kafka": |
| 188 | self.msg = msgkafka.MsgKafka() |
| 189 | self.msg.connect(config["message"]) |
| 190 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 191 | raise EngineException( |
| 192 | "Invalid configuration param '{}' at '[message]':'driver'".format( |
| 193 | config["message"]["driver"] |
| 194 | ) |
| 195 | ) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 196 | if not self.authconn: |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 197 | if config["authentication"]["backend"] == "keystone": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 198 | self.authconn = AuthconnKeystone( |
| 199 | config["authentication"], |
| 200 | self.db, |
| 201 | self.authenticator.role_permissions, |
| 202 | ) |
| K Sai Kiran | 7ddb073 | 2020-10-30 11:14:44 +0530 | [diff] [blame] | 203 | elif config["authentication"]["backend"] == "tacacs": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 204 | self.authconn = AuthconnTacacs( |
| 205 | config["authentication"], |
| 206 | self.db, |
| 207 | self.authenticator.role_permissions, |
| 208 | ) |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 209 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 210 | self.authconn = AuthconnInternal( |
| 211 | config["authentication"], |
| 212 | self.db, |
| 213 | self.authenticator.role_permissions, |
| 214 | ) |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 215 | # if not self.operations: |
| 216 | # if "resources_to_operations" in config["rbac"]: |
| 217 | # resources_to_operations_file = config["rbac"]["resources_to_operations"] |
| 218 | # else: |
| 219 | # possible_paths = ( |
| 220 | # __file__[:__file__.rfind("engine.py")] + "resources_to_operations.yml", |
| 221 | # "./resources_to_operations.yml" |
| 222 | # ) |
| 223 | # for config_file in possible_paths: |
| 224 | # if path.isfile(config_file): |
| 225 | # resources_to_operations_file = config_file |
| 226 | # break |
| 227 | # if not resources_to_operations_file: |
| 228 | # raise EngineException("Invalid permission configuration:" |
| 229 | # "resources_to_operations file missing") |
| 230 | # |
| 231 | # with open(resources_to_operations_file, 'r') as f: |
| garciadeblas | 4cd875d | 2023-02-14 19:05:34 +0100 | [diff] [blame] | 232 | # resources_to_operations = yaml.safeload(f) |
| tierno | 9e87a7f | 2020-03-23 09:24:10 +0000 | [diff] [blame] | 233 | # |
| 234 | # self.operations = [] |
| 235 | # |
| 236 | # for _, value in resources_to_operations["resources_to_operations"].items(): |
| 237 | # if value not in self.operations: |
| 238 | # self.operations += [value] |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame] | 239 | |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 240 | self.write_lock = Lock() |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 241 | # create one class per topic |
| 242 | for topic, topic_class in self.map_from_topic_to_class.items(): |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 243 | # if self.auth and topic_class in (UserTopicAuth, ProjectTopicAuth): |
| 244 | # self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 245 | self.map_topic[topic] = topic_class( |
| 246 | self.db, self.fs, self.msg, self.authconn |
| 247 | ) |
| 248 | |
| 249 | self.map_topic["pm_jobs"] = PmJobsTopic( |
| 250 | self.db, |
| 251 | config["prometheus"].get("host"), |
| 252 | config["prometheus"].get("port"), |
| 253 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 254 | except (DbException, FsException, MsgException) as e: |
| 255 | raise EngineException(str(e), http_code=e.http_code) |
| 256 | |
| 257 | def stop(self): |
| 258 | try: |
| 259 | if self.db: |
| 260 | self.db.db_disconnect() |
| 261 | if self.fs: |
| 262 | self.fs.fs_disconnect() |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 263 | if self.msg: |
| 264 | self.msg.disconnect() |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 265 | self.write_lock = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 266 | except (DbException, FsException, MsgException) as e: |
| 267 | raise EngineException(str(e), http_code=e.http_code) |
| 268 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 269 | def new_item( |
| 270 | self, rollback, session, topic, indata=None, kwargs=None, headers=None |
| 271 | ): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 272 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 273 | Creates a new entry into database. For nsds and vnfds it creates an almost empty DISABLED entry, |
| 274 | that must be completed with a call to method upload_content |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 275 | :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] | 276 | :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] | 277 | :param topic: it can be: users, projects, vim_accounts, sdns, nsrs, nsds, vnfds |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 278 | :param indata: data to be inserted |
| 279 | :param kwargs: used to override the indata descriptor |
| 280 | :param headers: http request headers |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 281 | :return: _id: identity of the inserted data. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 282 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 283 | if topic not in self.map_topic: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 284 | raise EngineException( |
| 285 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 286 | ) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 287 | with self.write_lock: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 288 | return self.map_topic[topic].new(rollback, session, indata, kwargs, headers) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 289 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 290 | def add_item( |
| 291 | self, rollback, session, topic, indata=None, kwargs=None, headers=None |
| 292 | ): |
| 293 | """ |
| 294 | register a cluster in the database. |
| 295 | :param rollback: list to append created items at database in case a rollback must to be done |
| 296 | :param session: contains the used login username and working project, force to avoid checkins, public |
| 297 | :param topic: it can be: cluster for adding cluster into database |
| 298 | :param indata: data to be inserted |
| 299 | :param kwargs: used to override the indata descriptor |
| 300 | :param headers: http request headers |
| 301 | :return: _id: identity of the inserted data. |
| 302 | """ |
| 303 | if topic not in self.map_topic: |
| 304 | raise EngineException( |
| 305 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 306 | ) |
| 307 | with self.write_lock: |
| 308 | return self.map_topic[topic].add(rollback, session, indata, kwargs, headers) |
| 309 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 310 | def upload_content(self, session, topic, _id, indata, kwargs, headers): |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 311 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 312 | Upload content for an already created entry (_id) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 313 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 314 | :param topic: it can be: users, projects, vnfds, nsds, |
| 315 | :param _id: server id of the item |
| 316 | :param indata: data to be inserted |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 317 | :param kwargs: used to override the indata descriptor |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 318 | :param headers: http request headers |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 319 | :return: _id: identity of the inserted data. |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 320 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 321 | if topic not in self.map_topic: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 322 | raise EngineException( |
| 323 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 324 | ) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 325 | with self.write_lock: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 326 | return self.map_topic[topic].upload_content( |
| 327 | session, _id, indata, kwargs, headers |
| 328 | ) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 329 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 330 | def clone( |
| 331 | self, rollback, session, topic, _id, indata=None, kwargs=None, headers=None |
| 332 | ): |
| 333 | if topic not in self.map_topic: |
| 334 | raise EngineException( |
| 335 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 336 | ) |
| 337 | with self.write_lock: |
| 338 | return self.map_topic[topic].clone( |
| 339 | rollback, session, _id, indata, kwargs, headers |
| 340 | ) |
| 341 | |
| 342 | def move_ksu(self, session, topic, _id, indata=None, kwargs=None): |
| 343 | if topic not in self.map_topic: |
| 344 | raise EngineException( |
| 345 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 346 | ) |
| 347 | |
| 348 | with self.write_lock: |
| 349 | return self.map_topic[topic].move_ksu(session, _id, indata, kwargs) |
| 350 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 351 | def get_cluster_creds_file(self, session, topic, _id, item, op_id): |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 352 | if topic not in self.map_topic: |
| 353 | raise EngineException( |
| 354 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 355 | ) |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 356 | return self.map_topic[topic].get_cluster_creds_file(session, _id, item, op_id) |
| 357 | |
| 358 | def get_cluster_creds(self, session, topic, _id, item): |
| 359 | if topic not in self.map_topic: |
| 360 | raise EngineException( |
| 361 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 362 | ) |
| 363 | return self.map_topic[topic].get_cluster_creds(session, _id, item) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 364 | |
| yshah | d23c6a5 | 2025-06-13 05:49:31 +0000 | [diff] [blame] | 365 | def update_item(self, session, topic, _id, item, indata): |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 366 | if topic not in self.map_topic: |
| 367 | raise EngineException( |
| 368 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 369 | ) |
| yshah | d23c6a5 | 2025-06-13 05:49:31 +0000 | [diff] [blame] | 370 | return self.map_topic[topic].update_item(session, _id, item, indata) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 371 | |
| 372 | def delete_ksu(self, session, topic, _id, indata): |
| 373 | if topic not in self.map_topic: |
| 374 | raise EngineException( |
| 375 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 376 | ) |
| 377 | with self.write_lock: |
| 378 | return self.map_topic[topic].delete_ksu( |
| 379 | session, _id, indata, not_send_msg=None |
| 380 | ) |
| 381 | |
| garciadeblas | b798f45 | 2025-08-05 18:21:26 +0200 | [diff] [blame] | 382 | def update_appinstance(self, session, topic, _id, item, indata): |
| 383 | if topic not in self.map_topic: |
| 384 | raise EngineException( |
| 385 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 386 | ) |
| 387 | return self.map_topic[topic].update_appinstance(session, _id, item, indata) |
| 388 | |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 389 | def get_item_list(self, session, topic, filter_q=None, api_req=False): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 390 | """ |
| 391 | Get a list of items |
| 392 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 393 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 394 | :param filter_q: filter of data to be applied |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 395 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 396 | :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] | 397 | """ |
| shrinithi | 28d887f | 2025-01-08 05:27:19 +0000 | [diff] [blame] | 398 | self.logger.info("it is getting into item list") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 399 | if topic not in self.map_topic: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 400 | raise EngineException( |
| 401 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 402 | ) |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 403 | return self.map_topic[topic].list(session, filter_q, api_req) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 404 | |
| shrinithi | 28d887f | 2025-01-08 05:27:19 +0000 | [diff] [blame] | 405 | def get_item_list_cluster(self, session, topic, filter_q=None, api_req=False): |
| 406 | """ |
| 407 | Get a list of items |
| 408 | :param session: contains the used login username and working project |
| 409 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 410 | :param filter_q: filter of data to be applied |
| 411 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| 412 | :return: The list, it can be empty if no one match the filter_q. |
| 413 | """ |
| 414 | self.logger.info("it is getting into item list cluster") |
| 415 | if topic not in self.map_topic: |
| 416 | raise EngineException( |
| 417 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 418 | ) |
| 419 | return self.map_topic[topic].list_both(session, filter_q, api_req) |
| 420 | |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 421 | def get_item(self, session, topic, _id, filter_q=None, api_req=False): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 422 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 423 | Get complete information on an item |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 424 | :param session: contains the used login username and working project |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 425 | :param topic: it can be: users, projects, vnfds, nsds, clusters, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 426 | :param _id: server id of the item |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 427 | :param filter_q: other arguments |
| Frank Bryden | 19b9752 | 2020-07-10 12:32:02 +0000 | [diff] [blame] | 428 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 429 | :return: dictionary, raise exception if not found. |
| 430 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 431 | if topic not in self.map_topic: |
| garciadeblas | f2af4a1 | 2023-01-24 16:56:54 +0100 | [diff] [blame] | 432 | raise EngineException( |
| 433 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 434 | ) |
| K Sai Kiran | 5758955 | 2021-01-27 21:38:34 +0530 | [diff] [blame] | 435 | return self.map_topic[topic].show(session, _id, filter_q, api_req) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 436 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 437 | def get_one_item(self, session, topic, _id, profile, filter_q=None, api_req=False): |
| 438 | """ |
| 439 | Get complete information on an item |
| 440 | :param session: contains the used login username and working project |
| 441 | :param topic: it can be: users, projects, vnfds, nsds, clusters profile, |
| 442 | :param _id: server id of the item |
| 443 | :param profile: contains the profile type |
| 444 | :param filter_q: other arguments |
| 445 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| 446 | :return: dictionary, raise exception if not found. |
| 447 | """ |
| 448 | if topic not in self.map_topic: |
| 449 | raise EngineException( |
| 450 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 451 | ) |
| 452 | return self.map_topic[topic].show_one(session, _id, profile, filter_q, api_req) |
| 453 | |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 454 | def get_file(self, session, topic, _id, path=None, accept_header=None): |
| 455 | """ |
| 456 | Get descriptor package or artifact file content |
| 457 | :param session: contains the used login username and working project |
| 458 | :param topic: it can be: users, projects, vnfds, nsds, |
| 459 | :param _id: server id of the item |
| 460 | :param path: artifact path or "$DESCRIPTOR" or None |
| 461 | :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain |
| 462 | :return: opened file plus Accept format or raises an exception |
| 463 | """ |
| 464 | if topic not in self.map_topic: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 465 | raise EngineException( |
| 466 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 467 | ) |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 468 | return self.map_topic[topic].get_file(session, _id, path, accept_header) |
| 469 | |
| yshah | d23c6a5 | 2025-06-13 05:49:31 +0000 | [diff] [blame] | 470 | def get_cluster_list_ksu(self, session, topic, filter_q=None, api_req=False): |
| 471 | """ |
| 472 | Get a list of items |
| 473 | :param session: contains the used login username and working project |
| 474 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 475 | :param filter_q: filter of data to be applied |
| 476 | :param api_req: True if this call is serving an external API request. False if serving internal request. |
| 477 | :return: The list, it can be empty if no one match the filter_q. |
| 478 | """ |
| 479 | self.logger.info("it is getting into item list cluster") |
| 480 | if topic not in self.map_topic: |
| 481 | raise EngineException( |
| 482 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 483 | ) |
| 484 | return self.map_topic[topic].cluster_list_ksu(session, filter_q, api_req) |
| 485 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 486 | def del_item_list(self, session, topic, _filter=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 487 | """ |
| 488 | Delete a list of items |
| 489 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 490 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 491 | :param _filter: filter of data to be applied |
| 492 | :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] | 493 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 494 | if topic not in self.map_topic: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 495 | raise EngineException( |
| 496 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 497 | ) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 498 | with self.write_lock: |
| 499 | return self.map_topic[topic].delete_list(session, _filter) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 500 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 501 | def del_item(self, session, topic, _id, not_send_msg=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 502 | """ |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 503 | Delete item by its internal id |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 504 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 505 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 506 | :param _id: server id of the item |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 507 | :param not_send_msg: If False, message will not be sent to kafka. |
| 508 | If a list, message is not sent, but content is stored in this variable so that the caller can send this |
| 509 | message using its own loop. If None, message is sent |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 510 | :return: dictionary with deleted item _id. It raises exception if not found. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 511 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 512 | if topic not in self.map_topic: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 513 | raise EngineException( |
| 514 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 515 | ) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 516 | with self.write_lock: |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 517 | return self.map_topic[topic].delete(session, _id, not_send_msg=not_send_msg) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 518 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 519 | def remove(self, session, topic, _id, not_send_msg=None): |
| 520 | """ |
| 521 | Delete item by its internal id |
| 522 | :param session: contains the used login username and working project |
| 523 | :param topic: it can be: users, projects, vnfds, nsds, clusters, |
| 524 | :param _id: server id of the item |
| 525 | :param not_send_msg: If False, message will not be sent to kafka. |
| 526 | If a list, message is not sent, but content is stored in this variable so that the caller can send this |
| 527 | message using its own loop. If None, message is sent |
| 528 | :return: dictionary with deleted item _id. It raises exception if not found. |
| 529 | """ |
| 530 | if topic not in self.map_topic: |
| 531 | raise EngineException( |
| 532 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 533 | ) |
| 534 | with self.write_lock: |
| 535 | return self.map_topic[topic].remove(session, _id, not_send_msg=not_send_msg) |
| 536 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 537 | def edit_item(self, session, topic, _id, indata=None, kwargs=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 538 | """ |
| 539 | Update an existing entry at database |
| 540 | :param session: contains the used login username and working project |
| 541 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 542 | :param _id: identifier to be updated |
| 543 | :param indata: data to be inserted |
| 544 | :param kwargs: used to override the indata descriptor |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 545 | :return: dictionary with edited item _id, raise exception if not found. |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 546 | """ |
| 547 | if topic not in self.map_topic: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 548 | raise EngineException( |
| 549 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 550 | ) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 551 | with self.write_lock: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 552 | return self.map_topic[topic].edit(session, _id, indata, kwargs) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 553 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 554 | def edit(self, session, topic, _id, item, indata=None, kwargs=None): |
| 555 | """ |
| 556 | Update an existing entry at database |
| 557 | :param session: contains the used login username and working project |
| 558 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 559 | :param _id: identifier to be updated |
| 560 | :param item: it shows the type of profiles |
| 561 | :param indata: data to be inserted |
| 562 | :param kwargs: used to override the indata descriptor |
| 563 | :return: dictionary with edited item _id, raise exception if not found. |
| 564 | """ |
| 565 | if topic not in self.map_topic: |
| 566 | raise EngineException( |
| 567 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 568 | ) |
| 569 | with self.write_lock: |
| 570 | return self.map_topic[topic].edit(session, _id, item, indata, kwargs) |
| 571 | |
| Gabriel Cuba | 84a60df | 2023-10-30 14:01:54 -0500 | [diff] [blame] | 572 | def cancel_item( |
| 573 | self, rollback, session, topic, indata=None, kwargs=None, headers=None |
| 574 | ): |
| 575 | """ |
| 576 | Cancels an item |
| 577 | :param rollback: list to append created items at database in case a rollback must to be done |
| 578 | :param session: contains the used login username and working project, force to avoid checkins, public |
| 579 | :param topic: it can be: users, projects, vim_accounts, sdns, nsrs, nsds, vnfds |
| 580 | :param indata: data to be inserted |
| 581 | :param kwargs: used to override the indata descriptor |
| 582 | :param headers: http request headers |
| 583 | :return: _id: identity of the inserted data. |
| 584 | """ |
| 585 | if topic not in self.map_topic: |
| 586 | raise EngineException( |
| 587 | "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR |
| 588 | ) |
| 589 | with self.write_lock: |
| 590 | self.map_topic[topic].cancel(rollback, session, indata, kwargs, headers) |
| 591 | |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 592 | def upgrade_db(self, current_version, target_version): |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 593 | if target_version not in self.map_target_version_to_int.keys(): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 594 | raise EngineException( |
| 595 | "Cannot upgrade to version '{}' with this version of code".format( |
| 596 | target_version |
| 597 | ), |
| 598 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 599 | ) |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 600 | |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 601 | if current_version == target_version: |
| 602 | return |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 603 | |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 604 | target_version_int = self.map_target_version_to_int[target_version] |
| 605 | |
| 606 | if not current_version: |
| 607 | # create database version |
| 608 | serial = urandom(32) |
| 609 | version_data = { |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 610 | "_id": "version", # Always "version" |
| 611 | "version_int": 1000, # version number |
| 612 | "version": "1.0", # version text |
| 613 | "date": "2018-10-25", # version date |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 614 | "description": "added serial", # changes in this version |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 615 | "status": "ENABLED", # ENABLED, DISABLED (migration in process), ERROR, |
| 616 | "serial": b64encode(serial), |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 617 | } |
| 618 | self.db.create("admin", version_data) |
| 619 | self.db.set_secret_key(serial) |
| 620 | current_version = "1.0" |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 621 | |
| 622 | if ( |
| 623 | current_version in ("1.0", "1.1") |
| 624 | and target_version_int >= self.map_target_version_to_int["1.2"] |
| 625 | ): |
| 626 | if self.config["authentication"]["backend"] == "internal": |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 627 | self.db.del_list("roles") |
| 628 | |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 629 | version_data = { |
| 630 | "_id": "version", |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 631 | "version_int": 1002, |
| 632 | "version": "1.2", |
| 633 | "date": "2019-06-11", |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 634 | "description": "set new format for roles_operations", |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | self.db.set_one("admin", {"_id": "version"}, version_data) |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 638 | current_version = "1.2" |
| Eduardo Sousa | 044f431 | 2019-05-20 15:17:35 +0100 | [diff] [blame] | 639 | # TODO add future migrations here |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 640 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 641 | def init_db(self, target_version="1.0"): |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 642 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 643 | 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] | 644 | 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] | 645 | :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] | 646 | :return: None if ok, exception if error or if the version is different. |
| 647 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 648 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 649 | version_data = self.db.get_one( |
| 650 | "admin", {"_id": "version"}, fail_on_empty=False, fail_on_more=True |
| 651 | ) |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 652 | # check database status is ok |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 653 | if version_data and version_data.get("status") != "ENABLED": |
| 654 | raise EngineException( |
| 655 | "Wrong database status '{}'".format(version_data["status"]), |
| 656 | HTTPStatus.INTERNAL_SERVER_ERROR, |
| 657 | ) |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 658 | |
| 659 | # check version |
| 660 | db_version = None if not version_data else version_data.get("version") |
| 661 | if db_version != target_version: |
| 662 | self.upgrade_db(db_version, target_version) |
| 663 | |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 664 | return |