# 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_header_counter1_0.py
# class OpenflowHeaderCounter1_0(BaseModel):
# 
#     # Per Flow
#     received_packets = models.BigIntegerField(null=True)
#     received_bytes = models.BigIntegerField(null=True)
#     duration_seconds = models.BigIntegerField(null=True)
#     duration_nanoseconds = models.BigIntegerField(null=True)
# 
#     # Foreign key to a openflow header 1_0
#     openflow_header1_0 = models.ForeignKey('OpenflowHeader1_0')
# 
#     def sync(self, header_description, sync_time):
#         self.received_packets = header_description['packetCount']
#         self.received_bytes = header_description['byteCount']
#         self.duration_seconds = header_description['durationSeconds']
#         self.duration_nanoseconds = header_description['durationNanoseconds']
#         try:
#             offc = OpenflowHeaderCounter1_0.objects.get(openflow_header1_0=self.openflow_header1_0)
#             if int(offc.received_packets) == int(self.received_packets):
#                 offc.openflow_header1_0.flow_status = False
#                 offc.openflow_header1_0.save()
#             else:
#                 offc.openflow_header1_0.flow_status = True
#                 offc.openflow_header1_0.last_synchronization_time = sync_time
#                 offc.openflow_header1_0.save()
#             offc.received_packets = self.received_packets
#             offc.received_bytes = self.received_bytes
#             offc.duration_seconds = self.duration_seconds
#             offc.duration_nanoseconds = self.duration_nanoseconds
#             offc.save()
#         except OpenflowHeaderCounter1_0.DoesNotExist:
#             self.save()


from django.db import models
from sdn.models.base_model import BaseModel
from sdn.models.openflow_header_counter1_0 import OpenflowHeaderCounter1_0
    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']

# 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_header_counter1_0.py
# class OpenflowHeaderCounter1_0(BaseModel):
# 
#     # Per Flow
#     received_packets = models.BigIntegerField(null=True)
#     received_bytes = models.BigIntegerField(null=True)
#     duration_seconds = models.BigIntegerField(null=True)
#     duration_nanoseconds = models.BigIntegerField(null=True)
# 
#     # Foreign key to a openflow header 1_0
#     openflow_header1_0 = models.ForeignKey('OpenflowHeader1_0')
# 
#     def sync(self, header_description, sync_time):
#         self.received_packets = header_description['packetCount']
#         self.received_bytes = header_description['byteCount']
#         self.duration_seconds = header_description['durationSeconds']
#         self.duration_nanoseconds = header_description['durationNanoseconds']
#         try:
#             offc = OpenflowHeaderCounter1_0.objects.get(openflow_header1_0=self.openflow_header1_0)
#             if int(offc.received_packets) == int(self.received_packets):
#                 offc.openflow_header1_0.flow_status = False
#                 offc.openflow_header1_0.save()
#             else:
#                 offc.openflow_header1_0.flow_status = True
#                 offc.openflow_header1_0.last_synchronization_time = sync_time
#                 offc.openflow_header1_0.save()
#             offc.received_packets = self.received_packets
#             offc.received_bytes = self.received_bytes
#             offc.duration_seconds = self.duration_seconds
#             offc.duration_nanoseconds = self.duration_nanoseconds
#             offc.save()
#         except OpenflowHeaderCounter1_0.DoesNotExist:
#             self.save()


# Path: sdn/models/openflow_header1_0.py
from django.db import models
from sdn.models.base_model import BaseModel
from sdn.models.openflow_header_counter1_0 import OpenflowHeaderCounter1_0

    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']