Commit 5f6777d9 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

NBI component:

- Removing unneeded database code
- Remove unneeded data files
- Remove unneeded requirements
- Remove unneeded config settings
parent 37ef2866
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -44,18 +44,6 @@ spec:
              value: "production"  # normal value is "production", change to "development" if developing
            - name: IETF_NETWORK_RENDERER
              value: "LIBYANG"
            - name: NBI_DATABASE
              value: "tfs_nbi"
            - name: CRDB_NAMESPACE
              value: "crdb"
            - name: CRDB_SQL_PORT
              value: "26257"
            - name: CRDB_USERNAME
              value: "tfs"
            - name: CRDB_PASSWORD
              value: "tfs123"
            - name: CRDB_SSLMODE
              value: "require"
          envFrom:
            - secretRef:
                name: kfk-kpi-data
+0 −4
Original line number Diff line number Diff line
@@ -35,7 +35,3 @@ requests==2.27.*
werkzeug==2.3.7
#websockets==12.0
websocket-client==1.8.0     # used by socketio to upgrate to websocket
psycopg2-binary==2.9.*
SQLAlchemy==1.4.*
sqlalchemy-cockroachdb==1.4.*
SQLAlchemy-Utils==0.38.*
+0 −18
Original line number Diff line number Diff line
@@ -20,8 +20,6 @@ from flask_restful import Api, Resource
from flask_socketio import Namespace, SocketIO
from common.tools.kafka.Variables import KafkaConfig, KafkaTopic
from nbi.Config import SECRET_KEY
from nbi.service.database.base import rebuild_database
from .database.Engine import Engine


LOGGER = logging.getLogger(__name__)
@@ -56,22 +54,6 @@ class NbiApplication:
            logger=True, engineio_logger=True
        )

        # Initialize the SQLAlchemy database engine
        LOGGER.info('Getting SQLAlchemy DB Engine...')
        self._db_engine = Engine.get_engine()
        if self._db_engine is None:
            LOGGER.error('Unable to get SQLAlchemy DB Engine. Exiting...')
            raise Exception('Unable to get SQLAlchemy DB Engine')

        # Try creating the database or log any issues
        try:
            Engine.create_database(self._db_engine)
        except Exception as e:  # More specific exception handling
            LOGGER.exception(f'Failed to check/create the database: {self._db_engine.url}. Error: {str(e)}')
            raise e

        rebuild_database(self._db_engine)

    def add_rest_api_resource(self, resource_class : Resource, *urls, **kwargs) -> None:
        self._api.add_resource(resource_class, *urls, **kwargs)

+0 −67
Original line number Diff line number Diff line
# Copyright 2022-2025 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.


import logging, sqlalchemy, sqlalchemy_utils
from typing import Optional
from common.Settings import get_setting

LOGGER = logging.getLogger(__name__)

APP_NAME = 'tfs'
ECHO = False  # true: dump SQL commands and transactions executed
CRDB_URI_TEMPLATE = (
    'cockroachdb://{:s}:{:s}@cockroachdb-public.{:s}.svc.cluster.local:{:s}/{:s}?sslmode={:s}'
)


class Engine:
    @staticmethod
    def get_engine() -> Optional[sqlalchemy.engine.Engine]:
        crdb_uri = get_setting('CRDB_URI', default=None)
        if crdb_uri is None:
            CRDB_NAMESPACE = get_setting('CRDB_NAMESPACE')
            CRDB_SQL_PORT = get_setting('CRDB_SQL_PORT')
            CRDB_DATABASE = get_setting('NBI_DATABASE')
            CRDB_USERNAME = get_setting('CRDB_USERNAME')
            CRDB_PASSWORD = get_setting('CRDB_PASSWORD')
            CRDB_SSLMODE = get_setting('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
            )
        except:  # pylint: disable=bare-except # pragma: no cover
            LOGGER.exception('Failed to connect to database: {:s}'.format(str(crdb_uri)))
            return None

        return engine

    @staticmethod
    def create_database(engine: sqlalchemy.engine.Engine) -> None:
        if not sqlalchemy_utils.database_exists(engine.url):
            sqlalchemy_utils.create_database(engine.url)

    @staticmethod
    def drop_database(engine: sqlalchemy.engine.Engine) -> None:
        if sqlalchemy_utils.database_exists(engine.url):
            sqlalchemy_utils.drop_database(engine.url)
+0 −14
Original line number Diff line number Diff line
# Copyright 2022-2025 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.
Loading