Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 2 | import commands |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 3 | from django.db import models |
| 4 | from plstackapi.core.models import PlCoreBase |
| 5 | from plstackapi.core.models import Slice |
| 6 | from plstackapi.openstack.driver import OpenStackDriver |
| 7 | |
| 8 | # Create your models here. |
| 9 | |
| 10 | class Subnet(PlCoreBase): |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 11 | subnet_id = models.CharField(max_length=256, unique=True) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 12 | cidr = models.CharField(max_length=20) |
| 13 | ip_version = models.IntegerField() |
| 14 | start = models.IPAddressField() |
| 15 | end = models.IPAddressField() |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 16 | slice = models.ForeignKey(Slice, related_name='subnet') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 17 | |
Tony Mack | f79ad8e | 2013-04-12 13:52:44 -0400 | [diff] [blame] | 18 | def __unicode__(self): return u'%s' % (self.slice.name) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 19 | |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 20 | 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 Mack | d8a837a | 2013-04-16 22:07:22 -0400 | [diff] [blame] | 32 | driver.add_router_interface(self.slice.router_id, self.subnet_id) |
Tony Mack | aa6b750 | 2013-04-17 21:58:34 -0400 | [diff] [blame] | 33 | #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr |
Tony Mack | 814922b | 2013-04-24 14:51:53 -0400 | [diff] [blame^] | 34 | #commands.getstatusoutput(add_route) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 35 | |
| 36 | super(Subnet, self).save(*args, **kwds) |
| 37 | |
| 38 | def delete(self, *args, **kwds): |
| 39 | driver = OpenStackDriver() |
| 40 | if self.subnet_id: |
Tony Mack | d8a837a | 2013-04-16 22:07:22 -0400 | [diff] [blame] | 41 | driver.delete_router_interface(self.slice.router_id, self.subnet_id) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 42 | driver.delete_subnet(self.subnet_id) |
Tony Mack | aa6b750 | 2013-04-17 21:58:34 -0400 | [diff] [blame] | 43 | #del_route = 'route del -net %s' % self.cidr |
Tony Mack | 814922b | 2013-04-24 14:51:53 -0400 | [diff] [blame^] | 44 | #commands.getstatusoutput(del_route) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 45 | super(Subnet, self).delete(*args, **kwds) |