diff --git a/manifests/nginx_ingress_http.yaml b/manifests/nginx_ingress_http.yaml index ed713bf29ad8228ab3f5b051af24519c2fb9ef09..619d85f7a82af48c71464038bf14a833903d1a58 100644 --- a/manifests/nginx_ingress_http.yaml +++ b/manifests/nginx_ingress_http.yaml @@ -66,6 +66,6 @@ spec: pathType: Prefix backend: service: - name: qkd-appservice + name: nbiservice port: - number: 8005 + number: 8080 diff --git a/manifests/qkd_appservice.yaml b/manifests/qkd_appservice.yaml index 4f89d6c6f8400b509dc595f551e8f181e70b2f51..ba02e2e1da34ac670f68e17c28e9847603401d3a 100644 --- a/manifests/qkd_appservice.yaml +++ b/manifests/qkd_appservice.yaml @@ -28,36 +28,35 @@ spec: spec: terminationGracePeriodSeconds: 5 containers: - - name: server - image: labs.etsi.org:5050/tfs/controller/qkd_app:latest - imagePullPolicy: Always - ports: - - containerPort: 10060 - - containerPort: 9192 - - containerPort: 8005 - env: - - name: LOG_LEVEL - value: "DEBUG" - - name: CRDB_DATABASE_APP - value: "qkd_app" - envFrom: - - secretRef: - name: crdb-data - - secretRef: - name: nats-data - readinessProbe: - exec: - command: ["/bin/grpc_health_probe", "-addr=:10060"] - livenessProbe: - exec: - command: ["/bin/grpc_health_probe", "-addr=:10060"] - resources: - requests: - cpu: 150m - memory: 128Mi - limits: - cpu: 500m - memory: 512Mi + - name: server + image: labs.etsi.org:5050/tfs/controller/qkd_app:latest + imagePullPolicy: Always + ports: + - containerPort: 10060 + - containerPort: 9192 + env: + - name: LOG_LEVEL + value: "INFO" + - name: CRDB_DATABASE + value: "qkd_app" + envFrom: + - secretRef: + name: crdb-data + - secretRef: + name: nats-data + readinessProbe: + exec: + command: ["/bin/grpc_health_probe", "-addr=:10060"] + livenessProbe: + exec: + command: ["/bin/grpc_health_probe", "-addr=:10060"] + resources: + requests: + cpu: 150m + memory: 128Mi + limits: + cpu: 500m + memory: 512Mi --- apiVersion: v1 kind: Service @@ -70,14 +69,11 @@ spec: selector: app: qkd-appservice ports: - - name: grpc - protocol: TCP - port: 10060 - targetPort: 10060 - - name: metrics - protocol: TCP - port: 9192 - targetPort: 9192 - - name: http - port: 8005 - targetPort: 8005 + - name: grpc + protocol: TCP + port: 10060 + targetPort: 10060 + - name: metrics + protocol: TCP + port: 9192 + targetPort: 9192 diff --git a/src/common/Constants.py b/src/common/Constants.py index 8b2e215a0ee669726430d12ea4ebac334f69c1ce..78cf76b0077638251cc95c2f294a1a075ac2a27b 100644 --- a/src/common/Constants.py +++ b/src/common/Constants.py @@ -114,15 +114,12 @@ DEFAULT_SERVICE_GRPC_PORTS = { # Default HTTP/REST-API service ports DEFAULT_SERVICE_HTTP_PORTS = { - ServiceNameEnum.CONTEXT .value : 8080, - ServiceNameEnum.NBI .value : 8080, - ServiceNameEnum.WEBUI .value : 8004, - ServiceNameEnum.QKD_APP .value : 8005, + ServiceNameEnum.NBI .value : 8080, + ServiceNameEnum.WEBUI.value : 8004, } # Default HTTP/REST-API service base URLs DEFAULT_SERVICE_HTTP_BASEURLS = { - ServiceNameEnum.NBI .value : None, - ServiceNameEnum.WEBUI .value : None, - ServiceNameEnum.QKD_APP .value : None, + ServiceNameEnum.NBI .value : None, + ServiceNameEnum.WEBUI.value : None, } diff --git a/src/common/Settings.py b/src/common/Settings.py index 13fcfc76966301599b0f5f39f2b188aea4e4d52a..5845ee5221be69e5d5cf00b60d9376a7fd6fcec3 100644 --- a/src/common/Settings.py +++ b/src/common/Settings.py @@ -15,9 +15,11 @@ import logging, os, re, time from typing import Dict, List from common.Constants import ( - DEFAULT_GRPC_BIND_ADDRESS, DEFAULT_GRPC_GRACE_PERIOD, DEFAULT_GRPC_MAX_WORKERS, DEFAULT_HTTP_BIND_ADDRESS, - DEFAULT_LOG_LEVEL, DEFAULT_METRICS_PORT, DEFAULT_SERVICE_GRPC_PORTS, DEFAULT_SERVICE_HTTP_BASEURLS, - DEFAULT_SERVICE_HTTP_PORTS, ServiceNameEnum + DEFAULT_GRPC_BIND_ADDRESS, DEFAULT_GRPC_GRACE_PERIOD, + DEFAULT_GRPC_MAX_WORKERS, DEFAULT_HTTP_BIND_ADDRESS, + DEFAULT_LOG_LEVEL, DEFAULT_METRICS_PORT, DEFAULT_SERVICE_GRPC_PORTS, + DEFAULT_SERVICE_HTTP_BASEURLS, DEFAULT_SERVICE_HTTP_PORTS, + ServiceNameEnum ) LOGGER = logging.getLogger(__name__) diff --git a/src/nbi/service/__main__.py b/src/nbi/service/__main__.py index 58fbb9625addc43c6b62d06d7a9caa3f648203d5..8f4ef87e03a3954227d777ab06c220d373b70c08 100644 --- a/src/nbi/service/__main__.py +++ b/src/nbi/service/__main__.py @@ -16,9 +16,10 @@ import logging, signal, sys, threading from prometheus_client import start_http_server from common.Constants import ServiceNameEnum from common.Settings import ( - ENVVAR_SUFIX_SERVICE_HOST, ENVVAR_SUFIX_SERVICE_PORT_GRPC, get_env_var_name, get_log_level, get_metrics_port, - wait_for_environment_variables) - + ENVVAR_SUFIX_SERVICE_HOST, ENVVAR_SUFIX_SERVICE_PORT_GRPC, + get_env_var_name, get_log_level, get_metrics_port, + wait_for_environment_variables +) from .NbiService import NbiService from .rest_server.RestServer import RestServer from .rest_server.nbi_plugins.etsi_bwm import register_etsi_bwm_api @@ -28,6 +29,7 @@ from .rest_server.nbi_plugins.ietf_l3vpn import register_ietf_l3vpn from .rest_server.nbi_plugins.ietf_network import register_ietf_network from .rest_server.nbi_plugins.ietf_network_slice import register_ietf_nss from .rest_server.nbi_plugins.ietf_acl import register_ietf_acl +from .rest_server.nbi_plugins.qkd_app import register_qkd_app from .rest_server.nbi_plugins.tfs_api import register_tfs_api terminate = threading.Event() @@ -72,6 +74,7 @@ def main(): register_ietf_network(rest_server) register_ietf_nss(rest_server) # Registering NSS entrypoint register_ietf_acl(rest_server) + register_qkd_app(rest_server) register_tfs_api(rest_server) rest_server.start() diff --git a/src/qkd_app/service/rest_server/qkd_app/Resources.py b/src/nbi/service/rest_server/nbi_plugins/qkd_app/Resources.py similarity index 100% rename from src/qkd_app/service/rest_server/qkd_app/Resources.py rename to src/nbi/service/rest_server/nbi_plugins/qkd_app/Resources.py diff --git a/src/qkd_app/service/rest_server/qkd_app/__init__.py b/src/nbi/service/rest_server/nbi_plugins/qkd_app/__init__.py similarity index 67% rename from src/qkd_app/service/rest_server/qkd_app/__init__.py rename to src/nbi/service/rest_server/nbi_plugins/qkd_app/__init__.py index 6fc23b371414dcb2bac4afde63524febf71e5337..30982f104e189eb4af5b3f49b1abe2b0e5fb2d85 100644 --- a/src/qkd_app/service/rest_server/qkd_app/__init__.py +++ b/src/nbi/service/rest_server/nbi_plugins/qkd_app/__init__.py @@ -4,7 +4,7 @@ # 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 +# 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, @@ -12,19 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -from qkd_app.service.rest_server.RestServer import RestServer -from .Resources import ( - CreateQKDApp, Index) +from nbi.service.rest_server.RestServer import RestServer +from .Resources import CreateQKDApp, Index URL_PREFIX = '/qkd_app' # Use 'path' type since some identifiers might contain char '/' and Flask is unable to recognize them in 'string' type. RESOURCES = [ # (endpoint_name, resource_class, resource_url) - ('api.index', Index, '/'), - ('api.register_qkd_app', CreateQKDApp, '/create_qkd_app'), + ('api.index', Index, '/'), + ('api.register_qkd_app', CreateQKDApp, '/create_qkd_app'), ] -def register_qkd_app(app_server : RestServer): +def register_qkd_app(rest_server : RestServer): for endpoint_name, resource_class, resource_url in RESOURCES: - app_server.add_resource(resource_class, URL_PREFIX + resource_url, endpoint=endpoint_name) + rest_server.add_resource(resource_class, URL_PREFIX + resource_url, endpoint=endpoint_name) diff --git a/src/qkd_app/service/__main__.py b/src/qkd_app/service/__main__.py index ed9bd011e1ae7bdd69165a2ed66ba46d97356130..17f3ac240c33b28454e2df43862dd66a50eb5c45 100644 --- a/src/qkd_app/service/__main__.py +++ b/src/qkd_app/service/__main__.py @@ -4,7 +4,7 @@ # 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 +# 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, @@ -17,21 +17,21 @@ import signal import sys import threading from prometheus_client import start_http_server -#from common.Constants import ServiceNameEnum -from common.Settings import ( - #ENVVAR_SUFIX_SERVICE_HOST, ENVVAR_SUFIX_SERVICE_PORT_GRPC, get_env_var_name, - get_log_level, get_metrics_port, wait_for_environment_variables) +from common.Settings import get_log_level, get_metrics_port from qkd_app.service.QKDAppService import AppService -from qkd_app.service.rest_server.RestServer import RestServer -from qkd_app.service.rest_server.qkd_app import register_qkd_app -#from common.message_broker.Factory import get_messagebroker_backend -#from common.message_broker.MessageBroker import MessageBroker from qkd_app.service.database.Engine import Engine from qkd_app.service.database.models._Base import rebuild_database +# Set up logging +LOG_LEVEL = get_log_level() +logging.basicConfig(level=LOG_LEVEL, format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s") +LOGGER = logging.getLogger(__name__) + +#LOGGER.addHandler(logging.StreamHandler(stream=sys.stderr)) +#LOGGER.setLevel(logging.WARNING) + # Event for terminating the service gracefully terminate = threading.Event() -LOGGER: logging.Logger = None def signal_handler(signum, frame): """ @@ -41,25 +41,11 @@ def signal_handler(signum, frame): terminate.set() def main(): - global LOGGER # Required due to global scope - - # Set up logging - log_level = get_log_level() - logging.basicConfig(level=log_level, format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s") - LOGGER = logging.getLogger(__name__) - - # Ensure necessary environment variables are set - wait_for_environment_variables([ - #get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_HOST ), - #get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_PORT_GRPC), - ]) - + LOGGER.info('Starting...') # Register signal handlers for graceful shutdown - signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) - LOGGER.info('Starting...') - # Start Prometheus metrics server metrics_port = get_metrics_port() start_http_server(metrics_port) @@ -90,13 +76,9 @@ def main(): grpc_service = AppService(db_engine, messagebroker) grpc_service.start() - # Start the REST server and register QKD apps - rest_server = RestServer() - register_qkd_app(rest_server) - rest_server.start() - LOGGER.info('Services started. Waiting for termination signal...') + # Wait for Ctrl+C or termination signal # Keep the process running until a termination signal is received while not terminate.wait(timeout=1.0): pass @@ -104,8 +86,6 @@ def main(): # Shutdown services gracefully on termination LOGGER.info('Terminating services...') grpc_service.stop() - rest_server.shutdown() - rest_server.join() LOGGER.info('Shutdown complete. Exiting...') return 0 diff --git a/src/qkd_app/service/database/Engine.py b/src/qkd_app/service/database/Engine.py index 8f528f9a1b3cacca2ea260901ab808461dd3183d..ec86618218cc396dea31404b88d33e46d320ed58 100644 --- a/src/qkd_app/service/database/Engine.py +++ b/src/qkd_app/service/database/Engine.py @@ -28,7 +28,7 @@ class Engine: if crdb_uri is None: CRDB_NAMESPACE = get_setting('CRDB_NAMESPACE') CRDB_SQL_PORT = get_setting('CRDB_SQL_PORT') - CRDB_DATABASE = get_setting('CRDB_DATABASE_APP') + CRDB_DATABASE = get_setting('CRDB_DATABASE') CRDB_USERNAME = get_setting('CRDB_USERNAME') CRDB_PASSWORD = get_setting('CRDB_PASSWORD') CRDB_SSLMODE = get_setting('CRDB_SSLMODE') diff --git a/src/qkd_app/service/rest_server/RestServer.py b/src/qkd_app/service/rest_server/RestServer.py deleted file mode 100644 index b2b830a8171b666eaafa695a1877868ce562b672..0000000000000000000000000000000000000000 --- a/src/qkd_app/service/rest_server/RestServer.py +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright 2022-2024 ETSI OSG/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. - -from common.Constants import ServiceNameEnum -from common.Settings import get_service_baseurl_http, get_service_port_http -from common.tools.service.GenericRestServer import GenericRestServer - -class RestServer(GenericRestServer): - """ - REST server class for handling HTTP requests related to QKD applications. - - Productivity Improvements: - - Simplifies initialization by automatically fetching the required service details (base URL and port). - - Extends a generic REST server class, ensuring reusability and consistency across services. - """ - def __init__(self, cls_name: str = __name__) -> None: - """ - Initialize the REST server with service-specific configuration. - - - Retrieves the port and base URL dynamically based on the QKD_APP service. - - Inherits from `GenericRestServer` for common REST functionality, reducing code duplication. - - Arguments: - cls_name (str): Name of the current class (default: `__name__`). - """ - # Dynamically fetch the HTTP bind port and base URL for the QKD_APP service - bind_port = get_service_port_http(ServiceNameEnum.QKD_APP) - base_url = get_service_baseurl_http(ServiceNameEnum.QKD_APP) - - # Pass these values to the parent `GenericRestServer` class for initialization - super().__init__(bind_port, base_url, cls_name=cls_name) - diff --git a/src/qkd_app/service/rest_server/__init__.py b/src/qkd_app/service/rest_server/__init__.py deleted file mode 100644 index 07d08814021ef82220611ee21c01ba01806682e9..0000000000000000000000000000000000000000 --- a/src/qkd_app/service/rest_server/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright 2022-2024 ETSI OSG/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.