blob: 47a50a09e1ec8bb4dbef172f2d0c059e3a5dea19 [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 Mack80c42542015-01-07 12:48:37 -050016class SyncControllerNetworks(OpenStackSyncStep):
Tony Mack51c4a7d2014-11-30 15:33:35 -050017 requested_interval = 0
Sapan Bhatia75d1d112015-01-16 22:14:16 +000018 provides=[ControllerNetwork, Network]
Tony Mack51c4a7d2014-11-30 15:33:35 -050019
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050020 def alloc_subnet(self, uuid):
Sapan Bhatiaf1705c12015-01-23 16:22:12 +000021 # 16 bits only
22 uuid_masked = uuid & 0xffff
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050023 a = 10
Sapan Bhatiaf1705c12015-01-23 16:22:12 +000024 b = uuid_masked >> 8
25 c = uuid_masked & 0xff
26 d = 0
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050027
Sapan Bhatiaf1705c12015-01-23 16:22:12 +000028 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
29 return cidr
30
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050031
Tony Mack51c4a7d2014-11-30 15:33:35 -050032 def fetch_pending(self, deleted):
33 if (deleted):
Tony Mack3066a952015-01-05 22:48:11 -050034 return ControllerNetwork.deleted_objects.all()
Tony Mack51c4a7d2014-11-30 15:33:35 -050035 else:
Tony Mack3066a952015-01-05 22:48:11 -050036 return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack51c4a7d2014-11-30 15:33:35 -050037
Tony Mack51c4a7d2014-11-30 15:33:35 -050038
Tony Mack06c8e472014-11-30 15:53:08 -050039 def save_controller_network(self, controller_network):
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000040 network_name = controller_network.network.name
41 subnet_name = '%s-%d'%(network_name,controller_network.pk)
42 cidr = self.alloc_subnet(controller_network.pk)
43 slice = controller_network.network.slices.all()[0] # XXX: FIXME!!
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050044
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000045 network_fields = {'endpoint':controller_network.controller.auth_url,
46 'admin_user':slice.creator.email, # XXX: FIXME
47 'tenant_name':slice.name, # XXX: FIXME
48 'admin_password':slice.creator.remote_password,
49 'name':network_name,
50 'subnet_name':subnet_name,
51 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
52 'cidr':cidr
53 }
Tony Mack51c4a7d2014-11-30 15:33:35 -050054
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000055 res = run_template('sync_controller_networks.yaml', network_fields, path = 'controller_networks',expected_num=2)
Tony Mack51c4a7d2014-11-30 15:33:35 -050056
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000057 network_id = res[0]['id']
58 subnet_id = res[1]['id']
59 controller_network.net_id = network_id
60 controller_network.subnet = cidr
61 controller_network.subnet_id = subnet_id
62 controller_network.save()
Tony Mack51c4a7d2014-11-30 15:33:35 -050063
Tony Mack51c4a7d2014-11-30 15:33:35 -050064
Tony Mack06c8e472014-11-30 15:53:08 -050065 def sync_record(self, controller_network):
66 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 -050067
Tony Mack06c8e472014-11-30 15:53:08 -050068 if not controller_network.controller.admin_user:
69 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
Tony Mack51c4a7d2014-11-30 15:33:35 -050070 return
71
Tony Mack06c8e472014-11-30 15:53:08 -050072 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)