# Path: sdn/models/base_model.py
# class BaseModel(models.Model):
#     class Meta:
#         # Makes django recognize model in split modules
#         app_label = 'sdn'
#         # Turns this into an abstract model (does not create table for it)
#         abstract = True
# 
#     # Default exception for models in manager
#     class ModelException(Exception):
#         # Get an instance of a logger
#         logger = logging.getLogger(__name__)
# 
#         def __init__(self, msg):
#             self.msg = msg
#             self.logger.warning(msg)
# 
#         def __str__(self):
#             return repr(self.msg)
# 
# Path: sdn/models/port.py
# class Port(BaseModel):
#     port_number = models.IntegerField()
#     hardware_address = models.CharField(max_length=100)
#     name = models.CharField(max_length=100)
#     config = models.IntegerField()
#     state = models.IntegerField()
#     current_features = models.IntegerField()
#     advertised_features = models.IntegerField()
#     supported_features = models.IntegerField()
#     peer_features = models.IntegerField()
# 
#     # Foreign key to switch
#     switch = models.ForeignKey(Switch)
# 
#     def sync(self, port_description, port_counters):
#         self.port_number = port_description['portNumber']
#         self.hardware_address = port_description['hardwareAddress']
#         self.name = port_description['name']
#         self.config = port_description['config']
#         self.state = port_description['state']
#         self.current_features = port_description['currentFeatures']
#         self.advertised_features = port_description['advertisedFeatures']
#         self.supported_features = port_description['supportedFeatures']
#         self.peer_features = port_description['peerFeatures']
# 
#         # Create a port counter
#         ofpc = OpenflowPortCounter1_0()
#         try:
#             # Verify if this port already exists
#             p = Port.objects.get(port_number=self.port_number, hardware_address=self.hardware_address, name=self.name)
#             p.config = self.config
#             p.state - self.state
#             p.currentFeatures = self.current_features
#             p.advertised_features = self.advertised_features
#             p.supported_features = self.supported_features
#             p.peer_features = self.peer_features
#             p.save()
#             # Assign port to a port counter
#             ofpc.port = p
#         except Port.DoesNotExist:
#             self.save()
#             # Assign port to a port counter
#             ofpc.port = self
#         # Call port counter sync method
#         ofpc.sync(port_counters)
# 
# Path: sdn/models/openflow_switch1_0.py
# class OpenflowSwitch1_0(Switch):
#     # Switch type
#     switch_type = models.CharField(max_length=3, choices=SWITCH_CHOICE)
# 
#     def sync(self, switch_description, table_description, header_description, port_description, sync_time, debug_time):
#         # TODO: Identify which are switch types)
#         self.switch_type = 'ofe'
#         self.buffers = switch_description['buffers']
#         self.software = switch_description['description']['software']
#         self.hardware = switch_description['description']['hardware']
#         self.manufacturer = switch_description['description']['manufacturer']
#         self.serialNum = switch_description['description']['serialNum']
#         self.datapath = switch_description['description']['datapath']
#         self.capabilities = switch_description['capabilities']
#         self.inet_address = switch_description['inetAddress']
#         self.connected_since = switch_description['connectedSince']
#         self.dpid = switch_description['dpid']
# 
#         # Create an openflow table
#         oft = OpenflowFlowTable1_0()
#         try:
#             # Verify if this openflowswitch already exists
#             ofs = OpenflowSwitch1_0.objects.get(controller=self.controller, dpid=self.dpid)
#             ofs.buffers = self.buffers
#             ofs.software = self.software
#             ofs.hardware = self.hardware
#             ofs.manufacturer = self.manufacturer
#             ofs.serialNum = self.serialNum
#             ofs.datapath = self.datapath
#             ofs.capabilities = self.capabilities
#             ofs.inet_address = self.inet_address
#             ofs.connected_since = self.connected_since
#             ofs.save()
#             # Assign openflow table to openflow switch
#             oft.openflow_switch1_0 = ofs
#         except OpenflowSwitch1_0.DoesNotExist:
#             self.save()
#             # Assign openflow table to openflow switch
#             oft.openflow_switch1_0 = self
# 
#         for p in switch_description['ports']:
#             port = Port()
#             port.switch = self
#             pn = None
#             for pn in port_description:
#                 if pn['portNumber'] == p['portNumber']:
#                     break
#             port.sync(p, pn)
#         # Call openflow table sync method
#         oft.sync(table_description, header_description, sync_time)
# 
#         logger.debug("Each switch " + str(int(time.time()) - debug_time) + " DPID " + str(self.dpid))


from django.db import models
from sdn.models.base_model import BaseModel
from sdn.models.port import Port
from sdn.models.openflow_switch1_0 import OpenflowSwitch1_0
# -*- coding: utf-8 -*-


# Create your models here.
class Link(BaseModel):
    src_port = models.ForeignKey(
        Port,
        verbose_name="destination port",
        related_name='dst_port'
    )
    src_port = models.ForeignKey(
        Port,
        verbose_name="source port",
        related_name='src_port'
    )


