blob: 72e7a5f452e01001d294b4c0b36d0bc899e5ae69 [file] [log] [blame]
Scott Baker58a9c7a2013-07-29 15:43:07 -07001import os
2import socket
3from django.db import models
Siobhan Tullycf04fb62014-01-11 11:25:57 -05004from core.models import PlCoreBase, Site, Slice, Sliver
Scott Baker58a9c7a2013-07-29 15:43:07 -07005from 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 Baker5f814b52013-08-09 14:51:21 -070021 sharedNetworkName = models.CharField(max_length=30, blank=True, null=True)
22 sharedNetworkId = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070023
24 def __unicode__(self): return u'%s' % (self.name)
25
26class Network(PlCoreBase):
27 name = models.CharField(max_length=32)
28 template = models.ForeignKey(NetworkTemplate)
29 subnet = models.CharField(max_length=32, blank=True)
30 ports = models.CharField(max_length=1024, blank=True, null=True)
31 labels = models.CharField(max_length=1024, blank=True, null=True)
Siobhan Tullyce652d02013-10-08 21:52:35 -040032 owner = models.ForeignKey(Slice, related_name="ownedNetworks", help_text="Slice that owns control of this Network")
Scott Baker58a9c7a2013-07-29 15:43:07 -070033
34 guaranteedBandwidth = models.IntegerField(default=0)
35 permitAllSlices = models.BooleanField(default=False)
36 permittedSlices = models.ManyToManyField(Slice, blank=True, related_name="availableNetworks")
Scott Baker87191e72013-08-06 08:55:07 -070037 slices = models.ManyToManyField(Slice, blank=True, related_name="networks", through="NetworkSlice")
Scott Baker58a9c7a2013-07-29 15:43:07 -070038 slivers = models.ManyToManyField(Sliver, blank=True, related_name="networks", through="NetworkSliver")
39
Scott Baker87191e72013-08-06 08:55:07 -070040 # for observer/manager
41 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
42 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
43 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
44
Scott Baker58a9c7a2013-07-29 15:43:07 -070045 def __unicode__(self): return u'%s' % (self.name)
46
47 def save(self, *args, **kwds):
48 if (not self.subnet) and (NO_OBSERVER):
49 from util.network_subnet_allocator import find_unused_subnet
50 self.subnet = find_unused_subnet(existing_subnets=[x.subnet for x in Network.objects.all()])
51 super(Network, self).save(*args, **kwds)
52
Scott Baker87191e72013-08-06 08:55:07 -070053class NetworkSlice(PlCoreBase):
54 # This object exists solely so we can implement the permission check when
55 # adding slices to networks. It adds no additional fields to the relation.
56
57 network = models.ForeignKey(Network)
58 slice = models.ForeignKey(Slice)
59
60 def save(self, *args, **kwds):
61 slice = self.slice
62 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
63 # to add a sliver to the network, then one of the following must be true:
64 # 1) sliver's slice is in network's permittedSlices list,
65 # 2) sliver's slice is network's owner, or
66 # 3) network's permitAllSlices is true
67 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
68
69 super(NetworkSlice, self).save(*args, **kwds)
70
71 def __unicode__(self): return u'%s-%s' % (self.network.name, self.slice.name)
72
Scott Baker58a9c7a2013-07-29 15:43:07 -070073class NetworkSliver(PlCoreBase):
74 network = models.ForeignKey(Network)
75 sliver = models.ForeignKey(Sliver)
Scott Baker026bfe72013-07-29 16:03:50 -070076 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Scott Bakerf4df9522013-08-19 17:56:45 -070077 port_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum port id")
Scott Baker58a9c7a2013-07-29 15:43:07 -070078
79 def save(self, *args, **kwds):
Scott Baker87191e72013-08-06 08:55:07 -070080 slice = self.sliver.slice
81 if (slice not in self.network.permittedSlices.all()) and (slice != self.network.owner) and (not self.network.permitAllSlices):
82 # to add a sliver to the network, then one of the following must be true:
83 # 1) sliver's slice is in network's permittedSlices list,
84 # 2) sliver's slice is network's owner, or
85 # 3) network's permitAllSlices is true
86 raise ValueError("Slice %s is not allowed to connect to network %s" % (str(slice), str(self.network)))
87
Scott Baker58a9c7a2013-07-29 15:43:07 -070088 if (not self.ip) and (NO_OBSERVER):
89 from util.network_subnet_allocator import find_unused_address
90 self.ip = find_unused_address(self.network.subnet,
91 [x.ip for x in self.network.networksliver_set.all()])
92 super(NetworkSliver, self).save(*args, **kwds)
93
94 def __unicode__(self): return u'%s-%s' % (self.network.name, self.sliver.instance_name)
95
96class Router(PlCoreBase):
97 name = models.CharField(max_length=32)
98 owner = models.ForeignKey(Slice, related_name="routers")
99 permittedNetworks = models.ManyToManyField(Network, blank=True, related_name="availableRouters")
100 networks = models.ManyToManyField(Network, blank=True, related_name="routers")
101
102 def __unicode__(self): return u'%s' % (self.name)
103
104class NetworkParameterType(PlCoreBase):
105 name = models.SlugField(help_text="The name of this parameter", max_length=128)
106 description = models.CharField(max_length=1024)
107
108 def __unicode__(self): return u'%s' % (self.name)
109
110class NetworkParameter(PlCoreBase):
111 parameter = models.ForeignKey(NetworkParameterType, related_name="parameters", help_text="The type of the parameter")
112 value = models.CharField(help_text="The value of this parameter", max_length=1024)
113
114 # The required fields to do a ObjectType lookup, and object_id assignment
115 content_type = models.ForeignKey(ContentType)
116 object_id = models.PositiveIntegerField()
117 content_object = generic.GenericForeignKey('content_type', 'object_id')
118
119 def __unicode__(self):
120 return self.parameter.name
121
122