blob: c9ea1efaea99c06c05448c7d072ba1fbc2040264 [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
Siobhan Tully4bc09f22013-04-10 21:15:21 -04006
7# Create your models here.
8
9class Subnet(PlCoreBase):
Tony Mackcdec0902013-04-15 00:38:49 -040010 subnet_id = models.CharField(max_length=256, unique=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040011 cidr = models.CharField(max_length=20)
12 ip_version = models.IntegerField()
13 start = models.IPAddressField()
14 end = models.IPAddressField()
Tony Mack771f4b92013-04-12 01:49:57 -040015 slice = models.ForeignKey(Slice, related_name='subnet')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040016
Tony Mackf79ad8e2013-04-12 13:52:44 -040017 def __unicode__(self): return u'%s' % (self.slice.name)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040018
Tony Mack31d34132013-04-15 00:02:06 -040019 def save(self, *args, **kwds):
Tony Mack31d34132013-04-15 00:02:06 -040020 if not self.subnet_id:
Tony Mackfdd4d802013-04-27 13:02:33 -040021 quantum_subnet = self.driver.create_subnet(name= self.slice.name,
Tony Mack31d34132013-04-15 00:02:06 -040022 network_id=self.slice.network_id,
23 cidr_ip = self.cidr,
24 ip_version=self.ip_version,
25 start = self.start,
26 end = self.end)
27 self.subnet_id = quantum_subnet['id']
28 # add subnet as interface to slice's router
Tony Mackfdd4d802013-04-27 13:02:33 -040029 self.driver.add_router_interface(self.slice.router_id, self.subnet_id)
Tony Mackaa6b7502013-04-17 21:58:34 -040030 #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
Tony Mack814922b2013-04-24 14:51:53 -040031 #commands.getstatusoutput(add_route)
Tony Mack31d34132013-04-15 00:02:06 -040032
33 super(Subnet, self).save(*args, **kwds)
34
35 def delete(self, *args, **kwds):
Tony Mack31d34132013-04-15 00:02:06 -040036 if self.subnet_id:
Tony Mackfdd4d802013-04-27 13:02:33 -040037 self.driver.delete_router_interface(self.slice.router_id, self.subnet_id)
38 self.driver.delete_subnet(self.subnet_id)
Tony Mackaa6b7502013-04-17 21:58:34 -040039 #del_route = 'route del -net %s' % self.cidr
Tony Mack814922b2013-04-24 14:51:53 -040040 #commands.getstatusoutput(del_route)
Tony Mack31d34132013-04-15 00:02:06 -040041 super(Subnet, self).delete(*args, **kwds)