# Path: sdn/models/base_model.py
# class BaseModel(models.Model):
#     class Meta:
#         # Makes django recognize model in split modules
#         app_label = 'sdn'
#         # Turns this into an abstract model (does not create table for it)
#         abstract = True
# 
#     # Default exception for models in manager
#     class ModelException(Exception):
#         # Get an instance of a logger
#         logger = logging.getLogger(__name__)
# 
#         def __init__(self, msg):
#             self.msg = msg
#             self.logger.warning(msg)
# 
#         def __str__(self):
#             return repr(self.msg)
# 
# Path: sdn/models/port.py
# class Port(BaseModel):
#     port_number = models.IntegerField()
#     hardware_address = models.CharField(max_length=100)
#     name = models.CharField(max_length=100)
#     config = models.IntegerField()
#     state = models.IntegerField()
#     current_features = models.IntegerField()
#     advertised_features = models.IntegerField()
#     supported_features = models.IntegerField()
#     peer_features = models.IntegerField()
# 
#     # Foreign key to switch
#     switch = models.ForeignKey(Switch)
# 
#     def sync(self, port_description, port_counters):
#         self.port_number = port_description['portNumber']
#         self.hardware_address = port_description['hardwareAddress']
#         self.name = port_description['name']
#         self.config = port_description['config']
#         self.state = port_description['state']
#         self.current_features = port_description['currentFeatures']
#         self.advertised_features = port_description['advertisedFeatures']
#         self.supported_features = port_description['supportedFeatures']
#         self.peer_features = port_description['peerFeatures']
# 
#         # Create a port counter
#         ofpc = OpenflowPortCounter1_0()
#         try:
#             # Verify if this port already exists
#             p = Port.objects.get(port_number=self.port_number, hardware_address=self.hardware_address, name=self.name)
#             p.config = self.config
#             p.state - self.state
#             p.currentFeatures = self.current_features
#             p.advertised_features = self.advertised_features
#             p.supported_features = self.supported_features
#             p.peer_features = self.peer_features
#             p.save()
#             # Assign port to a port counter
#             ofpc.port = p
#         except Port.DoesNotExist:
#             self.save()
#             # Assign port to a port counter
#             ofpc.port = self
#         # Call port counter sync method
#         ofpc.sync(port_counters)
# 
# Path: sdn/models/openflow_switch1_0.py
# class OpenflowSwitch1_0(Switch):
#     # Switch type
#     switch_type = models.CharField(max_length=3, choices=SWITCH_CHOICE)
# 
#     def sync(self, switch_description, table_description, header_description, port_description, sync_time, debug_time):
#         # TODO: Identify which are switch types)
#         self.switch_type = 'ofe'
#         self.buffers = switch_description['buffers']
#         self.software = switch_description['description']['software']
#         self.hardware = switch_description['description']['hardware']
#         self.manufacturer = switch_description['description']['manufacturer']
#         self.serialNum = switch_description['description']['serialNum']
#         self.datapath = switch_description['description']['datapath']
#         self.capabilities = switch_description['capabilities']
#         self.inet_address = switch_description['inetAddress']
#         self.connected_since = switch_description['connectedSince']
#         self.dpid = switch_description['dpid']
# 
#         # Create an openflow table
#         oft = OpenflowFlowTable1_0()
#         try:
#             # Verify if this openflowswitch already exists
#             ofs = OpenflowSwitch1_0.objects.get(controller=self.controller, dpid=self.dpid)
#             ofs.buffers = self.buffers
#             ofs.software = self.software
#             ofs.hardware = self.hardware
#             ofs.manufacturer = self.manufacturer
#             ofs.serialNum = self.serialNum
#             ofs.datapath = self.datapath
#             ofs.capabilities = self.capabilities
#             ofs.inet_address = self.inet_address
#             ofs.connected_since = self.connected_since
#             ofs.save()
#             # Assign openflow table to openflow switch
#             oft.openflow_switch1_0 = ofs
#         except OpenflowSwitch1_0.DoesNotExist:
#             self.save()
#             # Assign openflow table to openflow switch
#             oft.openflow_switch1_0 = self
# 
#         for p in switch_description['ports']:
#             port = Port()
#             port.switch = self
#             pn = None
#             for pn in port_description:
#                 if pn['portNumber'] == p['portNumber']:
#                     break
#             port.sync(p, pn)
#         # Call openflow table sync method
#         oft.sync(table_description, header_description, sync_time)
# 
#         logger.debug("Each switch " + str(int(time.time()) - debug_time) + " DPID " + str(self.dpid))


# Path: sdn/models/link.py
from django.db import models
from sdn.models.base_model import BaseModel
from sdn.models.port import Port
from sdn.models.openflow_switch1_0 import OpenflowSwitch1_0

# -*- coding: utf-8 -*-


# Create your models here.
class Link(BaseModel):
    src_port = models.ForeignKey(
        Port,
        verbose_name="destination port",
        related_name='dst_port'
    )
    src_port = models.ForeignKey(
        Port,
        verbose_name="source port",
        related_name='src_port'
    )


    dst_port = models.ForeignKey(Port)