blob: 6cb5e2e8a9350cea75253698a894fe1c1321832a [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 Mack7130ac32013-03-22 21:58:00 -040046class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040047 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -040048 name = models.CharField(help_text="The Name of the Slice", max_length=80)
49 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
50 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
51 omf_friendly = models.BooleanField()
52 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
53 slice_url = models.URLField(blank=True)
54 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
55
56 def __unicode__(self): return u'%s' % (self.name)
57
Tony Mackd7cb7712013-03-26 13:21:36 -040058 def save(self, *args, **kwds):
59 os_shell = OpenStackShell()
60 tenant_fields = {'name': self.name,
61 'enabled': self.enabled,
62 'description': self.description}
63 if not self.id:
64 # check if keystone tenant record exists
65 tenants = os_shell.keystone.tenants.findall(name=self.name)
66 if not tenants:
67 tenant = os_shell.keystone.tenants.create(**tenant_fields)
68 else:
69 tenant = tenants[0]
70 self.tenant_id = tenants.id
71 else:
72 # update record
73 os_shell.keystone.tenants.update(self.tenant_id, **tenant_fields)
74 super(Site, self).save(*args, **kwargs)
75
Tony Mack7130ac32013-03-22 21:58:00 -040076class DeploymentNetwork(PlCoreBase):
77 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
78
79 def __unicode__(self): return u'%s' % (self.name)
80
81class SiteDeploymentNetwork(PlCoreBase):
82 class Meta:
83 unique_together = ['site', 'deploymentNetwork']
84
85 site = models.ForeignKey(Site, related_name='deploymentNetworks')
86 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
87 name = models.CharField(default="Blah", max_length=100)
88
89
90 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
91
92
93class Sliver(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040094 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -040095 name = models.CharField(max_length=200, unique=True)
96 slice = models.ForeignKey(Slice)
97 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork)
98
99 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
100
101class Node(PlCoreBase):
102 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
103 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
104
105 def __unicode__(self): return u'%s' % (self.name)
106
Tony Mackd7cb7712013-03-26 13:21:36 -0400107class Network(PlCoreBase):
108 slice = models.ForeignKey(Slice, related_name='slice')
109 name = models.CharField(max_length=200, unique=True)
110 quantum_id = models.CharField(max_length=200, unique=True)
111
112 def __unicode__(self): return u'%s' % (self.name)
113
114 def save(self, *args, **kwargs):
115 os_shell = OpenStackShell()
116 network_fields = {'name': self.name}
117
118 if not self.id:
119 # check if quantum network record exists
120 networks = os_shell.quantum.list_networks(name=self.name)
121 if not networks:
122 network = os_shell.quantum.create_network(name=self.name,
123 admin_state_up=False)
124 else:
125 network = networks[0]
126 self.quantum_id = network.id
127 super(Network, self).save(*args, **kwargs)
128
129class SubNet(PlCoreBase):
130 network = models.ForeignKey(Network, related_name='network')
131 name = models.CharField(max_length=200, unique=True)
132 quantum_id = models.CharField(max_length=200, unique=True)
133 cidr = models.CharField(max_length=20)
134 ip_version = models.IntegerField()
135 start = models.IPAddressField()
136 end = models.IPAddressField()
137
138 def __unicode__(self): return u'%s' % (self.name)
139
140 def save(self, *args, **kwargs):
141 os_shell = OpenStackShell()
142 subnet_fields = {'network_id': self.network.quantum_id,
143 'name' : self.name,
144 'ip_version': self.ip_version,
145 'cidr': self.cidr,
146 'allocation_pools': ['start': self.start,
147 'end': self.end]
148 }
149 if not self.id:
150 subnets = os_shell.quantum.list_subnets(name=self.name)
151 if not subnets:
152 subnet = os_shell.quantum.create_subnet(**subnet_fields)
153 else:
154 subnet = subnets[0]
155 self.quantum_id = subnet.id
156 super(SubNet, self).save(*args, **kwargs)
157
158
159