blob: 242073d934bc9221bbd9e62aefed197ae4ec18c9 [file] [log] [blame]
Tony Mackf3bbe472014-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
Scott Baker76a840e2015-02-11 21:38:09 -08006from xos.config import Config
Tony Mackf3bbe472014-11-30 15:33:35 -05007from 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 Bhatia6c1cb842014-12-22 01:42:18 -050012from observer.ansible import *
Tony Mackf3bbe472014-11-30 15:33:35 -050013
Sapan Bhatia99f49682015-01-29 20:58:25 +000014import pdb
15
Tony Mackf3bbe472014-11-30 15:33:35 -050016logger = Logger(level=logging.INFO)
17
Tony Mackdd8746b2015-01-07 12:48:37 -050018class SyncControllerNetworks(OpenStackSyncStep):
Tony Mackf3bbe472014-11-30 15:33:35 -050019 requested_interval = 0
Sapan Bhatiab3048aa2015-01-27 03:52:19 +000020 provides=[Network]
Sapan Bhatia99f49682015-01-29 20:58:25 +000021 observes=ControllerNetwork
Tony Mackf3bbe472014-11-30 15:33:35 -050022
Sapan Bhatia189ed672014-12-19 13:21:30 -050023 def alloc_subnet(self, uuid):
Sapan Bhatiad99b1122015-01-23 16:22:12 +000024 # 16 bits only
25 uuid_masked = uuid & 0xffff
Sapan Bhatia189ed672014-12-19 13:21:30 -050026 a = 10
Sapan Bhatiad99b1122015-01-23 16:22:12 +000027 b = uuid_masked >> 8
28 c = uuid_masked & 0xff
29 d = 0
Sapan Bhatia189ed672014-12-19 13:21:30 -050030
Sapan Bhatiad99b1122015-01-23 16:22:12 +000031 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
32 return cidr
33
Sapan Bhatia189ed672014-12-19 13:21:30 -050034
Tony Mackf3bbe472014-11-30 15:33:35 -050035 def fetch_pending(self, deleted):
36 if (deleted):
Tony Macka7dbd422015-01-05 22:48:11 -050037 return ControllerNetwork.deleted_objects.all()
Tony Mackf3bbe472014-11-30 15:33:35 -050038 else:
Tony Macka7dbd422015-01-05 22:48:11 -050039 return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mackf3bbe472014-11-30 15:33:35 -050040
Tony Mackf3bbe472014-11-30 15:33:35 -050041
Tony Mack336e0f92014-11-30 15:53:08 -050042 def save_controller_network(self, controller_network):
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000043 network_name = controller_network.network.name
44 subnet_name = '%s-%d'%(network_name,controller_network.pk)
45 cidr = self.alloc_subnet(controller_network.pk)
46 slice = controller_network.network.slices.all()[0] # XXX: FIXME!!
Sapan Bhatia6c1cb842014-12-22 01:42:18 -050047
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000048 network_fields = {'endpoint':controller_network.controller.auth_url,
49 'admin_user':slice.creator.email, # XXX: FIXME
50 'tenant_name':slice.name, # XXX: FIXME
51 'admin_password':slice.creator.remote_password,
52 'name':network_name,
53 'subnet_name':subnet_name,
54 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
55 'cidr':cidr
56 }
Tony Mackf3bbe472014-11-30 15:33:35 -050057
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000058 res = run_template('sync_controller_networks.yaml', network_fields, path = 'controller_networks',expected_num=2)
Tony Mackf3bbe472014-11-30 15:33:35 -050059
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000060 network_id = res[0]['id']
61 subnet_id = res[1]['id']
62 controller_network.net_id = network_id
63 controller_network.subnet = cidr
64 controller_network.subnet_id = subnet_id
Sapan Bhatia5851db42015-01-27 03:52:43 +000065 controller_network.backend_status = '1 - OK'
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000066 controller_network.save()
Tony Mackf3bbe472014-11-30 15:33:35 -050067
Tony Mackf3bbe472014-11-30 15:33:35 -050068
Tony Mack336e0f92014-11-30 15:53:08 -050069 def sync_record(self, controller_network):
70 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 Mackf3bbe472014-11-30 15:33:35 -050071
Tony Mack336e0f92014-11-30 15:53:08 -050072 if not controller_network.controller.admin_user:
73 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
Tony Mackf3bbe472014-11-30 15:33:35 -050074 return
75
Tony Mack336e0f92014-11-30 15:53:08 -050076 if controller_network.network.owner and controller_network.network.owner.creator:
Sapan Bhatia650107c2015-01-23 16:22:27 +000077 self.save_controller_network(controller_network)
78 logger.info("saved network controller: %s" % (controller_network))
Tony Mackf3bbe472014-11-30 15:33:35 -050079
Tony Mack336e0f92014-11-30 15:53:08 -050080 def delete_record(self, controller_network):
81 driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator,
82 tenant=controller_network.network.owner.name,
83 controller=controller_network.controller.name)
84 if (controller_network.router_id) and (controller_network.subnet_id):
85 driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id)
86 if controller_network.subnet_id:
87 driver.delete_subnet(controller_network.subnet_id)
88 if controller_network.router_id:
89 driver.delete_router(controller_network.router_id)
90 if controller_network.net_id:
91 driver.delete_network(controller_network.net_id)