Commit 402ed56b authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- improved configuration of EventsCollector
parent 205d0633
Loading
Loading
Loading
Loading
+69 −38
Original line number Original line Diff line number Diff line
@@ -22,26 +22,57 @@ LOGGER.setLevel(logging.DEBUG)


class EventsCollector:
class EventsCollector:
    def __init__(
    def __init__(
        self, context_client_grpc : ContextClient, log_events_received=False
        self, context_client          : ContextClient,
        log_events_received           : bool = False,
        activate_context_collector    : bool = True,
        activate_topology_collector   : bool = True,
        activate_device_collector     : bool = True,
        activate_link_collector       : bool = True,
        activate_service_collector    : bool = True,
        activate_slice_collector      : bool = True,
        activate_connection_collector : bool = True,

    ) -> None:
    ) -> None:
        self._events_queue = queue.Queue()
        self._events_queue = queue.Queue()
        self._log_events_received = log_events_received
        self._log_events_received = log_events_received


        self._context_stream    = context_client_grpc.GetContextEvents(Empty())
        self._context_stream, self._context_thread = None, None
        self._topology_stream   = context_client_grpc.GetTopologyEvents(Empty())
        if activate_context_collector:
        self._device_stream     = context_client_grpc.GetDeviceEvents(Empty())
            self._context_stream = context_client.GetContextEvents(Empty())
        self._link_stream       = context_client_grpc.GetLinkEvents(Empty())
            self._context_thread = self._create_collector_thread(self._context_stream)
        self._service_stream    = context_client_grpc.GetServiceEvents(Empty())

        self._slice_stream      = context_client_grpc.GetSliceEvents(Empty())
        self._topology_stream, self._topology_thread = None, None
        self._connection_stream = context_client_grpc.GetConnectionEvents(Empty())
        if activate_topology_collector:

            self._topology_stream = context_client.GetTopologyEvents(Empty())
        self._context_thread    = threading.Thread(target=self._collect, args=(self._context_stream   ,), daemon=False)
            self._topology_thread = self._create_collector_thread(self._topology_stream)
        self._topology_thread   = threading.Thread(target=self._collect, args=(self._topology_stream  ,), daemon=False)

        self._device_thread     = threading.Thread(target=self._collect, args=(self._device_stream    ,), daemon=False)
        self._device_stream, self._device_thread = None, None
        self._link_thread       = threading.Thread(target=self._collect, args=(self._link_stream      ,), daemon=False)
        if activate_device_collector:
        self._service_thread    = threading.Thread(target=self._collect, args=(self._service_stream   ,), daemon=False)
            self._device_stream = context_client.GetDeviceEvents(Empty())
        self._slice_thread      = threading.Thread(target=self._collect, args=(self._slice_stream     ,), daemon=False)
            self._device_thread = self._create_collector_thread(self._device_stream)
        self._connection_thread = threading.Thread(target=self._collect, args=(self._connection_stream,), daemon=False)

        self._link_stream, self._link_thread = None, None
        if activate_link_collector:
            self._link_stream = context_client.GetLinkEvents(Empty())
            self._link_thread = self._create_collector_thread(self._link_stream)

        self._service_stream, self._service_thread = None, None
        if activate_service_collector:
            self._service_stream = context_client.GetServiceEvents(Empty())
            self._service_thread = self._create_collector_thread(self._service_stream)

        self._slice_stream, self._slice_thread = None, None
        if activate_slice_collector:
            self._slice_stream = context_client.GetSliceEvents(Empty())
            self._slice_thread = self._create_collector_thread(self._slice_stream)

        self._connection_stream, self._connection_thread = None, None
        if activate_connection_collector:
            self._connection_stream = context_client.GetConnectionEvents(Empty())
            self._connection_thread = self._create_collector_thread(self._connection_stream)

    def _create_collector_thread(self, stream, as_daemon : bool = False):
        return threading.Thread(target=self._collect, args=(stream,), daemon=as_daemon)


    def _collect(self, events_stream) -> None:
    def _collect(self, events_stream) -> None:
        try:
        try:
@@ -54,13 +85,13 @@ class EventsCollector:
                raise # pragma: no cover
                raise # pragma: no cover


    def start(self):
    def start(self):
        self._context_thread.start()
        if self._context_thread    is not None: self._context_thread.start()
        self._topology_thread.start()
        if self._topology_thread   is not None: self._topology_thread.start()
        self._device_thread.start()
        if self._device_thread     is not None: self._device_thread.start()
        self._link_thread.start()
        if self._link_thread       is not None: self._link_thread.start()
        self._service_thread.start()
        if self._service_thread    is not None: self._service_thread.start()
        self._slice_thread.start()
        if self._slice_thread      is not None: self._slice_thread.start()
        self._connection_thread.start()
        if self._connection_thread is not None: self._connection_thread.start()


    def get_event(self, block : bool = True, timeout : float = 0.1):
    def get_event(self, block : bool = True, timeout : float = 0.1):
        try:
        try:
@@ -83,18 +114,18 @@ class EventsCollector:
        return sorted(events, key=lambda e: e.event.timestamp)
        return sorted(events, key=lambda e: e.event.timestamp)


    def stop(self):
    def stop(self):
        self._context_stream.cancel()
        if self._context_stream    is not None: self._context_stream.cancel()
        self._topology_stream.cancel()
        if self._topology_stream   is not None: self._topology_stream.cancel()
        self._device_stream.cancel()
        if self._device_stream     is not None: self._device_stream.cancel()
        self._link_stream.cancel()
        if self._link_stream       is not None: self._link_stream.cancel()
        self._service_stream.cancel()
        if self._service_stream    is not None: self._service_stream.cancel()
        self._slice_stream.cancel()
        if self._slice_stream      is not None: self._slice_stream.cancel()
        self._connection_stream.cancel()
        if self._connection_stream is not None: self._connection_stream.cancel()


        self._context_thread.join()
        if self._context_thread    is not None: self._context_thread.join()
        self._topology_thread.join()
        if self._topology_thread   is not None: self._topology_thread.join()
        self._device_thread.join()
        if self._device_thread     is not None: self._device_thread.join()
        self._link_thread.join()
        if self._link_thread       is not None: self._link_thread.join()
        self._service_thread.join()
        if self._service_thread    is not None: self._service_thread.join()
        self._slice_thread.join()
        if self._slice_thread      is not None: self._slice_thread.join()
        self._connection_thread.join()
        if self._connection_thread is not None: self._connection_thread.join()