# 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/openflow_flowtable_counter1_0.py
# class OpenflowFlowTableCounter1_0(BaseModel):
# 
#     # Per Table
#     active_entries = models.IntegerField(null=True)
#     packet_lookups = models.BigIntegerField(null=True)
#     packet_matches = models.BigIntegerField(null=True)
# 
#     # Foreign key to a openflow table 1_0
#     openflow_table1_0 = models.ForeignKey('OpenflowFlowTable1_0')
# 
#     def sync(self, table_description):
#         self.active_entries = table_description['activeCount']
#         self.packet_lookups = table_description['lookupCount']
#         self.packet_matches = table_description['matchedCount']
#         try:
#             oftc = OpenflowFlowTableCounter1_0.objects.get(openflow_table1_0=self.openflow_table1_0)
#             oftc.active_entries = self.active_entries
#             oftc.packet_lookups = self.packet_lookups
#             oftc.packet_matches = self.packet_matches
#             oftc.save()
#         except OpenflowFlowTableCounter1_0.DoesNotExist:
#             self.save()
# 
# Path: sdn/models/openflow_header1_0.py
# class OpenflowHeader1_0(BaseModel):
#     # Fields for openflow switch specification version 1.0
#     ingress_port = models.IntegerField()
#     ethernet_source_address = models.CharField(max_length=100, db_index=True)
#     ethernet_destination_address = models.CharField(max_length=100, db_index=True)
#     ethernet_type = models.CharField(max_length=100, db_index=True)
#     vlan_id = models.IntegerField()
#     vlan_priority = models.IntegerField()
#     ip_source_address = models.CharField(max_length=100, db_index=True)
#     ip_destination_address = models.CharField(max_length=100, db_index=True)
#     ip_protocol = models.IntegerField()
#     ip_tos_bits = models.IntegerField()
# 
#     # If ICMP transport_source_port = ICMP Type
#     transport_source_port = models.IntegerField()
# 
#     # If ICMP transport_destination_port = ICMP Code
#     transport_destination_port = models.IntegerField()
# 
#     # Flow Status = active or idle (true or false)
#     flow_status = models.BooleanField(default=True)
# 
#     # Sync time to controll new and old flows
#     # TODO change timestamp field type
#     last_synchronization_time = models.CharField(max_length=100)
# 
#     # Foreign key to openflow table
#     openflow_table1_0 = models.ForeignKey('OpenflowFlowTable1_0')
# 
#     def sync(self, header_description, sync_time):
#         self.ingress_port = header_description['match']['inputPort']
#         self.ethernet_source_address = header_description['match']['dataLayerSource']
#         self.ethernet_destination_address = header_description['match']['dataLayerDestination']
#         self.ethernet_type = header_description['match']['dataLayerType']
#         self.vlan_id = header_description['match']['dataLayerVirtualLan']
#         self.vlan_priority = header_description['match']['dataLayerVirtualLanPriorityCodePoint']
#         self.ip_source_address = header_description['match']['networkSource']
#         self.ip_destination_address = header_description['match']['networkDestination']
#         self.ip_protocol = header_description['match']['networkProtocol']
#         self.ip_tos_bits = header_description['match']['networkTypeOfService']
#         self.transport_source_port = header_description['match']['transportSource']
#         self.transport_destination_port = header_description['match']['transportDestination']
#         self.last_synchronization_time = sync_time
# 
#         offc = OpenflowHeaderCounter1_0()
#         try:
#             ofh = OpenflowHeader1_0.objects.get(ingress_port=self.ingress_port,
#                 ethernet_source_address=self.ethernet_source_address,
#                 ethernet_destination_address=self.ethernet_destination_address,
#                 ethernet_type=self.ethernet_type,
#                 vlan_id=self.vlan_id,
#                 vlan_priority=self.vlan_priority,
#                 ip_source_address=self.ip_source_address,
#                 ip_destination_address=self.ip_destination_address,
#                 ip_protocol=self.ip_protocol,
#                 ip_tos_bits=self.ip_tos_bits,
#                 transport_source_port=self.transport_source_port,
#                 transport_destination_port=self.transport_destination_port)
#             ofh.last_synchronization_time = self.last_synchronization_time = sync_time
#             ofh.save()
#             offc.openflow_header1_0 = ofh
#         except OpenflowHeader1_0.DoesNotExist:
#             self.save()
#             offc.openflow_header1_0 = self
# 
#         # Adding counters for headers
#         offc.sync(header_description, sync_time)


from django.db import models
from sdn.models.base_model import BaseModel
from sdn.models.openflow_flowtable_counter1_0 import OpenflowFlowTableCounter1_0
from sdn.models.openflow_header1_0 import OpenflowHeader1_0
# -*- coding: utf-8 -*-


# Create your models here.
class OpenflowFlowTable1_0(BaseModel):
    # Foreign key to a openflow switch
    table_id = models.IntegerField()
    openflow_switch1_0 = models.ForeignKey('OpenflowSwitch1_0')

    def sync(self, table_description, header_description, sync_time):
        # The openflow switch 1.0 has one openflow table
        self.table_id = table_description['tableId']
        oftc = OpenflowFlowTableCounter1_0()
        oftable = OpenflowFlowTable1_0()
        try:

