Commit 7d86e582 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Pre-merge code cleanup

parent 6d29832c
Loading
Loading
Loading
Loading
+0 −80
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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
from enum import Enum

# Default logging level
DEFAULT_LOG_LEVEL = logging.WARNING

# Default gRPC server settings
DEFAULT_GRPC_BIND_ADDRESS = '0.0.0.0'
DEFAULT_GRPC_MAX_WORKERS  = 10
DEFAULT_GRPC_GRACE_PERIOD = 60

# Default HTTP server settings
DEFAULT_HTTP_BIND_ADDRESS = '0.0.0.0'

# Default Prometheus settings
DEFAULT_METRICS_PORT = 9192

# Default context and topology UUIDs
DEFAULT_CONTEXT_UUID = 'admin'
DEFAULT_TOPOLOGY_UUID = 'admin'

# Default service names
class ServiceNameEnum(Enum):
    CONTEXT       = 'context'
    DEVICE        = 'device'
    SERVICE       = 'service'
    SLICE         = 'slice'
    AUTOMATION    = 'automation'
    POLICY        = 'policy'
    MONITORING    = 'monitoring'
    DLT           = 'dlt'
    COMPUTE       = 'compute'
    CYBERSECURITY = 'cybersecurity'
    INTERDOMAIN   = 'interdomain'
    PATHCOMP      = 'pathcomp'
    WEBUI         = 'webui'

# Default gRPC service ports
DEFAULT_SERVICE_GRPC_PORTS = {
    ServiceNameEnum.CONTEXT      .value :  1010,
    ServiceNameEnum.DEVICE       .value :  2020,
    ServiceNameEnum.SERVICE      .value :  3030,
    ServiceNameEnum.SLICE        .value :  4040,
    ServiceNameEnum.AUTOMATION   .value :  5050,
    ServiceNameEnum.POLICY       .value :  6060,
    ServiceNameEnum.MONITORING   .value :  7070,
    ServiceNameEnum.DLT          .value :  8080,
    ServiceNameEnum.COMPUTE      .value :  9090,
    ServiceNameEnum.CYBERSECURITY.value : 10000,
    ServiceNameEnum.INTERDOMAIN  .value : 10010,
    ServiceNameEnum.PATHCOMP     .value : 10020,
}

# Default HTTP/REST-API service ports
DEFAULT_SERVICE_HTTP_PORTS = {
    ServiceNameEnum.CONTEXT   .value : 8080,
    ServiceNameEnum.COMPUTE   .value : 8080,
    ServiceNameEnum.WEBUI     .value : 8004,
}

# Default HTTP/REST-API service base URLs
DEFAULT_SERVICE_HTTP_BASEURLS = {
    ServiceNameEnum.CONTEXT   .value : '/api',
    ServiceNameEnum.COMPUTE   .value : '/restconf/data',
    ServiceNameEnum.WEBUI     .value : None,
}
+0 −26
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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 enum import Enum

class DeviceTypeEnum(Enum):
    EMULATED_OPTICAL_LINE_SYSTEM = 'emu-optical-line-system'
    EMULATED_PACKET_ROUTER       = 'emu-packet-router'
    MICROVAWE_RADIO_SYSTEM       = 'microwave-radio-system'
    OPTICAL_ROADM                = 'optical-roadm'
    OPTICAL_TRANDPONDER          = 'optical-trandponder'
    OPTICAL_LINE_SYSTEM          = 'optical-line-system'
    PACKET_ROUTER                = 'packet-router'
    PACKET_SWITCH                = 'packet-switch'
    P4_SWITCH                    = 'p4-switch'
+0 −98
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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, os, time
from typing import 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
)

LOGGER = logging.getLogger(__name__)

DEFAULT_RESTART_DELAY = 5.0 # seconds

ENVVAR_KUBERNETES_PORT            = 'KUBERNETES_PORT'
ENVVAR_GRPC_BIND_ADDRESS          = 'GRPC_BIND_ADDRESS'
ENVVAR_GRPC_MAX_WORKERS           = 'GRPC_MAX_WORKERS'
ENVVAR_GRPC_GRACE_PERIOD          = 'GRPC_GRACE_PERIOD'
ENVVAR_HTTP_BIND_ADDRESS          = 'HTTP_BIND_ADDRESS'
ENVVAR_LOG_LEVEL                  = 'LOG_LEVEL'
ENVVAR_METRICS_PORT               = 'METRICS_PORT'

ENVVAR_SUFIX_SERVICE_BASEURL_HTTP = 'SERVICE_BASEURL_HTTP'
ENVVAR_SUFIX_SERVICE_HOST         = 'SERVICE_HOST'
ENVVAR_SUFIX_SERVICE_PORT_GRPC    = 'SERVICE_PORT_GRPC'
ENVVAR_SUFIX_SERVICE_PORT_HTTP    = 'SERVICE_PORT_HTTP'

