<commit_before>#!/ms/dist/python/PROJ/core/2.5.0/bin/python
# ex: set expandtab softtabstop=4 shiftwidth=4: -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
# $Header$
# $Change$
# $DateTime$
# $Author$
# Copyright (C) 2008 <NAME>
#
# This module is part of Aquilon
"""Classes and Tables relating to network interfaces"""


from datetime import datetime
import sys
import os

if __name__ == '__main__':
    DIR = os.path.dirname(os.path.realpath(__file__))
    sys.path.insert(0, os.path.realpath(os.path.join(DIR, '..', '..', '..')))
    import aquilon.aqdb.depends

from sqlalchemy import (Column, Table, Integer, Sequence, String, Index,
                        Boolean, CheckConstraint, UniqueConstraint, DateTime,
                        ForeignKey, PrimaryKeyConstraint, insert, select)
from sqlalchemy.orm import mapper, relation, deferred

from aquilon.aqdb.column_types.aqmac import AqMac
from aquilon.aqdb.column_types.aqstr import AqStr
from aquilon.aqdb.column_types.IPV4  import IPV4
from aquilon.aqdb.db_factory         import Base
from aquilon.aqdb.net.network        import Network


class Interface(Base):
    __tablename__ = 'interface'

    id             = Column(Integer,
                            Sequence('interface_id_seq'), primary_key=True)

    interface_type = Column(AqStr(32), nullable = False) #TODO: index
    # TODO: When refactoring, change this to AqMac(17)
    mac            = Column(AqMac(18), nullable = False)
    ip             = Column(IPV4, nullable = True)
    network_id     = Column(Integer, ForeignKey(Network.__table__.c.id,
                                                name = 'iface_net_id_fk'),
                            nullable = True)

    creation_date  = deferred(Column('creation_date',
                                    DateTime, default = datetime.now,
                                    nullable = False))

    comments       = deferred(Column(
                                'comments',String(255))) #TODO FK to IP table)
    network        = relation(Network, backref = 'interfaces' )

    __mapper_args__ = {'polymorphic_on' : interface_type}

interface = Interface.__table__
interface.primary_key.name = 'interface_pk'

interface.append_constraint(UniqueConstraint('mac', name = 'iface_mac_addr_uk'))
interface.append_constraint(UniqueConstraint('ip',  name = 'iface_ip_addr_uk'))
Index('iface_net_id_idx', interface.c.network_id)

def populate(*args, **kw):
    from aquilon.aqdb.db_factory import db_factory, Base
    from sqlalchemy import insert

    dbf = db_factory()
    Base.metadata.bind = dbf.engine
    if 'debug' in args:
        Base.metadata.bind.echo = True
    s = dbf.session()

    interface.create(checkfirst=True)

    if len(s.query(Interface).all()) < 1:
        #print 'no interfaces yet'
        pass

    if Base.metadata.bind.echo == True:
        Base.metadata.bind.echo == False
<commit_msg>a new look at interfaces which are to sit in the middle of a few new many to many association maps<commit_after>#!/ms/dist/python/PROJ/core/2.5.0/bin/python
"""Class and Table relating to network interfaces"""

from datetime import datetime
import sys
import os

if __name__ == '__main__':
    DIR = os.path.dirname(os.path.realpath(__file__))
    sys.path.insert(0, os.path.realpath(os.path.join(DIR, '..', '..', '..')))
    import aquilon.aqdb.depends

from sqlalchemy import (Column, Table, Integer, Sequence, Index, String,
                        Boolean, CheckConstraint, UniqueConstraint, DateTime,
                        ForeignKey)

from sqlalchemy.orm import relation, deferred

from aquilon.aqdb.db_factory         import Base
from aquilon.aqdb.column_types.aqstr import AqStr
from aquilon.aqdb.column_types.IPV4  import IPV4
from aquilon.aqdb.net.network        import Network
from aquilon.aqdb.hw.hardware_entity import HardwareEntity

class Interface(Base):
    __tablename__ = 'interface'

    id                = Column(Integer,
                            Sequence('interface_seq'), primary_key=True)

    name              = Column(AqStr(32), nullable = False)
    interface_type    = Column(AqStr(32), nullable = False) #TODO: index

    #interface_type_id = Column(Integer, ForeignKey(), nullable = False)

    hardware_entity_id     = Column(Integer, ForeignKey(HardwareEntity.c.id,
                                                    name = 'iface_hw_ent_fk',
                                                    ondelete = 'CASCADE'),
                                    nullable = False)

    bootable   = Column(Boolean, default = False)

    hardware_entity = relation(HardwareEntity, backref = 'interfaces',
                               passive_deletes = True)

    creation_date = deferred(Column(DateTime, default=datetime.now,
                                    nullable = False ))
    
    comments      = deferred(Column(String(255)))

    #network       = relation(Network, backref = 'interfaces' )
    #interface_type      = relation(InterfaceType)

    __mapper_args__ = {'polymorphic_on' : interface_type}

interface = Interface.__table__
interface.primary_key.name = 'interface_pk'

#Index('interface_net_id_idx', interface.c.network_id)

table = interface

#public are eth0, eth1. Must have a mac and must have a bootable flag
#standalone ipmi have a mac, bootable = False
#proxy impi share a public interface: have a duplicate mac, bootable = False
#ilo have a mac (proxied behind the chassis) OA
#ALL have an IP and a mac


# Copyright (C) 2008 <NAME>
# This module is part of Aquilon

# ex: set expandtab softtabstop=4 shiftwidth=4: -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-


{'fraction_non_alphanumeric': 0.10241820768136557, 'fraction_numerical': 0.008179231863442389, 'mean_word_length': 2.605769230769231, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7577896', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1621, 'n_words': 417}