Skip to content
__init__.py 4.1 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (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.

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from typing import List, Tuple, Union
from flask_healthz import healthz, HealthError
from context.client.ContextClient import ContextClient
from device.client.DeviceClient import DeviceClient

def get_working_context() -> str:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    return session['context_uuid'] if 'context_uuid' in session else '---'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

def get_working_topology() -> str:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    return session['topology_uuid'] if 'topology_uuid' in session else '---'

def liveness():
    pass

def readiness():
    try:  # this component is ready when it is able to connect with the other components it depends on
        context_client = ContextClient()
        context_client.connect()
        context_client.close()
        device_client = DeviceClient()
        device_client.connect()
        device_client.close()
    except Exception as e:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        raise HealthError("Can't connect with the service: {:s}".format(str(e))) from e
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def json_to_list(json_str : str) -> List[Union[str, Tuple[str, str]]]:
    try:
        data = json.loads(json_str)
    except: # pylint: disable=bare-except
        return [('item', str(json_str))]

    if isinstance(data, dict):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return [('kv', (key, value)) for key, value in data.items()]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    elif isinstance(data, list):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return [('item', ', '.join(data))]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    else:
        return [('item', str(data))]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
class SetSubAppMiddleware():
    def __init__(self, app, web_app_root):
        self.app = app
        self.web_app_root = web_app_root
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = self.web_app_root
        environ['APPLICATION_ROOT'] = self.web_app_root
        return self.app(environ, start_response)

def create_app(use_config=None, web_app_root=None):
    app = Flask(__name__)
    if use_config:
        app.config.from_mapping(**use_config)
    
    app.config.update(HEALTHZ={
        'live': liveness,
        'ready': readiness
    })
    
    app.register_blueprint(healthz, url_prefix='/healthz')

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from webui.service.js.routes import js                  # pylint: disable=import-outside-toplevel
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    app.register_blueprint(js)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from webui.service.main.routes import main              # pylint: disable=import-outside-toplevel
    app.register_blueprint(main)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from webui.service.load_gen.routes import load_gen      # pylint: disable=import-outside-toplevel
    app.register_blueprint(load_gen)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from webui.service.service.routes import service        # pylint: disable=import-outside-toplevel
    app.register_blueprint(service)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from webui.service.slice.routes import slice            # pylint: disable=import-outside-toplevel,redefined-builtin
longllu's avatar
longllu committed
    app.register_blueprint(slice)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from webui.service.device.routes import device          # pylint: disable=import-outside-toplevel
    app.register_blueprint(device)
Pablo Armingol's avatar
Pablo Armingol committed
    
    from webui.service.topology.routes import topology          # pylint: disable=import-outside-toplevel
    app.register_blueprint(topology)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from webui.service.link.routes import link              # pylint: disable=import-outside-toplevel
    app.register_blueprint(link)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    app.jinja_env.globals.update({              # pylint: disable=no-member
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        'enumerate'           : enumerate,
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        'json_to_list'        : json_to_list,
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        'round'               : round,
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        'get_working_context' : get_working_context,
        'get_working_topology': get_working_topology,
    })
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    if web_app_root is not None:
        app.wsgi_app = SetSubAppMiddleware(app.wsgi_app, web_app_root)