blob: f91c0fc37c6f57ac1c2a2162d6cd48e3caa32e62 [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
Andy Bavier5e243e12013-10-18 14:11:00 -04008from util.logger import Logger, logging
9
Andy Bavier33584c52013-10-28 15:17:16 -040010logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040011
12class SyncSlices(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -040013 provides=[Slice]
14 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040015
16 def fetch_pending(self):
17 return Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
18
Tony Mack0ae3fe32013-10-08 08:47:53 -040019 def get_next_subnet(self):
20 # limit ourself to 10.0.x.x for now
21 valid_subnet = lambda net: net.startswith('10.0')
22 subnets = self.driver.shell.quantum.list_subnets()['subnets']
23 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
24 if valid_subnet(subnet['cidr'])]
25 ints.sort()
26 last_ip = IPAddress(ints[-1])
27 last_network = IPNetwork(str(last_ip) + "/24")
28 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
Andy Bavier5e243e12013-10-18 14:11:00 -040029 return next_network
Tony Mack0ae3fe32013-10-08 08:47:53 -040030
Tony Mackdacfb982013-09-24 21:57:16 -040031 def sync_record(self, slice):
Andy Bavier5e243e12013-10-18 14:11:00 -040032 logger.info("sync'ing slice %s" % slice.name)
Tony Mackdacfb982013-09-24 21:57:16 -040033 if not slice.tenant_id:
34 nova_fields = {'tenant_name': slice.name,
35 'description': slice.description,
36 'enabled': slice.enabled}
37 tenant = self.driver.create_tenant(**nova_fields)
38 slice.tenant_id = tenant.id
Sapan Bhatia24836f12013-08-27 10:16:05 -040039
Tony Mackdacfb982013-09-24 21:57:16 -040040 # XXX give caller an admin role at the tenant they've created
Tony Mack0ae3fe32013-10-08 08:47:53 -040041 self.driver.add_user_role(slice.creator.kuser_id, tenant.id, 'admin')
Sapan Bhatia24836f12013-08-27 10:16:05 -040042
Tony Mackdacfb982013-09-24 21:57:16 -040043 # refresh credentials using this tenant
Tony Mack0ae3fe32013-10-08 08:47:53 -040044 client_driver = self.driver.client_driver(tenant=tenant.name)
Sapan Bhatia24836f12013-08-27 10:16:05 -040045
Tony Mackdacfb982013-09-24 21:57:16 -040046 # create network
Tony Mack0ae3fe32013-10-08 08:47:53 -040047 network = client_driver.create_network(slice.name)
Tony Mackdacfb982013-09-24 21:57:16 -040048 slice.network_id = network['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040049
Tony Mackdacfb982013-09-24 21:57:16 -040050 # create router
Tony Mack0ae3fe32013-10-08 08:47:53 -040051 router = client_driver.create_router(slice.name)
Tony Mackdacfb982013-09-24 21:57:16 -040052 slice.router_id = router['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040053
Andy Bavier33584c52013-10-28 15:17:16 -040054 # create subnet for slice's private network
Tony Mackdacfb982013-09-24 21:57:16 -040055 next_subnet = self.get_next_subnet()
56 cidr = str(next_subnet.cidr)
57 ip_version = next_subnet.version
58 start = str(next_subnet[2])
59 end = str(next_subnet[-2])
Tony Mack0ae3fe32013-10-08 08:47:53 -040060 subnet = client_driver.create_subnet(name=slice.name,
Tony Mackdacfb982013-09-24 21:57:16 -040061 network_id = network['id'],
62 cidr_ip = cidr,
63 ip_version = ip_version,
64 start = start,
65 end = end)
66 slice.subnet_id = subnet['id']
67 # add subnet as interface to slice's router
Tony Mack0ae3fe32013-10-08 08:47:53 -040068 client_driver.add_router_interface(router['id'], subnet['id'])
Tony Mackdacfb982013-09-24 21:57:16 -040069 # add external route
Tony Mack0ae3fe32013-10-08 08:47:53 -040070 client_driver.add_external_route(subnet)
Sapan Bhatia24836f12013-08-27 10:16:05 -040071
72
Tony Mackdacfb982013-09-24 21:57:16 -040073 if slice.id and slice.tenant_id:
Andy Bavier606bf0e2013-10-18 15:11:29 -040074 self.driver.update_tenant(slice.tenant_id,
Tony Mackdacfb982013-09-24 21:57:16 -040075 description=slice.description,
76 enabled=slice.enabled)
Sapan Bhatia24836f12013-08-27 10:16:05 -040077
Tony Mackdacfb982013-09-24 21:57:16 -040078 slice.save()