blob: 30285c6ea02f5f6421712a553de479279f984d0d [file] [log] [blame]
Tony Mack7130ac32013-03-22 21:58:00 -04001from django.db import models
Tony Mackd7cb7712013-03-26 13:21:36 -04002from plstackapi.openstack.shell import OpenStackShell
Tony Mack7130ac32013-03-22 21:58:00 -04003
4# Create your models here.
5
6class PlCoreBase(models.Model):
7
8 created = models.DateTimeField(auto_now_add=True)
9 updated = models.DateTimeField(auto_now=True)
10
11 class Meta:
12 abstract = True
13
14class Site(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040015 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -040016 name = models.CharField(max_length=200, unique=True, help_text="Name for this Site")
17 site_url = models.URLField(help_text="Site's Home URL Page")
18 enabled = models.BooleanField(default=True, help_text="Status for this Site")
19 longitude = models.FloatField(null=True, blank=True)
20 latitude = models.FloatField(null=True, blank=True)
21 login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site")
22 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
23 abbreviated_name = models.CharField(max_length=80)
24
25 def __unicode__(self): return u'%s' % (self.name)
26
Tony Mackd7cb7712013-03-26 13:21:36 -040027 def save(self, *args, **kwargs):
28 os_shell = OpenStackShell()
29 tenant_fields = {'name': self.login_base,
30 'enabled': self.enabled,
31 'description': self.name}
32
33 if not self.id:
34 # check if keystone tenant record exists
35 tenants = os_shell.keystone.tenants.findall(name=self.login_base)
36 if not tenants:
37 tenant = os_shell.keystone.tenants.create(**tenant_fields)
38 else:
39 tenant = tenants[0]
40 self.tenant_id = tenants.id
41 else:
42 # update record
43 os_shell.keystone.tenants.update(self.tenant_id, **tenant_fields)
44 super(Site, self).save(*args, **kwargs)
45
Tony Macke1c8c382013-03-26 13:37:59 -040046 def delete(self, *args, **kwds):
47 # delete keystone tenant
48 os_shell = OpenStackShell()
49 tenant = os_shell.keystone.tenants.find(id=self.tenant_id)
50 os_shell.keystone.tenants.delete(tenant)
51
52 super(Site, self).delete(*args, **kwargs)
53
Tony Mack7130ac32013-03-22 21:58:00 -040054class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040055 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -040056 name = models.CharField(help_text="The Name of the Slice", max_length=80)
57 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
58 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
59 omf_friendly = models.BooleanField()
60 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
61 slice_url = models.URLField(blank=True)
62 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
63
64 def __unicode__(self): return u'%s' % (self.name)
65
Tony Mackd7cb7712013-03-26 13:21:36 -040066 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -040067 # sync keystone tenant
Tony Mackd7cb7712013-03-26 13:21:36 -040068 os_shell = OpenStackShell()
69 tenant_fields = {'name': self.name,
70 'enabled': self.enabled,
71 'description': self.description}
72 if not self.id:
73 # check if keystone tenant record exists
74 tenants = os_shell.keystone.tenants.findall(name=self.name)
75 if not tenants:
76 tenant = os_shell.keystone.tenants.create(**tenant_fields)
77 else:
78 tenant = tenants[0]
79 self.tenant_id = tenants.id
80 else:
81 # update record
82 os_shell.keystone.tenants.update(self.tenant_id, **tenant_fields)
83 super(Site, self).save(*args, **kwargs)
84
Tony Macke1c8c382013-03-26 13:37:59 -040085 def delete(self, *args, **kwds):
86 # delete keystone tenant
87 os_shell = OpenStackShell()
88 tenant = os_shell.keystone.tenants.find(id=self.tenant_id)
89 os_shell.keystone.tenants.delete(tenant)
90
91 super(Slice, self).delete(*args, **kwargs)
92
Tony Mack7130ac32013-03-22 21:58:00 -040093class DeploymentNetwork(PlCoreBase):
94 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
95
96 def __unicode__(self): return u'%s' % (self.name)
97
98class SiteDeploymentNetwork(PlCoreBase):
99 class Meta:
100 unique_together = ['site', 'deploymentNetwork']
101
102 site = models.ForeignKey(Site, related_name='deploymentNetworks')
103 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
104 name = models.CharField(default="Blah", max_length=100)
105
106
107 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
108
109
110class Sliver(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -0400111 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -0400112 name = models.CharField(max_length=200, unique=True)
113 slice = models.ForeignKey(Slice)
114 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork)
115
116 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
117
118class Node(PlCoreBase):
119 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
120 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
121
122 def __unicode__(self): return u'%s' % (self.name)
123
Tony Mackd7cb7712013-03-26 13:21:36 -0400124class Network(PlCoreBase):
125 slice = models.ForeignKey(Slice, related_name='slice')
126 name = models.CharField(max_length=200, unique=True)
127 quantum_id = models.CharField(max_length=200, unique=True)
128
129 def __unicode__(self): return u'%s' % (self.name)
130
131 def save(self, *args, **kwargs):
132 os_shell = OpenStackShell()
133 network_fields = {'name': self.name}
134
135 if not self.id:
136 # check if quantum network record exists
137 networks = os_shell.quantum.list_networks(name=self.name)
138 if not networks:
139 network = os_shell.quantum.create_network(name=self.name,
140 admin_state_up=False)
141 else:
142 network = networks[0]
143 self.quantum_id = network.id
144 super(Network, self).save(*args, **kwargs)
145
Tony Macke1c8c382013-03-26 13:37:59 -0400146 def delete(self, *args, **kwargs):
147 # delete quantum network
148 os_shell = OpenStackShell()
149 os_shell.quantum.delete_network(self.quantum_id)
150
151 super(Network, self).delete(*args, **kwargs)
152
Tony Mackd7cb7712013-03-26 13:21:36 -0400153class SubNet(PlCoreBase):
154 network = models.ForeignKey(Network, related_name='network')
155 name = models.CharField(max_length=200, unique=True)
156 quantum_id = models.CharField(max_length=200, unique=True)
157 cidr = models.CharField(max_length=20)
158 ip_version = models.IntegerField()
159 start = models.IPAddressField()
160 end = models.IPAddressField()
161
162 def __unicode__(self): return u'%s' % (self.name)
163
164 def save(self, *args, **kwargs):
165 os_shell = OpenStackShell()
166 subnet_fields = {'network_id': self.network.quantum_id,
167 'name' : self.name,
168 'ip_version': self.ip_version,
169 'cidr': self.cidr,
Tony Mack560788f2013-03-28 20:46:58 -0400170 'allocation_pools': {'start': self.start,
171 'end': self.end}
Tony Mackd7cb7712013-03-26 13:21:36 -0400172 }
173 if not self.id:
174 subnets = os_shell.quantum.list_subnets(name=self.name)
175 if not subnets:
176 subnet = os_shell.quantum.create_subnet(**subnet_fields)
177 else:
178 subnet = subnets[0]
179 self.quantum_id = subnet.id
180 super(SubNet, self).save(*args, **kwargs)
Tony Macke1c8c382013-03-26 13:37:59 -0400181
182
183 def delete(self, *args, **kwargs):
184 # delete quantum network
185 os_shell = OpenStackShell()
186 os_shell.quantum.delete_subnet(self.quantum_id)
187
188 super(SubNet, self).delete(*args, **kwargs)
Tony Mackd7cb7712013-03-26 13:21:36 -0400189
190
191