diff --git a/src/context/service/ContextServiceServicerImpl.py b/src/context/service/ContextServiceServicerImpl.py index 44409bd0caf59859ebf8f31b4e8c20f7d30942d2..6914e05a01b8382968b656ce818b91f10ffd7329 100644 --- a/src/context/service/ContextServiceServicerImpl.py +++ b/src/context/service/ContextServiceServicerImpl.py @@ -35,14 +35,11 @@ from common.proto.context_policy_pb2_grpc import ContextPolicyServiceServicer from common.rpc_method_wrapper.Decorator import create_metrics, safe_and_metered_rpc_method #from common.rpc_method_wrapper.ServiceExceptions import ( # InvalidArgumentException, NotFoundException, OperationFailedException) -from .database.methods.Context import ( - context_delete, context_get, context_list_ids, context_list_objs, context_set) -from .database.methods.Device import ( - device_delete, device_get, device_list_ids, device_list_objs, device_set) -#from .database.methods.Link import link_delete, link_get, link_list_ids, link_list_objs, link_set -#from .database.methods.Service import service_delete, service_get, service_list_ids, service_list_objs, service_set -from .database.methods.Topology import ( - topology_delete, topology_get, topology_list_ids, topology_list_objs, topology_set) +from .database.Context import context_delete, context_get, context_list_ids, context_list_objs, context_set +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.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 #from context.service.database.ConfigModel import ( @@ -200,31 +197,31 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer # ----- Link ------------------------------------------------------------------------------------------------------- -# @safe_and_metered_rpc_method(METRICS, LOGGER) -# def ListLinkIds(self, request : Empty, context : grpc.ServicerContext) -> LinkIdList: -# return link_list_ids(self.db_engine) + @safe_and_metered_rpc_method(METRICS, LOGGER) + def ListLinkIds(self, request : Empty, context : grpc.ServicerContext) -> LinkIdList: + return link_list_ids(self.db_engine) -# @safe_and_metered_rpc_method(METRICS, LOGGER) -# def ListLinks(self, request : Empty, context : grpc.ServicerContext) -> LinkList: -# return link_list_objs(self.db_engine) + @safe_and_metered_rpc_method(METRICS, LOGGER) + def ListLinks(self, request : Empty, context : grpc.ServicerContext) -> LinkList: + return link_list_objs(self.db_engine) -# @safe_and_metered_rpc_method(METRICS, LOGGER) -# def GetLink(self, request : LinkId, context : grpc.ServicerContext) -> Link: -# return link_get(self.db_engine, request) + @safe_and_metered_rpc_method(METRICS, LOGGER) + def GetLink(self, request : LinkId, context : grpc.ServicerContext) -> Link: + return link_get(self.db_engine, request) -# @safe_and_metered_rpc_method(METRICS, LOGGER) -# def SetLink(self, request : Link, context : grpc.ServicerContext) -> LinkId: -# link_id,updated = link_set(self.db_engine, request) -# #event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE -# #notify_event(self.messagebroker, TOPIC_LINK, event_type, {'link_id': link_id}) -# return link_id + @safe_and_metered_rpc_method(METRICS, LOGGER) + def SetLink(self, request : Link, context : grpc.ServicerContext) -> LinkId: + link_id,updated = link_set(self.db_engine, request) + #event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE + #notify_event(self.messagebroker, TOPIC_LINK, event_type, {'link_id': link_id}) + return link_id -# @safe_and_metered_rpc_method(METRICS, LOGGER) -# def RemoveLink(self, request : LinkId, context : grpc.ServicerContext) -> Empty: -# deleted = link_delete(self.db_engine, request) -# #if deleted: -# # notify_event(self.messagebroker, TOPIC_LINK, event_type, {'link_id': dict_link_id}) -# return Empty() + @safe_and_metered_rpc_method(METRICS, LOGGER) + def RemoveLink(self, request : LinkId, context : grpc.ServicerContext) -> Empty: + deleted = link_delete(self.db_engine, request) + #if deleted: + # notify_event(self.messagebroker, TOPIC_LINK, EventTypeEnum.EVENTTYPE_REMOVE, {'link_id': request}) + return Empty() @safe_and_metered_rpc_method(METRICS, LOGGER) def GetLinkEvents(self, request : Empty, context : grpc.ServicerContext) -> Iterator[LinkEvent]: diff --git a/src/context/service/database/methods/Context.py b/src/context/service/database/Context.py similarity index 98% rename from src/context/service/database/methods/Context.py rename to src/context/service/database/Context.py index fc53426e383e9483f525b9044d9af36d992aa186..85a06d65ecee7242b642a5a87b08c3215cc7a1cc 100644 --- a/src/context/service/database/methods/Context.py +++ b/src/context/service/database/Context.py @@ -21,7 +21,7 @@ from typing import Dict, List, Optional, Tuple from common.proto.context_pb2 import Context, ContextId, ContextIdList, ContextList from common.rpc_method_wrapper.ServiceExceptions import NotFoundException from common.tools.object_factory.Context import json_context_id -from context.service.database.models.ContextModel import ContextModel +from .models.ContextModel import ContextModel from .uuids.Context import context_get_uuid LOGGER = logging.getLogger(__name__) diff --git a/src/context/service/database/methods/Device.py b/src/context/service/database/Device.py similarity index 88% rename from src/context/service/database/methods/Device.py rename to src/context/service/database/Device.py index 39ae98de08bf2c120f124aaf79ed443cdaa76a66..a0e0a53e5c766c48cfdc6b8fde2c432bf4d634db 100644 --- a/src/context/service/database/methods/Device.py +++ b/src/context/service/database/Device.py @@ -21,15 +21,16 @@ from typing import Dict, List, Optional, Set, Tuple from common.proto.context_pb2 import Device, DeviceId, DeviceIdList, DeviceList from common.rpc_method_wrapper.ServiceExceptions import InvalidArgumentException, NotFoundException from common.tools.object_factory.Device import json_device_id -#from common.tools.grpc.Tools import grpc_message_to_json_string -#from context.service.database.models.ConfigRuleModel import ConfigRuleKindEnum, ConfigRuleModel -from context.service.database.models.DeviceModel import DeviceModel -from context.service.database.models.EndPointModel import EndPointModel -from context.service.database.models.RelationModels import TopologyDeviceModel -#from context.service.database.models.enums.ConfigAction import grpc_to_enum__config_action -from context.service.database.models.enums.DeviceDriver import grpc_to_enum__device_driver -from context.service.database.models.enums.DeviceOperationalStatus import grpc_to_enum__device_operational_status -from context.service.database.models.enums.KpiSampleType import grpc_to_enum__kpi_sample_type +from common.tools.grpc.Tools import grpc_message_to_json_string +from .models.ConfigRuleModel import ConfigRuleKindEnum, ConfigRuleModel +from .models.DeviceModel import DeviceModel +from .models.EndPointModel import EndPointModel +from .models.RelationModels import TopologyDeviceModel +from .models.enums.ConfigAction import grpc_to_enum__config_action +from .models.enums.DeviceDriver import grpc_to_enum__device_driver +from .models.enums.DeviceOperationalStatus import grpc_to_enum__device_operational_status +from .models.enums.KpiSampleType import grpc_to_enum__kpi_sample_type +from .uuids._Builder import get_uuid_random from .uuids.Device import device_get_uuid from .uuids.EndPoint import endpoint_get_uuid @@ -64,7 +65,7 @@ def device_get(db_engine : Engine, request : DeviceId) -> Device: def device_set(db_engine : Engine, request : Device) -> bool: raw_device_uuid = request.device_id.device_uuid.uuid raw_device_name = request.name - device_name = request.device_id.device_uuid.uuid if len(raw_device_name) == 0 else raw_device_name + device_name = raw_device_uuid if len(raw_device_name) == 0 else raw_device_name device_uuid = device_get_uuid(request.device_id, device_name=device_name, allow_random=True) device_type = request.device_type @@ -83,9 +84,11 @@ def device_set(db_engine : Engine, request : Device) -> bool: ['should be == request.device_id.device_uuid.uuid({:s})'.format(raw_device_uuid)] ) + raw_endpoint_uuid = endpoint.endpoint_id.endpoint_uuid.uuid raw_endpoint_name = endpoint.name endpoint_topology_uuid, endpoint_device_uuid, endpoint_uuid = endpoint_get_uuid( endpoint.endpoint_id, endpoint_name=raw_endpoint_name, allow_random=True) + endpoint_name = raw_endpoint_uuid if len(raw_endpoint_name) == 0 else raw_endpoint_name kpi_sample_types = [grpc_to_enum__kpi_sample_type(kst) for kst in endpoint.kpi_sample_types] @@ -93,7 +96,7 @@ def device_set(db_engine : Engine, request : Device) -> bool: 'endpoint_uuid' : endpoint_uuid, 'device_uuid' : endpoint_device_uuid, 'topology_uuid' : endpoint_topology_uuid, - 'name' : raw_endpoint_name, + 'name' : endpoint_name, 'endpoint_type' : endpoint.endpoint_type, 'kpi_sample_types': kpi_sample_types, }) @@ -101,20 +104,22 @@ def device_set(db_engine : Engine, request : Device) -> bool: if endpoint_topology_uuid not in topology_uuids: related_topologies.append({ 'topology_uuid': endpoint_topology_uuid, - 'device_uuid' : endpoint_device_uuid, + 'device_uuid' : device_uuid, }) topology_uuids.add(endpoint_topology_uuid) - #config_rules : List[Dict] = list() - #for position,config_rule in enumerate(request.device_config.config_rules): - # str_kind = config_rule.WhichOneof('config_rule') - # config_rules.append({ - # 'device_uuid': device_uuid, - # 'kind' : ConfigRuleKindEnum._member_map_.get(str_kind.upper()), # pylint: disable=no-member - # 'action' : grpc_to_enum__config_action(config_rule.action), - # 'position' : position, - # 'data' : grpc_message_to_json_string(getattr(config_rule, str_kind, {})), - # }) + config_rules : List[Dict] = list() + for position,config_rule in enumerate(request.device_config.config_rules): + configrule_uuid = get_uuid_random() + str_kind = config_rule.WhichOneof('config_rule') + config_rules.append({ + 'configrule_uuid': configrule_uuid, + 'device_uuid' : device_uuid, + 'position' : position, + 'kind' : ConfigRuleKindEnum._member_map_.get(str_kind.upper()), # pylint: disable=no-member + 'action' : grpc_to_enum__config_action(config_rule.action), + 'data' : grpc_message_to_json_string(getattr(config_rule, str_kind, {})), + }) device_data = [{ 'device_uuid' : device_uuid, @@ -152,8 +157,8 @@ def device_set(db_engine : Engine, request : Device) -> bool: index_elements=[TopologyDeviceModel.topology_uuid, TopologyDeviceModel.device_uuid] )) - #session.execute(delete(ConfigRuleModel).where(ConfigRuleModel.device_uuid == device_uuid)) - #session.execute(insert(ConfigRuleModel).values(config_rules)) + session.execute(delete(ConfigRuleModel).where(ConfigRuleModel.device_uuid == device_uuid)) + session.execute(insert(ConfigRuleModel).values(config_rules)) run_transaction(sessionmaker(bind=db_engine), callback) updated = False # TODO: improve and check if created/updated diff --git a/src/context/service/database/methods/Link.py b/src/context/service/database/Link.py similarity index 58% rename from src/context/service/database/methods/Link.py rename to src/context/service/database/Link.py index b98578c226e7b5810c15bf3d783038d6975a334c..93f90b3ea0aeba5b97ba8a360be1f289eb8f0d0a 100644 --- a/src/context/service/database/methods/Link.py +++ b/src/context/service/database/Link.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import time from sqlalchemy.dialects.postgresql import insert from sqlalchemy.engine import Engine from sqlalchemy.orm import Session, sessionmaker @@ -20,8 +19,11 @@ from sqlalchemy_cockroachdb import run_transaction from typing import Dict, List, Optional, Set, Tuple from common.proto.context_pb2 import Link, LinkId, LinkIdList, LinkList from common.rpc_method_wrapper.ServiceExceptions import NotFoundException -from context.service.database.models.LinkModel import LinkModel -from context.service.database.models.RelationModels import LinkEndPointModel, TopologyLinkModel +from common.tools.object_factory.Link import json_link_id +from .models.LinkModel import LinkModel +from .models.RelationModels import LinkEndPointModel, TopologyLinkModel +from .uuids.EndPoint import endpoint_get_uuid +from .uuids.Link import link_get_uuid def link_list_ids(db_engine : Engine) -> LinkIdList: def callback(session : Session) -> List[Dict]: @@ -38,81 +40,76 @@ def link_list_objs(db_engine : Engine) -> LinkList: return LinkList(links=run_transaction(sessionmaker(bind=db_engine), callback)) def link_get(db_engine : Engine, request : LinkId) -> Link: - link_uuid = request.link_uuid.uuid + link_uuid = link_get_uuid(request, allow_random=False) def callback(session : Session) -> Optional[Dict]: obj : Optional[LinkModel] = session.query(LinkModel)\ .filter_by(link_uuid=link_uuid).one_or_none() return None if obj is None else obj.dump() obj = run_transaction(sessionmaker(bind=db_engine), callback) - if obj is None: raise NotFoundException('Link', link_uuid) + if obj is None: + raw_link_uuid = request.link_uuid.uuid + raise NotFoundException('Link', raw_link_uuid, extra_details=[ + 'link_uuid generated was: {:s}'.format(link_uuid) + ]) return Link(**obj) def link_set(db_engine : Engine, request : Link) -> bool: - link_uuid = request.link_id.link_uuid.uuid - link_name = request.name + raw_link_uuid = request.link_id.link_uuid.uuid + raw_link_name = request.name + link_name = raw_link_uuid if len(raw_link_name) == 0 else raw_link_name + link_uuid = link_get_uuid(request.link_id, link_name=link_name, allow_random=True) - topology_keys : Set[Tuple[str, str]] = set() + topology_uuids : Set[str] = set() related_topologies : List[Dict] = list() link_endpoints_data : List[Dict] = list() for endpoint_id in request.link_endpoint_ids: - context_uuid = endpoint_id.topology_id.context_id.context_uuid.uuid - topology_uuid = endpoint_id.topology_id.topology_uuid.uuid - device_uuid = endpoint_id.device_id.device_uuid.uuid - endpoint_uuid = endpoint_id.endpoint_uuid.uuid + endpoint_topology_uuid, _, endpoint_uuid = endpoint_get_uuid( + endpoint_id, allow_random=False) link_endpoints_data.append({ 'link_uuid' : link_uuid, - 'context_uuid' : context_uuid, - 'topology_uuid': topology_uuid, - 'device_uuid' : device_uuid, 'endpoint_uuid': endpoint_uuid, }) - if len(context_uuid) > 0 and len(topology_uuid) > 0: - topology_key = (context_uuid, topology_uuid) - if topology_key not in topology_keys: - related_topologies.append({ - 'context_uuid': context_uuid, - 'topology_uuid': topology_uuid, - 'link_uuid': link_uuid, - }) - topology_keys.add(topology_key) + if endpoint_topology_uuid not in topology_uuids: + related_topologies.append({ + 'topology_uuid': endpoint_topology_uuid, + 'link_uuid': link_uuid, + }) + topology_uuids.add(endpoint_topology_uuid) + + link_data = [{ + 'link_uuid': link_uuid, + 'link_name': link_name, + }] def callback(session : Session) -> None: - obj : Optional[LinkModel] = session.query(LinkModel).with_for_update()\ - .filter_by(link_uuid=link_uuid).one_or_none() - is_update = obj is not None - if is_update: - obj.link_name = link_name - session.merge(obj) - else: - session.add(LinkModel(link_uuid=link_uuid, link_name=link_name, created_at=time.time())) - obj : Optional[LinkModel] = session.query(LinkModel)\ - .filter_by(link_uuid=link_uuid).one_or_none() + stmt = insert(LinkModel).values(link_data) + stmt = stmt.on_conflict_do_update( + index_elements=[LinkModel.link_uuid], + set_=dict(link_name = stmt.excluded.link_name) + ) + session.execute(stmt) stmt = insert(LinkEndPointModel).values(link_endpoints_data) stmt = stmt.on_conflict_do_nothing( - index_elements=[ - LinkEndPointModel.link_uuid, LinkEndPointModel.context_uuid, LinkEndPointModel.topology_uuid, - LinkEndPointModel.device_uuid, LinkEndPointModel.endpoint_uuid - ], + index_elements=[LinkEndPointModel.link_uuid, LinkEndPointModel.endpoint_uuid] ) session.execute(stmt) session.execute(insert(TopologyLinkModel).values(related_topologies).on_conflict_do_nothing( - index_elements=[ - TopologyLinkModel.context_uuid, TopologyLinkModel.topology_uuid, - TopologyLinkModel.link_uuid - ] + index_elements=[TopologyLinkModel.topology_uuid, TopologyLinkModel.link_uuid] )) + run_transaction(sessionmaker(bind=db_engine), callback) - return False # TODO: improve and check if created/updated + updated = False # TODO: improve and check if created/updated + return LinkId(**json_link_id(link_uuid)),updated def link_delete(db_engine : Engine, request : LinkId) -> bool: - link_uuid = request.link_uuid.uuid + link_uuid = link_get_uuid(request, allow_random=False) def callback(session : Session) -> bool: - session.query(TopologyLinkModel).filter_by(link_uuid=link_uuid).delete() - session.query(LinkEndPointModel).filter_by(link_uuid=link_uuid).delete() + #session.query(TopologyLinkModel).filter_by(link_uuid=link_uuid).delete() + #session.query(LinkEndPointModel).filter_by(link_uuid=link_uuid).delete() num_deleted = session.query(LinkModel).filter_by(link_uuid=link_uuid).delete() #db_link = session.query(LinkModel).filter_by(link_uuid=link_uuid).one_or_none() #session.query(LinkModel).filter_by(link_uuid=link_uuid).delete() diff --git a/src/context/service/database/methods/Service.py b/src/context/service/database/Service.py similarity index 99% rename from src/context/service/database/methods/Service.py rename to src/context/service/database/Service.py index 9f5e519df2c76d34f0415b3264f7849b92b0b461..3b6b4cc267c8087a2d1b2bc2c1e856b8ee22fbd7 100644 --- a/src/context/service/database/methods/Service.py +++ b/src/context/service/database/Service.py @@ -20,7 +20,7 @@ from sqlalchemy_cockroachdb import run_transaction from typing import Dict, List, Optional from common.proto.context_pb2 import ContextId, Service, ServiceId, ServiceIdList, ServiceList from common.rpc_method_wrapper.ServiceExceptions import InvalidArgumentException, NotFoundException -from context.service.database.models.ServiceModel import ServiceModel +from .models.ServiceModel import ServiceModel def service_list_ids(db_engine : Engine, request : ContextId) -> ServiceIdList: context_uuid = request.context_uuid.uuid diff --git a/src/context/service/database/methods/Topology.py b/src/context/service/database/Topology.py similarity index 97% rename from src/context/service/database/methods/Topology.py rename to src/context/service/database/Topology.py index 1abbc55622c490c5360fda103feb7ff4b22c9833..25fa02f4b6a336d8b1e6c1489c09ed2be0a76006 100644 --- a/src/context/service/database/methods/Topology.py +++ b/src/context/service/database/Topology.py @@ -21,8 +21,8 @@ from common.proto.context_pb2 import ContextId, Topology, TopologyId, TopologyId from common.rpc_method_wrapper.ServiceExceptions import InvalidArgumentsException, NotFoundException from common.tools.object_factory.Context import json_context_id from common.tools.object_factory.Topology import json_topology_id -#from context.service.database.models.RelationModels import TopologyDeviceModel, TopologyLinkModel -from context.service.database.models.TopologyModel import TopologyModel +#from .models.RelationModels import TopologyDeviceModel, TopologyLinkModel +from .models.TopologyModel import TopologyModel from .uuids.Context import context_get_uuid from .uuids.Topology import topology_get_uuid diff --git a/src/context/service/database/methods/uuids/__init__.py b/src/context/service/database/methods/uuids/__init__.py deleted file mode 100644 index 9953c820575d42fa88351cc8de022d880ba96e6a..0000000000000000000000000000000000000000 --- a/src/context/service/database/methods/uuids/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# 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. diff --git a/src/context/service/database/models/ConfigRuleModel.py b/src/context/service/database/models/ConfigRuleModel.py index a229f475d340e94619c9c4efb5fcb6e18a4cc7c1..9d56344e8fa1a2983d757e58f2700d510970727f 100644 --- a/src/context/service/database/models/ConfigRuleModel.py +++ b/src/context/service/database/models/ConfigRuleModel.py @@ -13,9 +13,8 @@ # limitations under the License. import enum, json -from sqlalchemy import Column, INTEGER, CheckConstraint, Enum, ForeignKeyConstraint, String, UniqueConstraint, text +from sqlalchemy import CheckConstraint, Column, Enum, ForeignKey, Integer, String from sqlalchemy.dialects.postgresql import UUID -from sqlalchemy.orm import relationship from typing import Dict from .enums.ConfigAction import ORM_ConfigActionEnum from ._Base import _Base @@ -26,40 +25,19 @@ class ConfigRuleKindEnum(enum.Enum): ACL = 'acl' class ConfigRuleModel(_Base): - __tablename__ = 'config_rule' + __tablename__ = 'configrule' - config_rule_uuid = Column(UUID(as_uuid=False), primary_key=True, server_default=text('uuid_generate_v4()')) - device_uuid = Column(UUID(as_uuid=False)) # for device config rules - context_uuid = Column(UUID(as_uuid=False)) # for service/slice config rules - service_uuid = Column(UUID(as_uuid=False)) # for service config rules - #slice_uuid = Column(UUID(as_uuid=False)) # for slice config rules - kind = Column(Enum(ConfigRuleKindEnum)) - action = Column(Enum(ORM_ConfigActionEnum)) - position = Column(INTEGER, nullable=False) - data = Column(String, nullable=False) + configrule_uuid = Column(UUID(as_uuid=False), primary_key=True) + device_uuid = Column(ForeignKey('device.device_uuid', ondelete='CASCADE')) + position = Column(Integer, nullable=False) + kind = Column(Enum(ConfigRuleKindEnum)) + action = Column(Enum(ORM_ConfigActionEnum)) + data = Column(String, nullable=False) __table_args__ = ( CheckConstraint(position >= 0, name='check_position_value'), - UniqueConstraint('device_uuid', 'position', name='unique_per_device'), - UniqueConstraint('context_uuid', 'service_uuid', 'position', name='unique_per_service'), - #UniqueConstraint('context_uuid', 'slice_uuid', 'position', name='unique_per_slice'), - ForeignKeyConstraint( - ['device_uuid'], - ['device.device_uuid'], - ondelete='CASCADE'), - ForeignKeyConstraint( - ['context_uuid', 'service_uuid'], - ['service.context_uuid', 'service.service_uuid'], - ondelete='CASCADE'), - #ForeignKeyConstraint( - # ['context_uuid', 'slice_uuid'], - # ['slice.context_uuid', 'slice.slice_uuid'], - # ondelete='CASCADE'), + #UniqueConstraint('device_uuid', 'position', name='unique_per_device'), ) - device = relationship('DeviceModel', back_populates='config_rules') - service = relationship('ServiceModel', back_populates='config_rules') - #slice = relationship('SliceModel', back_populates='config_rules') - def dump(self) -> Dict: return {self.kind.value: json.loads(self.data)} diff --git a/src/context/service/database/models/DeviceModel.py b/src/context/service/database/models/DeviceModel.py index 33e7804115443b9f5300c77d066b09f3db9972f1..50db8e7bb7a93131e3a4ef635a887eb62316022f 100644 --- a/src/context/service/database/models/DeviceModel.py +++ b/src/context/service/database/models/DeviceModel.py @@ -23,15 +23,16 @@ from ._Base import _Base class DeviceModel(_Base): __tablename__ = 'device' + device_uuid = Column(UUID(as_uuid=False), primary_key=True) device_name = Column(String, nullable=False) device_type = Column(String, nullable=False) device_operational_status = Column(Enum(ORM_DeviceOperationalStatusEnum)) device_drivers = Column(ARRAY(Enum(ORM_DeviceDriverEnum), dimensions=1)) - topology_devices = relationship('TopologyDeviceModel', back_populates='device') - #config_rules = relationship('ConfigRuleModel', passive_deletes=True, back_populates='device', lazy='joined') - endpoints = relationship('EndPointModel', passive_deletes=True, back_populates='device', lazy='joined') + #topology_devices = relationship('TopologyDeviceModel', back_populates='device') + config_rules = relationship('ConfigRuleModel', passive_deletes=True) # lazy='joined', back_populates='device' + endpoints = relationship('EndPointModel', passive_deletes=True) # lazy='joined', back_populates='device' def dump_id(self) -> Dict: return {'device_uuid': {'uuid': self.device_uuid}} @@ -44,8 +45,8 @@ class DeviceModel(_Base): 'device_operational_status': self.device_operational_status.value, 'device_drivers' : [driver.value for driver in self.device_drivers], 'device_config' : {'config_rules': [ - #config_rule.dump() - #for config_rule in sorted(self.config_rules, key=operator.attrgetter('position')) + config_rule.dump() + for config_rule in sorted(self.config_rules, key=operator.attrgetter('position')) ]}, 'device_endpoints' : [ endpoint.dump() diff --git a/src/context/service/database/models/EndPointModel.py b/src/context/service/database/models/EndPointModel.py index 804b688470ad652b35d976e9aebe3bde3ae8d743..f9d5f7658a041f3670fe3256e9c3a863f4682d5b 100644 --- a/src/context/service/database/models/EndPointModel.py +++ b/src/context/service/database/models/EndPointModel.py @@ -23,7 +23,7 @@ class EndPointModel(_Base): __tablename__ = 'endpoint' endpoint_uuid = Column(UUID(as_uuid=False), primary_key=True) - device_uuid = Column(UUID(as_uuid=False), ForeignKey('device.device_uuid', ondelete='CASCADE')) + device_uuid = Column(UUID(as_uuid=False), ForeignKey('device.device_uuid', ondelete='CASCADE' )) topology_uuid = Column(UUID(as_uuid=False), ForeignKey('topology.topology_uuid', ondelete='RESTRICT')) name = Column(String) endpoint_type = Column(String) diff --git a/src/context/service/database/models/LinkModel.py b/src/context/service/database/models/LinkModel.py index eec871e7720dc6a7f20ceacb8620f25f5f5c41e0..053dc0122797c99a956053d40af00e51559cbda5 100644 --- a/src/context/service/database/models/LinkModel.py +++ b/src/context/service/database/models/LinkModel.py @@ -13,7 +13,7 @@ # limitations under the License. from typing import Dict -from sqlalchemy import Column, Float, String +from sqlalchemy import Column, String from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import relationship from ._Base import _Base @@ -23,9 +23,8 @@ class LinkModel(_Base): link_uuid = Column(UUID(as_uuid=False), primary_key=True) link_name = Column(String, nullable=False) - created_at = Column(Float) - topology_links = relationship('TopologyLinkModel', back_populates='link') + #topology_links = relationship('TopologyLinkModel', back_populates='link') link_endpoints = relationship('LinkEndPointModel', back_populates='link') #, lazy='joined') def dump_id(self) -> Dict: diff --git a/src/context/service/database/models/RelationModels.py b/src/context/service/database/models/RelationModels.py index 38d93bee78cb27b654b11135f0aff3b3f982e0bb..89e8e05e082058f28f15421912a9bfbfcd294959 100644 --- a/src/context/service/database/models/RelationModels.py +++ b/src/context/service/database/models/RelationModels.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from sqlalchemy import Column, ForeignKey, ForeignKeyConstraint -from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy import Column, ForeignKey #, ForeignKeyConstraint +#from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import relationship from ._Base import _Base @@ -22,28 +22,14 @@ from ._Base import _Base # connection_fk = ForeignKeyField(ConnectionModel) # sub_service_fk = ForeignKeyField(ServiceModel) -#class LinkEndPointModel(_Base): -# __tablename__ = 'link_endpoint' -# -# link_uuid = Column(UUID(as_uuid=False), primary_key=True) -# context_uuid = Column(UUID(as_uuid=False), primary_key=True) -# topology_uuid = Column(UUID(as_uuid=False), primary_key=True) -# device_uuid = Column(UUID(as_uuid=False), primary_key=True) -# endpoint_uuid = Column(UUID(as_uuid=False), primary_key=True) -# -# link = relationship('LinkModel', back_populates='link_endpoints', lazy='joined') -# endpoint = relationship('EndPointModel', back_populates='link_endpoints', lazy='joined') -# -# __table_args__ = ( -# ForeignKeyConstraint( -# ['link_uuid'], -# ['link.link_uuid'], -# ondelete='CASCADE'), -# ForeignKeyConstraint( -# ['context_uuid', 'topology_uuid', 'device_uuid', 'endpoint_uuid'], -# ['endpoint.context_uuid', 'endpoint.topology_uuid', 'endpoint.device_uuid', 'endpoint.endpoint_uuid'], -# ondelete='CASCADE'), -# ) +class LinkEndPointModel(_Base): + __tablename__ = 'link_endpoint' + + link_uuid = Column(ForeignKey('link.link_uuid', ondelete='CASCADE' ), primary_key=True) + endpoint_uuid = Column(ForeignKey('endpoint.endpoint_uuid', ondelete='RESTRICT'), primary_key=True) + + link = relationship('LinkModel', back_populates='link_endpoints', lazy='joined') + endpoint = relationship('EndPointModel', lazy='joined') # back_populates='link_endpoints' #class ServiceEndPointModel(_Base): # __tablename__ = 'service_endpoint' @@ -94,26 +80,14 @@ class TopologyDeviceModel(_Base): topology_uuid = Column(ForeignKey('topology.topology_uuid', ondelete='RESTRICT'), primary_key=True) device_uuid = Column(ForeignKey('device.device_uuid', ondelete='CASCADE' ), primary_key=True) - topology = relationship('TopologyModel', back_populates='topology_devices', lazy='joined') - device = relationship('DeviceModel', back_populates='topology_devices', lazy='joined') + #topology = relationship('TopologyModel', lazy='joined') # back_populates='topology_devices' + device = relationship('DeviceModel', lazy='joined') # back_populates='topology_devices' -#class TopologyLinkModel(_Base): -# __tablename__ = 'topology_link' -# -# context_uuid = Column(UUID(as_uuid=False), primary_key=True) -# topology_uuid = Column(UUID(as_uuid=False), primary_key=True) -# link_uuid = Column(UUID(as_uuid=False), primary_key=True) -# -# topology = relationship('TopologyModel', back_populates='topology_links', lazy='joined') -# link = relationship('LinkModel', back_populates='topology_links', lazy='joined') -# -# __table_args__ = ( -# ForeignKeyConstraint( -# ['context_uuid', 'topology_uuid'], -# ['topology.context_uuid', 'topology.topology_uuid'], -# ondelete='CASCADE'), -# ForeignKeyConstraint( -# ['link_uuid'], -# ['link.link_uuid'], -# ondelete='CASCADE'), -# ) +class TopologyLinkModel(_Base): + __tablename__ = 'topology_link' + + topology_uuid = Column(ForeignKey('topology.topology_uuid', ondelete='RESTRICT'), primary_key=True) + link_uuid = Column(ForeignKey('link.link_uuid', ondelete='CASCADE' ), primary_key=True) + + #topology = relationship('TopologyModel', lazy='joined') # back_populates='topology_links' + link = relationship('LinkModel', lazy='joined') # back_populates='topology_links' diff --git a/src/context/service/database/models/TopologyModel.py b/src/context/service/database/models/TopologyModel.py index f7053b603e1f77d786636418cbc944383a37b0c0..e0119beadf631cfca0297d011e03448ce93b0efe 100644 --- a/src/context/service/database/models/TopologyModel.py +++ b/src/context/service/database/models/TopologyModel.py @@ -25,9 +25,9 @@ class TopologyModel(_Base): context_uuid = Column(UUID(as_uuid=False), ForeignKey('context.context_uuid')) topology_name = Column(String, nullable=False) - context = relationship('ContextModel', back_populates='topologies') - topology_devices = relationship('TopologyDeviceModel', back_populates='topology') - #topology_links = relationship('TopologyLinkModel', back_populates='topology') + context = relationship('ContextModel', back_populates='topologies') + topology_devices = relationship('TopologyDeviceModel') # back_populates='topology' + topology_links = relationship('TopologyLinkModel' ) # back_populates='topology' def dump_id(self) -> Dict: return { @@ -40,5 +40,5 @@ class TopologyModel(_Base): 'topology_id': self.dump_id(), 'name' : self.topology_name, 'device_ids' : [{'device_uuid': {'uuid': td.device_uuid}} for td in self.topology_devices], - #'link_ids' : [{'link_uuid' : {'uuid': td.link_uuid }} for td in self.topology_links ], + 'link_ids' : [{'link_uuid' : {'uuid': tl.link_uuid }} for tl in self.topology_links ], } diff --git a/src/context/service/database/methods/uuids/Context.py b/src/context/service/database/uuids/Context.py similarity index 100% rename from src/context/service/database/methods/uuids/Context.py rename to src/context/service/database/uuids/Context.py diff --git a/src/context/service/database/methods/uuids/Device.py b/src/context/service/database/uuids/Device.py similarity index 100% rename from src/context/service/database/methods/uuids/Device.py rename to src/context/service/database/uuids/Device.py diff --git a/src/context/service/database/methods/uuids/EndPoint.py b/src/context/service/database/uuids/EndPoint.py similarity index 100% rename from src/context/service/database/methods/uuids/EndPoint.py rename to src/context/service/database/uuids/EndPoint.py diff --git a/src/context/service/database/methods/uuids/Link.py b/src/context/service/database/uuids/Link.py similarity index 100% rename from src/context/service/database/methods/uuids/Link.py rename to src/context/service/database/uuids/Link.py diff --git a/src/context/service/database/methods/uuids/Topology.py b/src/context/service/database/uuids/Topology.py similarity index 100% rename from src/context/service/database/methods/uuids/Topology.py rename to src/context/service/database/uuids/Topology.py diff --git a/src/context/service/database/methods/uuids/_Builder.py b/src/context/service/database/uuids/_Builder.py similarity index 100% rename from src/context/service/database/methods/uuids/_Builder.py rename to src/context/service/database/uuids/_Builder.py diff --git a/src/context/service/database/methods/__init__.py b/src/context/service/database/uuids/__init__.py similarity index 100% rename from src/context/service/database/methods/__init__.py rename to src/context/service/database/uuids/__init__.py diff --git a/src/context/tests/Objects.py b/src/context/tests/Objects.py index 1e50fe3c17070a1260660f8cbbb83bee8a578e47..c350d4f2040f0d85971345c980c502db3072e343 100644 --- a/src/context/tests/Objects.py +++ b/src/context/tests/Objects.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from typing import Dict, List, Tuple from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME from common.proto.kpi_sample_types_pb2 import KpiSampleType from common.tools.object_factory.ConfigRule import json_config_rule_set @@ -48,167 +49,96 @@ PACKET_PORT_SAMPLE_TYPES = [ # ----- Device --------------------------------------------------------------------------------------------------------- -EP1 = '5610e2c0-8abe-4127-80d0-7c68aff1c19e' -EP2 = '7eb80584-2587-4e71-b10c-f3a5c48e84ab' -EP3 = '368baf47-0540-4ab4-add8-a19b5167162c' -EP100 = '6a923121-36e1-4b5e-8cd6-90aceca9b5cf' - - -DEVICE_R1_NAME = 'R1' -DEVICE_R1_ID = json_device_id(DEVICE_R1_NAME) -DEVICE_R1_EPS = [ - json_endpoint(DEVICE_R1_ID, EP2, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), - json_endpoint(DEVICE_R1_ID, EP3, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), - json_endpoint(DEVICE_R1_ID, EP100, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), -] -DEVICE_R1_RULES = [ - json_config_rule_set('dev/rsrc1/value', 'value1'), - json_config_rule_set('dev/rsrc2/value', 'value2'), - json_config_rule_set('dev/rsrc3/value', 'value3'), -] -DEVICE_R1 = json_device_packetrouter_disabled( - DEVICE_R1_NAME, endpoints=DEVICE_R1_EPS, config_rules=DEVICE_R1_RULES) - - -DEVICE_R2_NAME = 'R2' -DEVICE_R2_ID = json_device_id(DEVICE_R2_NAME) -DEVICE_R2_EPS = [ - json_endpoint(DEVICE_R2_ID, EP1, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), - json_endpoint(DEVICE_R2_ID, EP3, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), - json_endpoint(DEVICE_R2_ID, EP100, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), -] -DEVICE_R2_RULES = [ - json_config_rule_set('dev/rsrc1/value', 'value4'), - json_config_rule_set('dev/rsrc2/value', 'value5'), - json_config_rule_set('dev/rsrc3/value', 'value6'), -] -DEVICE_R2 = json_device_packetrouter_disabled( - DEVICE_R2_NAME, endpoints=DEVICE_R2_EPS, config_rules=DEVICE_R2_RULES) - - -DEVICE_R3_NAME = 'R3' -DEVICE_R3_ID = json_device_id(DEVICE_R3_NAME) -DEVICE_R3_EPS = [ - json_endpoint(DEVICE_R3_ID, EP2, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), - json_endpoint(DEVICE_R3_ID, EP3, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), - json_endpoint(DEVICE_R3_ID, EP100, '10G', topology_id=TOPOLOGY_ID, kpi_sample_types=PACKET_PORT_SAMPLE_TYPES), -] -DEVICE_R3_RULES = [ - json_config_rule_set('dev/rsrc1/value', 'value4'), - json_config_rule_set('dev/rsrc2/value', 'value5'), - json_config_rule_set('dev/rsrc3/value', 'value6'), -] -DEVICE_R3 = json_device_packetrouter_disabled( - DEVICE_R3_NAME, endpoints=DEVICE_R3_EPS, config_rules=DEVICE_R3_RULES) +def compose_device(name : str, endpoint_names : List[str]) -> Tuple[str, Dict, Dict]: + device_id = json_device_id(name) + endpoints = [ + json_endpoint(device_id, endpoint_name, 'copper', topology_id=TOPOLOGY_ID, + kpi_sample_types=PACKET_PORT_SAMPLE_TYPES) + for endpoint_name in endpoint_names + ] + config_rules = [ + json_config_rule_set('dev/rsrc1/value', 'value1'), + json_config_rule_set('dev/rsrc2/value', 'value2'), + json_config_rule_set('dev/rsrc3/value', 'value3'), + ] + device = json_device_packetrouter_disabled(name, endpoints=endpoints, config_rules=config_rules) + return name, device_id, device + +DEVICE_R1_NAME, DEVICE_R1_ID, DEVICE_R1 = compose_device('R1', ['1.2', '1.3', '2.2', '2.3']) +DEVICE_R2_NAME, DEVICE_R2_ID, DEVICE_R2 = compose_device('R2', ['1.1', '1.3', '2.1', '2.3']) +DEVICE_R3_NAME, DEVICE_R3_ID, DEVICE_R3 = compose_device('R3', ['1.1', '1.2', '2.1', '2.2']) # ----- Link ----------------------------------------------------------------------------------------------------------- -LINK_R1_R2_UUID = 'c8f92eec-340e-4d31-8d7e-7074927dc889' -LINK_R1_R2_ID = json_link_id(LINK_R1_R2_UUID) -LINK_R1_R2_EPIDS = [ - json_endpoint_id(DEVICE_R1_ID, EP2, topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R2_ID, EP1, topology_id=TOPOLOGY_ID), -] -LINK_R1_R2 = json_link(LINK_R1_R2_UUID, LINK_R1_R2_EPIDS) - - -LINK_R2_R3_UUID = 'f9e3539a-d8f9-4737-b4b4-cacf7f90aa5d' -LINK_R2_R3_ID = json_link_id(LINK_R2_R3_UUID) -LINK_R2_R3_EPIDS = [ - json_endpoint_id(DEVICE_R2_ID, EP3, topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R3_ID, EP2, topology_id=TOPOLOGY_ID), -] -LINK_R2_R3 = json_link(LINK_R2_R3_UUID, LINK_R2_R3_EPIDS) - +def compose_link(name : str, endpoint_ids : List[Tuple[str, str]]) -> Tuple[str, Dict, Dict]: + link_id = json_link_id(name) + endpoint_ids = [ + json_endpoint_id(device_id, endpoint_name, topology_id=TOPOLOGY_ID) + for device_id, endpoint_name in endpoint_ids + ] + link = json_link(name, endpoint_ids) + return name, link_id, link -LINK_R1_R3_UUID = '1f1a988c-47a9-41b2-afd9-ebd6d434a0b4' -LINK_R1_R3_ID = json_link_id(LINK_R1_R3_UUID) -LINK_R1_R3_EPIDS = [ - json_endpoint_id(DEVICE_R1_ID, EP3, topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R3_ID, EP1, topology_id=TOPOLOGY_ID), -] -LINK_R1_R3 = json_link(LINK_R1_R3_UUID, LINK_R1_R3_EPIDS) +LINK_R1_R2_NAME, LINK_R1_R2_ID, LINK_R1_R2 = compose_link('R1==R2', [(DEVICE_R1_ID, '1.2'), (DEVICE_R2_ID, '1.1')]) +LINK_R2_R3_NAME, LINK_R2_R3_ID, LINK_R2_R3 = compose_link('R2==R3', [(DEVICE_R2_ID, '1.3'), (DEVICE_R3_ID, '1.2')]) +LINK_R1_R3_NAME, LINK_R1_R3_ID, LINK_R1_R3 = compose_link('R1==R3', [(DEVICE_R1_ID, '1.3'), (DEVICE_R3_ID, '1.1')]) # ----- Service -------------------------------------------------------------------------------------------------------- -SERVICE_R1_R2_UUID = 'f0432e7b-bb83-4880-9c5d-008c4925ce7d' -SERVICE_R1_R2_ID = json_service_id(SERVICE_R1_R2_UUID, context_id=CONTEXT_ID) -SERVICE_R1_R2_EPIDS = [ - json_endpoint_id(DEVICE_R1_ID, EP100, topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R2_ID, EP100, topology_id=TOPOLOGY_ID), -] -SERVICE_R1_R2_CONST = [ - json_constraint_custom('latency[ms]', '15.2'), - json_constraint_custom('jitter[us]', '1.2'), -] -SERVICE_R1_R2_RULES = [ - json_config_rule_set('svc/rsrc1/value', 'value7'), - json_config_rule_set('svc/rsrc2/value', 'value8'), - json_config_rule_set('svc/rsrc3/value', 'value9'), -] -SERVICE_R1_R2 = json_service_l3nm_planned( - SERVICE_R1_R2_UUID, endpoint_ids=SERVICE_R1_R2_EPIDS, constraints=SERVICE_R1_R2_CONST, - config_rules=SERVICE_R1_R2_RULES) - - -SERVICE_R1_R3_UUID = 'fab21cef-542a-4948-bb4a-a0468abfa925' -SERVICE_R1_R3_ID = json_service_id(SERVICE_R1_R3_UUID, context_id=CONTEXT_ID) -SERVICE_R1_R3_EPIDS = [ - json_endpoint_id(DEVICE_R1_ID, 'EP100', topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R3_ID, 'EP100', topology_id=TOPOLOGY_ID), -] -SERVICE_R1_R3_CONST = [ - json_constraint_custom('latency[ms]', '5.8'), - json_constraint_custom('jitter[us]', '0.1'), -] -SERVICE_R1_R3_RULES = [ - json_config_rule_set('svc/rsrc1/value', 'value7'), - json_config_rule_set('svc/rsrc2/value', 'value8'), - json_config_rule_set('svc/rsrc3/value', 'value9'), -] -SERVICE_R1_R3 = json_service_l3nm_planned( - SERVICE_R1_R3_UUID, endpoint_ids=SERVICE_R1_R3_EPIDS, constraints=SERVICE_R1_R3_CONST, - config_rules=SERVICE_R1_R3_RULES) - - -SERVICE_R2_R3_UUID = '1f2a808f-62bb-4eaa-94fb-448ed643e61a' -SERVICE_R2_R3_ID = json_service_id(SERVICE_R2_R3_UUID, context_id=CONTEXT_ID) -SERVICE_R2_R3_EPIDS = [ - json_endpoint_id(DEVICE_R2_ID, 'EP100', topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R3_ID, 'EP100', topology_id=TOPOLOGY_ID), -] -SERVICE_R2_R3_CONST = [ - json_constraint_custom('latency[ms]', '23.1'), - json_constraint_custom('jitter[us]', '3.4'), -] -SERVICE_R2_R3_RULES = [ - json_config_rule_set('svc/rsrc1/value', 'value7'), - json_config_rule_set('svc/rsrc2/value', 'value8'), - json_config_rule_set('svc/rsrc3/value', 'value9'), -] -SERVICE_R2_R3 = json_service_l3nm_planned( - SERVICE_R2_R3_UUID, endpoint_ids=SERVICE_R2_R3_EPIDS, constraints=SERVICE_R2_R3_CONST, - config_rules=SERVICE_R2_R3_RULES) +def compose_service( + name : str, endpoint_ids : List[Tuple[str, str]], latency_ms : float, jitter_us : float +) -> Tuple[str, Dict, Dict]: + service_id = json_service_id(name, context_id=CONTEXT_ID) + endpoint_ids = [ + json_endpoint_id(device_id, endpoint_name, topology_id=TOPOLOGY_ID) + for device_id, endpoint_name in endpoint_ids + ] + constraints = [ + json_constraint_custom('latency[ms]', str(latency_ms)), + json_constraint_custom('jitter[us]', str(jitter_us)), + ] + config_rules = [ + json_config_rule_set('svc/rsrc1/value', 'value7'), + json_config_rule_set('svc/rsrc2/value', 'value8'), + json_config_rule_set('svc/rsrc3/value', 'value9'), + ] + service = json_service_l3nm_planned( + name, endpoint_ids=endpoint_ids, constraints=constraints, config_rules=config_rules) + return name, service_id, service + +SERVICE_R1_R2_NAME, SERVICE_R1_R2_ID, SERVICE_R1_R2 = compose_service( + 'R1-R2', [(DEVICE_R1_ID, '2.2'), (DEVICE_R2_ID, '2.1')], 15.2, 1.2) + +SERVICE_R1_R3_NAME, SERVICE_R1_R3_ID, SERVICE_R1_R3 = compose_service( + 'R1-R3', [(DEVICE_R1_ID, '2.3'), (DEVICE_R3_ID, '2.1')], 5.8, 0.1) + +SERVICE_R2_R3_NAME, SERVICE_R2_R3_ID, SERVICE_R2_R3 = compose_service( + 'R2-R3', [(DEVICE_R2_ID, '2.3'), (DEVICE_R3_ID, '2.2')], 23.1, 3.4) # ----- Connection ----------------------------------------------------------------------------------------------------- -CONNECTION_R1_R3_UUID = 'CON:R1/EP100-R3/EP100' -CONNECTION_R1_R3_ID = json_connection_id(CONNECTION_R1_R3_UUID) -CONNECTION_R1_R3_EPIDS = [ - json_endpoint_id(DEVICE_R1_ID, 'EP100', topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R1_ID, 'EP2', topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R2_ID, 'EP1', topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R2_ID, 'EP3', topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R3_ID, 'EP2', topology_id=TOPOLOGY_ID), - json_endpoint_id(DEVICE_R3_ID, 'EP100', topology_id=TOPOLOGY_ID), -] -CONNECTION_R1_R3_SVCIDS = [SERVICE_R1_R2_ID, SERVICE_R2_R3_ID] -CONNECTION_R1_R3 = json_connection( - CONNECTION_R1_R3_UUID, service_id=SERVICE_R1_R3_ID, path_hops_endpoint_ids=CONNECTION_R1_R3_EPIDS, - sub_service_ids=CONNECTION_R1_R3_SVCIDS) +def compose_connection( + name : str, service_id : Dict, endpoint_ids : List[Tuple[str, str]], sub_service_ids : List[Dict] = [] +) -> Tuple[str, Dict, Dict]: + connection_id = json_connection_id(name) + endpoint_ids = [ + json_endpoint_id(device_id, endpoint_name, topology_id=TOPOLOGY_ID) + for device_id, endpoint_name in endpoint_ids + ] + connection = json_connection( + name, service_id=service_id, path_hops_endpoint_ids=endpoint_ids, sub_service_ids=sub_service_ids) + return name, connection_id, connection + +CONNECTION_R1_R3_NAME, CONNECTION_R1_R3_ID, CONNECTION_R1_R3 = compose_connection( + 'CON:R1/2.3-R3/2.1', SERVICE_R1_R3_ID, [ + (DEVICE_R1_ID, '2.3'), + (DEVICE_R1_ID, '1.2'), (DEVICE_R2_ID, '1.1'), + (DEVICE_R2_ID, '1.3'), (DEVICE_R3_ID, '1.2'), + (DEVICE_R3_ID, '2.1') + ], sub_service_ids=[SERVICE_R1_R2_ID, SERVICE_R2_R3_ID]) # ----- PolicyRule ------------------------------------------------------------------------------------------------------- -POLICY_RULE_UUID = '56380225-3e40-4f74-9162-529f8dcb96a1' -POLICY_RULE_ID = json_policy_rule_id(POLICY_RULE_UUID) -POLICY_RULE = json_policy_rule(POLICY_RULE_UUID) +POLICY_RULE_NAME = '56380225-3e40-4f74-9162-529f8dcb96a1' +POLICY_RULE_ID = json_policy_rule_id(POLICY_RULE_NAME) +POLICY_RULE = json_policy_rule(POLICY_RULE_NAME) diff --git a/src/context/tests/conftest.py b/src/context/tests/conftest.py index 872c51ccf5f8ab05e361e32ef9612259b3834c1d..8bf4156c57d6c121b33f4f5c0cb0c2adfb55959c 100644 --- a/src/context/tests/conftest.py +++ b/src/context/tests/conftest.py @@ -48,7 +48,7 @@ def context_db_mb(request) -> Tuple[sqlalchemy.engine.Engine, MessageBroker]: yield _db_engine, _msg_broker _msg_broker.terminate() -RAW_METRICS = None +RAW_METRICS = dict() @pytest.fixture(scope='session') def context_service(context_db_mb : Tuple[Database, MessageBroker]): # pylint: disable=redefined-outer-name diff --git a/src/context/tests/test_context.py b/src/context/tests/test_context.py index 915989eb791ba7965801b162e24b33d626410e18..443d36c92a7871ba93e02be0828fa1036bd2abdd 100644 --- a/src/context/tests/test_context.py +++ b/src/context/tests/test_context.py @@ -15,7 +15,7 @@ import copy, grpc, pytest from common.proto.context_pb2 import Context, ContextId, Empty from context.client.ContextClient import ContextClient -from context.service.database.methods.uuids.Context import context_get_uuid +from context.service.database.uuids.Context import context_get_uuid #from context.client.EventsCollector import EventsCollector from .Objects import CONTEXT, CONTEXT_ID, CONTEXT_NAME diff --git a/src/context/tests/test_device.py b/src/context/tests/test_device.py index 381b5d4fdec4a1d748171b9c85d084b1bf71de21..e53ad747c3934c34e81d8ba80b069c125a761cb8 100644 --- a/src/context/tests/test_device.py +++ b/src/context/tests/test_device.py @@ -16,7 +16,7 @@ import copy, grpc, pytest from common.proto.context_pb2 import ( Context, ContextId, Device, DeviceDriverEnum, DeviceId, DeviceOperationalStatusEnum, Empty, Topology, TopologyId) from context.client.ContextClient import ContextClient -from context.service.database.methods.uuids.Device import device_get_uuid +from context.service.database.uuids.Device import device_get_uuid #from context.client.EventsCollector import EventsCollector from .Objects import CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID, DEVICE_R1_NAME, TOPOLOGY, TOPOLOGY_ID @@ -88,11 +88,11 @@ def test_device(context_client : ContextClient) -> None: assert response.device_id.device_uuid.uuid == device_uuid assert response.name == DEVICE_R1_NAME assert response.device_type == 'packet-router' - #assert len(response.device_config.config_rules) == 3 + assert len(response.device_config.config_rules) == 3 assert response.device_operational_status == DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_DISABLED assert len(response.device_drivers) == 1 assert DeviceDriverEnum.DEVICEDRIVER_OPENCONFIG in response.device_drivers - assert len(response.device_endpoints) == 3 + assert len(response.device_endpoints) == 4 # ----- List when the object exists -------------------------------------------------------------------------------- response = context_client.ListDeviceIds(Empty()) @@ -104,11 +104,11 @@ def test_device(context_client : ContextClient) -> None: assert response.devices[0].device_id.device_uuid.uuid == device_uuid assert response.devices[0].name == DEVICE_R1_NAME assert response.devices[0].device_type == 'packet-router' - #assert len(response.devices[0].device_config.config_rules) == 3 + assert len(response.devices[0].device_config.config_rules) == 3 assert response.devices[0].device_operational_status == DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_DISABLED assert len(response.devices[0].device_drivers) == 1 assert DeviceDriverEnum.DEVICEDRIVER_OPENCONFIG in response.devices[0].device_drivers - assert len(response.devices[0].device_endpoints) == 3 + assert len(response.devices[0].device_endpoints) == 4 # ----- Update the object ------------------------------------------------------------------------------------------ new_device_name = 'new' @@ -131,12 +131,12 @@ def test_device(context_client : ContextClient) -> None: assert response.device_id.device_uuid.uuid == device_uuid assert response.name == new_device_name assert response.device_type == 'packet-router' - #assert len(response.device_config.config_rules) == 3 + assert len(response.device_config.config_rules) == 3 assert response.device_operational_status == DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED assert len(response.device_drivers) == 2 assert DeviceDriverEnum.DEVICEDRIVER_UNDEFINED in response.device_drivers assert DeviceDriverEnum.DEVICEDRIVER_OPENCONFIG in response.device_drivers - assert len(response.device_endpoints) == 3 + assert len(response.device_endpoints) == 4 # ----- List when the object is modified --------------------------------------------------------------------------- response = context_client.ListDeviceIds(Empty()) @@ -148,12 +148,12 @@ def test_device(context_client : ContextClient) -> None: assert response.devices[0].device_id.device_uuid.uuid == device_uuid assert response.devices[0].name == new_device_name assert response.devices[0].device_type == 'packet-router' - #assert len(response.devices[0].device_config.config_rules) == 3 + assert len(response.devices[0].device_config.config_rules) == 3 assert response.devices[0].device_operational_status == DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED assert len(response.devices[0].device_drivers) == 2 assert DeviceDriverEnum.DEVICEDRIVER_UNDEFINED in response.devices[0].device_drivers assert DeviceDriverEnum.DEVICEDRIVER_OPENCONFIG in response.devices[0].device_drivers - assert len(response.devices[0].device_endpoints) == 3 + assert len(response.devices[0].device_endpoints) == 4 # ----- Create object relation ------------------------------------------------------------------------------------- #TOPOLOGY_WITH_DEVICE = copy.deepcopy(TOPOLOGY) @@ -163,11 +163,11 @@ def test_device(context_client : ContextClient) -> None: #assert response.topology_uuid.uuid == topology_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - # event = events_collector.get_event(block=True) - # assert isinstance(event, TopologyEvent) - # assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE - # assert response.context_id.context_uuid.uuid == context_uuid - # assert response.topology_uuid.uuid == topology_uuid + #event = events_collector.get_event(block=True) + #assert isinstance(event, TopologyEvent) + #assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE + #assert response.context_id.context_uuid.uuid == context_uuid + #assert response.topology_uuid.uuid == topology_uuid # ----- Check relation was created --------------------------------------------------------------------------------- response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID)) @@ -178,7 +178,7 @@ def test_device(context_client : ContextClient) -> None: assert len(response.link_ids) == 0 # ----- Remove the object ------------------------------------------------------------------------------------------ - #context_client.RemoveDevice(DeviceId(**DEVICE_R1_ID)) + context_client.RemoveDevice(DeviceId(**DEVICE_R1_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- #event = events_collector.get_event(block=True) @@ -187,15 +187,21 @@ def test_device(context_client : ContextClient) -> None: #assert event.device_id.device_uuid.uuid == device_uuid # ----- List after deleting the object ----------------------------------------------------------------------------- - #response = context_client.ListDeviceIds(Empty()) - #assert len(response.device_ids) == 0 + response = context_client.ListDeviceIds(Empty()) + assert len(response.device_ids) == 0 + + response = context_client.ListDevices(Empty()) + assert len(response.devices) == 0 - #response = context_client.ListDevices(Empty()) - #assert len(response.devices) == 0 + response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID)) + assert response.topology_id.context_id.context_uuid.uuid == context_uuid + assert response.topology_id.topology_uuid.uuid == topology_uuid + assert len(response.device_ids) == 0 + assert len(response.link_ids) == 0 # ----- Clean dependencies used in the test and capture related events --------------------------------------------- - #context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) - #context_client.RemoveContext(ContextId(**CONTEXT_ID)) + context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) + context_client.RemoveContext(ContextId(**CONTEXT_ID)) #events = events_collector.get_events(block=True, count=2) #assert isinstance(events[0], TopologyEvent) diff --git a/src/context/tests/_test_link.py b/src/context/tests/test_link.py similarity index 51% rename from src/context/tests/_test_link.py rename to src/context/tests/test_link.py index 963fd72cf2c44c0d2d84b74728466df2595f9ebd..ec767f1c9bba56fe3d1f3ece4ef7b00b0951a9be 100644 --- a/src/context/tests/_test_link.py +++ b/src/context/tests/test_link.py @@ -13,172 +13,194 @@ # limitations under the License. import copy, grpc, pytest -from common.Constants import DEFAULT_CONTEXT_UUID, DEFAULT_TOPOLOGY_UUID from common.proto.context_pb2 import Context, ContextId, Device, DeviceId, Empty, Link, LinkId, Topology, TopologyId from context.client.ContextClient import ContextClient #from context.client.EventsCollector import EventsCollector +from context.service.database.uuids.Link import link_get_uuid from .Objects import ( - CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID, DEVICE_R1_UUID, DEVICE_R2, DEVICE_R2_ID, DEVICE_R2_UUID, LINK_R1_R2, - LINK_R1_R2_ID, LINK_R1_R2_UUID, TOPOLOGY, TOPOLOGY_ID) + CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID, DEVICE_R2, DEVICE_R2_ID, LINK_R1_R2, LINK_R1_R2_ID, LINK_R1_R2_NAME, + TOPOLOGY, TOPOLOGY_ID) -def grpc_link(context_client_grpc : ContextClient) -> None: +@pytest.mark.depends(on=['context/tests/test_device.py::test_device']) +def test_link(context_client : ContextClient) -> None: # ----- Initialize the EventsCollector ----------------------------------------------------------------------------- #events_collector = EventsCollector( - # context_client_grpc, log_events_received=True, + # context_client, log_events_received=True, # activate_context_collector = False, activate_topology_collector = False, activate_device_collector = False, # activate_link_collector = True, activate_service_collector = False, activate_slice_collector = False, # activate_connection_collector = False) #events_collector.start() # ----- Prepare dependencies for the test and capture related events ----------------------------------------------- - response = context_client_grpc.SetContext(Context(**CONTEXT)) - assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID + response = context_client.SetContext(Context(**CONTEXT)) + context_uuid = response.context_uuid.uuid - response = context_client_grpc.SetTopology(Topology(**TOPOLOGY)) - assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID - assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID + response = context_client.SetTopology(Topology(**TOPOLOGY)) + topology_uuid = response.topology_uuid.uuid - response = context_client_grpc.SetDevice(Device(**DEVICE_R1)) - assert response.device_uuid.uuid == DEVICE_R1_UUID + response = context_client.SetDevice(Device(**DEVICE_R1)) + device_r1_uuid = response.device_uuid.uuid - response = context_client_grpc.SetDevice(Device(**DEVICE_R2)) - assert response.device_uuid.uuid == DEVICE_R2_UUID + response = context_client.SetDevice(Device(**DEVICE_R2)) + device_r2_uuid = response.device_uuid.uuid # events = events_collector.get_events(block=True, count=4) # assert isinstance(events[0], ContextEvent) # assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - # assert events[0].context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID + # assert events[0].context_id.context_uuid.uuid == context_uuid # assert isinstance(events[1], TopologyEvent) # assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - # assert events[1].topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID - # assert events[1].topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID + # assert events[1].topology_id.context_id.context_uuid.uuid == context_uuid + # assert events[1].topology_id.topology_uuid.uuid == topology_uuid # assert isinstance(events[2], DeviceEvent) # assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - # assert events[2].device_id.device_uuid.uuid == DEVICE_R1_UUID + # assert events[2].device_id.device_uuid.uuid == device_r1_uuid # assert isinstance(events[3], DeviceEvent) # assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - # assert events[3].device_id.device_uuid.uuid == DEVICE_R2_UUID + # assert events[3].device_id.device_uuid.uuid == device_r2_uuid # ----- Get when the object does not exist ------------------------------------------------------------------------- + link_id = LinkId(**LINK_R1_R2_ID) + link_uuid = link_get_uuid(link_id, allow_random=False) with pytest.raises(grpc.RpcError) as e: - context_client_grpc.GetLink(LinkId(**LINK_R1_R2_ID)) + context_client.GetLink(link_id) assert e.value.code() == grpc.StatusCode.NOT_FOUND - assert e.value.details() == 'Link({:s}) not found'.format(LINK_R1_R2_UUID) + MSG = 'Link({:s}) not found; link_uuid generated was: {:s}' + assert e.value.details() == MSG.format(LINK_R1_R2_NAME, link_uuid) # ----- List when the object does not exist ------------------------------------------------------------------------ - response = context_client_grpc.ListLinkIds(Empty()) + response = context_client.ListLinkIds(Empty()) assert len(response.link_ids) == 0 - response = context_client_grpc.ListLinks(Empty()) + response = context_client.ListLinks(Empty()) assert len(response.links) == 0 # ----- Create the object ------------------------------------------------------------------------------------------ - response = context_client_grpc.SetLink(Link(**LINK_R1_R2)) - assert response.link_uuid.uuid == LINK_R1_R2_UUID + response = context_client.SetLink(Link(**LINK_R1_R2)) + assert response.link_uuid.uuid == link_uuid # ----- Check create event ----------------------------------------------------------------------------------------- #event = events_collector.get_event(block=True) #assert isinstance(event, LinkEvent) #assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert event.link_id.link_uuid.uuid == LINK_R1_R2_UUID + #assert event.link_id.link_uuid.uuid == link_uuid # ----- Get when the object exists --------------------------------------------------------------------------------- - response = context_client_grpc.GetLink(LinkId(**LINK_R1_R2_ID)) - assert response.link_id.link_uuid.uuid == LINK_R1_R2_UUID - assert response.name == '' + response = context_client.GetLink(LinkId(**LINK_R1_R2_ID)) + assert response.link_id.link_uuid.uuid == link_uuid + assert response.name == LINK_R1_R2_NAME assert len(response.link_endpoint_ids) == 2 # ----- List when the object exists -------------------------------------------------------------------------------- - response = context_client_grpc.ListLinkIds(Empty()) + response = context_client.ListLinkIds(Empty()) assert len(response.link_ids) == 1 - assert response.link_ids[0].link_uuid.uuid == LINK_R1_R2_UUID + assert response.link_ids[0].link_uuid.uuid == link_uuid - response = context_client_grpc.ListLinks(Empty()) + response = context_client.ListLinks(Empty()) assert len(response.links) == 1 - assert response.links[0].link_id.link_uuid.uuid == LINK_R1_R2_UUID - assert response.links[0].name == '' + assert response.links[0].link_id.link_uuid.uuid == link_uuid + assert response.links[0].name == LINK_R1_R2_NAME assert len(response.links[0].link_endpoint_ids) == 2 # ----- Update the object ------------------------------------------------------------------------------------------ - new_link_name = 'l1' + new_link_name = 'new' LINK_UPDATED = copy.deepcopy(LINK_R1_R2) LINK_UPDATED['name'] = new_link_name - response = context_client_grpc.SetLink(Link(**LINK_UPDATED)) - assert response.link_uuid.uuid == LINK_R1_R2_UUID + response = context_client.SetLink(Link(**LINK_UPDATED)) + assert response.link_uuid.uuid == link_uuid # ----- Check update event ----------------------------------------------------------------------------------------- #event = events_collector.get_event(block=True) #assert isinstance(event, LinkEvent) #assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE - #assert event.link_id.link_uuid.uuid == LINK_R1_R2_UUID + #assert event.link_id.link_uuid.uuid == link_uuid # ----- Get when the object is modified ---------------------------------------------------------------------------- - response = context_client_grpc.GetLink(LinkId(**LINK_R1_R2_ID)) - assert response.link_id.link_uuid.uuid == LINK_R1_R2_UUID + response = context_client.GetLink(LinkId(**LINK_R1_R2_ID)) + assert response.link_id.link_uuid.uuid == link_uuid assert response.name == new_link_name assert len(response.link_endpoint_ids) == 2 # ----- List when the object is modified --------------------------------------------------------------------------- - response = context_client_grpc.ListLinkIds(Empty()) + response = context_client.ListLinkIds(Empty()) assert len(response.link_ids) == 1 - assert response.link_ids[0].link_uuid.uuid == LINK_R1_R2_UUID + assert response.link_ids[0].link_uuid.uuid == link_uuid - response = context_client_grpc.ListLinks(Empty()) + response = context_client.ListLinks(Empty()) assert len(response.links) == 1 - assert response.links[0].link_id.link_uuid.uuid == LINK_R1_R2_UUID + assert response.links[0].link_id.link_uuid.uuid == link_uuid assert response.links[0].name == new_link_name assert len(response.links[0].link_endpoint_ids) == 2 # ----- Create object relation ------------------------------------------------------------------------------------- - TOPOLOGY_WITH_LINK = copy.deepcopy(TOPOLOGY) - TOPOLOGY_WITH_LINK['link_ids'].append(LINK_R1_R2_ID) - response = context_client_grpc.SetTopology(Topology(**TOPOLOGY_WITH_LINK)) - assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID - assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID + #TOPOLOGY_WITH_LINK = copy.deepcopy(TOPOLOGY) + #TOPOLOGY_WITH_LINK['link_ids'].append(LINK_R1_R2_ID) + #response = context_client.SetTopology(Topology(**TOPOLOGY_WITH_LINK)) + #assert response.context_id.context_uuid.uuid == context_uuid + #assert response.topology_uuid.uuid == topology_uuid # ----- Check update event ----------------------------------------------------------------------------------------- #event = events_collector.get_event(block=True) #assert isinstance(event, TopologyEvent) #assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE - #assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID - #assert response.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID + #assert response.context_id.context_uuid.uuid == context_uuid + #assert response.topology_uuid.uuid == topology_uuid # ----- Check relation was created --------------------------------------------------------------------------------- - response = context_client_grpc.GetTopology(TopologyId(**TOPOLOGY_ID)) - assert response.topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID - assert response.topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID + response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID)) + assert response.topology_id.context_id.context_uuid.uuid == context_uuid + assert response.topology_id.topology_uuid.uuid == topology_uuid assert len(response.device_ids) == 2 - assert response.device_ids[0].device_uuid.uuid in {DEVICE_R1_UUID, DEVICE_R2_UUID} - assert response.device_ids[1].device_uuid.uuid in {DEVICE_R1_UUID, DEVICE_R2_UUID} + assert response.device_ids[0].device_uuid.uuid in {device_r1_uuid, device_r2_uuid} + assert response.device_ids[1].device_uuid.uuid in {device_r1_uuid, device_r2_uuid} assert len(response.link_ids) == 1 - assert response.link_ids[0].link_uuid.uuid == LINK_R1_R2_UUID + assert response.link_ids[0].link_uuid.uuid == link_uuid # ----- Remove the object ------------------------------------------------------------------------------------------ - context_client_grpc.RemoveLink(LinkId(**LINK_R1_R2_ID)) - context_client_grpc.RemoveDevice(DeviceId(**DEVICE_R1_ID)) - context_client_grpc.RemoveDevice(DeviceId(**DEVICE_R2_ID)) - context_client_grpc.RemoveTopology(TopologyId(**TOPOLOGY_ID)) - context_client_grpc.RemoveContext(ContextId(**CONTEXT_ID)) + context_client.RemoveLink(LinkId(**LINK_R1_R2_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - #events = events_collector.get_events(block=True, count=5) - #assert isinstance(events[0], LinkEvent) + #event = events_collector.get_event(block=True) + #assert isinstance(event, LinkEvent) + #assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + #assert event.link_id.link_uuid.uuid == link_uuid + + # ----- List after deleting the object ----------------------------------------------------------------------------- + response = context_client.ListLinkIds(Empty()) + assert len(response.link_ids) == 0 + + response = context_client.ListLinks(Empty()) + assert len(response.links) == 0 + + response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID)) + assert response.topology_id.context_id.context_uuid.uuid == context_uuid + assert response.topology_id.topology_uuid.uuid == topology_uuid + assert len(response.device_ids) == 2 + assert response.device_ids[0].device_uuid.uuid in {device_r1_uuid, device_r2_uuid} + assert response.device_ids[1].device_uuid.uuid in {device_r1_uuid, device_r2_uuid} + assert len(response.link_ids) == 0 + + # ----- Clean dependencies used in the test and capture related events --------------------------------------------- + context_client.RemoveDevice(DeviceId(**DEVICE_R1_ID)) + context_client.RemoveDevice(DeviceId(**DEVICE_R2_ID)) + context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) + context_client.RemoveContext(ContextId(**CONTEXT_ID)) + + #events = events_collector.get_events(block=True, count=4) + #assert isinstance(events[0], DeviceEvent) #assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[0].link_id.link_uuid.uuid == LINK_R1_R2_UUID + #assert events[0].device_id.device_uuid.uuid == device_r1_uuid #assert isinstance(events[1], DeviceEvent) #assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[1].device_id.device_uuid.uuid == DEVICE_R1_UUID - #assert isinstance(events[2], DeviceEvent) + #assert events[1].device_id.device_uuid.uuid == device_r2_uuid + #assert isinstance(events[2], TopologyEvent) #assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[2].device_id.device_uuid.uuid == DEVICE_R2_UUID - #assert isinstance(events[3], TopologyEvent) + #assert events[2].topology_id.context_id.context_uuid.uuid == context_uuid + #assert events[2].topology_id.topology_uuid.uuid == topology_uuid + #assert isinstance(events[3], ContextEvent) #assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[3].topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID - #assert events[3].topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID - #assert isinstance(events[4], ContextEvent) - #assert events[4].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[4].context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID + #assert events[3].context_id.context_uuid.uuid == context_uuid # ----- Stop the EventsCollector ----------------------------------------------------------------------------------- #events_collector.stop() diff --git a/src/context/tests/test_topology.py b/src/context/tests/test_topology.py index 142887d09031596b7e82e40f424b0100e31199c5..51b224007d7a60f4db6b4f015360e197dea4ec7f 100644 --- a/src/context/tests/test_topology.py +++ b/src/context/tests/test_topology.py @@ -15,7 +15,7 @@ import copy, grpc, pytest from common.proto.context_pb2 import Context, ContextId, Topology, TopologyId from context.client.ContextClient import ContextClient -from context.service.database.methods.uuids.Topology import topology_get_uuid +from context.service.database.uuids.Topology import topology_get_uuid #from context.client.EventsCollector import EventsCollector from .Objects import CONTEXT, CONTEXT_ID, CONTEXT_NAME, TOPOLOGY, TOPOLOGY_ID, TOPOLOGY_NAME diff --git a/test-context.sh b/test-context.sh index 7ad303ca94280b13901980c1cdbcd35583c0041e..79a9d5653621db615030a09e5b362438bfae637e 100755 --- a/test-context.sh +++ b/test-context.sh @@ -44,7 +44,8 @@ coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose --ma context/tests/test_hasher.py \ context/tests/test_context.py \ context/tests/test_topology.py \ - context/tests/test_device.py + context/tests/test_device.py \ + context/tests/test_link.py echo echo "Coverage report:"