Revision a24fbf37 flowspec/models.py

b/flowspec/models.py
1 1
from django.db import models
2 2
from django.contrib.auth.models import User
3 3

  
4
import nxpy as np
5
from ncclient import manager
6
from ncclient.transport.errors import AuthenticationError, SSHError
7
from lxml import etree as ET
8

  
4 9
FRAGMENT_CODES = (
5 10
    ("dont-fragment", "Don't fragment"),
6 11
    ("first-fragment", "First fragment"),
......
22 27

  
23 28
    
24 29
class MatchAddress(models.Model):
25
    destination = models.CharField(max_length=255)
30
    address = models.CharField(max_length=255)
31
    def __unicode__(self):
32
        return self.address
26 33
    class Meta:
27 34
        db_table = u'match_address'
28 35

  
29 36
class MatchPort(models.Model):
30 37
    port = models.CharField(max_length=24)
38
    def __unicode__(self):
39
        return self.port
31 40
    class Meta:
32 41
        db_table = u'match_port'    
33 42

  
34 43
class MatchDscp(models.Model):
35 44
    dscp = models.CharField(max_length=24)
45
    def __unicode__(self):
46
        return self.dscp
36 47
    class Meta:
37 48
        db_table = u'match_dscp'
38 49

  
39 50
class MatchFragmentType(models.Model):
40 51
    fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES)
52
    def __unicode__(self):
53
        return self.fragmenttype
41 54
    class Meta:
42 55
        db_table = u'match_fragment_type'
43 56
    
44 57
class MatchIcmpCode(models.Model):
45 58
    icmp_code = models.CharField(max_length=64)
59
    def __unicode__(self):
60
        return self.icmp_code
46 61
    class Meta:
47 62
        db_table = u'match_icmp_code'    
48 63

  
......
58 73

  
59 74
class MatchProtocol(models.Model):
60 75
    protocol = models.CharField(max_length=64)
76
    def __unicode__(self):
77
        return self.protocol
61 78
    class Meta:
62 79
        db_table = u'match_protocol'    
63 80

  
64 81
class MatchTcpFlag(models.Model):
65 82
    tcp_flag = models.CharField(max_length=255)
83
    def __unicode__(self):
84
        return self.tcp_flag
66 85
    class Meta:
67 86
        db_table = u'match_tcp_flag'    
68 87
    
69 88
class ThenAction(models.Model):
70 89
    action = models.CharField(max_length=60, choices=THEN_CHOICES)
71 90
    action_value = models.CharField(max_length=255, blank=True, null=True)
91
    def __unicode__(self):
92
        return "%s %s" %(self.action, self.action_value)
72 93
    class Meta:
73 94
        db_table = u'then_action'
74 95

  
......
85 106
    matchicmpcode = models.ForeignKey(MatchIcmpCode, blank=True, null=True)
86 107
    matchicmptype = models.ForeignKey(MatchIcmpType, blank=True, null=True)
87 108
    matchpacketlength = models.ForeignKey(MatchPacketLength, blank=True, null=True)
88
    matchport = models.ManyToManyField(MatchPort, blank=True, null=True)
109
    matchport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchPort")
89 110
    matchprotocol = models.ForeignKey(MatchProtocol, blank=True, null=True)
90
    matchSource = models.ManyToManyField(MatchAddress, blank=True, null=True, related_name="matchSource")
91
    matchSourcePort = models.ForeignKey(MatchPort, blank=True, null=True, related_name="matchSourcePort")
111
    matchSource = models.ForeignKey(MatchAddress, blank=True, null=True, related_name="matchSource")
112
    matchSourcePort = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort")
92 113
    matchTcpFlag = models.ForeignKey(MatchTcpFlag, blank=True, null=True)
93 114
    class Meta:
94 115
        db_table = u'match'
......
101 122
    filed = models.DateTimeField(auto_now_add=True)
102 123
    last_updated = models.DateTimeField(auto_now=True)
103 124
    expires = models.DateTimeField()
125
    def __unicode__(self):
126
        return self.name
127
    
104 128
    class Meta:
105
        db_table = u'route'
129
        db_table = u'route'
130

  
131
    def save(self, *args, **kwargs):
132
        # Begin translation to device xml configuration
133
        device = np.Device()
134
        flow = np.Flow()
135
        route = np.Route()
136
        flow.routes.append(route)
137
        device.routing_options.append(flow)
138
        route.name = self.name
139
        match = self.match
140
        if match.matchSource:
141
            route.match['source'].append(match.matchSource.address)
142
        if match.matchDestination:
143
            route.match['destination'].append(match.matchDestination.address)
144
        if match.matchprotocol:
145
            route.match['protocol'].append(match.matchprotocol.protocol)
146
        if match.matchport:
147
            for port in match.matchport.all():
148
                route.match['port'].append(port.port)
149
        if match.matchDestinationPort:
150
            for port in match.matchDestinationPort.all():
151
                route.match['destination-port'].append(port.port)
152
        if match.matchSourcePort:
153
            for port in match.matchSourcePort.all():
154
                route.match['source-port'].append(port.port)
155
        if match.matchicmpcode:
156
            route.match['icmp-code'].append(match.matchicmpcode.icmp_code)
157
        if match.matchicmptype:
158
            route.match['icmp-type'].append(match.matchicmptype.icmp_type)
159
        if match.matchTcpFlag:
160
            route.match['tcp-flags'].append(match.matchTcpFlag.tcp_flags)
161
        if match.matchdscp:
162
            for dscp in match.matchdscp.all():
163
                route.match['dscp'].append(dscp.dscp)
164
        if match.matchfragmenttype:
165
            route.match['fragment'].append(match.matchfragmenttype.fragmenttype)
166
        then = self.then
167
        for thenaction in then.thenaction.all():
168
            if thenaction.action_value:
169
                route.then[thenaction.action] = thenaction.action_value
170
            else:
171
                route.then[thenaction.action] = True
172
        print ET.tostring(device.export())
173
        super(Route, self).save(*args, **kwargs)

Also available in: Unified diff