def wait_for_environment_variables(
    required_environment_variables : List[str] = [], wait_delay_seconds : float = DEFAULT_RESTART_DELAY
):
    if ENVVAR_KUBERNETES_PORT not in os.environ: return # We're not running in Kubernetes, nothing to wait for
    missing_variables = set(required_environment_variables).difference(set(os.environ.keys()))
    if len(missing_variables) == 0: return # We have all environment variables defined
    msg = 'Variables({:s}) are missing in Environment({:s}), restarting in {:f} seconds...'
    LOGGER.error(msg.format(str(missing_variables), str(os.environ), wait_delay_seconds))
    time.sleep(wait_delay_seconds)
    raise Exception('Restarting...')

def get_setting(name, **kwargs):
    value = os.environ.get(name)
    if 'settings' in kwargs:
        value = kwargs['settings'].pop(name, value)
    if value is not None: return value
    if 'default' in kwargs: return kwargs['default']
    raise Exception('Setting({:s}) not specified in environment or configuration'.format(str(name)))

def get_env_var_name(service_name : ServiceNameEnum, env_var_group):
    return ('{:s}SERVICE_{:s}'.format(service_name.value, env_var_group)).upper()

def get_service_host(service_name : ServiceNameEnum):
    envvar_name = get_env_var_name(service_name, ENVVAR_SUFIX_SERVICE_HOST)
    default_value = ('{:s}service'.format(service_name.value))
    return get_setting(envvar_name, default=default_value)

def get_service_port_grpc(service_name : ServiceNameEnum):
    envvar_name = get_env_var_name(service_name, ENVVAR_SUFIX_SERVICE_PORT_GRPC)
    default_value = DEFAULT_SERVICE_GRPC_PORTS.get(service_name.value)
    return get_setting(envvar_name, default=default_value)

def get_service_port_http(service_name : ServiceNameEnum):
    envvar_name = get_env_var_name(service_name, ENVVAR_SUFIX_SERVICE_PORT_HTTP)
    default_value = DEFAULT_SERVICE_HTTP_PORTS.get(service_name.value)
    return get_setting(envvar_name, default=default_value)

def get_service_baseurl_http(service_name : ServiceNameEnum):
    envvar_name = get_env_var_name(service_name, ENVVAR_SUFIX_SERVICE_BASEURL_HTTP)
    default_value = DEFAULT_SERVICE_HTTP_BASEURLS.get(service_name.value)
    return get_setting(envvar_name, default=default_value)

def get_log_level():
    return get_setting(ENVVAR_LOG_LEVEL, default=DEFAULT_LOG_LEVEL)

def get_metrics_port():
    return get_setting(ENVVAR_METRICS_PORT, default=DEFAULT_METRICS_PORT)

def get_grpc_bind_address():
    return get_setting(ENVVAR_GRPC_BIND_ADDRESS, default=DEFAULT_GRPC_BIND_ADDRESS)

def get_grpc_max_workers():
    return get_setting(ENVVAR_GRPC_MAX_WORKERS, default=DEFAULT_GRPC_MAX_WORKERS)

def get_grpc_grace_period():
    return get_setting(ENVVAR_GRPC_GRACE_PERIOD, default=DEFAULT_GRPC_GRACE_PERIOD)

def get_http_bind_address():
    return get_setting(ENVVAR_HTTP_BIND_ADDRESS, default=DEFAULT_HTTP_BIND_ADDRESS)
+0 −14
Original line number Diff line number Diff line
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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.
+0 −31
Original line number Diff line number Diff line
from enum import Enum

class SliceStatus(Enum):
    PLANNED = 0
    INIT    = 1
    ACTIVE  = 2
    DEINIT  = 3

ANY_TO_ENUM = {
    0: SliceStatus.PLANNED,
    1: SliceStatus.INIT,
    2: SliceStatus.ACTIVE,
    3: SliceStatus.DEINIT,

    '0': SliceStatus.PLANNED,
    '1': SliceStatus.INIT,
    '2': SliceStatus.ACTIVE,
    '3': SliceStatus.DEINIT,

    'planned': SliceStatus.PLANNED,
    'init': SliceStatus.INIT,
    'active': SliceStatus.ACTIVE,
    'deinit': SliceStatus.DEINIT,
}

def slicestatus_enum_values():
    return {m.value for m in SliceStatus.__members__.values()}

def to_slicestatus_enum(int_or_str):
    if isinstance(int_or_str, str): int_or_str = int_or_str.lower()
    return ANY_TO_ENUM.get(int_or_str)
Loading