commit a24fbf371d668afb813b2b788ca93a8f9dd4f18e
Author: Leonidas Poulopoulos <leopoul@noc.grnet.gr>
Date:   Wed Nov 16 17:06:48 2011 +0200

    Added an admin interface. Incorporated nxpy with gbp flowspec feature

diff --git a/flowspec/models.py b/flowspec/models.py
index 60bad93..1282553 100644
--- a/flowspec/models.py
+++ b/flowspec/models.py
@@ -1,6 +1,11 @@
 from django.db import models
 from django.contrib.auth.models import User
 
+import nxpy as np
+from ncclient import manager
+from ncclient.transport.errors import AuthenticationError, SSHError
+from lxml import etree as ET
+
 FRAGMENT_CODES = (
     ("dont-fragment", "Don't fragment"),
     ("first-fragment", "First fragment"),
@@ -22,27 +27,37 @@ THEN_CHOICES = (
 
     
 class MatchAddress(models.Model):
-    destination = models.CharField(max_length=255)
+    address = models.CharField(max_length=255)
+    def __unicode__(self):
+        return self.address
     class Meta:
         db_table = u'match_address'
 
 class MatchPort(models.Model):
     port = models.CharField(max_length=24)
+    def __unicode__(self):
+        return self.port
     class Meta:
         db_table = u'match_port'    
 
 class MatchDscp(models.Model):
     dscp = models.CharField(max_length=24)
+    def __unicode__(self):
+        return self.dscp
     class Meta:
         db_table = u'match_dscp'
 
 class MatchFragmentType(models.Model):
     fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES)
+    def __unicode__(self):
+        return self.fragmenttype
     class Meta:
         db_table = u'match_fragment_type'
     
 class MatchIcmpCode(models.Model):
     icmp_code = models.CharField(max_length=64)
+    def __unicode__(self):
+        return self.icmp_code
     class Meta:
         db_table = u'match_icmp_code'    
 
@@ -58,17 +73,23 @@ class MatchPacketLength(models.Model):
 
 class MatchProtocol(models.Model):
     protocol = models.CharField(max_length=64)
+    def __unicode__(self):
+        return self.protocol
     class Meta:
         db_table = u'match_protocol'    
 
 class MatchTcpFlag(models.Model):
     tcp_flag = models.CharField(max_length=255)
+    def __unicode__(self):
+        return self.tcp_flag
     class Meta:
         db_table = u'match_tcp_flag'    
     
 class ThenAction(models.Model):
     action = models.CharField(max_length=60, choices=THEN_CHOICES)
     action_value = models.CharField(max_length=255, blank=True, null=True)
+    def __unicode__(self):
+        return "%s %s" %(self.action, self.action_value)
     class Meta:
         db_table = u'then_action'
 
@@ -85,10 +106,10 @@ class MatchStatement(models.Model):
     matchicmpcode = models.ForeignKey(MatchIcmpCode, blank=True, null=True)
     matchicmptype = models.ForeignKey(MatchIcmpType, blank=True, null=True)
     matchpacketlength = models.ForeignKey(MatchPacketLength, blank=True, null=True)
-    matchport = models.ManyToManyField(MatchPort, blank=True, null=True)
+    matchport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchPort")
     matchprotocol = models.ForeignKey(MatchProtocol, blank=True, null=True)
-    matchSource = models.ManyToManyField(MatchAddress, blank=True, null=True, related_name="matchSource")
-    matchSourcePort = models.ForeignKey(MatchPort, blank=True, null=True, related_name="matchSourcePort")
+    matchSource = models.ForeignKey(MatchAddress, blank=True, null=True, related_name="matchSource")
+    matchSourcePort = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort")
     matchTcpFlag = models.ForeignKey(MatchTcpFlag, blank=True, null=True)
     class Meta:
         db_table = u'match'
@@ -101,5 +122,52 @@ class Route(models.Model):
     filed = models.DateTimeField(auto_now_add=True)
     last_updated = models.DateTimeField(auto_now=True)
     expires = models.DateTimeField()
+    def __unicode__(self):
+        return self.name
+    
     class Meta:
-        db_table = u'route'
\ No newline at end of file
+        db_table = u'route'
+
+    def save(self, *args, **kwargs):
+        # Begin translation to device xml configuration
+        device = np.Device()
+        flow = np.Flow()
+        route = np.Route()
+        flow.routes.append(route)
+        device.routing_options.append(flow)
+        route.name = self.name
+        match = self.match
+        if match.matchSource:
+            route.match['source'].append(match.matchSource.address)
+        if match.matchDestination:
+            route.match['destination'].append(match.matchDestination.address)
+        if match.matchprotocol:
+            route.match['protocol'].append(match.matchprotocol.protocol)
+        if match.matchport:
+            for port in match.matchport.all():
+                route.match['port'].append(port.port)
+        if match.matchDestinationPort:
+            for port in match.matchDestinationPort.all():
+                route.match['destination-port'].append(port.port)
+        if match.matchSourcePort:
+            for port in match.matchSourcePort.all():
+                route.match['source-port'].append(port.port)
+        if match.matchicmpcode:
+            route.match['icmp-code'].append(match.matchicmpcode.icmp_code)
+        if match.matchicmptype:
+            route.match['icmp-type'].append(match.matchicmptype.icmp_type)
+        if match.matchTcpFlag:
+            route.match['tcp-flags'].append(match.matchTcpFlag.tcp_flags)
+        if match.matchdscp:
+            for dscp in match.matchdscp.all():
+                route.match['dscp'].append(dscp.dscp)
+        if match.matchfragmenttype:
+            route.match['fragment'].append(match.matchfragmenttype.fragmenttype)
+        then = self.then
+        for thenaction in then.thenaction.all():
+            if thenaction.action_value:
+                route.then[thenaction.action] = thenaction.action_value
+            else:
+                route.then[thenaction.action] = True
+        print ET.tostring(device.export())
+        super(Route, self).save(*args, **kwargs)
\ No newline at end of file
