blob: 53dc06b9a12de387b010a60067bff77e149fc7f6 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mack0ae3fe32013-10-08 08:47:53 -04003from netaddr import IPAddress, IPNetwork
Tony Mackae7f30c2013-09-25 12:46:50 -04004from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04005from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04006from observer.openstacksyncstep import OpenStackSyncStep
7from core.models.slice import Slice
Sapan Bhatia24836f12013-08-27 10:16:05 -04008
9class SyncSlices(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -040010 provides=[Slice]
11 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040012
13 def fetch_pending(self):
14 return Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
15
Tony Mack0ae3fe32013-10-08 08:47:53 -040016 def get_next_subnet(self):
17 # limit ourself to 10.0.x.x for now
18 valid_subnet = lambda net: net.startswith('10.0')
19 subnets = self.driver.shell.quantum.list_subnets()['subnets']
20 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
21 if valid_subnet(subnet['cidr'])]
22 ints.sort()
23 last_ip = IPAddress(ints[-1])
24 last_network = IPNetwork(str(last_ip) + "/24")
25 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
26
Tony Mackdacfb982013-09-24 21:57:16 -040027 def sync_record(self, slice):
28 if not slice.tenant_id:
29 nova_fields = {'tenant_name': slice.name,
30 'description': slice.description,
31 'enabled': slice.enabled}
32 tenant = self.driver.create_tenant(**nova_fields)
33 slice.tenant_id = tenant.id
Sapan Bhatia24836f12013-08-27 10:16:05 -040034
Tony Mackdacfb982013-09-24 21:57:16 -040035 # XXX give caller an admin role at the tenant they've created
Tony Mack0ae3fe32013-10-08 08:47:53 -040036 self.driver.add_user_role(slice.creator.kuser_id, tenant.id, 'admin')
Sapan Bhatia24836f12013-08-27 10:16:05 -040037
Tony Mackdacfb982013-09-24 21:57:16 -040038 # refresh credentials using this tenant
Tony Mack0ae3fe32013-10-08 08:47:53 -040039 client_driver = self.driver.client_driver(tenant=tenant.name)
Sapan Bhatia24836f12013-08-27 10:16:05 -040040
Tony Mackdacfb982013-09-24 21:57:16 -040041 # create network
Tony Mack0ae3fe32013-10-08 08:47:53 -040042 network = client_driver.create_network(slice.name)
Tony Mackdacfb982013-09-24 21:57:16 -040043 slice.network_id = network['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040044
Tony Mackdacfb982013-09-24 21:57:16 -040045 # create router
Tony Mack0ae3fe32013-10-08 08:47:53 -040046 router = client_driver.create_router(slice.name)
Tony Mackdacfb982013-09-24 21:57:16 -040047 slice.router_id = router['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040048
Tony Mackdacfb982013-09-24 21:57:16 -040049 # create subnet
50 next_subnet = self.get_next_subnet()
51 cidr = str(next_subnet.cidr)
52 ip_version = next_subnet.version
53 start = str(next_subnet[2])
54 end = str(next_subnet[-2])
Tony Mack0ae3fe32013-10-08 08:47:53 -040055 subnet = client_driver.create_subnet(name=slice.name,
Tony Mackdacfb982013-09-24 21:57:16 -040056 network_id = network['id'],
57 cidr_ip = cidr,
58 ip_version = ip_version,
59 start = start,
60 end = end)
61 slice.subnet_id = subnet['id']
62 # add subnet as interface to slice's router
Tony Mack0ae3fe32013-10-08 08:47:53 -040063 client_driver.add_router_interface(router['id'], subnet['id'])
Tony Mackdacfb982013-09-24 21:57:16 -040064 # add external route
Tony Mack0ae3fe32013-10-08 08:47:53 -040065 client_driver.add_external_route(subnet)
Sapan Bhatia24836f12013-08-27 10:16:05 -040066
67
Tony Mackdacfb982013-09-24 21:57:16 -040068 if slice.id and slice.tenant_id:
Tony Mack0ae3fe32013-10-08 08:47:53 -040069 client_driver.update_tenant(slice.tenant_id,
Tony Mackdacfb982013-09-24 21:57:16 -040070 description=slice.description,
71 enabled=slice.enabled)
Sapan Bhatia24836f12013-08-27 10:16:05 -040072
Tony Mackdacfb982013-09-24 21:57:16 -040073 slice.save()