blob: 82d6bc72b7f782719de686fc9d54267486edfafd [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.network import *
Sapan Bhatia24836f12013-08-27 10:16:05 -04007
8class SyncNetworks(OpenStackSyncStep):
Tony Mackce79de02013-09-24 10:12:33 -04009 provides=[Network]
10 requested_interval = 0
Sapan Bhatia24836f12013-08-27 10:16:05 -040011
Tony Mack66646d52013-09-24 21:47:12 -040012 def fetch_pending(self):
13 return Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
14
Tony Mackce79de02013-09-24 10:12:33 -040015 def save_network(self, network):
16 if not network.network_id:
17 if network.template.sharedNetworkName:
18 network.network_id = network.template.sharedNetworkId
19 (network.subnet_id, network.subnet) = self.driver.get_network_subnet(network.network_id)
20 else:
21 network_name = network.name
Sapan Bhatia24836f12013-08-27 10:16:05 -040022
Tony Mackce79de02013-09-24 10:12:33 -040023 # create network
24 os_network = self.driver.create_network(network_name, shared=True)
25 network.network_id = os_network['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040026
Tony Mackce79de02013-09-24 10:12:33 -040027 # create router
28 router = self.driver.create_router(network_name)
29 network.router_id = router['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040030
Tony Mackce79de02013-09-24 10:12:33 -040031 # create subnet
32 next_subnet = self.get_next_subnet()
33 cidr = str(next_subnet.cidr)
34 ip_version = next_subnet.version
35 start = str(next_subnet[2])
36 end = str(next_subnet[-2])
37 subnet = self.driver.create_subnet(name=network_name,
38 network_id = network.network_id,
39 cidr_ip = cidr,
40 ip_version = ip_version,
41 start = start,
42 end = end)
43 network.subnet = cidr
44 network.subnet_id = subnet['id']
Tony Mack387a73f2013-09-18 07:59:14 -040045 # add subnet as interface to slice's router
46 self.driver.add_router_interface(router['id'], subnet['id'])
47 # add external route
48 self.driver.add_external_route(subnet)
Sapan Bhatia24836f12013-08-27 10:16:05 -040049
Tony Mackce79de02013-09-24 10:12:33 -040050 def sync_record(self, site):
51 if network.owner and network.owner.creator:
Tony Mack66646d52013-09-24 21:47:12 -040052 try:
53 # update manager context
54 real_driver = self.driver
55 self.driver = self.driver.client_driver(network.owner.creator, network.owner.name)
56 self.save_network(network)
57 self.driver = real_driver
58 logger.info("saved network: %s" % (network))
59 except Exception,e:
60 logger.log_exc("save network failed: %s" % network)
61 raise e
Sapan Bhatia24836f12013-08-27 10:16:05 -040062