Commit 492a0c66 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- Shifted to single ALL topic in Message Broker to ensure proper ordering of delivered messages.
parent dafb2eab
Loading
Loading
Loading
Loading
+34 −7
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ class EventTopicEnum(enum.Enum):
    SLICE         = 'slice'
    TOPOLOGY      = 'topology'
    OPTICALCONFIG = 'optical-config'
    ALL           = 'all'
  

TOPIC_TO_EVENTCLASS = {
@@ -49,13 +50,18 @@ CONSUME_TIMEOUT = 0.5 # seconds

LOGGER = logging.getLogger(__name__)

# NOTE: Forced to use a single "ALL" topic so that we ensure all messages are kept ordered.
#       Consumer filters appropriate ones while delivering.
# TODO: Upgrade this schema with proper in-topic filters to enhance performance.

def notify_event(
    messagebroker : MessageBroker, topic_enum : EventTopicEnum, event_type : EventTypeEnum, fields : Dict[str, str]
) -> None:
    event = {'event': {'timestamp': {'timestamp': time.time()}, 'event_type': event_type}}
    for field_name, field_value in fields.items():
        event[field_name] = field_value
    messagebroker.publish(Message(topic_enum.value, json.dumps(event)))
    #messagebroker.publish(Message(topic_enum.value, json.dumps(event)))
    messagebroker.publish(Message(EventTopicEnum.ALL.value, json.dumps(event)))

def notify_event_context(messagebroker : MessageBroker, event_type : EventTypeEnum, context_id : Dict) -> None:
    notify_event(messagebroker, EventTopicEnum.CONTEXT, event_type, {'context_id': context_id})
@@ -87,11 +93,32 @@ def notify_event_policy_rule(messagebroker : MessageBroker, event_type : EventTy
def consume_events(
    messagebroker : MessageBroker, topic_enums : Set[EventTopicEnum], consume_timeout : float = CONSUME_TIMEOUT
) -> Iterator:
    topic_names = [topic_enum.value for topic_enum in topic_enums]
    #topic_names = [topic_enum.value for topic_enum in topic_enums]
    topic_names = [EventTopicEnum.ALL.value]
    for message in messagebroker.consume(topic_names, consume_timeout=consume_timeout):
        event_class = TOPIC_TO_EVENTCLASS.get(message.topic)
        if event_class is None:
            MSG = 'No EventClass defined for Topic({:s}). Ignoring...'
            LOGGER.warning(MSG.format(str(message.topic)))
        #event_class = TOPIC_TO_EVENTCLASS.get(message.topic)
        #if event_class is None:
        #    MSG = 'No EventClass defined for Topic({:s}). Ignoring...'
        #    LOGGER.warning(MSG.format(str(message.topic)))
        #    continue
        data = json.loads(message.content)
        if 'context_id' in data and EventTopicEnum.CONTEXT in topic_enums:
            yield ContextEvent(**data)
        elif 'topology_id' in data and EventTopicEnum.TOPOLOGY in topic_enums:
            yield TopologyEvent(**data)
        elif 'device_id' in data and EventTopicEnum.DEVICE in topic_enums:
            yield DeviceEvent(**data)
        elif 'opticalconfig_id' in data and EventTopicEnum.OPTICALCONFIG in topic_enums:
            yield DeviceEvent(**data)
        elif 'link_id' in data and EventTopicEnum.LINK in topic_enums:
            yield LinkEvent(**data)
        elif 'service_id' in data and EventTopicEnum.SERVICE in topic_enums:
            yield ServiceEvent(**data)
        elif 'slice_id' in data and EventTopicEnum.SLICE in topic_enums:
            yield SliceEvent(**data)
        elif 'connection_id' in data and EventTopicEnum.CONNECTION in topic_enums:
            yield ConnectionEvent(**data)
        else:
            MSG = 'Unable to identify EventClass for Message({:s}). Ignoring...'
            LOGGER.warning(MSG.format(str(message)))
            continue
        yield event_class(**json.loads(message.content))