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 |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 6 | from xos.config import Config |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 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 * |
S.Çağlar Onur | ad135a9 | 2015-02-18 09:59:55 -0500 | [diff] [blame^] | 13 | from openstack.driver import OpenStackDriver |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 14 | |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 15 | import pdb |
| 16 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 17 | logger = Logger(level=logging.INFO) |
| 18 | |
Tony Mack | dd8746b | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 19 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 20 | requested_interval = 0 |
Sapan Bhatia | b3048aa | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 21 | provides=[Network] |
Sapan Bhatia | 99f4968 | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 22 | observes=ControllerNetwork |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 23 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 24 | def alloc_subnet(self, uuid): |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 25 | # 16 bits only |
| 26 | uuid_masked = uuid & 0xffff |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 27 | a = 10 |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 28 | b = uuid_masked >> 8 |
| 29 | c = uuid_masked & 0xff |
| 30 | d = 0 |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 31 | |
Sapan Bhatia | d99b112 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 32 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 33 | return cidr |
| 34 | |
Sapan Bhatia | 189ed67 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 35 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 36 | def fetch_pending(self, deleted): |
| 37 | if (deleted): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 38 | return ControllerNetwork.deleted_objects.all() |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 39 | else: |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 40 | return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 41 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 42 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 43 | def save_controller_network(self, controller_network): |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 44 | network_name = controller_network.network.name |
| 45 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 46 | cidr = self.alloc_subnet(controller_network.pk) |
| 47 | slice = controller_network.network.slices.all()[0] # XXX: FIXME!! |
Sapan Bhatia | 6c1cb84 | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 48 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 49 | network_fields = {'endpoint':controller_network.controller.auth_url, |
| 50 | 'admin_user':slice.creator.email, # XXX: FIXME |
| 51 | 'tenant_name':slice.name, # XXX: FIXME |
| 52 | 'admin_password':slice.creator.remote_password, |
| 53 | 'name':network_name, |
| 54 | 'subnet_name':subnet_name, |
| 55 | 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name), |
| 56 | 'cidr':cidr |
| 57 | } |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 58 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 59 | 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] | 60 | |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 61 | network_id = res[0]['id'] |
| 62 | subnet_id = res[1]['id'] |
| 63 | controller_network.net_id = network_id |
| 64 | controller_network.subnet = cidr |
| 65 | controller_network.subnet_id = subnet_id |
Sapan Bhatia | 5851db4 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 66 | controller_network.backend_status = '1 - OK' |
Sapan Bhatia | b0464ba | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 67 | controller_network.save() |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 68 | |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 69 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 70 | def sync_record(self, controller_network): |
| 71 | 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] | 72 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 73 | if not controller_network.controller.admin_user: |
| 74 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 75 | return |
| 76 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 77 | if controller_network.network.owner and controller_network.network.owner.creator: |
Sapan Bhatia | 650107c | 2015-01-23 16:22:27 +0000 | [diff] [blame] | 78 | self.save_controller_network(controller_network) |
| 79 | logger.info("saved network controller: %s" % (controller_network)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 80 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 81 | def delete_record(self, controller_network): |
| 82 | driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator, |
| 83 | tenant=controller_network.network.owner.name, |
| 84 | controller=controller_network.controller.name) |
| 85 | if (controller_network.router_id) and (controller_network.subnet_id): |
| 86 | driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id) |
| 87 | if controller_network.subnet_id: |
| 88 | driver.delete_subnet(controller_network.subnet_id) |
| 89 | if controller_network.router_id: |
| 90 | driver.delete_router(controller_network.router_id) |
| 91 | if controller_network.net_id: |
| 92 | driver.delete_network(controller_network.net_id) |