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 |
| 12 | |
| 13 | logger = Logger(level=logging.INFO) |
| 14 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 15 | class SyncControllerNetworks(OpenStackSyncStep): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 16 | requested_interval = 0 |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 17 | provides=[Network, ControllerNetworks, Sliver] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 18 | |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 19 | def alloc_subnet(self, uuid): |
| 20 | a = 10 |
| 21 | b = uuid >> 32 |
| 22 | c = uuid & 0xffffffff |
| 23 | d = 0 |
| 24 | |
| 25 | cidr = '%d.%d.%d.%d/24'%(a,b,c,d) |
| 26 | return cidr |
| 27 | |
| 28 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 29 | def fetch_pending(self, deleted): |
| 30 | if (deleted): |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 31 | return ControllerNetworks.deleted_objects.all() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 32 | else: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 33 | return ControllerNetworks.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 34 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 35 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 36 | def save_controller_network(self, controller_network): |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 37 | network_name = controller_network.network.name |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 38 | subnet_name = '%s-%d'%(network_name,controller_network.pk) |
| 39 | cidr = self.alloc_subnet(controller_network.pk) |
| 40 | network_fields = {'endpoint':controller_network.controller.auth_url, |
| 41 | 'admin_user':controller_network.network.slice.creator.email, |
| 42 | 'tenant_name':controller_network.network.slice.slicename, |
| 43 | 'admin_password':controller_network.controller.admin_password, |
| 44 | 'name':network_name, |
| 45 | 'subnet_name':subnet_name, |
| 46 | 'cidr':cidr |
| 47 | } |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 48 | |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 49 | res = run_template('sync_controller_networks.yaml', network_fields) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 50 | |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 51 | if (len(res)!=2): |
| 52 | raise Exception('Could not sync network %s'%controller_network.network.name) |
| 53 | else: |
| 54 | network_id = res[0]['id'] |
| 55 | subnet_id = res[1]['id'] |
| 56 | controller_network.net_id = network_id |
| 57 | controller_network.subnet = cidr |
| 58 | controller_network.subnet_id = subnet_id |
| 59 | controller_network.save() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 60 | |
Sapan Bhatia | bc13cb7 | 2014-12-19 13:21:30 -0500 | [diff] [blame] | 61 | logger.info("sync'ed subnet (%s) for network: %s" % (controller_network.subnet, controller_network.network)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 62 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 63 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 64 | def sync_record(self, controller_network): |
| 65 | 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] | 66 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 67 | if not controller_network.controller.admin_user: |
| 68 | logger.info("controller %r has no admin_user, skipping" % controller_network.controller) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 69 | return |
| 70 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 71 | self.driver = self.driver.admin_driver(controller=controller_network.controller,tenant='admin') |
| 72 | if controller_network.network.owner and controller_network.network.owner.creator: |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 73 | try: |
| 74 | # update manager context |
| 75 | # Bring back |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 76 | self.save_controller_network(controller_network) |
| 77 | logger.info("saved network controller: %s" % (controller_network)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 78 | except Exception,e: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 79 | logger.log_exc("save network controller failed: %s" % controller_network) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 80 | raise e |
| 81 | |
| 82 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 83 | 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) |