# 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/controller.py
# class Controller(BaseModel):
#     # Ip address
#     ip_address = models.CharField(max_length=50)
# 
#     # Port
#     listen_port = models.CharField(max_length=10)
# 
#     # Name
#     name = models.CharField(max_length=50)
# 
#     # Description
#     description = models.CharField(max_length=100)
# 
#     # Controller type
#     controller_type = models.CharField(max_length=1, choices=CONTROLLER_CHOICE)
# 
#     def __unicode__(self):
#         return self.name + " - " + self.description
# 
# 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)


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


# Create your models here.
class Host(BaseModel):
    mac_address = models.CharField(max_length=100, db_index=True)
    ipv4_address = models.CharField(max_length=100, db_index=True)
    port = models.ForeignKey(Port, null=True)

    # Foreign key to controller
    controller = models.ForeignKey(Controller)

    def sync(self, host_description):
        # TODO for more interfaces
        if (len(host_description['mac']) > 0):
            self.mac_address = host_description['mac'][0]
        else:
            self.mac_address = 'none'
        if (len(host_description['ipv4']) > 0):
            self.ipv4_address = host_description['ipv4'][0]
        else:
            self.ipv4_address = 'none'
        if (len(host_description['attachmentPoint']) > 0):
            prt_num = host_description['attachmentPoint'][0]['port']
            if (prt_num > 0):
                dpid = host_description['attachmentPoint'][0]['switchDPID']

# 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/controller.py
# class Controller(BaseModel):
#     # Ip address
#     ip_address = models.CharField(max_length=50)
# 
#     # Port
#     listen_port = models.CharField(max_length=10)
# 
#     # Name
#     name = models.CharField(max_length=50)
# 
#     # Description
#     description = models.CharField(max_length=100)
# 
#     # Controller type
#     controller_type = models.CharField(max_length=1, choices=CONTROLLER_CHOICE)
# 
#     def __unicode__(self):
#         return self.name + " - " + self.description
# 
# 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/host.py
from django.db import models
from sdn.models.base_model import BaseModel
from sdn.models.controller import Controller
from sdn.models.port import Port

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


# Create your models here.
class Host(BaseModel):
    mac_address = models.CharField(max_length=100, db_index=True)
    ipv4_address = models.CharField(max_length=100, db_index=True)
    port = models.ForeignKey(Port, null=True)

    # Foreign key to controller
    controller = models.ForeignKey(Controller)

    def sync(self, host_description):
        # TODO for more interfaces
        if (len(host_description['mac']) > 0):
            self.mac_address = host_description['mac'][0]
        else:
            self.mac_address = 'none'
        if (len(host_description['ipv4']) > 0):
            self.ipv4_address = host_description['ipv4'][0]
        else:
            self.ipv4_address = 'none'
        if (len(host_description['attachmentPoint']) > 0):
            prt_num = host_description['attachmentPoint'][0]['port']
            if (prt_num > 0):
                dpid = host_description['attachmentPoint'][0]['switchDPID']

                self.port = Port.objects.get(port_number=prt_num, switch__dpid=dpid)