| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 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 | |
| 16 | """ |
| 17 | This module implements a thread that reads from kafka bus implementing all the subscriptions. |
| 18 | It is based on asyncio. |
| 19 | To avoid race conditions it uses same engine class as the main module for database changes |
| 20 | For the moment this module only deletes NS instances when they are terminated with the autoremove flag |
| 21 | """ |
| 22 | |
| 23 | import logging |
| 24 | import threading |
| 25 | import asyncio |
| 26 | from http import HTTPStatus |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 27 | |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 28 | from osm_common import dbmongo, dbmemory, msglocal, msgkafka |
| 29 | from osm_common.dbbase import DbException |
| 30 | from osm_common.msgbase import MsgException |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 31 | from osm_nbi.engine import EngineException |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 32 | from osm_nbi.notifications import NsLcmNotification |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 33 | |
| 34 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 35 | |
| 36 | |
| 37 | class SubscriptionException(Exception): |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 38 | def __init__(self, message, http_code=HTTPStatus.BAD_REQUEST): |
| 39 | self.http_code = http_code |
| 40 | Exception.__init__(self, message) |
| 41 | |
| 42 | |
| 43 | class SubscriptionThread(threading.Thread): |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 44 | def __init__(self, config, engine): |
| 45 | """ |
| 46 | Constructor of class |
| 47 | :param config: configuration parameters of database and messaging |
| 48 | :param engine: an instance of Engine class, used for deleting instances |
| 49 | """ |
| 50 | threading.Thread.__init__(self) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 51 | self.to_terminate = False |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 52 | self.config = config |
| 53 | self.db = None |
| 54 | self.msg = None |
| 55 | self.engine = engine |
| 56 | self.loop = None |
| 57 | self.logger = logging.getLogger("nbi.subscriptions") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 58 | self.aiomain_task_admin = ( |
| 59 | None # asyncio task for receiving admin actions from kafka bus |
| 60 | ) |
| 61 | self.aiomain_task = ( |
| 62 | None # asyncio task for receiving normal actions from kafka bus |
| 63 | ) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 64 | self.internal_session = { # used for a session to the engine methods |
| tierno | 86e916a | 2019-05-29 21:39:37 +0000 | [diff] [blame] | 65 | "project_id": (), |
| 66 | "set_project": (), |
| 67 | "admin": True, |
| 68 | "force": False, |
| 69 | "public": None, |
| 70 | "method": "delete", |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 71 | } |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 72 | self.nslcm = None |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 73 | |
| tierno | ee27072 | 2019-06-07 14:44:09 +0000 | [diff] [blame] | 74 | async def start_kafka(self): |
| 75 | # timeout_wait_for_kafka = 3*60 |
| 76 | kafka_working = True |
| 77 | while not self.to_terminate: |
| 78 | try: |
| 79 | # bug 710 635. The library aiokafka does not recieve anything when the topci at kafka has not been |
| 80 | # created. |
| 81 | # Before subscribe, send dummy messages |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 82 | await self.msg.aiowrite( |
| 83 | "admin", "echo", "dummy message", loop=self.loop |
| 84 | ) |
| tierno | ee27072 | 2019-06-07 14:44:09 +0000 | [diff] [blame] | 85 | await self.msg.aiowrite("ns", "echo", "dummy message", loop=self.loop) |
| 86 | await self.msg.aiowrite("nsi", "echo", "dummy message", loop=self.loop) |
| 87 | if not kafka_working: |
| 88 | self.logger.critical("kafka is working again") |
| 89 | kafka_working = True |
| tierno | f55e7ed | 2020-01-21 00:10:09 +0000 | [diff] [blame] | 90 | if not self.aiomain_task_admin: |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 91 | await asyncio.sleep(10, loop=self.loop) |
| 92 | self.logger.debug("Starting admin subscription task") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 93 | self.aiomain_task_admin = asyncio.ensure_future( |
| 94 | self.msg.aioread( |
| 95 | ("admin",), |
| 96 | loop=self.loop, |
| 97 | group_id=False, |
| 98 | aiocallback=self._msg_callback, |
| 99 | ), |
| 100 | loop=self.loop, |
| 101 | ) |
| tierno | f55e7ed | 2020-01-21 00:10:09 +0000 | [diff] [blame] | 102 | if not self.aiomain_task: |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 103 | await asyncio.sleep(10, loop=self.loop) |
| 104 | self.logger.debug("Starting non-admin subscription task") |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 105 | self.aiomain_task = asyncio.ensure_future( |
| 106 | self.msg.aioread( |
| 107 | ("ns", "nsi"), |
| 108 | loop=self.loop, |
| 109 | aiocallback=self._msg_callback, |
| 110 | ), |
| 111 | loop=self.loop, |
| 112 | ) |
| 113 | done, _ = await asyncio.wait( |
| 114 | [self.aiomain_task, self.aiomain_task_admin], |
| 115 | timeout=None, |
| 116 | loop=self.loop, |
| 117 | return_when=asyncio.FIRST_COMPLETED, |
| 118 | ) |
| tierno | f55e7ed | 2020-01-21 00:10:09 +0000 | [diff] [blame] | 119 | try: |
| 120 | if self.aiomain_task_admin in done: |
| 121 | exc = self.aiomain_task_admin.exception() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 122 | self.logger.error( |
| 123 | "admin subscription task exception: {}".format(exc) |
| 124 | ) |
| tierno | f55e7ed | 2020-01-21 00:10:09 +0000 | [diff] [blame] | 125 | self.aiomain_task_admin = None |
| 126 | if self.aiomain_task in done: |
| 127 | exc = self.aiomain_task.exception() |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 128 | self.logger.error( |
| 129 | "non-admin subscription task exception: {}".format(exc) |
| 130 | ) |
| tierno | f55e7ed | 2020-01-21 00:10:09 +0000 | [diff] [blame] | 131 | self.aiomain_task = None |
| 132 | except asyncio.CancelledError: |
| 133 | pass |
| tierno | ee27072 | 2019-06-07 14:44:09 +0000 | [diff] [blame] | 134 | except Exception as e: |
| 135 | if self.to_terminate: |
| 136 | return |
| 137 | if kafka_working: |
| 138 | # logging only first time |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 139 | self.logger.critical( |
| 140 | "Error accessing kafka '{}'. Retrying ...".format(e) |
| 141 | ) |
| tierno | ee27072 | 2019-06-07 14:44:09 +0000 | [diff] [blame] | 142 | kafka_working = False |
| 143 | await asyncio.sleep(10, loop=self.loop) |
| 144 | |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 145 | def run(self): |
| 146 | """ |
| 147 | Start of the thread |
| 148 | :return: None |
| 149 | """ |
| 150 | self.loop = asyncio.new_event_loop() |
| 151 | try: |
| 152 | if not self.db: |
| 153 | if self.config["database"]["driver"] == "mongo": |
| 154 | self.db = dbmongo.DbMongo() |
| 155 | self.db.db_connect(self.config["database"]) |
| 156 | elif self.config["database"]["driver"] == "memory": |
| 157 | self.db = dbmemory.DbMemory() |
| 158 | self.db.db_connect(self.config["database"]) |
| 159 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 160 | raise SubscriptionException( |
| 161 | "Invalid configuration param '{}' at '[database]':'driver'".format( |
| 162 | self.config["database"]["driver"] |
| 163 | ) |
| 164 | ) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 165 | if not self.msg: |
| 166 | config_msg = self.config["message"].copy() |
| 167 | config_msg["loop"] = self.loop |
| 168 | if config_msg["driver"] == "local": |
| 169 | self.msg = msglocal.MsgLocal() |
| 170 | self.msg.connect(config_msg) |
| 171 | elif config_msg["driver"] == "kafka": |
| 172 | self.msg = msgkafka.MsgKafka() |
| 173 | self.msg.connect(config_msg) |
| 174 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 175 | raise SubscriptionException( |
| 176 | "Invalid configuration param '{}' at '[message]':'driver'".format( |
| 177 | config_msg["driver"] |
| 178 | ) |
| 179 | ) |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 180 | self.nslcm = NsLcmNotification(self.db) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 181 | except (DbException, MsgException) as e: |
| 182 | raise SubscriptionException(str(e), http_code=e.http_code) |
| 183 | |
| 184 | self.logger.debug("Starting") |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 185 | while not self.to_terminate: |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 186 | try: |
| tierno | ee27072 | 2019-06-07 14:44:09 +0000 | [diff] [blame] | 187 | |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 188 | self.loop.run_until_complete( |
| 189 | asyncio.ensure_future(self.start_kafka(), loop=self.loop) |
| 190 | ) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 191 | # except asyncio.CancelledError: |
| 192 | # break # if cancelled it should end, breaking loop |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 193 | except Exception as e: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 194 | if not self.to_terminate: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 195 | self.logger.exception( |
| 196 | "Exception '{}' at messaging read loop".format(e), exc_info=True |
| 197 | ) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 198 | |
| 199 | self.logger.debug("Finishing") |
| 200 | self._stop() |
| 201 | self.loop.close() |
| 202 | |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 203 | async def _msg_callback(self, topic, command, params): |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 204 | """ |
| 205 | Callback to process a received message from kafka |
| 206 | :param topic: topic received |
| 207 | :param command: command received |
| 208 | :param params: rest of parameters |
| 209 | :return: None |
| 210 | """ |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 211 | msg_to_send = [] |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 212 | try: |
| 213 | if topic == "ns": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 214 | if command == "terminated" and params["operationState"] in ( |
| 215 | "COMPLETED", |
| 216 | "PARTIALLY_COMPLETED", |
| 217 | ): |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 218 | self.logger.debug("received ns terminated {}".format(params)) |
| 219 | if params.get("autoremove"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 220 | self.engine.del_item( |
| 221 | self.internal_session, |
| 222 | "nsrs", |
| 223 | _id=params["nsr_id"], |
| 224 | not_send_msg=msg_to_send, |
| 225 | ) |
| 226 | self.logger.debug( |
| 227 | "ns={} deleted from database".format(params["nsr_id"]) |
| 228 | ) |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 229 | # Check for nslcm notification |
| 230 | if isinstance(params, dict): |
| 231 | # Check availability of operationState and command |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 232 | if ( |
| 233 | (not params.get("operationState")) |
| 234 | or (not command) |
| 235 | or (not params.get("operationParams")) |
| 236 | ): |
| 237 | self.logger.debug( |
| 238 | "Message can not be used for notification of nslcm" |
| 239 | ) |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 240 | else: |
| 241 | nsd_id = params["operationParams"].get("nsdId") |
| 242 | ns_instance_id = params["operationParams"].get("nsInstanceId") |
| 243 | # Any one among nsd_id, ns_instance_id should be present. |
| 244 | if not (nsd_id or ns_instance_id): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 245 | self.logger.debug( |
| 246 | "Message can not be used for notification of nslcm" |
| 247 | ) |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 248 | else: |
| 249 | op_state = params["operationState"] |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 250 | event_details = { |
| 251 | "topic": topic, |
| 252 | "command": command.upper(), |
| 253 | "params": params, |
| 254 | } |
| 255 | subscribers = self.nslcm.get_subscribers( |
| 256 | nsd_id, |
| 257 | ns_instance_id, |
| 258 | command.upper(), |
| 259 | op_state, |
| 260 | event_details, |
| 261 | ) |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 262 | # self.logger.debug("subscribers list: ") |
| 263 | # self.logger.debug(subscribers) |
| tierno | 2278fa4 | 2020-08-10 13:53:57 +0000 | [diff] [blame] | 264 | if subscribers: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 265 | asyncio.ensure_future( |
| 266 | self.nslcm.send_notifications( |
| 267 | subscribers, loop=self.loop |
| 268 | ), |
| 269 | loop=self.loop, |
| 270 | ) |
| K Sai Kiran | bb70c81 | 2020-04-28 14:48:31 +0530 | [diff] [blame] | 271 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 272 | self.logger.debug( |
| 273 | "Message can not be used for notification of nslcm" |
| 274 | ) |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 275 | elif topic == "nsi": |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 276 | if command == "terminated" and params["operationState"] in ( |
| 277 | "COMPLETED", |
| 278 | "PARTIALLY_COMPLETED", |
| 279 | ): |
| Felipe Vicens | 09e6542 | 2019-01-22 15:06:46 +0100 | [diff] [blame] | 280 | self.logger.debug("received nsi terminated {}".format(params)) |
| 281 | if params.get("autoremove"): |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 282 | self.engine.del_item( |
| 283 | self.internal_session, |
| 284 | "nsis", |
| 285 | _id=params["nsir_id"], |
| 286 | not_send_msg=msg_to_send, |
| 287 | ) |
| 288 | self.logger.debug( |
| 289 | "nsis={} deleted from database".format(params["nsir_id"]) |
| 290 | ) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 291 | elif topic == "admin": |
| 292 | self.logger.debug("received {} {} {}".format(topic, command, params)) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 293 | if command in ["echo", "ping"]: # ignored commands |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 294 | pass |
| 295 | elif command == "revoke_token": |
| 296 | if params: |
| 297 | if isinstance(params, dict) and "_id" in params: |
| 298 | tid = params.get("_id") |
| 299 | self.engine.authenticator.tokens_cache.pop(tid, None) |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 300 | self.logger.debug( |
| 301 | "token '{}' removed from token_cache".format(tid) |
| 302 | ) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 303 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 304 | self.logger.debug( |
| 305 | "unrecognized params in command '{} {}': {}".format( |
| 306 | topic, command, params |
| 307 | ) |
| 308 | ) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 309 | else: |
| 310 | self.engine.authenticator.tokens_cache.clear() |
| 311 | self.logger.debug("token_cache cleared") |
| 312 | else: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 313 | self.logger.debug( |
| 314 | "unrecognized command '{} {}'".format(topic, command) |
| 315 | ) |
| delacruzramo | ad682a5 | 2019-12-10 16:26:34 +0100 | [diff] [blame] | 316 | # writing to kafka must be done with our own loop. For this reason it is not allowed Engine to do that, |
| tierno | bee3bad | 2019-12-05 12:26:01 +0000 | [diff] [blame] | 317 | # but content to be written is stored at msg_to_send |
| 318 | for msg in msg_to_send: |
| 319 | await self.msg.aiowrite(*msg, loop=self.loop) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 320 | except (EngineException, DbException, MsgException) as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 321 | self.logger.error( |
| 322 | "Error while processing topic={} command={}: {}".format( |
| 323 | topic, command, e |
| 324 | ) |
| 325 | ) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 326 | except Exception as e: |
| garciadeblas | 4568a37 | 2021-03-24 09:19:48 +0100 | [diff] [blame] | 327 | self.logger.exception( |
| 328 | "Exception while processing topic={} command={}: {}".format( |
| 329 | topic, command, e |
| 330 | ), |
| 331 | exc_info=True, |
| 332 | ) |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 333 | |
| 334 | def _stop(self): |
| 335 | """ |
| 336 | Close all connections |
| 337 | :return: None |
| 338 | """ |
| 339 | try: |
| 340 | if self.db: |
| 341 | self.db.db_disconnect() |
| 342 | if self.msg: |
| 343 | self.msg.disconnect() |
| 344 | except (DbException, MsgException) as e: |
| 345 | raise SubscriptionException(str(e), http_code=e.http_code) |
| 346 | |
| 347 | def terminate(self): |
| 348 | """ |
| 349 | This is a threading safe method to terminate this thread. Termination is done asynchronous afterwards, |
| 350 | but not immediately. |
| 351 | :return: None |
| 352 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 353 | self.to_terminate = True |
| tierno | ee27072 | 2019-06-07 14:44:09 +0000 | [diff] [blame] | 354 | if self.aiomain_task: |
| 355 | self.loop.call_soon_threadsafe(self.aiomain_task.cancel) |
| tierno | f55e7ed | 2020-01-21 00:10:09 +0000 | [diff] [blame] | 356 | if self.aiomain_task_admin: |
| 357 | self.loop.call_soon_threadsafe(self.aiomain_task_admin.cancel) |