Commit 098a6cbe authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Common - Tests:

- Corrected filters in InMemoryTimeSeriesDatabase
- Extended MockServicerImpl_Context to track devices and links in topologies
- Corrected method QueryKpiData in MockServicerImpl_Monitoring
parent 3b36d20e
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -27,7 +27,16 @@ class InMemoryTimeSeriesDatabase:
        end_timestamp : Optional[float] = None
    ) -> pandas.DataFrame:
        data = self._data
        if len(kpi_uuids) > 0: data = data[data.kpi_uuid in kpi_uuids]
        if start_timestamp is not None: data = data[data.timestamp >= pandas.to_datetime(start_timestamp)]
        if end_timestamp   is not None: data = data[data.timestamp <= pandas.to_datetime(end_timestamp  )]

        if len(kpi_uuids) > 0:
            data = data[data.kpi_uuid.isin(kpi_uuids)]

        if start_timestamp is not None:
            start_datetime = pandas.to_datetime(start_timestamp, unit='s')
            data = data[data.timestamp >= start_datetime]

        if end_timestamp is not None:
            end_datetime = pandas.to_datetime(end_timestamp, unit='s')
            data = data[data.timestamp <= end_datetime]

        return data
+87 −5
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
# limitations under the License.

import grpc, json, logging
from typing import Any, Dict, Iterator, List, Set
from typing import Any, Dict, Iterator, List, Set, Tuple
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.proto.context_pb2 import (
    Connection, ConnectionEvent, ConnectionId, ConnectionIdList, ConnectionList,
@@ -229,13 +229,54 @@ class MockServicerImpl_Context(ContextServiceServicer):

    def SetDevice(self, request: Context, context : grpc.ServicerContext) -> DeviceId:
        LOGGER.info('[SetDevice] request={:s}'.format(grpc_message_to_json_string(request)))
        reply = self._set(request, 'device', request.device_id.device_uuid.uuid, 'device_id', TOPIC_DEVICE)
        device_uuid = request.device_id.device_uuid.uuid
        reply = self._set(request, 'device', device_uuid, 'device_id', TOPIC_DEVICE)
        device = get_entry(context, self.database, 'device', device_uuid)

        context_topology_uuids : Set[Tuple[str, str]] = set()
        context_topology_uuids.add((DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME))
        for endpoint in device.device_endpoints:
            endpoint_context_uuid = endpoint.endpoint_id.topology_id.context_id.context_uuid.uuid
            if len(endpoint_context_uuid) == 0: endpoint_context_uuid = DEFAULT_CONTEXT_NAME
            endpoint_topology_uuid = endpoint.endpoint_id.topology_id.topology_uuid.uuid
            if len(endpoint_topology_uuid) == 0: endpoint_topology_uuid = DEFAULT_TOPOLOGY_NAME
            context_topology_uuids.add((endpoint_context_uuid, endpoint_topology_uuid))

        for context_uuid,topology_uuid in context_topology_uuids:
            container_name = 'topology[{:s}]'.format(str(context_uuid))
            topology = get_entry(context, self.database, container_name, topology_uuid)
            for device_id in topology.device_ids:
                if device_id.device_uuid.uuid == device_uuid: break
            else:
                # device not found, add it
                topology.device_ids.add().device_uuid.uuid = device_uuid

        LOGGER.info('[SetDevice] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

    def RemoveDevice(self, request: DeviceId, context : grpc.ServicerContext) -> Empty:
        LOGGER.info('[RemoveDevice] request={:s}'.format(grpc_message_to_json_string(request)))
        reply = self._del(request, 'device', request.device_uuid.uuid, 'device_id', TOPIC_DEVICE, context)
        device_uuid = request.device_uuid.uuid
        device = get_entry(context, self.database, 'device', device_uuid)
        reply = self._del(request, 'device', device_uuid, 'device_id', TOPIC_DEVICE, context)

        context_topology_uuids : Set[Tuple[str, str]] = set()
        context_topology_uuids.add((DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME))
        for endpoint in device.device_endpoints:
            endpoint_context_uuid = endpoint.endpoint_id.topology_id.context_id.context_uuid.uuid
            if len(endpoint_context_uuid) == 0: endpoint_context_uuid = DEFAULT_CONTEXT_NAME
            endpoint_topology_uuid = endpoint.endpoint_id.topology_id.topology_uuid.uuid
            if len(endpoint_topology_uuid) == 0: endpoint_topology_uuid = DEFAULT_TOPOLOGY_NAME
            context_topology_uuids.add((endpoint_context_uuid, endpoint_topology_uuid))

        for context_uuid,topology_uuid in context_topology_uuids:
            container_name = 'topology[{:s}]'.format(str(context_uuid))
            topology = get_entry(context, self.database, container_name, topology_uuid)
            for device_id in topology.device_ids:
                if device_id.device_uuid.uuid == device_uuid:
                    topology.device_ids.remove(device_id)
                    break

        LOGGER.info('[RemoveDevice] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

@@ -293,13 +334,54 @@ class MockServicerImpl_Context(ContextServiceServicer):

    def SetLink(self, request: Context, context : grpc.ServicerContext) -> LinkId:
        LOGGER.info('[SetLink] request={:s}'.format(grpc_message_to_json_string(request)))
        reply = self._set(request, 'link', request.link_id.link_uuid.uuid, 'link_id', TOPIC_LINK)
        link_uuid = request.link_id.link_uuid.uuid
        reply = self._set(request, 'link', link_uuid, 'link_id', TOPIC_LINK)
        link = get_entry(context, self.database, 'link', link_uuid)

        context_topology_uuids : Set[Tuple[str, str]] = set()
        context_topology_uuids.add((DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME))
        for endpoint_id in link.link_endpoint_ids:
            endpoint_context_uuid = endpoint_id.topology_id.context_id.context_uuid.uuid
            if len(endpoint_context_uuid) == 0: endpoint_context_uuid = DEFAULT_CONTEXT_NAME
            endpoint_topology_uuid = endpoint_id.topology_id.topology_uuid.uuid
            if len(endpoint_topology_uuid) == 0: endpoint_topology_uuid = DEFAULT_TOPOLOGY_NAME
            context_topology_uuids.add((endpoint_context_uuid, endpoint_topology_uuid))

        for context_uuid,topology_uuid in context_topology_uuids:
            container_name = 'topology[{:s}]'.format(str(context_uuid))
            topology = get_entry(context, self.database, container_name, topology_uuid)
            for link_id in topology.link_ids:
                if link_id.link_uuid.uuid == link_uuid: break
            else:
                # link not found, add it
                topology.link_ids.add().link_uuid.uuid = link_uuid

        LOGGER.info('[SetLink] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

    def RemoveLink(self, request: LinkId, context : grpc.ServicerContext) -> Empty:
        LOGGER.info('[RemoveLink] request={:s}'.format(grpc_message_to_json_string(request)))
        reply = self._del(request, 'link', request.link_uuid.uuid, 'link_id', TOPIC_LINK, context)
        link_uuid = request.link_uuid.uuid
        link = get_entry(context, self.database, 'link', link_uuid)
        reply = self._del(request, 'link', link_uuid, 'link_id', TOPIC_LINK, context)

        context_topology_uuids : Set[Tuple[str, str]] = set()
        context_topology_uuids.add((DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME))
        for endpoint_id in link.link_endpoint_ids:
            endpoint_context_uuid = endpoint_id.topology_id.context_id.context_uuid.uuid
            if len(endpoint_context_uuid) == 0: endpoint_context_uuid = DEFAULT_CONTEXT_NAME
            endpoint_topology_uuid = endpoint_id.topology_id.topology_uuid.uuid
            if len(endpoint_topology_uuid) == 0: endpoint_topology_uuid = DEFAULT_TOPOLOGY_NAME
            context_topology_uuids.add((endpoint_context_uuid, endpoint_topology_uuid))

        for context_uuid,topology_uuid in context_topology_uuids:
            container_name = 'topology[{:s}]'.format(str(context_uuid))
            topology = get_entry(context, self.database, container_name, topology_uuid)
            for link_id in topology.link_ids:
                if link_id.link_uuid.uuid == link_uuid:
                    topology.link_ids.remove(link_id)
                    break

        LOGGER.info('[RemoveLink] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

+2 −1
Original line number Diff line number Diff line
@@ -97,6 +97,7 @@ class MockServicerImpl_Monitoring(MonitoringServiceServicer):
        if end_timestamp <= 0: end_timestamp = None

        df_samples = self.ts_db.filter(kpi_uuids, start_timestamp=start_timestamp, end_timestamp=end_timestamp)
        LOGGER.info('[QueryKpiData] df_samples={:s}'.format(df_samples.to_string()))
        reply = RawKpiTable()
        kpi_uuid__to__raw_kpi_list = dict()

@@ -110,7 +111,7 @@ class MockServicerImpl_Monitoring(MonitoringServiceServicer):
                kpi_uuid__to__raw_kpi_list[kpi_uuid] = raw_kpi_list

            raw_kpi = raw_kpi_list.raw_kpis.add()
            raw_kpi.timestamp.timestamp = df_sample.timestamp
            raw_kpi.timestamp.timestamp = df_sample.timestamp.timestamp()
            raw_kpi.kpi_value.floatVal  = df_sample.value

        LOGGER.info('[QueryKpiData] reply={:s}'.format(grpc_message_to_json_string(reply)))