blob: 43769a77b3279be357a45232e40d1920590cad5d [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
12
13logger = Logger(level=logging.INFO)
14
Tony Mack06c8e472014-11-30 15:53:08 -050015class SyncControllerNetworks(OpenStackSyncStep):
Tony Mack51c4a7d2014-11-30 15:33:35 -050016 requested_interval = 0
Tony Mack06c8e472014-11-30 15:53:08 -050017 provides=[Network, ControllerNetworks, Sliver]
Tony Mack51c4a7d2014-11-30 15:33:35 -050018
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050019 def alloc_subnet(self, uuid):
20 a = 10
21 b = uuid >> 32
22 c = uuid & 0xffffffff
23 d = 0
24
25 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
26 return cidr
27
28
Tony Mack51c4a7d2014-11-30 15:33:35 -050029 def fetch_pending(self, deleted):
30 if (deleted):
Tony Mack06c8e472014-11-30 15:53:08 -050031 return ControllerNetworks.deleted_objects.all()
Tony Mack51c4a7d2014-11-30 15:33:35 -050032 else:
Tony Mack06c8e472014-11-30 15:53:08 -050033 return ControllerNetworks.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack51c4a7d2014-11-30 15:33:35 -050034
Tony Mack51c4a7d2014-11-30 15:33:35 -050035
Tony Mack06c8e472014-11-30 15:53:08 -050036 def save_controller_network(self, controller_network):
Tony Mack06c8e472014-11-30 15:53:08 -050037 network_name = controller_network.network.name
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050038 subnet_name = '%s-%d'%(network_name,controller_network.pk)
39 cidr = self.alloc_subnet(controller_network.pk)
40 network_fields = {'endpoint':controller_network.controller.auth_url,
41 'admin_user':controller_network.network.slice.creator.email,
42 'tenant_name':controller_network.network.slice.slicename,
43 'admin_password':controller_network.controller.admin_password,
44 'name':network_name,
45 'subnet_name':subnet_name,
46 'cidr':cidr
47 }
Tony Mack51c4a7d2014-11-30 15:33:35 -050048
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050049 res = run_template('sync_controller_networks.yaml', network_fields)
Tony Mack51c4a7d2014-11-30 15:33:35 -050050
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050051 if (len(res)!=2):
52 raise Exception('Could not sync network %s'%controller_network.network.name)
53 else:
54 network_id = res[0]['id']
55 subnet_id = res[1]['id']
56 controller_network.net_id = network_id
57 controller_network.subnet = cidr
58 controller_network.subnet_id = subnet_id
59 controller_network.save()
Tony Mack51c4a7d2014-11-30 15:33:35 -050060
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050061 logger.info("sync'ed subnet (%s) for network: %s" % (controller_network.subnet, controller_network.network))
Tony Mack51c4a7d2014-11-30 15:33:35 -050062
Tony Mack51c4a7d2014-11-30 15:33:35 -050063
Tony Mack06c8e472014-11-30 15:53:08 -050064 def sync_record(self, controller_network):
65 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 -050066
Tony Mack06c8e472014-11-30 15:53:08 -050067 if not controller_network.controller.admin_user:
68 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
Tony Mack51c4a7d2014-11-30 15:33:35 -050069 return
70
Tony Mack06c8e472014-11-30 15:53:08 -050071 self.driver = self.driver.admin_driver(controller=controller_network.controller,tenant='admin')
72 if controller_network.network.owner and controller_network.network.owner.creator:
Tony Mack51c4a7d2014-11-30 15:33:35 -050073 try:
74 # update manager context
75 # Bring back
Tony Mack06c8e472014-11-30 15:53:08 -050076 self.save_controller_network(controller_network)
77 logger.info("saved network controller: %s" % (controller_network))
Tony Mack51c4a7d2014-11-30 15:33:35 -050078 except Exception,e:
Tony Mack06c8e472014-11-30 15:53:08 -050079 logger.log_exc("save network controller failed: %s" % controller_network)
Tony Mack51c4a7d2014-11-30 15:33:35 -050080 raise e
81
82
Tony Mack06c8e472014-11-30 15:53:08 -050083 def delete_record(self, controller_network):
84 driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator,
85 tenant=controller_network.network.owner.name,
86 controller=controller_network.controller.name)
87 if (controller_network.router_id) and (controller_network.subnet_id):
88 driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id)
89 if controller_network.subnet_id:
90 driver.delete_subnet(controller_network.subnet_id)
91 if controller_network.router_id:
92 driver.delete_router(controller_network.router_id)
93 if controller_network.net_id:
94 driver.delete_network(controller_network.net_id)