Newer
Older
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# 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 concurrent import futures
import grpc
from grpc_health.v1.health import OVERALL_HEALTH, HealthServicer
from grpc_health.v1.health_pb2 import HealthCheckResponse
from grpc_health.v1.health_pb2_grpc import add_HealthServicer_to_server
from common.Constants import (DEFAULT_GRPC_BIND_ADDRESS,
DEFAULT_GRPC_GRACE_PERIOD,
DEFAULT_GRPC_MAX_WORKERS)
from common.proto.optical_attack_detector_pb2_grpc import \
add_OpticalAttackDetectorServiceServicer_to_server
from opticalattackdetector.Config import GRPC_SERVICE_PORT
from opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl import \
OpticalAttackDetectorServiceServicerImpl
LOGGER = logging.getLogger(__name__)
class OpticalAttackDetectorService:
def __init__(
self,
address=DEFAULT_GRPC_BIND_ADDRESS,
port=GRPC_SERVICE_PORT,
max_workers=DEFAULT_GRPC_MAX_WORKERS,
grace_period=DEFAULT_GRPC_GRACE_PERIOD,
):
self.address = address
self.port = port
self.endpoint = None
self.max_workers = max_workers
self.grace_period = grace_period
self.attack_detector_servicer = None
self.health_servicer = None
self.pool = None
self.server = None
def start(self):
self.endpoint = "{:s}:{:s}".format(str(self.address), str(self.port))
LOGGER.debug(
"Starting Service (tentative endpoint: {:s}, max_workers: {:s})...".format(
str(self.endpoint), str(self.max_workers)
)
)
self.pool = futures.ThreadPoolExecutor(max_workers=self.max_workers)
self.server = grpc.server(self.pool) # , interceptors=(tracer_interceptor,))
self.attack_detector_servicer = OpticalAttackDetectorServiceServicerImpl()
add_OpticalAttackDetectorServiceServicer_to_server(
self.attack_detector_servicer, self.server
)
self.health_servicer = HealthServicer(
experimental_non_blocking=True,
experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1),
)
add_HealthServicer_to_server(self.health_servicer, self.server)
port = self.server.add_insecure_port(self.endpoint)
self.endpoint = "{:s}:{:s}".format(str(self.address), str(port))
LOGGER.info("Listening on {:s}...".format(self.endpoint))
self.server.start()
self.health_servicer.set(
OVERALL_HEALTH, HealthCheckResponse.SERVING
) # pylint: disable=maybe-no-member
LOGGER.debug("Service started")
def stop(self):
LOGGER.debug(
"Stopping service (grace period {:s} seconds)...".format(
str(self.grace_period)
)
)
self.health_servicer.enter_graceful_shutdown()
self.server.stop(self.grace_period)
LOGGER.debug("Service stopped")