# 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/openflow_flowtable_counter1_0.py
# class OpenflowFlowTableCounter1_0(BaseModel):
# 
#     # Per Table
#     active_entries = models.IntegerField(null=True)
#     packet_lookups = models.BigIntegerField(null=True)
#     packet_matches = models.BigIntegerField(null=True)
# 
#     # Foreign key to a openflow table 1_0
#     openflow_table1_0 = models.ForeignKey('OpenflowFlowTable1_0')
# 
#     def sync(self, table_description):
#         self.active_entries = table_description['activeCount']
#         self.packet_lookups = table_description['lookupCount']
#         self.packet_matches = table_description['matchedCount']
#         try:
#             oftc = OpenflowFlowTableCounter1_0.objects.get(openflow_table1_0=self.openflow_table1_0)
#             oftc.active_entries = self.active_entries
#             oftc.packet_lookups = self.packet_lookups
#             oftc.packet_matches = self.packet_matches
#             oftc.save()
#         except OpenflowFlowTableCounter1_0.DoesNotExist:
#             self.save()
# 
# Path: sdn/models/openflow_header1_0.py
# class OpenflowHeader1_0(BaseModel):
#     # Fields for openflow switch specification version 1.0
#     ingress_port = models.IntegerField()
#     ethernet_source_address = models.CharField(max_length=100, db_index=True)
#     ethernet_destination_address = models.CharField(max_length=100, db_index=True)
#     ethernet_type = models.CharField(max_length=100, db_index=True)
#     vlan_id = models.IntegerField()
#     vlan_priority = models.IntegerField()
#     ip_source_address = models.CharField(max_length=100, db_index=True)
#     ip_destination_address = models.CharField(max_length=100, db_index=True)
#     ip_protocol = models.IntegerField()
#     ip_tos_bits = models.IntegerField()
# 
#     # If ICMP transport_source_port = ICMP Type
#     transport_source_port = models.IntegerField()
# 
#     # If ICMP transport_destination_port = ICMP Code
#     transport_destination_port = models.IntegerField()
# 
#     # Flow Status = active or idle (true or false)
#     flow_status = models.BooleanField(default=True)
# 
#     # Sync time to controll new and old flows
#     # TODO change timestamp field type
#     last_synchronization_time = models.CharField(max_length=100)
# 
#     # Foreign key to openflow table
#     openflow_table1_0 = models.ForeignKey('OpenflowFlowTable1_0')
# 
#     def sync(self, header_description, sync_time):
#         self.ingress_port = header_description['match']['inputPort']
#         self.ethernet_source_address = header_description['match']['dataLayerSource']
#         self.ethernet_destination_address = header_description['match']['dataLayerDestination']
#         self.ethernet_type = header_description['match']['dataLayerType']
#         self.vlan_id = header_description['match']['dataLayerVirtualLan']
#         self.vlan_priority = header_description['match']['dataLayerVirtualLanPriorityCodePoint']
#         self.ip_source_address = header_description['match']['networkSource']
#         self.ip_destination_address = header_description['match']['networkDestination']
#         self.ip_protocol = header_description['match']['networkProtocol']
#         self.ip_tos_bits = header_description['match']['networkTypeOfService']
#         self.transport_source_port = header_description['match']['transportSource']
#         self.transport_destination_port = header_description['match']['transportDestination']
#         self.last_synchronization_time = sync_time
# 
#         offc = OpenflowHeaderCounter1_0()
#         try:
#             ofh = OpenflowHeader1_0.objects.get(ingress_port=self.ingress_port,
#                 ethernet_source_address=self.ethernet_source_address,
#                 ethernet_destination_address=self.ethernet_destination_address,
#                 ethernet_type=self.ethernet_type,
#                 vlan_id=self.vlan_id,
#                 vlan_priority=self.vlan_priority,
#                 ip_source_address=self.ip_source_address,
#                 ip_destination_address=self.ip_destination_address,
#                 ip_protocol=self.ip_protocol,
#                 ip_tos_bits=self.ip_tos_bits,
#                 transport_source_port=self.transport_source_port,
#                 transport_destination_port=self.transport_destination_port)
#             ofh.last_synchronization_time = self.last_synchronization_time = sync_time
#             ofh.save()
#             offc.openflow_header1_0 = ofh
#         except OpenflowHeader1_0.DoesNotExist:
#             self.save()
#             offc.openflow_header1_0 = self
# 
#         # Adding counters for headers
#         offc.sync(header_description, sync_time)


# Path: sdn/models/openflow_flowtable1_0.py
from django.db import models
from sdn.models.base_model import BaseModel
from sdn.models.openflow_flowtable_counter1_0 import OpenflowFlowTableCounter1_0
from sdn.models.openflow_header1_0 import OpenflowHeader1_0

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


# Create your models here.
class OpenflowFlowTable1_0(BaseModel):
    # Foreign key to a openflow switch
    table_id = models.IntegerField()
    openflow_switch1_0 = models.ForeignKey('OpenflowSwitch1_0')

    def sync(self, table_description, header_description, sync_time):
        # The openflow switch 1.0 has one openflow table
        self.table_id = table_description['tableId']
        oftc = OpenflowFlowTableCounter1_0()
        oftable = OpenflowFlowTable1_0()
        try:

            oft = OpenflowFlowTable1_0.objects.get(table_id=self.table_id, openflow_switch1_0=self.openflow_switch1_0)