Commit 0859b4b2 authored by Waleed Akbar's avatar Waleed Akbar
Browse files

KpiDescriptor with attributes inserted into the Kpi Table. Working

parent 6a191bd3
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
#!/bin/bash
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# 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.


PROJECTDIR=`pwd`

cd $PROJECTDIR/src
# RCFILE=$PROJECTDIR/coverage/.coveragerc
# coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#     kpi_manager/tests/test_unitary.py

# python3 kpi_manager/tests/test_unitary.py

RCFILE=$PROJECTDIR/coverage/.coveragerc
python3 -m pytest --log-level=INFO --log-cli-level=INFO --verbose \
    kpi_manager/service/database/KpiDBtests.py
 No newline at end of file
+19 −13
Original line number Diff line number Diff line
@@ -14,13 +14,14 @@


import logging, grpc
import sqlalchemy, sqlalchemy_utils
from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
from common.proto.context_pb2 import Empty
from common.proto.kpi_manager_pb2_grpc import KpiManagerServiceServicer
from common.proto.kpi_manager_pb2 import KpiId, KpiDescriptor, KpiDescriptorFilter, KpiDescriptorList
from monitoring.service.NameMapping import NameMapping
# from monitoring.service import ManagementDBTools
from telemetry.database.managementDB import managementDB
from kpi_manager.service.database.Kpi_DB import Kpi_DB

