Commit d2316472 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- removed old code
- implemented basic support for Slice entity
- implemented detailed perf eval report in unitary tests
parent 763397eb
Loading
Loading
Loading
Loading
+28 −170
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ import grpc, json, logging, sqlalchemy
#from sqlalchemy.dialects.postgresql import UUID, insert
from typing import Iterator
from common.message_broker.MessageBroker import MessageBroker
#from common.orm.backend.Tools import key_to_str
from common.proto.context_pb2 import (
    Connection, ConnectionEvent, ConnectionId, ConnectionIdList, ConnectionList,
    Context, ContextEvent, ContextId, ContextIdList, ContextList,
@@ -39,6 +38,7 @@ from .database.Context import context_delete, context_get, context_list_ids, con
from .database.Device import device_delete, device_get, device_list_ids, device_list_objs, device_set
from .database.Link import link_delete, link_get, link_list_ids, link_list_objs, link_set
from .database.Service import service_delete, service_get, service_list_ids, service_list_objs, service_set
from .database.Slice import slice_delete, slice_get, slice_list_ids, slice_list_objs, slice_set, slice_unset
from .database.Topology import topology_delete, topology_get, topology_list_ids, topology_list_objs, topology_set
#from common.tools.grpc.Tools import grpc_message_to_json, grpc_message_to_json_string
#from context.service.Database import Database
@@ -265,180 +265,38 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer

    # ----- Slice ----------------------------------------------------------------------------------------------------

#    @safe_and_metered_rpc_method(METRICS, LOGGER)
#    def ListSliceIds(self, request : ContextId, context : grpc.ServicerContext) -> SliceIdList:
#        with self.lock:
#            db_context : ContextModel = get_object(self.database, ContextModel, request.context_uuid.uuid)
#            db_slices : Set[SliceModel] = get_related_objects(db_context, SliceModel)
#            db_slices = sorted(db_slices, key=operator.attrgetter('pk'))
#            return SliceIdList(slice_ids=[db_slice.dump_id() for db_slice in db_slices])
    @safe_and_metered_rpc_method(METRICS, LOGGER)
    def ListSliceIds(self, request : ContextId, context : grpc.ServicerContext) -> SliceIdList:
        return slice_list_ids(self.db_engine, request)

#    @safe_and_metered_rpc_method(METRICS, LOGGER)
#    def ListSlices(self, request : ContextId, context : grpc.ServicerContext) -> SliceList:
#        with self.lock:
#            db_context : ContextModel = get_object(self.database, ContextModel, request.context_uuid.uuid)
#            db_slices : Set[SliceModel] = get_related_objects(db_context, SliceModel)
#            db_slices = sorted(db_slices, key=operator.attrgetter('pk'))
#            return SliceList(slices=[db_slice.dump() for db_slice in db_slices])
    @safe_and_metered_rpc_method(METRICS, LOGGER)
    def ListSlices(self, request : ContextId, context : grpc.ServicerContext) -> SliceList:
        return slice_list_objs(self.db_engine, request)

#    @safe_and_metered_rpc_method(METRICS, LOGGER)
#    def GetSlice(self, request : SliceId, context : grpc.ServicerContext) -> Slice:
#        with self.lock:
#            str_key = key_to_str([request.context_id.context_uuid.uuid, request.slice_uuid.uuid])
#            db_slice : SliceModel = get_object(self.database, SliceModel, str_key)
#            return Slice(**db_slice.dump(
#                include_endpoint_ids=True, include_constraints=True, include_config_rules=True,
#                include_service_ids=True, include_subslice_ids=True))
    @safe_and_metered_rpc_method(METRICS, LOGGER)
    def GetSlice(self, request : SliceId, context : grpc.ServicerContext) -> Slice:
        return slice_get(self.db_engine, request)

#    @safe_and_metered_rpc_method(METRICS, LOGGER)
#    def SetSlice(self, request : Slice, context : grpc.ServicerContext) -> SliceId:
#        with self.lock:
#            context_uuid = request.slice_id.context_id.context_uuid.uuid
#            db_context : ContextModel = get_object(self.database, ContextModel, context_uuid)
#
#            for i,endpoint_id in enumerate(request.slice_endpoint_ids):
#                endpoint_topology_context_uuid = endpoint_id.topology_id.context_id.context_uuid.uuid
#                if len(endpoint_topology_context_uuid) > 0 and context_uuid != endpoint_topology_context_uuid:
#                    raise InvalidArgumentException(
#                        'request.slice_endpoint_ids[{:d}].topology_id.context_id.context_uuid.uuid'.format(i),
#                        endpoint_topology_context_uuid,
#                        ['should be == {:s}({:s})'.format(
#                            'request.slice_id.context_id.context_uuid.uuid', context_uuid)])
#
#            slice_uuid = request.slice_id.slice_uuid.uuid
#            str_slice_key = key_to_str([context_uuid, slice_uuid])
#
#            constraints_result = set_constraints(
#                self.database, str_slice_key, 'slice', request.slice_constraints)
#            db_constraints = constraints_result[0][0]
#
#            running_config_rules = update_config(
#                self.database, str_slice_key, 'slice', request.slice_config.config_rules)
#            db_running_config = running_config_rules[0][0]
#
#            result : Tuple[SliceModel, bool] = update_or_create_object(self.database, SliceModel, str_slice_key, {
#                'context_fk'          : db_context,
#                'slice_uuid'          : slice_uuid,
#                'slice_constraints_fk': db_constraints,
#                'slice_status'        : grpc_to_enum__slice_status(request.slice_status.slice_status),
#                'slice_config_fk'     : db_running_config,
#                'slice_owner_uuid'    : request.slice_owner.owner_uuid.uuid,
#                'slice_owner_string'  : request.slice_owner.owner_string,
#            })
#            db_slice, updated = result
#
#            for i,endpoint_id in enumerate(request.slice_endpoint_ids):
#                endpoint_uuid                  = endpoint_id.endpoint_uuid.uuid
#                endpoint_device_uuid           = endpoint_id.device_id.device_uuid.uuid
#                endpoint_topology_uuid         = endpoint_id.topology_id.topology_uuid.uuid
#                endpoint_topology_context_uuid = endpoint_id.topology_id.context_id.context_uuid.uuid
#
#                str_endpoint_key = key_to_str([endpoint_device_uuid, endpoint_uuid])
#                if len(endpoint_topology_context_uuid) > 0 and len(endpoint_topology_uuid) > 0:
#                    str_topology_key = key_to_str([endpoint_topology_context_uuid, endpoint_topology_uuid])
#                    str_endpoint_key = key_to_str([str_endpoint_key, str_topology_key], separator=':')
#
#                db_endpoint : EndPointModel = get_object(self.database, EndPointModel, str_endpoint_key)
#
#                str_slice_endpoint_key = key_to_str([str_slice_key, str_endpoint_key], separator='--')
#                result : Tuple[SliceEndPointModel, bool] = get_or_create_object(
#                    self.database, SliceEndPointModel, str_slice_endpoint_key, {
#                        'slice_fk': db_slice, 'endpoint_fk': db_endpoint})
#                #db_slice_endpoint, slice_endpoint_created = result
#
#            for i,service_id in enumerate(request.slice_service_ids):
#                service_uuid         = service_id.service_uuid.uuid
#                service_context_uuid = service_id.context_id.context_uuid.uuid
#                str_service_key = key_to_str([service_context_uuid, service_uuid])
#                db_service : ServiceModel = get_object(self.database, ServiceModel, str_service_key)
#
#                str_slice_service_key = key_to_str([str_slice_key, str_service_key], separator='--')
#                result : Tuple[SliceServiceModel, bool] = get_or_create_object(
#                    self.database, SliceServiceModel, str_slice_service_key, {
#                        'slice_fk': db_slice, 'service_fk': db_service})
#                #db_slice_service, slice_service_created = result
#
#            for i,subslice_id in enumerate(request.slice_subslice_ids):
#                subslice_uuid         = subslice_id.slice_uuid.uuid
#                subslice_context_uuid = subslice_id.context_id.context_uuid.uuid
#                str_subslice_key = key_to_str([subslice_context_uuid, subslice_uuid])
#                db_subslice : SliceModel = get_object(self.database, SliceModel, str_subslice_key)
#
#                str_slice_subslice_key = key_to_str([str_slice_key, str_subslice_key], separator='--')
#                result : Tuple[SliceSubSliceModel, bool] = get_or_create_object(
#                    self.database, SliceSubSliceModel, str_slice_subslice_key, {
#                        'slice_fk': db_slice, 'sub_slice_fk': db_subslice})
#                #db_slice_subslice, slice_subslice_created = result
#
    @safe_and_metered_rpc_method(METRICS, LOGGER)
    def SetSlice(self, request : Slice, context : grpc.ServicerContext) -> SliceId:
        slice_id,updated = slice_set(self.db_engine, request)
        #event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE
#            dict_slice_id = db_slice.dump_id()
#            notify_event(self.messagebroker, TOPIC_SLICE, event_type, {'slice_id': dict_slice_id})
#            return SliceId(**dict_slice_id)
        #notify_event(self.messagebroker, TOPIC_SLICE, event_type, {'slice_id': slice_id})
        return slice_id

#    @safe_and_metered_rpc_method(METRICS, LOGGER)
#    def UnsetSlice(self, request : Slice, context : grpc.ServicerContext) -> SliceId:
#        with self.lock:
#            context_uuid = request.slice_id.context_id.context_uuid.uuid
#            db_context : ContextModel = get_object(self.database, ContextModel, context_uuid)
#
#            for i,endpoint_id in enumerate(request.slice_endpoint_ids):
#                endpoint_topology_context_uuid = endpoint_id.topology_id.context_id.context_uuid.uuid
#                if len(endpoint_topology_context_uuid) > 0 and context_uuid != endpoint_topology_context_uuid:
#                    raise InvalidArgumentException(
#                        'request.slice_endpoint_ids[{:d}].topology_id.context_id.context_uuid.uuid'.format(i),
#                        endpoint_topology_context_uuid,
#                        ['should be == {:s}({:s})'.format(
#                            'request.slice_id.context_id.context_uuid.uuid', context_uuid)])
#
#            slice_uuid = request.slice_id.slice_uuid.uuid
#            str_slice_key = key_to_str([context_uuid, slice_uuid])
#
#            if len(request.slice_constraints) > 0:
#                raise NotImplementedError('UnsetSlice: removal of constraints')
#            if len(request.slice_config.config_rules) > 0:
#                raise NotImplementedError('UnsetSlice: removal of config rules')
#            if len(request.slice_endpoint_ids) > 0:
#                raise NotImplementedError('UnsetSlice: removal of endpoints')
#
#            updated = False
#
#            for service_id in request.slice_service_ids:
#                service_uuid         = service_id.service_uuid.uuid
#                service_context_uuid = service_id.context_id.context_uuid.uuid
#                str_service_key = key_to_str([service_context_uuid, service_uuid])
#                str_slice_service_key = key_to_str([str_slice_key, str_service_key], separator='--')
#                SliceServiceModel(self.database, str_slice_service_key).delete()
#                updated = True
#
#            for subslice_id in request.slice_subslice_ids:
#                subslice_uuid         = subslice_id.slice_uuid.uuid
#                subslice_context_uuid = subslice_id.context_id.context_uuid.uuid
#                str_subslice_key = key_to_str([subslice_context_uuid, subslice_uuid])
#                str_slice_subslice_key = key_to_str([str_slice_key, str_subslice_key], separator='--')
#                SliceSubSliceModel(self.database, str_slice_subslice_key).delete()
#                updated = True
#
#            event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE
#            db_slice : SliceModel = get_object(self.database, SliceModel, str_slice_key)
#            dict_slice_id = db_slice.dump_id()
#            notify_event(self.messagebroker, TOPIC_SLICE, event_type, {'slice_id': dict_slice_id})
#            return SliceId(**dict_slice_id)
    @safe_and_metered_rpc_method(METRICS, LOGGER)
    def UnsetSlice(self, request : Slice, context : grpc.ServicerContext) -> SliceId:
        slice_id,updated = slice_unset(self.db_engine, request)
        #if updated:
        #    notify_event(self.messagebroker, TOPIC_SLICE, EventTypeEnum.EVENTTYPE_UPDATE, {'slice_id': slice_id})
        return slice_id

#    @safe_and_metered_rpc_method(METRICS, LOGGER)
#    def RemoveSlice(self, request : SliceId, context : grpc.ServicerContext) -> Empty:
#        with self.lock:
#            context_uuid = request.context_id.context_uuid.uuid
#            slice_uuid = request.slice_uuid.uuid
#            db_slice = SliceModel(self.database, key_to_str([context_uuid, slice_uuid]), auto_load=False)
#            found = db_slice.load()
#            if not found: return Empty()
#
#            dict_slice_id = db_slice.dump_id()
#            db_slice.delete()
#
#            event_type = EventTypeEnum.EVENTTYPE_REMOVE
#            notify_event(self.messagebroker, TOPIC_SLICE, event_type, {'slice_id': dict_slice_id})
#            return Empty()
    @safe_and_metered_rpc_method(METRICS, LOGGER)
    def RemoveSlice(self, request : SliceId, context : grpc.ServicerContext) -> Empty:
        deleted = slice_delete(self.db_engine, request)
        #if deleted:
        #    notify_event(self.messagebroker, TOPIC_SLICE, EventTypeEnum.EVENTTYPE_REMOVE, {'slice_id': request})
        return Empty()

    @safe_and_metered_rpc_method(METRICS, LOGGER)
    def GetSliceEvents(self, request : Empty, context : grpc.ServicerContext) -> Iterator[SliceEvent]:
+0 −16
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.

# Autopopulate the component with fake data for testing purposes?
POPULATE_FAKE_DATA = False
+0 −49
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 copy
from common.proto.context_pb2 import Connection, Context, Device, Link, Service, Topology
from context.client.ContextClient import ContextClient
from context.tests.Objects import (
    CONNECTION_R1_R3, CONTEXT, TOPOLOGY, DEVICE_R1, DEVICE_R1_ID, DEVICE_R2, DEVICE_R2_ID, DEVICE_R3, DEVICE_R3_ID,
    LINK_R1_R2, LINK_R1_R2_ID, LINK_R1_R3, LINK_R1_R3_ID, LINK_R2_R3, LINK_R2_R3_ID, SERVICE_R1_R2, SERVICE_R1_R3,
    SERVICE_R2_R3)

def populate(host=None, port=None):
    client = ContextClient(host=host, port=port)

    client.SetContext(Context(**CONTEXT))
    client.SetTopology(Topology(**TOPOLOGY))
    client.SetDevice(Device(**DEVICE_R1))
    client.SetDevice(Device(**DEVICE_R2))
    client.SetDevice(Device(**DEVICE_R3))

    client.SetLink(Link(**LINK_R1_R2))
    client.SetLink(Link(**LINK_R1_R3))
    client.SetLink(Link(**LINK_R2_R3))

    TOPOLOGY_WITH_DEVICES_AND_LINKS = copy.deepcopy(TOPOLOGY)
    TOPOLOGY_WITH_DEVICES_AND_LINKS['device_ids'].append(DEVICE_R1_ID)
    TOPOLOGY_WITH_DEVICES_AND_LINKS['device_ids'].append(DEVICE_R2_ID)
    TOPOLOGY_WITH_DEVICES_AND_LINKS['device_ids'].append(DEVICE_R3_ID)
    TOPOLOGY_WITH_DEVICES_AND_LINKS['link_ids'].append(LINK_R1_R2_ID)
    TOPOLOGY_WITH_DEVICES_AND_LINKS['link_ids'].append(LINK_R1_R3_ID)
    TOPOLOGY_WITH_DEVICES_AND_LINKS['link_ids'].append(LINK_R2_R3_ID)
    client.SetTopology(Topology(**TOPOLOGY_WITH_DEVICES_AND_LINKS))

    client.SetService(Service(**SERVICE_R1_R2))
    client.SetService(Service(**SERVICE_R2_R3))

    client.SetService(Service(**SERVICE_R1_R3))
    client.SetConnection(Connection(**CONNECTION_R1_R3))
+0 −246

File deleted.

Preview size limit exceeded, changes collapsed.

+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.
Loading