Newer
Older
# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (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.
Alberto Gonzalez Barneo
committed
import logging
import sqlalchemy
Alberto Gonzalez Barneo
committed
from common.Constants import ServiceNameEnum
from common.Settings import get_service_port_grpc
from common.message_broker.MessageBroker import MessageBroker
Alberto Gonzalez Barneo
committed
from common.proto.qkd_app_pb2_grpc import add_AppServiceServicer_to_server
Alberto Gonzalez Barneo
committed
from common.tools.service.GenericGrpcService import GenericGrpcService
Alberto Gonzalez Barneo
committed
from qkd_app.service.QKDAppServiceServicerImpl import AppServiceServicerImpl
Alberto Gonzalez Barneo
committed
Alberto Gonzalez Barneo
committed
# Configure maximum number of workers for gRPC
GRPC_MAX_WORKERS = 200 # Adjusted for high concurrency
Alberto Gonzalez Barneo
committed
LOGGER = logging.getLogger(__name__)
class AppService(GenericGrpcService):
Alberto Gonzalez Barneo
committed
"""
gRPC Service for handling QKD App-related operations.
This class initializes the gRPC server and installs the servicers.
"""
Alberto Gonzalez Barneo
committed
def __init__(
Alberto Gonzalez Barneo
committed
self, db_engine: sqlalchemy.engine.Engine, messagebroker: MessageBroker, cls_name: str = __name__
Alberto Gonzalez Barneo
committed
) -> None:
Alberto Gonzalez Barneo
committed
"""
Initializes the AppService with the provided database engine and message broker.
Sets up the gRPC server to handle app-related requests.
Args:
db_engine (sqlalchemy.engine.Engine): Database engine for handling app data.
messagebroker (MessageBroker): Message broker for inter-service communication.
cls_name (str): Class name for logging purposes (default is __name__).
"""
# Get the port for the gRPC AppService
port = get_service_port_grpc(ServiceNameEnum.QKD_APP)
Alberto Gonzalez Barneo
committed
# Initialize the base class with port and max worker configuration
Alberto Gonzalez Barneo
committed
super().__init__(port, max_workers=GRPC_MAX_WORKERS, cls_name=cls_name)
Alberto Gonzalez Barneo
committed
# Initialize the AppServiceServicer with the database and message broker
Alberto Gonzalez Barneo
committed
self.app_servicer = AppServiceServicerImpl(db_engine, messagebroker)
def install_servicers(self):
Alberto Gonzalez Barneo
committed
"""
Installs the AppService servicers to the gRPC server.
This allows the server to handle requests for QKD app operations.
"""
Alberto Gonzalez Barneo
committed
add_AppServiceServicer_to_server(self.app_servicer, self.server)
Alberto Gonzalez Barneo
committed
LOGGER.debug("AppService servicer installed")