from telemetry.database.TelemetryModel import Kpi as KpiModel
from common.proto.context_pb2 import DeviceId, LinkId, ServiceId, SliceId,\
@@ -33,7 +34,12 @@ METRICS_POOL = MetricsPool('Monitoring', 'KpiManager')
class KpiManagerServiceServicerImpl(KpiManagerServiceServicer):
    def __init__(self, name_mapping : NameMapping):
        LOGGER.info('Init KpiManagerService')
        self.managementDBobj = managementDB()
        self.Kpi_DBobj = Kpi_DB()
    
    @staticmethod
    def create_database_if_not_exist(engine: sqlalchemy.engine.Engine) -> None:
        if not sqlalchemy_utils.database_exists(engine.url):
            sqlalchemy_utils.create_database(engine.url)

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def SetKpiDescriptor(self, request: KpiDescriptor, grpc_context: grpc.ServicerContext # type: ignore
@@ -45,15 +51,15 @@ class KpiManagerServiceServicerImpl(KpiManagerServiceServicer):
            kpi_to_insert.kpi_id          = request.kpi_id.kpi_id.uuid
            kpi_to_insert.kpi_description = request.kpi_description
            kpi_to_insert.kpi_sample_type = request.kpi_sample_type
            kpi_to_insert.device_id       = request.service_id.service_uuid.uuid 
            kpi_to_insert.endpoint_id     = request.device_id.device_uuid.uuid 
            kpi_to_insert.service_id      = request.slice_id.slice_uuid.uuid 
            kpi_to_insert.slice_id        = request.endpoint_id.endpoint_uuid.uuid
            kpi_to_insert.device_id       = request.device_id.device_uuid.uuid
            kpi_to_insert.endpoint_id     = request.endpoint_id.endpoint_uuid.uuid 
            kpi_to_insert.service_id      = request.service_id.service_uuid.uuid 
            kpi_to_insert.slice_id        = request.slice_id.slice_uuid.uuid 
            kpi_to_insert.connection_id   = request.connection_id.connection_uuid.uuid
            # kpi_to_insert.link_id         = request.link_id.link_id.uuid
            self.managementDBobj.add_row_to_db(kpi_to_insert)
            if(self.Kpi_DBobj.add_row_to_db(kpi_to_insert)):
                response.kpi_id.uuid = request.kpi_id.kpi_id.uuid
            LOGGER.info("Added Row: {:}".format(response))
                # LOGGER.info("Added Row: {:}".format(response))
            return response
        except Exception as e:
            LOGGER.info("Unable to create KpiModel class object. {:}".format(e))
@@ -65,7 +71,7 @@ class KpiManagerServiceServicerImpl(KpiManagerServiceServicer):
        LOGGER.info("Received gRPC message object: {:}".format(request))
        try: 
            kpi_id_to_search = request.kpi_id.uuid
            row = self.managementDBobj.search_db_row_by_id(KpiModel, 'kpi_id', kpi_id_to_search)
            row = self.Kpi_DBobj.search_db_row_by_id(KpiModel, 'kpi_id', kpi_id_to_search)
            if row is not None:
                response.kpi_id.kpi_id.uuid                 = row.kpi_id
                response.kpi_description                    = row.kpi_description
@@ -85,7 +91,7 @@ class KpiManagerServiceServicerImpl(KpiManagerServiceServicer):
        LOGGER.info("Received gRPC message object: {:}".format(request))
        try:
            kpi_id_to_search = request.kpi_id.uuid
            self.managementDBobj.delete_db_row_by_id(KpiModel, 'kpi_id', kpi_id_to_search)
            self.Kpi_DBobj.delete_db_row_by_id(KpiModel, 'kpi_id', kpi_id_to_search)
        except Exception as e:
            LOGGER.info('Unable to search kpi id. {:}'.format(e))
        finally:
@@ -102,7 +108,7 @@ class KpiManagerServiceServicerImpl(KpiManagerServiceServicer):
        filter_to_apply['device_id']       = request.device_id[0].device_uuid.uuid
        filter_to_apply['kpi_sample_type'] = request.kpi_sample_type[0]
        try:
            rows = self.managementDBobj.select_with_filter(KpiModel, **filter_to_apply)
            rows = self.Kpi_DBobj.select_with_filter(KpiModel, **filter_to_apply)
        except Exception as e:
            LOGGER.info('Unable to apply filter on kpi descriptor. {:}'.format(e))
        try:
+6 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import threading
from confluent_kafka import KafkaError
from confluent_kafka import Producer as KafkaProducer
from confluent_kafka import Consumer as KafkaConsumer
from kpi_manager.service.database.Kpi_DB import Kpi_DB

LOGGER             = logging.getLogger(__name__)
KAFKA_SERVER_IP    = '10.152.183.175:9092'
@@ -84,4 +85,8 @@ class KpiValueComposer:
        # LOGGER.info("Extracted Rows that match the KPIs {:}".format(matching_rows))
        return matching_rows
        
    @staticmethod
    def request_kpi_descriptor_from_db():
        col_name = "kpi_description"
        kpi_name = KPIs_TO_SEARCH[0]
        Kpi_DB.search_db_row_by_id()
+30 −0
Original line number Diff line number Diff line
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# 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.


import logging
from kpi_manager.service.database.Kpi_DB import Kpi_DB

LOGGER = logging.getLogger(__name__)


def test_create_db_object():
    LOGGER.info('>>> test_create_db_object : START<<< ')
    kpiDBobj = Kpi_DB()

def test_verify_Tables():
    LOGGER.info('>>> test_verify_Tables : START <<< ')
    kpiDBobj = Kpi_DB()
    kpiDBobj.create_tables()
    kpiDBobj.verify_tables()
+49 −0
Original line number Diff line number Diff line
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# 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.

import logging, sqlalchemy, sqlalchemy_utils
# from common.Settings import get_setting

LOGGER = logging.getLogger(__name__)

APP_NAME = 'tfs'
ECHO = False                # False: No dump SQL commands and transactions executed
CRDB_URI_TEMPLATE = 'cockroachdb://{:s}:{:s}@127.0.0.1:{:s}/{:s}?sslmode={:s}'
CRDB_NAMESPACE = "crdb"
CRDB_SQL_PORT  = "26257"
CRDB_DATABASE  = "kpi"
CRDB_USERNAME  = "tfs"
CRDB_PASSWORD  = "tfs123"
CRDB_SSLMODE   = "require"
# CRDB_URI_TEMPLATE = 'cockroachdb://{:s}:{:s}@cockroachdb-public.{:s}.svc.cluster.local:{:s}/{:s}?sslmode={:s}'

class KpiEngine:
    # def __init__(self):
    #     self.engine = self.get_engine()  

    @staticmethod
    def get_engine() -> sqlalchemy.engine.Engine:
        crdb_uri = CRDB_URI_TEMPLATE.format(
                CRDB_USERNAME, CRDB_PASSWORD, CRDB_SQL_PORT, CRDB_DATABASE, CRDB_SSLMODE)
        # crdb_uri = CRDB_URI_TEMPLATE.format(
        #         CRDB_USERNAME, CRDB_PASSWORD, CRDB_NAMESPACE, CRDB_SQL_PORT, CRDB_DATABASE, CRDB_SSLMODE)
        try:
            # engine = sqlalchemy.create_engine(
            #     crdb_uri, connect_args={'application_name': APP_NAME}, echo=ECHO, future=True)
            engine = sqlalchemy.create_engine(crdb_uri, echo=False)
            LOGGER.info(' KpiDBmanager initalized with DB URL: {:}'.format(crdb_uri))
        except: # pylint: disable=bare-except # pragma: no cover
            LOGGER.exception('Failed to connect to database: {:s}'.format(str(crdb_uri)))
            return None # type: ignore
        return engine # type: ignore
Loading