blob: 329bb57e1a121ccfd4794a89e7746309e96e52fe [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import os
2import base64
3import struct
4import socket
Scott Bakerb63ea792016-08-11 10:24:48 -07005from netaddr import IPAddress, IPNetwork
Scott Bakerb63ea792016-08-11 10:24:48 -07006from xos.config import Config
Scott Baker8b75e852016-08-16 15:04:59 -07007from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
Scott Bakeraf599eb2017-03-21 12:43:26 -07008from synchronizers.new_base.syncstep import *
Scott Bakerb63ea792016-08-11 10:24:48 -07009from xos.logger import observer_logger as logger
Scott Bakeraf599eb2017-03-21 12:43:26 -070010from synchronizers.new_base.ansible_helper import *
Scott Bakerb63ea792016-08-11 10:24:48 -070011from xos.config import Config
Scott Bakeraf599eb2017-03-21 12:43:26 -070012from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -070013
14class SyncControllerNetworks(OpenStackSyncStep):
15 requested_interval = 0
16 provides=[Network]
17 observes=ControllerNetwork
18 playbook='sync_controller_networks.yaml'
19
20 def alloc_subnet(self, uuid):
21 # 16 bits only
22 uuid_masked = uuid & 0xffff
23 a = 10
24 b = uuid_masked >> 8
25 c = uuid_masked & 0xff
26 d = 0
27
28 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
29 return cidr
30
31 def alloc_gateway(self, subnet):
32 # given a CIDR, allocate a default gateway using the .1 address within
33 # the subnet.
34 # 10.123.0.0/24 --> 10.123.0.1
35 # 207.141.192.128/28 --> 207.141.192.129
36 (network, bits) = subnet.split("/")
37 network=network.strip()
38 bits=int(bits.strip())
39 netmask = (~(pow(2,32-bits)-1) & 0xFFFFFFFF)
40 ip = struct.unpack("!L", socket.inet_aton(network))[0]
41 ip = ip & netmask | 1
42 return socket.inet_ntoa(struct.pack("!L", ip))
43
Scott Baker11cb69f2016-08-25 16:17:52 -070044 def get_segmentation_id(self, controller_network):
Scott Baker7bd1cd42016-08-29 15:30:45 -070045 driver = self.driver.admin_driver(controller = controller_network.controller)
Scott Baker11cb69f2016-08-25 16:17:52 -070046 neutron_network = driver.shell.neutron.list_networks(controller_network.network_id)["networks"][0]
Sapan Bhatia259205e2017-01-24 19:32:59 +010047 if "provider:segmentation_id" in neutron_network:
Scott Baker11cb69f2016-08-25 16:17:52 -070048 return neutron_network["provider:segmentation_id"]
49 else:
50 return None
51
Scott Bakerb63ea792016-08-11 10:24:48 -070052 def save_controller_network(self, controller_network):
53 network_name = controller_network.network.name
54 subnet_name = '%s-%d'%(network_name,controller_network.pk)
55 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 print "CIDR_MS", cidr
60 else:
61 cidr = self.alloc_subnet(controller_network.pk)
62 print "CIDR_AMS", cidr
63
64 if controller_network.network.start_ip and controller_network.network.start_ip.strip():
65 start_ip = controller_network.network.start_ip.strip()
66 else:
67 start_ip = None
68
69 if controller_network.network.end_ip and controller_network.network.end_ip.strip():
70 end_ip = controller_network.network.end_ip.strip()
71 else:
72 end_ip = None
73
74 self.cidr=cidr
75 slice = controller_network.network.owner
76
Scott Baker11cb69f2016-08-25 16:17:52 -070077 controller_network.gateway = self.alloc_gateway(cidr)
78
Scott Bakerb63ea792016-08-11 10:24:48 -070079 network_fields = {'endpoint':controller_network.controller.auth_url,
80 'endpoint_v3': controller_network.controller.auth_url_v3,
81 'admin_user':slice.creator.email,
82 'admin_password':slice.creator.remote_password,
83 'admin_project':slice.name,
84 'domain': controller_network.controller.domain,
85 'name':network_name,
86 'subnet_name':subnet_name,
87 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
88 'cidr':cidr,
Scott Baker11cb69f2016-08-25 16:17:52 -070089 'gateway': controller_network.gateway,
Scott Bakerb63ea792016-08-11 10:24:48 -070090 'start_ip':start_ip,
91 'end_ip':end_ip,
92 'use_vtn':getattr(Config(), "networking_use_vtn", False),
93 'delete':False
94 }
95 return network_fields
96
97 def map_sync_outputs(self, controller_network,res):
98 network_id = res[0]['network']['id']
99 subnet_id = res[1]['subnet']['id']
100 controller_network.net_id = network_id
101 controller_network.subnet = self.cidr
102 controller_network.subnet_id = subnet_id
103 controller_network.backend_status = '1 - OK'
Scott Baker11cb69f2016-08-25 16:17:52 -0700104 if not controller_network.segmentation_id:
Scott Baker75bae452017-03-27 20:10:58 -0700105 controller_network.segmentation_id = str(self.get_segmentation_id(controller_network))
Scott Bakerb63ea792016-08-11 10:24:48 -0700106 controller_network.save()
107
108
109 def map_sync_inputs(self, controller_network):
Scott Baker3de3d582016-09-09 15:26:43 -0700110 # make sure to not sync a shared network
Scott Bakerb63ea792016-08-11 10:24:48 -0700111 if (controller_network.network.template.shared_network_name or controller_network.network.template.shared_network_id):
112 return SyncStep.SYNC_WITHOUT_RUNNING
Scott Baker3de3d582016-09-09 15:26:43 -0700113
Scott Bakerb63ea792016-08-11 10:24:48 -0700114 if not controller_network.controller.admin_user:
115 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
116 return
117
118 if controller_network.network.owner and controller_network.network.owner.creator:
119 return self.save_controller_network(controller_network)
120 else:
121 raise Exception('Could not save network controller %s'%controller_network)
122
123 def map_delete_inputs(self, controller_network):
Scott Baker3de3d582016-09-09 15:26:43 -0700124 # make sure to not delete a shared network
125 if (controller_network.network.template.shared_network_name or controller_network.network.template.shared_network_id):
Scott Bakerb63ea792016-08-11 10:24:48 -0700126 return
Scott Baker3de3d582016-09-09 15:26:43 -0700127
Scott Bakerb63ea792016-08-11 10:24:48 -0700128 try:
129 slice = controller_network.network.owner # XXX: FIXME!!
130 except:
131 raise Exception('Could not get slice for Network %s'%controller_network.network.name)
132
133 network_name = controller_network.network.name
134 subnet_name = '%s-%d'%(network_name,controller_network.pk)
135 cidr = controller_network.subnet
136 network_fields = {'endpoint':controller_network.controller.auth_url,
137 'admin_user':slice.creator.email, # XXX: FIXME
Scott Bakerda1a2162016-12-11 23:45:04 -0800138 'admin_project':slice.name, # XXX: FIXME
Scott Bakerb63ea792016-08-11 10:24:48 -0700139 'admin_password':slice.creator.remote_password,
140 'name':network_name,
141 'subnet_name':subnet_name,
142 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
143 'cidr':cidr,
144 'delete':True
145 }
146
147 return network_fields
148