X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FNBI.git;a=blobdiff_plain;f=osm_nbi%2Fsubscriptions.py;h=ec70d0c2755cb0405facbd8df0b0a19b5c4e03a0;hp=393918c3e210a7c575295cfb4fa94de739d732b5;hb=61e0c52d4715b5726a369959d69a1305d6790ae2;hpb=ad682a52ef94fa2662e2a0f6e3f81fb7c8f5e0fe diff --git a/osm_nbi/subscriptions.py b/osm_nbi/subscriptions.py index 393918c..ec70d0c 100644 --- a/osm_nbi/subscriptions.py +++ b/osm_nbi/subscriptions.py @@ -24,10 +24,12 @@ import logging import threading import asyncio from http import HTTPStatus + from osm_common import dbmongo, dbmemory, msglocal, msgkafka from osm_common.dbbase import DbException from osm_common.msgbase import MsgException from osm_nbi.engine import EngineException +from osm_nbi.notifications import NsLcmNotification __author__ = "Alfonso Tierno " @@ -65,6 +67,7 @@ class SubscriptionThread(threading.Thread): "public": None, "method": "delete", } + self.nslcm = None async def start_kafka(self): # timeout_wait_for_kafka = 3*60 @@ -80,21 +83,32 @@ class SubscriptionThread(threading.Thread): if not kafka_working: self.logger.critical("kafka is working again") kafka_working = True - if not self.aiomain_task_admin or self.aiomain_task_admin._state == "FINISHED": + if not self.aiomain_task_admin: await asyncio.sleep(10, loop=self.loop) self.logger.debug("Starting admin subscription task") self.aiomain_task_admin = asyncio.ensure_future(self.msg.aioread(("admin",), loop=self.loop, group_id=False, aiocallback=self._msg_callback), loop=self.loop) - if not self.aiomain_task or self.aiomain_task._state == "FINISHED": + if not self.aiomain_task: await asyncio.sleep(10, loop=self.loop) self.logger.debug("Starting non-admin subscription task") self.aiomain_task = asyncio.ensure_future(self.msg.aioread(("ns", "nsi"), loop=self.loop, aiocallback=self._msg_callback), loop=self.loop) - await asyncio.wait([self.aiomain_task, self.aiomain_task_admin], - timeout=None, loop=self.loop, return_when=asyncio.FIRST_COMPLETED) + done, _ = await asyncio.wait([self.aiomain_task, self.aiomain_task_admin], + timeout=None, loop=self.loop, return_when=asyncio.FIRST_COMPLETED) + try: + if self.aiomain_task_admin in done: + exc = self.aiomain_task_admin.exception() + self.logger.error("admin subscription task exception: {}".format(exc)) + self.aiomain_task_admin = None + if self.aiomain_task in done: + exc = self.aiomain_task.exception() + self.logger.error("non-admin subscription task exception: {}".format(exc)) + self.aiomain_task = None + except asyncio.CancelledError: + pass except Exception as e: if self.to_terminate: return @@ -133,7 +147,7 @@ class SubscriptionThread(threading.Thread): else: raise SubscriptionException("Invalid configuration param '{}' at '[message]':'driver'".format( config_msg["driver"])) - + self.nslcm = NsLcmNotification(self.db) except (DbException, MsgException) as e: raise SubscriptionException(str(e), http_code=e.http_code) @@ -169,6 +183,29 @@ class SubscriptionThread(threading.Thread): self.engine.del_item(self.internal_session, "nsrs", _id=params["nsr_id"], not_send_msg=msg_to_send) self.logger.debug("ns={} deleted from database".format(params["nsr_id"])) + # Check for nslcm notification + if isinstance(params, dict): + # Check availability of operationState and command + if (not params.get("operationState")) or (not command) or (not params.get("operationParams")): + self.logger.debug("Message can not be used for notification of nslcm") + else: + nsd_id = params["operationParams"].get("nsdId") + ns_instance_id = params["operationParams"].get("nsInstanceId") + # Any one among nsd_id, ns_instance_id should be present. + if not (nsd_id or ns_instance_id): + self.logger.debug("Message can not be used for notification of nslcm") + else: + op_state = params["operationState"] + event_details = {"topic": topic, "command": command.upper(), "params": params} + subscribers = self.nslcm.get_subscribers(nsd_id, ns_instance_id, command.upper(), op_state, + event_details) + # self.logger.debug("subscribers list: ") + # self.logger.debug(subscribers) + if subscribers: + asyncio.ensure_future(self.nslcm.send_notifications(subscribers, loop=self.loop), + loop=self.loop) + else: + self.logger.debug("Message can not be used for notification of nslcm") elif topic == "nsi": if command == "terminated" and params["operationState"] in ("COMPLETED", "PARTIALLY_COMPLETED"): self.logger.debug("received nsi terminated {}".format(params)) @@ -226,3 +263,5 @@ class SubscriptionThread(threading.Thread): self.to_terminate = True if self.aiomain_task: self.loop.call_soon_threadsafe(self.aiomain_task.cancel) + if self.aiomain_task_admin: + self.loop.call_soon_threadsafe(self.aiomain_task_admin.cancel)