Commit 56648279 authored by Carlos Natalino's avatar Carlos Natalino
Browse files

Initial skeleton implementation.

parent c04e4b9d
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -4,16 +4,16 @@ from concurrent import futures
from grpc_health.v1.health import HealthServicer, OVERALL_HEALTH
from grpc_health.v1.health_pb2 import HealthCheckResponse
from grpc_health.v1.health_pb2_grpc import add_HealthServicer_to_server
from opticalcentralizedattackdetector.proto.centralized_attack_detector_pb2_grpc import (
    add_CentralizedAttackDetectorServiceServicer_to_server)
from opticalcentralizedattackdetector.service.CentralizedAttackDetectorServiceServicerImpl import (
    CentralizedAttackDetectorServiceServicerImpl)
from opticalcentralizedattackdetector.proto.optical_centralized_attack_detector_pb2_grpc import (
    add_OpticalCentralizedAttackDetectorServiceServicer_to_server)
from opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl import (
    OpticalCentralizedAttackDetectorServiceServicerImpl)
from opticalcentralizedattackdetector.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD

BIND_ADDRESS = '0.0.0.0'
LOGGER = logging.getLogger(__name__)

class CentralizedAttackDetectorService:
class OpticalCentralizedAttackDetectorService:
    def __init__(
        self, address=BIND_ADDRESS, port=GRPC_SERVICE_PORT, max_workers=GRPC_MAX_WORKERS,
        grace_period=GRPC_GRACE_PERIOD):
