Commit 4a0c12b4 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

First functional version of Device after rework of Database API.

Device:
- Implemented Driver API with some special resources to enable retrieving endpoints from devices, interfaces, and network instances.
- Implemented special resource_keys (_connect/*) to enable AddDevice to obtain settings used to connect to remote devices (address, port, username, password, etc.)
- Implemented Emulated Driver (only for testing)
- Implemented OpenConfig Driver (missing to implement monitoring; templates for Infinera devices to be tested with other device vendors)
- Minor bugs and code improvements
- Improved test unit to enable easy testing of vendor devices and keep credentials secret.
parent ad32a828
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
syntax = "proto3";
package context;

import "kpi_sample_types.proto";
//import "kpi_sample_types.proto";

service ContextService {
  rpc ListContextIds   (Empty     ) returns (       ContextIdList ) {}
@@ -243,6 +243,7 @@ message EndPointId {
message EndPoint {
  EndPointId endpoint_id = 1;
  string endpoint_type = 2;
  //repeated kpi_sample_types.KpiSampleType kpi_sample_types = 3;
}


@@ -257,7 +258,6 @@ message ConfigRule {
  ConfigActionEnum action = 1;
  string resource_key = 2;
  string resource_value = 3;
  kpi_sample_types.KpiSampleType kpi_sample_type = 4;
}


+13 −13
Original line number Diff line number Diff line
@@ -18,23 +18,23 @@ export REDIS_SERVICE_PORT=$(kubectl get service contextservice-public --namespac
# First destroy old coverage file
rm -f $COVERAGEFILE

#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    common/orm/tests/test_unitary.py \
#    common/message_broker/tests/test_unitary.py \
#    common/rpc_method_wrapper/tests/test_unitary.py
coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    common/orm/tests/test_unitary.py \
    common/message_broker/tests/test_unitary.py \
    common/rpc_method_wrapper/tests/test_unitary.py

#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    centralizedattackdetector/tests/test_unitary.py
coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    centralizedattackdetector/tests/test_unitary.py

#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    context/tests/test_unitary.py
coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    context/tests/test_unitary.py

coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    device/tests/test_unitary_driverapi.py \
    device/tests/test_unitary.py
    #device/tests/test_unitary_driverapi.py \

#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    service/tests/test_unitary.py
coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    service/tests/test_unitary.py

#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
#    compute/tests/test_unitary.py
coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    compute/tests/test_unitary.py
+98 −108

File changed.

Preview size limit exceeded, changes collapsed.

+9 −3
Original line number Diff line number Diff line
@@ -6,7 +6,9 @@ from . import context_pb2 as context__pb2


class ContextServiceStub(object):
    """Missing associated documentation comment in .proto file."""
    """import "kpi_sample_types.proto";

    """

    def __init__(self, channel):
        """Constructor.
@@ -167,7 +169,9 @@ class ContextServiceStub(object):


class ContextServiceServicer(object):
    """Missing associated documentation comment in .proto file."""
    """import "kpi_sample_types.proto";

    """

    def ListContextIds(self, request, context):
        """Missing associated documentation comment in .proto file."""
@@ -510,7 +514,9 @@ def add_ContextServiceServicer_to_server(servicer, server):

 # This class is part of an EXPERIMENTAL API.
class ContextService(object):
    """Missing associated documentation comment in .proto file."""
    """import "kpi_sample_types.proto";

    """

    @staticmethod
    def ListContextIds(request,

src/device/_old_code/Tools.py

deleted100644 → 0
+0 −120
Original line number Diff line number Diff line
import grpc, logging
from typing import Dict, List, Set, Tuple
from common.Checkers import chk_options, chk_string
from common.database.api.Database import Database
from common.database.api.context.Constants import DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID
from common.database.api.context.topology.device.Endpoint import Endpoint
from common.database.api.context.topology.device.OperationalStatus import OperationalStatus, \
    operationalstatus_enum_values, to_operationalstatus_enum
from common.exceptions.ServiceException import ServiceException
from common.tools.service.DeviceCheckers import check_device_endpoint_exists
from common.tools.service.EndpointIdCheckers import check_endpoint_id
from common.tools.service.EnumCheckers import check_enum
from common.tools.service.DeviceCheckers import check_device_exists, check_device_not_exists
from device.proto.context_pb2 import Device, DeviceId

# For each method name, define acceptable device operational statuses. Empty set means accept all.
ACCEPTED_DEVICE_OPERATIONAL_STATUSES : Dict[str, Set[OperationalStatus]] = {
    'AddDevice': set([OperationalStatus.ENABLED, OperationalStatus.DISABLED]),
    'UpdateDevice': set([OperationalStatus.KEEP_STATE, OperationalStatus.ENABLED, OperationalStatus.DISABLED]),
}

def _check_device_exists(method_name : str, database : Database, device_id : str):
    if method_name in ['AddDevice']:
        check_device_not_exists(database, DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID, device_id)
    elif method_name in ['UpdateDevice', 'DeleteDevice']:
        check_device_exists(database, DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID, device_id)
    else:                                       # pragma: no cover (test requires malforming the code)
        msg = 'Unexpected condition [_check_device_exists(method_name={}, device_id={})]'
        msg = msg.format(str(method_name), str(device_id))
        raise ServiceException(grpc.StatusCode.UNIMPLEMENTED, msg)

def _check_device_endpoint_exists_or_get_pointer(
    method_name : str, database : Database, parent_name : str, device_id : str, endpoint_id : str):

    if method_name in ['AddDevice']:
        db_context = database.context(DEFAULT_CONTEXT_ID)
        db_topology = db_context.topology(DEFAULT_TOPOLOGY_ID)
        db_device = db_topology.device(device_id)
        return db_device.endpoint(endpoint_id)
    elif method_name in ['UpdateDevice', 'DeleteDevice']:
        return check_device_endpoint_exists(
            database, parent_name, DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID, device_id, endpoint_id)
    else:                                       # pragma: no cover (test requires malforming the code)
        msg = 'Unexpected condition [_check_device_exists(method_name={}, device_id={})]'
        msg = msg.format(str(method_name), str(device_id))
        raise ServiceException(grpc.StatusCode.UNIMPLEMENTED, msg)

def check_device_operational_status(method_name : str, value : str) -> OperationalStatus:
    return check_enum(
        'OperationalStatus', method_name, value, to_operationalstatus_enum, ACCEPTED_DEVICE_OPERATIONAL_STATUSES)

def check_device_request(
    method_name : str, request : Device, database : Database, logger : logging.Logger
    ) -> Tuple[str, str, str, OperationalStatus, List[Tuple[Endpoint, str]]]:

    # ----- Parse attributes -------------------------------------------------------------------------------------------
    try:
        device_id     = chk_string ('device.device_id.device_id.uuid',
                                    request.device_id.device_id.uuid,
                                    allow_empty=False)
        device_type   = chk_string ('device.device_type',
                                    request.device_type,
                                    allow_empty=False)
        device_config = chk_string ('device.device_config.device_config',
                                    request.device_config.device_config,
                                    allow_empty=True)
        device_opstat = chk_options('device.devOperationalStatus',
                                    request.devOperationalStatus,
                                    operationalstatus_enum_values())
    except Exception as e:
        logger.exception('Invalid arguments:')
        raise ServiceException(grpc.StatusCode.INVALID_ARGUMENT, str(e))

    device_opstat = check_device_operational_status(method_name, device_opstat)

    # ----- Check if device exists in database -------------------------------------------------------------------------
    _check_device_exists(method_name, database, device_id)

    # ----- Parse endpoints and check if they exist in the database as device endpoints --------------------------------
    add_topology_devices_endpoints : Dict[str, Dict[str, Set[str]]] = {}
    db_endpoints__port_types : List[Tuple[Endpoint, str]] = []
    for endpoint_number,endpoint in enumerate(request.endpointList):
        parent_name = 'Endpoint(#{}) of Context({})/Topology({})/Device({})'
        parent_name = parent_name.format(endpoint_number, DEFAULT_CONTEXT_ID, DEFAULT_TOPOLOGY_ID, device_id)

        _, ep_device_id, ep_port_id = check_endpoint_id(
            logger, endpoint_number, parent_name, endpoint.port_id, add_topology_devices_endpoints,
            predefined_device_id=device_id, acceptable_device_ids=set([device_id]),
            prevent_same_device_multiple_times=False)

        try:
            ep_port_type = chk_string('endpoint[#{}].port_type'.format(endpoint_number),
                                      endpoint.port_type,
                                      allow_empty=False)
        except Exception as e:
            logger.exception('Invalid arguments:')
            raise ServiceException(grpc.StatusCode.INVALID_ARGUMENT, str(e))

        db_endpoint = _check_device_endpoint_exists_or_get_pointer(
            method_name, database, parent_name, ep_device_id, ep_port_id)
        db_endpoints__port_types.append((db_endpoint, ep_port_type))

    return device_id, device_type, device_config, device_opstat, db_endpoints__port_types

def check_device_id_request(
    method_name : str, request : DeviceId, database : Database, logger : logging.Logger) -> str:

    # ----- Parse attributes -------------------------------------------------------------------------------------------
    try:
        device_id = chk_string('device_id.device_id.uuid',
                               request.device_id.uuid,
                               allow_empty=False)
    except Exception as e:
        logger.exception('Invalid arguments:')
        raise ServiceException(grpc.StatusCode.INVALID_ARGUMENT, str(e))

    # ----- Check if device exists in database ---------------------------------------------------------------------------
    _check_device_exists(method_name, database, device_id)

    return device_id
Loading