Skip to content
Snippets Groups Projects
LinkModel.py 3.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • # Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    #
    # 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.
    
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    import operator
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from sqlalchemy import CheckConstraint, Column, DateTime, Float, ForeignKey, Integer, String
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from sqlalchemy.dialects.postgresql import UUID
    from sqlalchemy.orm import relationship
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from typing import Dict
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from ._Base import _Base
    
    class LinkModel(_Base):
        __tablename__ = 'link'
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        link_uuid           = Column(UUID(as_uuid=False), primary_key=True)
        link_name           = Column(String, nullable=False)
        total_capacity_gbps = Column(Float, nullable=True)
        used_capacity_gbps  = Column(Float, nullable=True)
        created_at          = Column(DateTime, nullable=False)
        updated_at          = Column(DateTime, nullable=False)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        #topology_links = relationship('TopologyLinkModel', back_populates='link')
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        link_endpoints = relationship('LinkEndPointModel') # lazy='joined', back_populates='link'
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        __table_args__ = (
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            CheckConstraint(total_capacity_gbps >= 0, name='check_value_total_capacity_gbps'),
            CheckConstraint(used_capacity_gbps  >= 0, name='check_value_used_capacity_gbps' ),
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        def dump_id(self) -> Dict:
            return {'link_uuid': {'uuid': self.link_uuid}}
    
        def dump(self) -> Dict:
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            result = {
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
                'link_id'          : self.dump_id(),
                'name'             : self.link_name,
                'link_endpoint_ids': [
                    link_endpoint.endpoint.dump_id()
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
                    for link_endpoint in sorted(self.link_endpoints, key=operator.attrgetter('position'))
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            if self.total_capacity_gbps is not None:
                attributes : Dict = result.setdefault('attributes', dict())
                attributes.setdefault('total_capacity_gbps', self.total_capacity_gbps)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            if self.used_capacity_gbps is not None:
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
                attributes : Dict = result.setdefault('attributes', dict())
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
                attributes.setdefault('used_capacity_gbps', self.used_capacity_gbps)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
            return result
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    
    class LinkEndPointModel(_Base):
        __tablename__ = 'link_endpoint'
    
        link_uuid     = Column(ForeignKey('link.link_uuid',         ondelete='CASCADE' ), primary_key=True)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        endpoint_uuid = Column(ForeignKey('endpoint.endpoint_uuid', ondelete='RESTRICT'), primary_key=True, index=True)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        position      = Column(Integer, nullable=False)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        link     = relationship('LinkModel',     back_populates='link_endpoints') #, lazy='selectin'
        endpoint = relationship('EndPointModel', lazy='selectin') # back_populates='link_endpoints'
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    
        __table_args__ = (
            CheckConstraint(position >= 0, name='check_position_value'),
        )