blob: 8f51fb81832b32ed37f96d8c835a9e4b31f0a9eb [file] [log] [blame]
Scott Baker58a9c7a2013-07-29 15:43:07 -07001import os
2import socket
3from django.db import models
4from core.models import PlCoreBase, Site, Slice, Sliver
5from django.contrib.contenttypes.models import ContentType
6from django.contrib.contenttypes import generic
7
8# If true, then IP addresses will be allocated by the model. If false, then
9# we will assume the observer handles it.
Scott Baker026bfe72013-07-29 16:03:50 -070010NO_OBSERVER=False
Scott Baker58a9c7a2013-07-29 15:43:07 -070011
12class NetworkTemplate(PlCoreBase):
13 VISIBILITY_CHOICES = (('public', 'public'), ('private', 'private'))
Scott Baker87e5e092013-08-07 18:58:10 -070014 TRANSLATION_CHOICES = (('none', 'none'), ('NAT', 'NAT'))
Scott Baker58a9c7a2013-07-29 15:43:07 -070015
16 name = models.CharField(max_length=32)
17 description = models.CharField(max_length=1024, blank=True, null=True)
18 guaranteedBandwidth = models.IntegerField(default=0)
19 visibility = models.CharField(max_length=30, choices=VISIBILITY_CHOICES, default="private")
Scott Baker87e5e092013-08-07 18:58:10 -070020 translation = models.CharField(max_length=30, choices=TRANSLATION_CHOICES, default="none")
Scott Baker58a9c7a2013-07-29 15:43:07 -070021
22 def __unicode__(self): return u'%s' % (self.name)
23
24class Network(PlCoreBase):
25 name = models.CharField(max_length=32)
26 template = models.ForeignKey(NetworkTemplate)
27 subnet = models.CharField(max_length=32, blank=True)
28 ports = models.CharField(max_length=1024, blank=True, null=True)
29 labels = models.CharField(max_length=1024, blank=True, null=True)
30 owner = models.ForeignKey(Slice, related_name="ownedNetworks")
31
32 guaranteedBandwidth = models.IntegerField(default=0)
33 permitAllSlices = models.BooleanField(default=False)
34 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
Scott Baker87191e72013-08-06 08:55:07 -070035 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
Scott Baker58a9c7a2013-07-29 15:43:07 -070036 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
37
Scott Baker87191e72013-08-06 08:55:07 -070038 # for observer/manager
39 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
40 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
41 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
42
Scott Baker58a9c7a2013-07-29 15:43:07 -070043 def __unicode__(self): return u'%s' % (self.name)
44
45 def save(self, *args, **kwds):
46 if (not self.subnet) and (NO_OBSERVER):
47 from util.network_subnet_allocator import find_unused_subnet
48 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
49 super(Network, self).save(*args, **kwds)
50
Scott Baker87191e72013-08-06 08:55:07 -070051class NetworkSlice(PlCoreBase):
52 # This object exists solely so we can implement the permission check when
53 # adding slices to networks. It adds no additional fields to the relation.
54
55 network = models.ForeignKey(Network)
56 slice = models.ForeignKey(Slice)
57
58 def save(self, *args, **kwds):
59 slice = self.slice
60 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
61 # to add a sliver to the network, then one of the following must be true:
62 # 1) sliver's slice is in network's permittedSlices list,
63 # 2) sliver's slice is network's owner, or
64 # 3) network's permitAllSlices is true
65 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
66
67 super(NetworkSlice, self).save(*args, **kwds)
68
69 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
70
Scott Baker58a9c7a2013-07-29 15:43:07 -070071class NetworkSliver(PlCoreBase):
72 network = models.ForeignKey(Network)
73 sliver = models.ForeignKey(Sliver)
Scott Baker026bfe72013-07-29 16:03:50 -070074 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Scott Baker58a9c7a2013-07-29 15:43:07 -070075
76 def save(self, *args, **kwds):
Scott Baker87191e72013-08-06 08:55:07 -070077 slice = self.sliver.slice
78 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
79 # to add a sliver to the network, then one of the following must be true:
80 # 1) sliver's slice is in network's permittedSlices list,
81 # 2) sliver's slice is network's owner, or
82 # 3) network's permitAllSlices is true
83 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
84
Scott Baker58a9c7a2013-07-29 15:43:07 -070085 if (not self.ip) and (NO_OBSERVER):
86 from util.network_subnet_allocator import find_unused_address
87 self.ip = find_unused_address(self.network.subnet,
88 [x.ip for x in self.network.networksliver_set.all()])
89 super(NetworkSliver, self).save(*args, **kwds)
90
91 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
92
93class Router(PlCoreBase):
94 name = models.CharField(max_length=32)
95 owner = models.ForeignKey(Slice, related_name="routers")
96 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
97 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
98
99 def __unicode__(self): return u'%s' % (self.name)
100
101class NetworkParameterType(PlCoreBase):
102 name = models.SlugField(help_text="The name of this parameter", max_length=128)
103 description = models.CharField(max_length=1024)
104
105 def __unicode__(self): return u'%s' % (self.name)
106
107class NetworkParameter(PlCoreBase):
108 parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter")
109 value = models.CharField(help_text="The value of this parameter", max_length=1024)
110
111 # The required fields to do a ObjectType lookup, and object_id assignment
112 content_type = models.ForeignKey(ContentType)
113 object_id = models.PositiveIntegerField()
114 content_object = generic.GenericForeignKey('content_type', 'object_id')
115
116 def __unicode__(self):
117 return self.parameter.name
118
119