MockServicerImpl_Monitoring.py 6 KB
Newer Older
# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
import enum, grpc, logging
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from queue import Queue
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from typing import Any, Optional
from common.proto.context_pb2 import Empty
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.proto.monitoring_pb2 import Kpi, KpiDescriptor, KpiDescriptorList, KpiId, KpiQuery, RawKpiTable
from common.proto.monitoring_pb2_grpc import MonitoringServiceServicer
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.tools.grpc.Tools import grpc_message_to_json_string
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .InMemoryObjectDatabase import InMemoryObjectDatabase
from .InMemoryTimeSeriesDatabase import InMemoryTimeSeriesDatabase
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

LOGGER = logging.getLogger(__name__)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
class IMDB_ContainersEnum(enum.Enum):
    KPI_DESCRIPTORS = 'kpi_descriptor'

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
class MockServicerImpl_Monitoring(MonitoringServiceServicer):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __init__(
        self, queue_samples : Optional[Queue] = None
    ) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[__init__] Creating Servicer...')
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        if queue_samples is None: queue_samples = Queue()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.queue_samples = queue_samples
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.obj_db = InMemoryObjectDatabase()
        self.ts_db  = InMemoryTimeSeriesDatabase()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[__init__] Servicer Created')
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    # ----- Common -----------------------------------------------------------------------------------------------------

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def _set(self, container_name, entry_uuid, entry_id_field_name, entry) -> Any:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        entry = self.obj_db.set_entry(container_name, entry_uuid, entry)
        return getattr(entry, entry_id_field_name)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def _del(self, container_name, entry_uuid, grpc_context) -> Empty:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.obj_db.del_entry(container_name, entry_uuid, grpc_context)
        return Empty()

    # ----- KPI Descriptor ---------------------------------------------------------------------------------------------

    def GetKpiDescriptorList(self, request : Empty, context : grpc.ServicerContext) -> KpiDescriptorList:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[GetKpiDescriptorList] request={:s}'.format(grpc_message_to_json_string(request)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        kpi_descriptor_list = self.obj_db.get_entries(IMDB_ContainersEnum.KPI_DESCRIPTORS.value)
        reply = KpiDescriptorList(kpi_descriptor_list=kpi_descriptor_list)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[GetKpiDescriptorList] reply={:s}'.format(grpc_message_to_json_string(reply)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return reply

    def GetKpiDescriptor(self, request : KpiId, context : grpc.ServicerContext) -> KpiDescriptor:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[GetKpiDescriptor] request={:s}'.format(grpc_message_to_json_string(request)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        reply = self.obj_db.get_entry(IMDB_ContainersEnum.KPI_DESCRIPTORS.value, request.kpi_id.uuid, context)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[GetKpiDescriptor] reply={:s}'.format(grpc_message_to_json_string(reply)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return reply

    def SetKpi(self, request : KpiDescriptor, context : grpc.ServicerContext) -> KpiId:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[SetKpi] request={:s}'.format(grpc_message_to_json_string(request)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        reply = self._set(IMDB_ContainersEnum.KPI_DESCRIPTORS.value, request.kpi_id.kpi_id.uuid, 'kpi_id', request)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[SetKpi] reply={:s}'.format(grpc_message_to_json_string(reply)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return reply

    def DeleteKpi(self, request : KpiId, context : grpc.ServicerContext) -> Empty:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[DeleteKpi] request={:s}'.format(grpc_message_to_json_string(request)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        reply = self._del(IMDB_ContainersEnum.KPI_DESCRIPTORS.value, request.kpi_id.kpi_id.uuid, context)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[DeleteKpi] reply={:s}'.format(grpc_message_to_json_string(reply)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return reply

    # ----- KPI Sample -------------------------------------------------------------------------------------------------

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def IncludeKpi(self, request : Kpi, context : grpc.ServicerContext) -> Empty:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[IncludeKpi] request={:s}'.format(grpc_message_to_json_string(request)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.queue_samples.put(request)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        reply = Empty()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[IncludeKpi] reply={:s}'.format(grpc_message_to_json_string(reply)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return reply

    def QueryKpiData(self, request : KpiQuery, context : grpc.ServicerContext) -> RawKpiTable:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[QueryKpiData] request={:s}'.format(grpc_message_to_json_string(request)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        # TODO: add filters for request.monitoring_window_s
        # TODO: add filters for request.last_n_samples
        kpi_uuids = [kpi_id.kpi_id.uuid for kpi_id in request.kpi_ids]

        start_timestamp = request.start_timestamp.timestamp
        if start_timestamp <= 0: start_timestamp = None

        end_timestamp = request.end_timestamp.timestamp
        if end_timestamp <= 0: end_timestamp = None

        df_samples = self.ts_db.filter(kpi_uuids, start_timestamp=start_timestamp, end_timestamp=end_timestamp)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        #LOGGER.debug('[QueryKpiData] df_samples={:s}'.format(df_samples.to_string()))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        reply = RawKpiTable()
        kpi_uuid__to__raw_kpi_list = dict()

        for df_sample in df_samples.itertuples():
            kpi_uuid  = df_sample.kpi_uuid
            if kpi_uuid in kpi_uuid__to__raw_kpi_list:
                raw_kpi_list = kpi_uuid__to__raw_kpi_list[kpi_uuid]
            else:
                raw_kpi_list = reply.raw_kpi_lists.add()    # pylint: disable=no-member
                raw_kpi_list.kpi_id.kpi_id.uuid = kpi_uuid
                kpi_uuid__to__raw_kpi_list[kpi_uuid] = raw_kpi_list

            raw_kpi = raw_kpi_list.raw_kpis.add()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            raw_kpi.timestamp.timestamp = df_sample.timestamp.timestamp()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            raw_kpi.kpi_value.floatVal  = df_sample.value

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[QueryKpiData] reply={:s}'.format(grpc_message_to_json_string(reply)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return reply