blob: fa1f43980579fde5c3b009de24d386dfbbf82ad4 [file] [log] [blame]
Tony Mack51c4a7d2014-11-30 15:33:35 -05001import os
2import base64
3from collections import defaultdict
4from netaddr import IPAddress, IPNetwork
5from django.db.models import F, Q
6from planetstack.config import Config
7from observer.openstacksyncstep import OpenStackSyncStep
8from core.models.network import *
9from core.models.slice import *
10from core.models.sliver import Sliver
11from util.logger import Logger, logging
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050012from observer.ansible import *
Tony Mack51c4a7d2014-11-30 15:33:35 -050013
14logger = Logger(level=logging.INFO)
15
Tony Mack3066a952015-01-05 22:48:11 -050016class SyncControllerNetwork(OpenStackSyncStep):
Tony Mack51c4a7d2014-11-30 15:33:35 -050017 requested_interval = 0
Tony Mack3066a952015-01-05 22:48:11 -050018 provides=[Network, ControllerNetwork, Sliver]
Tony Mack51c4a7d2014-11-30 15:33:35 -050019
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050020 def alloc_subnet(self, uuid):
21 a = 10
22 b = uuid >> 32
23 c = uuid & 0xffffffff
24 d = 0
25
26 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
27 return cidr
28
29
Tony Mack51c4a7d2014-11-30 15:33:35 -050030 def fetch_pending(self, deleted):
31 if (deleted):
Tony Mack3066a952015-01-05 22:48:11 -050032 return ControllerNetwork.deleted_objects.all()
Tony Mack51c4a7d2014-11-30 15:33:35 -050033 else:
Tony Mack3066a952015-01-05 22:48:11 -050034 return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack51c4a7d2014-11-30 15:33:35 -050035
Tony Mack51c4a7d2014-11-30 15:33:35 -050036
Tony Mack06c8e472014-11-30 15:53:08 -050037 def save_controller_network(self, controller_network):
Tony Mack06c8e472014-11-30 15:53:08 -050038 network_name = controller_network.network.name
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050039 subnet_name = '%s-%d'%(network_name,controller_network.pk)
40 cidr = self.alloc_subnet(controller_network.pk)
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050041 slice = controller_network.network.slices.all()[0] # XXX: FIXME!!
42
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050043 network_fields = {'endpoint':controller_network.controller.auth_url,
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050044 'admin_user':slice.creator.email, # XXX: FIXME
45 'tenant_name':slice.slicename, # XXX: FIXME
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050046 'admin_password':controller_network.controller.admin_password,
47 'name':network_name,
48 'subnet_name':subnet_name,
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050049 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050050 'cidr':cidr
51 }
Tony Mack51c4a7d2014-11-30 15:33:35 -050052
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050053 res = run_template('sync_controller_networks.yaml', network_fields, path = 'controller_networks')
Tony Mack51c4a7d2014-11-30 15:33:35 -050054
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050055 if (len(res)!=2):
56 raise Exception('Could not sync network %s'%controller_network.network.name)
57 else:
58 network_id = res[0]['id']
59 subnet_id = res[1]['id']
60 controller_network.net_id = network_id
61 controller_network.subnet = cidr
62 controller_network.subnet_id = subnet_id
63 controller_network.save()
Tony Mack51c4a7d2014-11-30 15:33:35 -050064
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050065 logger.info("sync'ed subnet (%s) for network: %s" % (controller_network.subnet, controller_network.network))
Tony Mack51c4a7d2014-11-30 15:33:35 -050066
Tony Mack51c4a7d2014-11-30 15:33:35 -050067
Tony Mack06c8e472014-11-30 15:53:08 -050068 def sync_record(self, controller_network):
69 logger.info("sync'ing network controller %s for network %s slice %s controller %s" % (controller_network, controller_network.network, str(controller_network.network.owner), controller_network.controller))
Tony Mack51c4a7d2014-11-30 15:33:35 -050070
Tony Mack06c8e472014-11-30 15:53:08 -050071 if not controller_network.controller.admin_user:
72 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
Tony Mack51c4a7d2014-11-30 15:33:35 -050073 return
74
Tony Mack06c8e472014-11-30 15:53:08 -050075 if controller_network.network.owner and controller_network.network.owner.creator:
Tony Mack51c4a7d2014-11-30 15:33:35 -050076 try:
77 # update manager context
78 # Bring back
Tony Mack06c8e472014-11-30 15:53:08 -050079 self.save_controller_network(controller_network)
80 logger.info("saved network controller: %s" % (controller_network))
Tony Mack51c4a7d2014-11-30 15:33:35 -050081 except Exception,e:
Tony Mack06c8e472014-11-30 15:53:08 -050082 logger.log_exc("save network controller failed: %s" % controller_network)
Tony Mack51c4a7d2014-11-30 15:33:35 -050083 raise e
84
85
Tony Mack06c8e472014-11-30 15:53:08 -050086 def delete_record(self, controller_network):
87 driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator,
88 tenant=controller_network.network.owner.name,
89 controller=controller_network.controller.name)
90 if (controller_network.router_id) and (controller_network.subnet_id):
91 driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id)
92 if controller_network.subnet_id:
93 driver.delete_subnet(controller_network.subnet_id)
94 if controller_network.router_id:
95 driver.delete_router(controller_network.router_id)
96 if controller_network.net_id:
97 driver.delete_network(controller_network.net_id)