#!/usr/bin/python3
"""
// © University of Southampton IT Innovation Centre, 2018
//
// Copyright in this software belongs to University of Southampton
// IT Innovation Centre of Gamma House, Enterprise Road,
// Chilworth Science Park, Southampton, SO16 7NS, UK.
//
// This software may not be used, sold, licensed, transferred, copied
// or reproduced in whole or in part in any manner or form or in or
// on any media by any person other than in accordance with the terms
// of the Licence Agreement supplied with the software, or otherwise
// without the prior written consent of the copyright owners.
//
// This software is distributed WITHOUT ANY WARRANTY, without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE, except where stated in the Licence Agreement supplied with
// the software.
//
//      Created By :            Nikolay Stanchev
//      Created Date :          15-05-2018
//      Created for Project :   FLAME
"""


# Python standard libs
from os.path import dirname, abspath
import os

# PIP installed libs
from pyramid.config import Configurator
from sqlalchemy import engine_from_config

# CLMC-service imports
from clmcservice.models.meta import DBSession, Base


ROOT_DIR = dirname(abspath(__file__))  # get the path of the root package (clmcservice) as a global variable


def main(global_config, **settings):
    """
    This function returns a Pyramid WSGI application.
    """

    engine = engine_from_config(settings, 'sqlalchemy.')  # initialise a database engine by using the 'sqlalchemy' setting in the configuration .ini file
    DBSession.configure(bind=engine)  # bind the engine to a DB session
    Base.metadata.bind = engine  # bind the engine to the Base class metadata

    settings['sfemc_fqdn'] = os.environ['SFEMC_FQDN']  # read the SFEMC FQDN from the OS environment
    settings['sfemc_port'] = int(os.environ.get('SFEMC_PORT', 8080))  # read the SFEMC port number from the OS environment, if not set use 8080 as default
    settings['sdn_controller_ip'] = os.environ['SDN_CONTROLLER_IP']  # read the SDN controller IP address from the OS environment
    settings['sdn_controller_port'] = int(os.environ.get('SDN_CONTROLLER_PORT', 8080))  # read the SDN controller port number from the OS environment, if not set use 8080 as default

    settings['influx_port'] = int(settings['influx_port'])  # the influx port setting must be converted to integer instead of a string
    settings['kapacitor_port'] = int(settings['kapacitor_port'])  # the kapacitor port setting must be converted to integer instead of a string

    settings["network_bandwidth"] = int(settings["network_bandwidth"])  # TODO currently assumed fixed bandwidth across all links

    config = Configurator(settings=settings)

    config.add_tween('clmcservice.tweens.accept_header_tween_factory')

    # add routes of the WHOAMI API
    config.add_route('whoami_endpoints', '/whoami/endpoints')
    config.add_route('whoami_endpoints_instance', 'whoami/endpoints/instance')

    # add routes of the GRAPH API
    config.add_route('graph_build', '/graph/temporal')
    config.add_route('graph_manage', '/graph/temporal/{graph_id}')
    config.add_route('graph_algorithms_rtt', '/graph/temporal/{graph_id}/round-trip-time')
    config.add_route('graph_network_topology', '/graph/network')
    config.add_route('graph_execute_pipeline', '/graph/monitor')
    config.add_route('graph_manage_pipeline', '/graph/monitor/{request_id}')

    # add routes of the Alerts Configuration API
    config.add_route('alerts_configuration', '/alerts')

    config.scan()  # This method scans the packages and finds any views related to the routes added in the app configuration
    return config.make_wsgi_app()