@@ -36,8 +36,8 @@ class CentralizedAttackDetectorService:
        self.pool = futures.ThreadPoolExecutor(max_workers=self.max_workers)
        self.server = grpc.server(self.pool) # , interceptors=(tracer_interceptor,))

        self.centralized_attack_detector_servicer = CentralizedAttackDetectorServiceServicerImpl()
        add_CentralizedAttackDetectorServiceServicer_to_server(self.centralized_attack_detector_servicer, self.server)
        self.centralized_attack_detector_servicer = OpticalCentralizedAttackDetectorServiceServicerImpl()
        add_OpticalCentralizedAttackDetectorServiceServicer_to_server(self.centralized_attack_detector_servicer, self.server)

        self.health_servicer = HealthServicer(
            experimental_non_blocking=True, experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
+4 −4
Original line number Diff line number Diff line
@@ -2,16 +2,16 @@ import grpc, logging
from common.rpc_method_wrapper.Decorator import create_metrics, safe_and_metered_rpc_method
from opticalcentralizedattackdetector.proto.context_pb2 import Empty, Service
from opticalcentralizedattackdetector.proto.monitoring_pb2 import KpiList
from opticalcentralizedattackdetector.proto.centralized_attack_detector_pb2_grpc import (
    CentralizedAttackDetectorServiceServicer)
from opticalcentralizedattackdetector.proto.optical_centralized_attack_detector_pb2_grpc import (
    OpticalCentralizedAttackDetectorServiceServicer)

LOGGER = logging.getLogger(__name__)

SERVICE_NAME = 'CentralizedAttackDetector'
SERVICE_NAME = 'OpticalCentralizedAttackDetector'
METHOD_NAMES = ['NotifyServiceUpdate', 'DetectAttack', 'ReportSummarizedKpi', 'ReportKpi']
METRICS = create_metrics(SERVICE_NAME, METHOD_NAMES)

class CentralizedAttackDetectorServiceServicerImpl(CentralizedAttackDetectorServiceServicer):
class OpticalCentralizedAttackDetectorServiceServicerImpl(OpticalCentralizedAttackDetectorServiceServicer):

    def __init__(self):
        LOGGER.debug('Creating Servicer...')
+8 −8
Original line number Diff line number Diff line
import logging, signal, sys, threading
from prometheus_client import start_http_server
from common.Settings import get_setting
from centralizedattackdetector.Config import (
from opticalcentralizedattackdetector.Config import (
    GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, METRICS_PORT)
from centralizedattackdetector.service.CentralizedAttackDetectorService import CentralizedAttackDetectorService
from opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorService import OpticalCentralizedAttackDetectorService

terminate = threading.Event()
LOGGER = None
@@ -15,7 +15,7 @@ def signal_handler(signal, frame): # pylint: disable=redefined-outer-name
def main():
    global LOGGER # pylint: disable=global-statement

    service_port = get_setting('CENTRALIZEDATTACKDETECTORSERVICE_SERVICE_PORT_GRPC', default=GRPC_SERVICE_PORT)
    service_port = get_setting('OPTICALCENTRALIZEDATTACKDETECTORSERVICE_SERVICE_PORT_GRPC', default=GRPC_SERVICE_PORT)
    max_workers  = get_setting('MAX_WORKERS',                                               default=GRPC_MAX_WORKERS )
    grace_period = get_setting('GRACE_PERIOD',                                              default=GRPC_GRACE_PERIOD)
    log_level    = get_setting('LOG_LEVEL',                                                 default=LOG_LEVEL        )
@@ -33,7 +33,7 @@ def main():
    start_http_server(metrics_port)

    # Starting CentralizedCybersecurity service
    grpc_service = CentralizedAttackDetectorService(
    grpc_service = OpticalCentralizedAttackDetectorService(
        port=service_port, max_workers=max_workers, grace_period=grace_period)
    grpc_service.start()

+14 −14
Original line number Diff line number Diff line
import logging, pytest
from opticalcentralizedattackdetector.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD
from opticalcentralizedattackdetector.client.CentralizedAttackDetectorClient import CentralizedAttackDetectorClient
from opticalcentralizedattackdetector.client.OpticalCentralizedAttackDetectorClient import OpticalCentralizedAttackDetectorClient
from opticalcentralizedattackdetector.proto.context_pb2 import Empty, Service
from opticalcentralizedattackdetector.proto.monitoring_pb2 import Kpi, KpiList
from opticalcentralizedattackdetector.service.CentralizedAttackDetectorService import CentralizedAttackDetectorService
from opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorService import OpticalCentralizedAttackDetectorService

port = 10000 + GRPC_SERVICE_PORT # avoid privileged ports

@@ -11,31 +11,31 @@ LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)

@pytest.fixture(scope='session')
def centralized_attack_detector_service():
    _service = CentralizedAttackDetectorService(
def optical_centralized_attack_detector_service():
    _service = OpticalCentralizedAttackDetectorService(
        port=port, max_workers=GRPC_MAX_WORKERS, grace_period=GRPC_GRACE_PERIOD)
    _service.start()
    yield _service
    _service.stop()

@pytest.fixture(scope='session')
def centralized_attack_detector_client(centralized_attack_detector_service):
    _client = CentralizedAttackDetectorClient(address='127.0.0.1', port=port)
def optical_centralized_attack_detector_client(optical_centralized_attack_detector_service):
    _client = OpticalCentralizedAttackDetectorClient(address='127.0.0.1', port=port)
    yield _client
    _client.close()

def test_notify_service_update(centralized_attack_detector_client: CentralizedAttackDetectorClient):
def test_notify_service_update(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient):
    service = Service()
    centralized_attack_detector_client.NotifyServiceUpdate(service)
    optical_centralized_attack_detector_client.NotifyServiceUpdate(service)

def test_detect_attack(centralized_attack_detector_client: CentralizedAttackDetectorClient):
def test_detect_attack(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient):
    request = Empty()
    centralized_attack_detector_client.DetectAttack(request)
    optical_centralized_attack_detector_client.DetectAttack(request)

def test_report_summarized_kpi(centralized_attack_detector_client: CentralizedAttackDetectorClient):
def test_report_summarized_kpi(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient):
    kpi_list = KpiList()
    centralized_attack_detector_client.ReportSummarizedKpi(kpi_list)
    optical_centralized_attack_detector_client.ReportSummarizedKpi(kpi_list)

def test_report_kpi(centralized_attack_detector_client: CentralizedAttackDetectorClient):
def test_report_kpi(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient):
    kpi_list = KpiList()
    centralized_attack_detector_client.ReportKpi(kpi_list)
    optical_centralized_attack_detector_client.ReportKpi(kpi_list)