blob: 02a1b23d247f2376aec68eb6466c1402aababe83 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
3from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04004from observer.openstacksyncstep import OpenStackSyncStep
5from core.models.network import *
Sapan Bhatia24836f12013-08-27 10:16:05 -04006
7class SyncNetworks(OpenStackSyncStep):
Tony Mackce79de02013-09-24 10:12:33 -04008 provides=[Network]
9 requested_interval = 0
Sapan Bhatia24836f12013-08-27 10:16:05 -040010
Tony Mack66646d52013-09-24 21:47:12 -040011 def fetch_pending(self):
12 return Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
13
Tony Mackce79de02013-09-24 10:12:33 -040014 def save_network(self, network):
15 if not network.network_id:
16 if network.template.sharedNetworkName:
17 network.network_id = network.template.sharedNetworkId
18 (network.subnet_id, network.subnet) = self.driver.get_network_subnet(network.network_id)
19 else:
20 network_name = network.name
Sapan Bhatia24836f12013-08-27 10:16:05 -040021
Tony Mackce79de02013-09-24 10:12:33 -040022 # create network
23 os_network = self.driver.create_network(network_name, shared=True)
24 network.network_id = os_network['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040025
Tony Mackce79de02013-09-24 10:12:33 -040026 # create router
27 router = self.driver.create_router(network_name)
28 network.router_id = router['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040029
Tony Mackce79de02013-09-24 10:12:33 -040030 # create subnet
31 next_subnet = self.get_next_subnet()
32 cidr = str(next_subnet.cidr)
33 ip_version = next_subnet.version
34 start = str(next_subnet[2])
35 end = str(next_subnet[-2])
36 subnet = self.driver.create_subnet(name=network_name,
37 network_id = network.network_id,
38 cidr_ip = cidr,
39 ip_version = ip_version,
40 start = start,
41 end = end)
42 network.subnet = cidr
43 network.subnet_id = subnet['id']
Tony Mack387a73f2013-09-18 07:59:14 -040044 # add subnet as interface to slice's router
45 self.driver.add_router_interface(router['id'], subnet['id'])
46 # add external route
47 self.driver.add_external_route(subnet)
Sapan Bhatia24836f12013-08-27 10:16:05 -040048
Tony Mackce79de02013-09-24 10:12:33 -040049 def sync_record(self, site):
50 if network.owner and network.owner.creator:
Tony Mack66646d52013-09-24 21:47:12 -040051 try:
52 # update manager context
53 real_driver = self.driver
54 self.driver = self.driver.client_driver(network.owner.creator, network.owner.name)
55 self.save_network(network)
56 self.driver = real_driver
57 logger.info("saved network: %s" % (network))
58 except Exception,e:
59 logger.log_exc("save network failed: %s" % network)
60 raise e
Sapan Bhatia24836f12013-08-27 10:16:05 -040061