aboutsummaryrefslogtreecommitdiff
path: root/rpkid/rpki/gui/models.py
blob: 3f601f7cb6ea84e8e104a90de17e1cc75047f463 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
$Id$

Copyright (C) 2012  SPARTA, Inc. a Parsons Company

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND SPARTA DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS.  IN NO EVENT SHALL SPARTA BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

Common classes for reuse in apps.
"""

import struct

from django.db import models

import rpki.resource_set
import rpki.POW

class IPv6AddressField(models.Field):
    "Field large enough to hold a 128-bit unsigned integer."

    __metaclass__ = models.SubfieldBase

    def db_type(self, connection):
        return 'binary(16)'

    def to_python(self, value):
        if isinstance(value, rpki.POW.IPAddress):
            return value
        return rpki.POW.IPAddress.from_bytes(value)

    def get_db_prep_value(self, value, connection, prepared):
        return value.to_bytes()

class IPv4AddressField(models.Field):
    "Wrapper around rpki.POW.IPAddress."

    __metaclass__ = models.SubfieldBase

    def db_type(self, connection):
        return 'int UNSIGNED'

    def to_python(self, value):
        if isinstance(value, rpki.POW.IPAddress):
            return value
        return rpki.POW.IPAddress(value)

    def get_db_prep_value(self, value, connection, prepared):
        return long(value)

class Prefix(models.Model):
    """Common implementation for models with an IP address range.

    Expects that `range_cls` is set to the appropriate subclass of
    rpki.resource_set.resource_range_ip."""

    def as_resource_range(self):
        """
        Returns the prefix as a rpki.resource_set.resource_range_ip object.
        """
        return self.range_cls(self.prefix_min, self.prefix_max)

    def prefixlen(self):
        "Returns the prefix length for the prefix in this object."
        return self.as_resource_range().prefixlen()

    def get_prefix_display(self):
        "Return a string representatation of this IP prefix."
        return str(self.as_resource_range())

    def __unicode__(self):
        """This method may be overridden by subclasses.  The default
        implementation calls get_prefix_display(). """
        return self.get_prefix_display()

    class Meta:
        abstract = True
        
        # default sort order reflects what "sh ip bgp" outputs
        ordering = ('prefix_min',)

class PrefixV4(Prefix):
    "IPv4 Prefix."

    range_cls = rpki.resource_set.resource_range_ipv4

    prefix_min = IPv4AddressField(db_index=True, null=False)
    prefix_max = IPv4AddressField(db_index=True, null=False)

    class Meta(Prefix.Meta):
        abstract = True

class PrefixV6(Prefix):
    "IPv6 Prefix."

    range_cls = rpki.resource_set.resource_range_ipv6

    prefix_min = IPv6AddressField(db_index=True, null=False)
    prefix_max = IPv6AddressField(db_index=True, null=False)

    class Meta(Prefix.Meta):
        abstract = True

class ASN(models.Model):
    """Represents a range of ASNs.

    This model is abstract, and is intended to be reused by applications."""

    min = models.PositiveIntegerField(null=False)
    max = models.PositiveIntegerField(null=False)

    class Meta:
        abstract = True
        ordering = ('min', 'max')

    def as_resource_range(self):
        return rpki.resource_set.resource_range_as(self.min, self.max)

    def __unicode__(self):
        return u'AS%s' % self.as_resource_range()

# vim:sw=4 ts=8 expandtab