diff --git a/src/context/service/database/models/DeviceModel.py b/src/context/service/database/models/DeviceModel.py index 5e40b4dbb3388fc14f8f30efdcac90b5bcd55940..0613caba6a433d4440908f4f796c26349f8d9af7 100644 --- a/src/context/service/database/models/DeviceModel.py +++ b/src/context/service/database/models/DeviceModel.py @@ -34,13 +34,11 @@ class DeviceModel(_Base): updated_at = Column(DateTime, nullable=False) #topology_devices = relationship('TopologyDeviceModel', back_populates='device') - config_rules = relationship('DeviceConfigRuleModel', passive_deletes=True) # lazy='joined', back_populates='device' - endpoints = relationship('EndPointModel', passive_deletes=True) # lazy='joined', back_populates='device' - components = relationship('ComponentModel', passive_deletes=True) # lazy='joined', back_populates='device' - controller = relationship('DeviceModel', remote_side=[device_uuid], passive_deletes=True) # lazy='joined', back_populates='device' - - # ------------------- Experimental ----------------------------------- - optical_config= relationship('OpticalConfigModel',passive_deletes=True) + config_rules = relationship('DeviceConfigRuleModel', passive_deletes=True) # lazy='joined', back_populates='device' + endpoints = relationship('EndPointModel', passive_deletes=True) # lazy='joined', back_populates='device' + components = relationship('ComponentModel', passive_deletes=True) # lazy='joined', back_populates='device' + controller = relationship('DeviceModel', remote_side=[device_uuid], passive_deletes=True) # lazy='joined', back_populates='device' + optical_config = relationship('OpticalConfigModel', passive_deletes=True) def dump_id(self) -> Dict: return {'device_uuid': {'uuid': self.device_uuid}} diff --git a/src/context/service/database/models/EndPointModel.py b/src/context/service/database/models/EndPointModel.py index a1bccae5f3cf7d9d5c9703d0f4ee001c2eed81d5..37a03393bd6a571d8587be7f9c345097971d5111 100644 --- a/src/context/service/database/models/EndPointModel.py +++ b/src/context/service/database/models/EndPointModel.py @@ -33,9 +33,9 @@ class EndPointModel(_Base): updated_at = Column(DateTime, nullable=False) endpoint_location = Column(String, nullable=True) - device = relationship('DeviceModel', back_populates='endpoints') # lazy='selectin' - topology = relationship('TopologyModel', lazy='selectin') - optical_link_endpoints = relationship('OpticalLinkEndPointModel', back_populates='endpoint' ) + device = relationship('DeviceModel', back_populates='endpoints') # lazy='selectin' + topology = relationship('TopologyModel', lazy='selectin') + optical_link_endpoints = relationship('OpticalLinkEndPointModel', back_populates='endpoint' ) #link_endpoints = relationship('LinkEndPointModel', back_populates='endpoint' ) #service_endpoints = relationship('ServiceEndPointModel', back_populates='endpoint' ) diff --git a/src/context/service/database/models/Slot.py b/src/context/service/database/models/Slot.py index 9932600a500d02ca143d24fe65eca27431a82827..3d8602bd40464eafa8a6234881b100c73134bed7 100644 --- a/src/context/service/database/models/Slot.py +++ b/src/context/service/database/models/Slot.py @@ -12,75 +12,51 @@ # See the License for the specific language governing permissions and # limitations under the License. - -from common.tools.object_factory.OpticalLink import order_dict -import logging - -from sqlalchemy.types import PickleType , TypeDecorator ,Integer - - +from sqlalchemy.types import TypeDecorator, Integer class SlotType(TypeDecorator): - impl = Integer def process_bind_param(self, value, dialect): - if value is not None: - - value =order_dict(value) - - bin_num="0b" + bin_num = "0b" for i,(key,val) in enumerate(value.items()): bin_num =bin_num + f"{val}" - int_num = int(bin_num,2) - return int_num def process_result_value(self, value, dialect): - if value is not None: - slot= dict() + slot = dict() bin_num = bin(value) - sliced_num=bin_num[2:] + sliced_num = bin_num[2:] for i in range(len(sliced_num)): slot[str(i+1)]=int(sliced_num[i]) return slot - - -class C_Slot (SlotType): - start_point=0 - - +class C_Slot(SlotType): + start_point = 0 + def process_result_value(self, value, dialect): - if value is not None: - slot= dict() + slot = dict() bin_num = bin(value) - sliced_num=bin_num[2:] + sliced_num = bin_num[2:] if (len(sliced_num) != 20) : for i in range(0,20 - len(sliced_num)): - sliced_num='0'+sliced_num - + sliced_num = '0' + sliced_num for i in range(len(sliced_num)): slot[str(self.start_point+i+1)]=int(sliced_num[i]) - return slot - - -class L_Slot (SlotType): - start_point=100 +class L_Slot (SlotType): + start_point = 100 - def process_result_value(self, value, dialect): - if value is not None: - slot= dict() + slot = dict() bin_num = bin(value) - sliced_num=bin_num[2:] + sliced_num = bin_num[2:] if (len(sliced_num) != 20) : for i in range(0,20 - len(sliced_num)): sliced_num='0'+sliced_num @@ -89,11 +65,9 @@ class L_Slot (SlotType): return slot class S_Slot (SlotType): - start_point=500 - - + start_point = 500 + def process_result_value(self, value, dialect): - if value is not None: slot= dict() bin_num = bin(value) diff --git a/src/context/service/database/models/TopologyModel.py b/src/context/service/database/models/TopologyModel.py index 41a9b5aa01aa8823ecd6221892dfbfae9f7b4ca1..cd731ed60efd6be266dea4bf3c9847bbceb0370e 100644 --- a/src/context/service/database/models/TopologyModel.py +++ b/src/context/service/database/models/TopologyModel.py @@ -27,12 +27,10 @@ class TopologyModel(_Base): created_at = Column(DateTime, nullable=False) updated_at = Column(DateTime, nullable=False) - context = relationship('ContextModel', back_populates='topologies', lazy='selectin') - topology_devices = relationship('TopologyDeviceModel') # back_populates='topology' - topology_links = relationship('TopologyLinkModel' ) # back_populates='topology' - - #-------------------------- Experimental --------------------------- - topology_optical_links= relationship("TopologyOpticalLinkModel") + context = relationship('ContextModel', back_populates='topologies', lazy='selectin') + topology_devices = relationship('TopologyDeviceModel') # back_populates='topology' + topology_links = relationship('TopologyLinkModel' ) # back_populates='topology' + topology_optical_links = relationship("TopologyOpticalLinkModel") def dump_id(self) -> Dict: return { @@ -42,12 +40,11 @@ class TopologyModel(_Base): def dump(self) -> Dict: return { - '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': tl.link_uuid }} for tl in self.topology_links ], - 'optical_link_ids' : [{'link_uuid' : {'uuid': to.optical_link_uuid }} for to in self.topology_optical_links ], - + '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': tl.link_uuid }} for tl in self.topology_links ], + 'optical_link_ids': [{'link_uuid' : {'uuid': to.optical_link_uuid}} for to in self.topology_optical_links], } def dump_details(self) -> Dict: @@ -64,11 +61,11 @@ class TopologyModel(_Base): for ol in self.topology_optical_links ] return { - 'topology_id': self.dump_id(), - 'name' : self.topology_name, - 'devices' : devices, - 'links' : links, - 'optical_links':optical_links + 'topology_id' : self.dump_id(), + 'name' : self.topology_name, + 'devices' : devices, + 'links' : links, + 'optical_links': optical_links, } class TopologyDeviceModel(_Base): @@ -94,8 +91,8 @@ class TopologyLinkModel(_Base): class TopologyOpticalLinkModel(_Base): __tablename__ = 'topology_optical_link' - topology_uuid = Column(ForeignKey('topology.topology_uuid', ondelete='RESTRICT'), primary_key=True, index=True) - optical_link_uuid = Column(ForeignKey('opticallink.opticallink_uuid', ondelete='CASCADE' ), primary_key=True, index=True) + topology_uuid = Column(ForeignKey('topology.topology_uuid', ondelete='RESTRICT'), primary_key=True, index=True) + optical_link_uuid = Column(ForeignKey('opticallink.opticallink_uuid', ondelete='CASCADE' ), primary_key=True, index=True) - topology = relationship('TopologyModel', lazy='selectin', viewonly=True) # back_populates='topology_optical_links' - optical_link = relationship('OpticalLinkModel', lazy='selectin') # back_populates='topology_optical_links' + topology = relationship('TopologyModel', lazy='selectin', viewonly=True) # back_populates='topology_optical_links' + optical_link = relationship('OpticalLinkModel', lazy='selectin') # back_populates='topology_optical_links' diff --git a/src/context/service/database/uuids/EndPoint.py b/src/context/service/database/uuids/EndPoint.py index 9653324e5034e84cdbf834cb8a813c927a3a3c59..b8c97a1bbe584d927d8d5bab0ef9036794f3e9a8 100644 --- a/src/context/service/database/uuids/EndPoint.py +++ b/src/context/service/database/uuids/EndPoint.py @@ -18,26 +18,21 @@ from common.method_wrappers.ServiceExceptions import InvalidArgumentsException from ._Builder import get_uuid_from_string, get_uuid_random from .Device import device_get_uuid from .Topology import topology_get_uuid -import logging def endpoint_get_uuid( endpoint_id : EndPointId, endpoint_name : str = '', allow_random : bool = False ) -> Tuple[str, str, str]: - device_uuid = device_get_uuid(endpoint_id.device_id, allow_random=False) _,topology_uuid = topology_get_uuid(endpoint_id.topology_id, allow_random=False, allow_default=True) raw_endpoint_uuid = endpoint_id.endpoint_uuid.uuid if len(raw_endpoint_uuid) > 0: prefix_for_name = '{:s}/{:s}'.format(topology_uuid, device_uuid) - return topology_uuid, device_uuid, get_uuid_from_string(raw_endpoint_uuid, prefix_for_name=prefix_for_name) if len(endpoint_name) > 0: - prefix_for_name = '{:s}/{:s}'.format(topology_uuid, device_uuid) return topology_uuid, device_uuid, get_uuid_from_string(endpoint_name, prefix_for_name=prefix_for_name) if allow_random: - return topology_uuid, device_uuid, get_uuid_random() raise InvalidArgumentsException([ diff --git a/src/context/service/database/uuids/OpticalConfig.py b/src/context/service/database/uuids/OpticalConfig.py index 7bf75a9a259639cd354b9056eba93806a4844051..f2cd443a983d5b37009ddc1c2dc821dce960ddb8 100644 --- a/src/context/service/database/uuids/OpticalConfig.py +++ b/src/context/service/database/uuids/OpticalConfig.py @@ -1,28 +1,35 @@ +# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. from common.method_wrappers.ServiceExceptions import InvalidArgumentsException -from ._Builder import get_uuid_from_string, get_uuid_random from common.proto.context_pb2 import DeviceId +from ._Builder import get_uuid_from_string, get_uuid_random def channel_get_uuid( - channel_name :str , device_id:str, allow_random : bool = False + channel_name : str , device_id : str, allow_random : bool = False ) -> str: - - if len(channel_name) > 0: return get_uuid_from_string(channel_name) + device_id if allow_random: return get_uuid_random() raise InvalidArgumentsException([ ('channel uuid', channel_name), - ], extra_details=['Channel name is required to produce a channel UUID']) - def transponder_get_uuid( - opticalconfig_id :str , allow_random : bool = False + opticalconfig_id : str, allow_random : bool = False ) -> str: - - if opticalconfig_id is not None: return get_uuid_from_string(f"{opticalconfig_id}-transponder") if allow_random: return get_uuid_random() @@ -31,30 +38,27 @@ def transponder_get_uuid( ('transponder uuid', opticalconfig_id), ], extra_details=['opticalconfig id is required to produce a transponder UUID']) - def roadm_get_uuid( - opticalconfig_id :str , allow_random : bool = False + opticalconfig_id : str, allow_random : bool = False ) -> str: - - if opticalconfig_id is not None: return get_uuid_from_string(f"{opticalconfig_id}-roadm") - + if allow_random: return get_uuid_random() raise InvalidArgumentsException([ ('roadm uuid', opticalconfig_id), ], extra_details=['opticalconfig id is required to produce a roadm UUID']) - - -def opticalconfig_get_uuid ( device_id: DeviceId, allow_random : bool = False) -> str : +def opticalconfig_get_uuid( + device_id : DeviceId, allow_random : bool = False +) -> str: device_uuid = device_id.device_uuid.uuid if (len(device_uuid)>0): return get_uuid_from_string(f"{device_uuid}_opticalconfig") if allow_random: return get_uuid_random() + raise InvalidArgumentsException([ ('DeviceId ', device_id), - - ], extra_details=['device_id is required to produce a OpticalConfig UUID']) \ No newline at end of file + ], extra_details=['device_id is required to produce a OpticalConfig UUID']) diff --git a/src/context/service/database/uuids/OpticalEndPoint.py b/src/context/service/database/uuids/OpticalEndPoint.py index 395ca55c98332d3dbff3afd6c338e93175452ce2..fedafd6f470af174a6c4701e330f93fea00a5a3f 100644 --- a/src/context/service/database/uuids/OpticalEndPoint.py +++ b/src/context/service/database/uuids/OpticalEndPoint.py @@ -17,26 +17,19 @@ from common.proto.context_pb2 import EndPointId from common.method_wrappers.ServiceExceptions import InvalidArgumentsException from ._Builder import get_uuid_from_string, get_uuid_random from .Device import device_get_uuid -from .Topology import topology_get_uuid -import logging def optical_endpoint_get_uuid( endpoint_id : EndPointId, endpoint_name : str = '', allow_random : bool = False ) -> Tuple[str, str, str]: device_uuid = device_get_uuid(endpoint_id.device_id, allow_random=False) - raw_endpoint_uuid = endpoint_id.endpoint_uuid.uuid - if len(raw_endpoint_uuid) > 0: prefix_for_name = '{:s}'.format( device_uuid) - return device_uuid, get_uuid_from_string(raw_endpoint_uuid, prefix_for_name=prefix_for_name) if len(endpoint_name) > 0: - prefix_for_name = '{:s}'.format( device_uuid) return device_uuid, get_uuid_from_string(endpoint_name, prefix_for_name=prefix_for_name) if allow_random: - return device_uuid, get_uuid_random() raise InvalidArgumentsException([ diff --git a/src/context/service/database/uuids/OpticalLink.py b/src/context/service/database/uuids/OpticalLink.py index 0caead511d470174796b0fcf6686dd2a83e0b0b9..152686423aa808194f0582075557e6133e405071 100644 --- a/src/context/service/database/uuids/OpticalLink.py +++ b/src/context/service/database/uuids/OpticalLink.py @@ -1,21 +1,32 @@ +# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -from common.proto.context_pb2 import LinkId from common.method_wrappers.ServiceExceptions import InvalidArgumentsException +from common.proto.context_pb2 import LinkId from ._Builder import get_uuid_from_string, get_uuid_random -optical_detail_sp="Optical_link_detail" +optical_detail_sp = "Optical_link_detail" + def opticaldetail_get_uuid( link_id : LinkId,allow_random=False ) -> str: link_uuid = link_id.link_uuid.uuid - if len(link_uuid) > 0: str_uuid=f"{link_uuid}{optical_detail_sp}" return get_uuid_from_string(str_uuid) - - if allow_random: return get_uuid_random() + if allow_random: return get_uuid_random() raise InvalidArgumentsException([ ('link_id.link_uuid.uuid', link_uuid), - ], extra_details=['At least one is required to produce a Optical Link Detail UUID']) diff --git a/src/context/service/database/uuids/_Builder.py b/src/context/service/database/uuids/_Builder.py index df5791b382b36d80ab67ee22ecc840acda4a9d69..39c98de69d577ce2722693e57c4ee678124f9e30 100644 --- a/src/context/service/database/uuids/_Builder.py +++ b/src/context/service/database/uuids/_Builder.py @@ -14,7 +14,7 @@ from typing import Optional, Union from uuid import UUID, uuid4, uuid5 -import logging + # Generate a UUIDv5-like from the SHA-1 of "TFS" and no namespace to be used as the NAMESPACE for all # the context UUIDs generated. For efficiency purposes, the UUID is hardcoded; however, it is produced # using the following code: @@ -32,12 +32,10 @@ def get_uuid_from_string(str_uuid_or_name : Union[str, UUID], prefix_for_name : raise Exception(MSG.format(str(repr(str_uuid_or_name)))) try: # try to parse as UUID - return str(UUID(str_uuid_or_name)) except: # pylint: disable=bare-except # produce a UUID within TFS namespace from parameter if prefix_for_name is not None: - str_uuid_or_name = '{:s}/{:s}'.format(prefix_for_name, str_uuid_or_name) return str(uuid5(NAMESPACE_TFS, str_uuid_or_name))