blob: e94bfedd0c4bfccd8ef9db60991c8a2f515186ae [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
Tony Mack31d34132013-04-15 00:02:06 -04002import commands
Siobhan Tully4bc09f22013-04-10 21:15:21 -04003from django.db import models
4from plstackapi.core.models import PlCoreBase
5from plstackapi.core.models import Slice
6from plstackapi.openstack.driver import OpenStackDriver
7
8# Create your models here.
9
10class Subnet(PlCoreBase):
Tony Mackcdec0902013-04-15 00:38:49 -040011 subnet_id = models.CharField(max_length=256, unique=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040012 cidr = models.CharField(max_length=20)
13 ip_version = models.IntegerField()
14 start = models.IPAddressField()
15 end = models.IPAddressField()
Tony Mack771f4b92013-04-12 01:49:57 -040016 slice = models.ForeignKey(Slice, related_name='subnet')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040017
Tony Mackf79ad8e2013-04-12 13:52:44 -040018 def __unicode__(self): return u'%s' % (self.slice.name)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040019
Tony Mack31d34132013-04-15 00:02:06 -040020 def save(self, *args, **kwds):
21
22 driver = OpenStackDriver()
23 if not self.subnet_id:
24 quantum_subnet = driver.create_subnet(name= self.slice.name,
25 network_id=self.slice.network_id,
26 cidr_ip = self.cidr,
27 ip_version=self.ip_version,
28 start = self.start,
29 end = self.end)
30 self.subnet_id = quantum_subnet['id']
31 # add subnet as interface to slice's router
Tony Mackd8a837a2013-04-16 22:07:22 -040032 driver.add_router_interface(self.slice.router_id, self.subnet_id)
Tony Mack31d34132013-04-15 00:02:06 -040033 add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
34 commands.getstatusoutput(add_route)
35
36 super(Subnet, self).save(*args, **kwds)
37
38 def delete(self, *args, **kwds):
39 driver = OpenStackDriver()
40 if self.subnet_id:
Tony Mackd8a837a2013-04-16 22:07:22 -040041 driver.delete_router_interface(self.slice.router_id, self.subnet_id)
Tony Mack31d34132013-04-15 00:02:06 -040042 driver.delete_subnet(self.subnet_id)
Tony Mackb09cedb2013-04-15 07:19:25 -040043 del_route = 'route del -net %s' % self.cidr
Tony Mack31d34132013-04-15 00:02:06 -040044 commands.getstatusoutput(del_route)
45 super(Subnet, self).delete(*args, **kwds)