Skip to content
Snippets Groups Projects
Commit a24fbf37 authored by Leonidas Poulopoulos's avatar Leonidas Poulopoulos
Browse files

Added an admin interface. Incorporated nxpy with gbp flowspec feature

parent a3af8464
Branches
Tags
No related merge requests found
from django.contrib import admin
from flowspy.flowspec.models import *
admin.site.register(MatchAddress)
admin.site.register(MatchPort)
admin.site.register(MatchDscp)
admin.site.register(MatchFragmentType)
admin.site.register(MatchIcmpCode)
admin.site.register(MatchIcmpType)
admin.site.register(MatchPacketLength)
admin.site.register(MatchProtocol)
admin.site.register(MatchTcpFlag)
admin.site.register(ThenAction)
admin.site.register(ThenStatement)
admin.site.register(MatchStatement)
admin.site.register(Route)
This diff is collapsed.
from django.db import models from django.db import models
from django.contrib.auth.models import User 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 = ( FRAGMENT_CODES = (
("dont-fragment", "Don't fragment"), ("dont-fragment", "Don't fragment"),
("first-fragment", "First fragment"), ("first-fragment", "First fragment"),
...@@ -22,27 +27,37 @@ THEN_CHOICES = ( ...@@ -22,27 +27,37 @@ THEN_CHOICES = (
class MatchAddress(models.Model): class MatchAddress(models.Model):
destination = models.CharField(max_length=255) address = models.CharField(max_length=255)
def __unicode__(self):
return self.address
class Meta: class Meta:
db_table = u'match_address' db_table = u'match_address'
class MatchPort(models.Model): class MatchPort(models.Model):
port = models.CharField(max_length=24) port = models.CharField(max_length=24)
def __unicode__(self):
return self.port
class Meta: class Meta:
db_table = u'match_port' db_table = u'match_port'
class MatchDscp(models.Model): class MatchDscp(models.Model):
dscp = models.CharField(max_length=24) dscp = models.CharField(max_length=24)
def __unicode__(self):
return self.dscp
class Meta: class Meta:
db_table = u'match_dscp' db_table = u'match_dscp'
class MatchFragmentType(models.Model): class MatchFragmentType(models.Model):
fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES) fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES)
def __unicode__(self):
return self.fragmenttype
class Meta: class Meta:
db_table = u'match_fragment_type' db_table = u'match_fragment_type'
class MatchIcmpCode(models.Model): class MatchIcmpCode(models.Model):
icmp_code = models.CharField(max_length=64) icmp_code = models.CharField(max_length=64)
def __unicode__(self):
return self.icmp_code
class Meta: class Meta:
db_table = u'match_icmp_code' db_table = u'match_icmp_code'
...@@ -58,17 +73,23 @@ class MatchPacketLength(models.Model): ...@@ -58,17 +73,23 @@ class MatchPacketLength(models.Model):
class MatchProtocol(models.Model): class MatchProtocol(models.Model):
protocol = models.CharField(max_length=64) protocol = models.CharField(max_length=64)
def __unicode__(self):
return self.protocol
class Meta: class Meta:
db_table = u'match_protocol' db_table = u'match_protocol'
class MatchTcpFlag(models.Model): class MatchTcpFlag(models.Model):
tcp_flag = models.CharField(max_length=255) tcp_flag = models.CharField(max_length=255)
def __unicode__(self):
return self.tcp_flag
class Meta: class Meta:
db_table = u'match_tcp_flag' db_table = u'match_tcp_flag'
class ThenAction(models.Model): class ThenAction(models.Model):
action = models.CharField(max_length=60, choices=THEN_CHOICES) action = models.CharField(max_length=60, choices=THEN_CHOICES)
action_value = models.CharField(max_length=255, blank=True, null=True) action_value = models.CharField(max_length=255, blank=True, null=True)
def __unicode__(self):
return "%s %s" %(self.action, self.action_value)
class Meta: class Meta:
db_table = u'then_action' db_table = u'then_action'
...@@ -85,10 +106,10 @@ class MatchStatement(models.Model): ...@@ -85,10 +106,10 @@ class MatchStatement(models.Model):
matchicmpcode = models.ForeignKey(MatchIcmpCode, blank=True, null=True) matchicmpcode = models.ForeignKey(MatchIcmpCode, blank=True, null=True)
matchicmptype = models.ForeignKey(MatchIcmpType, blank=True, null=True) matchicmptype = models.ForeignKey(MatchIcmpType, blank=True, null=True)
matchpacketlength = models.ForeignKey(MatchPacketLength, 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) matchprotocol = models.ForeignKey(MatchProtocol, blank=True, null=True)
matchSource = models.ManyToManyField(MatchAddress, blank=True, null=True, related_name="matchSource") matchSource = models.ForeignKey(MatchAddress, blank=True, null=True, related_name="matchSource")
matchSourcePort = models.ForeignKey(MatchPort, blank=True, null=True, related_name="matchSourcePort") matchSourcePort = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort")
matchTcpFlag = models.ForeignKey(MatchTcpFlag, blank=True, null=True) matchTcpFlag = models.ForeignKey(MatchTcpFlag, blank=True, null=True)
class Meta: class Meta:
db_table = u'match' db_table = u'match'
...@@ -101,5 +122,52 @@ class Route(models.Model): ...@@ -101,5 +122,52 @@ class Route(models.Model):
filed = models.DateTimeField(auto_now_add=True) filed = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True) last_updated = models.DateTimeField(auto_now=True)
expires = models.DateTimeField() expires = models.DateTimeField()
def __unicode__(self):
return self.name
class Meta: class Meta:
db_table = u'route' db_table = u'route'
\ No newline at end of file
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
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin: # Uncomment the next two lines to enable the admin:
# from django.contrib import admin from django.contrib import admin
# admin.autodiscover() admin.autodiscover()
urlpatterns = patterns('', urlpatterns = patterns('',
# Example: # Example:
# (r'^flowspy/', include('flowspy.foo.urls')), # (r'^flowspy/', include('flowspy.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation: # Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin: # Uncomment the next line to enable the admin:
# (r'^admin/', include(admin.site.urls)), (r'^admin/', include(admin.site.urls)),
) )
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment