blob: 5375a8d96a0067939d8b5f32d393834b36f2db94 [file] [log] [blame]
Tony Mackf3bbe472014-11-30 15:33:35 -05001import os
2import base64
Scott Baker3ba6b5d2016-03-06 12:03:56 -08003import struct
4import socket
Tony Mackf3bbe472014-11-30 15:33:35 -05005from collections import defaultdict
6from netaddr import IPAddress, IPNetwork
7from django.db.models import F, Q
Scott Baker76a840e2015-02-11 21:38:09 -08008from xos.config import Config
Sapan Bhatia67ea0d72016-01-14 11:41:38 -05009from synchronizers.base.openstacksyncstep import OpenStackSyncStep
Sapan Bhatia003e84c2016-01-15 11:05:52 -050010from synchronizers.base.syncstep import *
Tony Mackf3bbe472014-11-30 15:33:35 -050011from core.models.network import *
12from core.models.slice import *
Tony Mack3de59e32015-08-19 11:58:18 -040013from core.models.instance import Instance
Scott Baker0ae266e2016-01-14 15:30:20 -080014from xos.logger import observer_logger as logger
Sapan Bhatia003e84c2016-01-15 11:05:52 -050015from synchronizers.base.ansible import *
S.Çağlar Onurad135a92015-02-18 09:59:55 -050016from openstack.driver import OpenStackDriver
Scott Baker287137c2016-01-04 22:50:28 -080017from xos.config import Config
Sapan Bhatia9028c9a2015-05-09 18:14:40 +020018import json
Tony Mackf3bbe472014-11-30 15:33:35 -050019
Sapan Bhatia99f49682015-01-29 20:58:25 +000020import pdb
21
Tony Mackdd8746b2015-01-07 12:48:37 -050022class SyncControllerNetworks(OpenStackSyncStep):
Tony Mackf3bbe472014-11-30 15:33:35 -050023 requested_interval = 0
Sapan Bhatiab3048aa2015-01-27 03:52:19 +000024 provides=[Network]
Sapan Bhatia99f49682015-01-29 20:58:25 +000025 observes=ControllerNetwork
Sapan Bhatia321b70e2015-08-19 12:20:47 -040026 playbook='sync_controller_networks.yaml'
Tony Mackf3bbe472014-11-30 15:33:35 -050027
Sapan Bhatia189ed672014-12-19 13:21:30 -050028 def alloc_subnet(self, uuid):
Sapan Bhatiad99b1122015-01-23 16:22:12 +000029 # 16 bits only
30 uuid_masked = uuid & 0xffff
Sapan Bhatia189ed672014-12-19 13:21:30 -050031 a = 10
Sapan Bhatiad99b1122015-01-23 16:22:12 +000032 b = uuid_masked >> 8
33 c = uuid_masked & 0xff
34 d = 0
Sapan Bhatia189ed672014-12-19 13:21:30 -050035
Sapan Bhatiad99b1122015-01-23 16:22:12 +000036 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
37 return cidr
38
Scott Baker49a1b4d2016-02-08 16:02:52 -080039 def alloc_gateway(self, subnet):
Scott Baker21dbaf62016-03-06 11:17:16 -080040 # given a CIDR, allocate a default gateway using the .1 address within
41 # the subnet.
42 # 10.123.0.0/24 --> 10.123.0.1
43 # 207.141.192.128/28 --> 207.141.192.129
44 (network, bits) = subnet.split("/")
45 network=network.strip()
46 bits=int(bits.strip())
47 netmask = (~(pow(2,32-bits)-1) & 0xFFFFFFFF)
48 ip = struct.unpack("!L", socket.inet_aton(network))[0]
49 ip = ip & netmask | 1
50 return socket.inet_ntoa(struct.pack("!L", ip))
Sapan Bhatia189ed672014-12-19 13:21:30 -050051
Tony Mack336e0f92014-11-30 15:53:08 -050052 def save_controller_network(self, controller_network):
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000053 network_name = controller_network.network.name
54 subnet_name = '%s-%d'%(network_name,controller_network.pk)
Scott Baker49a1b4d2016-02-08 16:02:52 -080055 if controller_network.subnet and controller_network.subnet.strip():
56 # If a subnet is already specified (pass in by the creator), then
57 # use that rather than auto-generating one.
58 cidr = controller_network.subnet.strip()
59 else:
60 cidr = self.alloc_subnet(controller_network.pk)
Sapan Bhatia70a3e142015-09-16 19:14:41 +020061 self.cidr=cidr
Scott Baker1ebe12d2015-08-21 16:12:33 -070062 slice = controller_network.network.owner
Sapan Bhatia6c1cb842014-12-22 01:42:18 -050063
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000064 network_fields = {'endpoint':controller_network.controller.auth_url,
Tony Mack09a2f072015-09-14 00:53:39 +000065 'endpoint_v3': controller_network.controller.auth_url_v3,
Scott Baker1ebe12d2015-08-21 16:12:33 -070066 'admin_user':slice.creator.email,
67 'tenant_name':slice.name,
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000068 'admin_password':slice.creator.remote_password,
Scott Baker4a20ce62015-09-21 20:24:45 -070069 'domain': controller_network.controller.domain,
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000070 'name':network_name,
71 'subnet_name':subnet_name,
72 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
Sapan Bhatia1b4394a2015-05-09 18:18:56 +020073 'cidr':cidr,
Scott Baker49a1b4d2016-02-08 16:02:52 -080074 'gateway':self.alloc_gateway(cidr),
Scott Baker287137c2016-01-04 22:50:28 -080075 'use_vtn':getattr(Config(), "networking_use_vtn", False),
Scott Baker49a1b4d2016-02-08 16:02:52 -080076 'delete':False
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000077 }
Sapan Bhatia321b70e2015-08-19 12:20:47 -040078 return network_fields
Tony Mackf3bbe472014-11-30 15:33:35 -050079
Sapan Bhatia321b70e2015-08-19 12:20:47 -040080 def map_sync_outputs(self, controller_network,res):
Tony Mack3944db22015-09-30 18:40:16 +000081 network_id = res[0]['id']
82 subnet_id = res[1]['id']
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000083 controller_network.net_id = network_id
Sapan Bhatia70a3e142015-09-16 19:14:41 +020084 controller_network.subnet = self.cidr
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000085 controller_network.subnet_id = subnet_id
Sapan Bhatia5851db42015-01-27 03:52:43 +000086 controller_network.backend_status = '1 - OK'
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000087 controller_network.save()
Tony Mackf3bbe472014-11-30 15:33:35 -050088
Tony Mackf3bbe472014-11-30 15:33:35 -050089
Sapan Bhatia321b70e2015-08-19 12:20:47 -040090 def map_sync_inputs(self, controller_network):
Scott Baker3aaa76b2015-12-09 22:55:19 -080091 # XXX This check should really be made from booleans, rather than using hardcoded network names
Scott Bakerbb650ed2016-02-03 18:30:08 -080092 #if (controller_network.network.template.name not in ['Private', 'Private-Indirect', 'Private-Direct', 'management_template'):
93 # logger.info("skipping network controller %s because it is not private" % controller_network)
94 # # We only sync private networks
95 # return SyncStep.SYNC_WITHOUT_RUNNING
96
97 # hopefully a better approach than above
98 if (controller_network.network.template.shared_network_name or controller_network.network.template.shared_network_id):
Scott Baker1a36d662015-10-12 18:28:00 -070099 return SyncStep.SYNC_WITHOUT_RUNNING
Sapan Bhatiadb631b22015-05-27 17:23:58 +0200100
Tony Mack336e0f92014-11-30 15:53:08 -0500101 if not controller_network.controller.admin_user:
102 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
Tony Mackf3bbe472014-11-30 15:33:35 -0500103 return
104
Tony Mack336e0f92014-11-30 15:53:08 -0500105 if controller_network.network.owner and controller_network.network.owner.creator:
Sapan Bhatia321b70e2015-08-19 12:20:47 -0400106 return self.save_controller_network(controller_network)
107 else:
108 raise Exception('Could not save network controller %s'%controller_network)
Tony Mackf3bbe472014-11-30 15:33:35 -0500109
Sapan Bhatia321b70e2015-08-19 12:20:47 -0400110 def map_delete_inputs(self, controller_network):
Scott Baker3aaa76b2015-12-09 22:55:19 -0800111 # XXX This check should really be made from booleans, rather than using hardcoded network names
112 if (controller_network.network.template.name not in ['Private', 'Private-Indirect', 'Private-Direct']):
Sapan Bhatiadb631b22015-05-27 17:23:58 +0200113 # We only sync private networks
114 return
Sapan Bhatia53a0e9b2015-05-09 18:16:24 +0200115 try:
116 slice = controller_network.network.owner # XXX: FIXME!!
117 except:
118 raise Exception('Could not get slice for Network %s'%controller_network.network.name)
119
120 network_name = controller_network.network.name
121 subnet_name = '%s-%d'%(network_name,controller_network.pk)
122 cidr = controller_network.subnet
123 network_fields = {'endpoint':controller_network.controller.auth_url,
124 'admin_user':slice.creator.email, # XXX: FIXME
125 'tenant_name':slice.name, # XXX: FIXME
126 'admin_password':slice.creator.remote_password,
127 'name':network_name,
128 'subnet_name':subnet_name,
129 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
130 'cidr':cidr,
131 'delete':True
132 }
133
Sapan Bhatia321b70e2015-08-19 12:20:47 -0400134 return network_fields
Sapan Bhatia53a0e9b2015-05-09 18:16:24 +0200135
136 """
Tony Mack336e0f92014-11-30 15:53:08 -0500137 driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator,
138 tenant=controller_network.network.owner.name,
139 controller=controller_network.controller.name)
140 if (controller_network.router_id) and (controller_network.subnet_id):
141 driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id)
142 if controller_network.subnet_id:
143 driver.delete_subnet(controller_network.subnet_id)
144 if controller_network.router_id:
145 driver.delete_router(controller_network.router_id)
146 if controller_network.net_id:
147 driver.delete_network(controller_network.net_id)
Sapan Bhatia53a0e9b2015-05-09 18:16:24 +0200148 """