Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from collections import defaultdict |
| 4 | from netaddr import IPAddress, IPNetwork |
| 5 | from django.db.models import F, Q |
| 6 | from planetstack.config import Config |
| 7 | from observer.openstacksyncstep import OpenStackSyncStep |
| 8 | from core.models.network import * |
| 9 | from core.models.slice import * |
| 10 | from core.models.sliver import Sliver |
| 11 | from util.logger import Logger, logging |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 12 | from observer.ansible import * |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 13 | |
| 14 | logger = Logger(level=logging.INFO) |
| 15 | |
Tony Mack | dd8746b | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 16 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 17 | requested_interval = 0 |
Sapan Bhatia | a4bc78b | 2015-01-16 22:14:16 +0000 | [diff] [blame] | 18 | provides=[ControllerNetwork, Network] |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 19 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 20 | def alloc_subnet(self, uuid): |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 21 | # 16 bits only |
| 22 | uuid_masked = uuid & 0xffff |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 23 | a = 10 |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 24 | b = uuid_masked >> 8 |
| 25 | c = uuid_masked & 0xff |
| 26 | d = 0 |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 27 | |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 28 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 29 | return cidr |
| 30 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 31 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 32 | def fetch_pending(self, deleted): |
| 33 | if (deleted): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 34 | return ControllerNetwork.deleted_objects.all() |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 35 | else: |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 36 | return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 37 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 38 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 39 | def save_controller_network(self, controller_network): |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 40 | 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 Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 44 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 45 | 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 Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 54 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 55 | res = run_template('sync_controller_networks.yaml', network_fields, path = 'controller_networks',expected_num=2) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 56 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 57 | 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 Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 63 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 64 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 65 | 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 Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 67 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 68 | if not controller_network.controller.admin_user: |
| 69 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 70 | return |
| 71 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 72 | if controller_network.network.owner and controller_network.network.owner.creator: |
Sapan Bhatia | 650107c | 2015-01-23 16:22:27 +0000 | [diff] [blame] | 73 | self.save_controller_network(controller_network) |
| 74 | logger.info("saved network controller: %s" % (controller_network)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 75 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 76 | def delete_record(self, controller_network): |
| 77 | driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator, |
| 78 | tenant=controller_network.network.owner.name, |
| 79 | controller=controller_network.controller.name) |
| 80 | if (controller_network.router_id) and (controller_network.subnet_id): |
| 81 | driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id) |
| 82 | if controller_network.subnet_id: |
| 83 | driver.delete_subnet(controller_network.subnet_id) |
| 84 | if controller_network.router_id: |
| 85 | driver.delete_router(controller_network.router_id) |
| 86 | if controller_network.net_id: |
| 87 | driver.delete_network(controller_network.net_id) |