Tony Mack | 51c4a7d | 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 | dedc41e | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 12 | from observer.ansible import * |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 13 | |
Sapan Bhatia | 39a775f | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 14 | import pdb |
| 15 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 16 | logger = Logger(level=logging.INFO) |
| 17 | |
Tony Mack | 80c4254 | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 18 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 19 | requested_interval = 0 |
Sapan Bhatia | 5d662c7 | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 20 | provides=[Network] |
Sapan Bhatia | 39a775f | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 21 | observes=ControllerNetwork |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 22 | |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 23 | def alloc_subnet(self, uuid): |
Sapan Bhatia | f1705c1 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 24 | # 16 bits only |
| 25 | uuid_masked = uuid & 0xffff |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 26 | a = 10 |
Sapan Bhatia | f1705c1 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 27 | b = uuid_masked >> 8 |
| 28 | c = uuid_masked & 0xff |
| 29 | d = 0 |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 30 | |
Sapan Bhatia | f1705c1 | 2015-01-23 16:22:12 +0000 | [diff] [blame] | 31 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 32 | return cidr |
| 33 | |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 34 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 35 | def fetch_pending(self, deleted): |
| 36 | if (deleted): |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 37 | return ControllerNetwork.deleted_objects.all() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 38 | else: |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 39 | return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 40 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 41 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 42 | def save_controller_network(self, controller_network): |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 43 | 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 Bhatia | dedc41e | 2014-12-22 01:42:18 -0500 | [diff] [blame] | 47 | |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 48 | 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 Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 57 | |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 58 | res = run_template('sync_controller_networks.yaml', network_fields, path = 'controller_networks',expected_num=2) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 59 | |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 60 | 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 Bhatia | c88c9a8 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 65 | controller_network.backend_status = '1 - OK' |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 66 | controller_network.save() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 67 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 68 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 69 | 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 Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 71 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 72 | if not controller_network.controller.admin_user: |
| 73 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 74 | return |
| 75 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 76 | if controller_network.network.owner and controller_network.network.owner.creator: |
Sapan Bhatia | 7b4cee7 | 2015-01-23 16:22:27 +0000 | [diff] [blame] | 77 | self.save_controller_network(controller_network) |
| 78 | logger.info("saved network controller: %s" % (controller_network)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 79 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 80 | 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) |