Skip to content
Snippets Groups Projects
LinkModel.py 2.31 KiB
Newer Older
  • Learn to ignore specific revisions
  • Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    # Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (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
    from sqlalchemy import CheckConstraint, Column, DateTime, 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)
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        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
    
        def dump_id(self) -> Dict:
            return {'link_uuid': {'uuid': self.link_uuid}}
    
        def dump(self) -> Dict:
            return {
                '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
    
    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='joined')
        endpoint = relationship('EndPointModel', lazy='joined') # back_populates='link_endpoints'
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    
        __table_args__ = (
            CheckConstraint(position >= 0, name='check_position_value'),
        )