blob: 990cb876a53cc2c7f1aad3fdbdcf6d6a3a9c479a [file] [log] [blame]
Sapan Bhatia037c9472016-01-14 11:44:43 -05001import os
2import base64
3from collections import defaultdict
4from netaddr import IPAddress, IPNetwork
5from django.db.models import F, Q
6from xos.config import Config
7from synchronizers.base.openstacksyncstep import OpenStackSyncStep
Sapan Bhatiaf0538b82016-01-15 11:05:52 -05008from synchronizers.base.syncstep import *
Sapan Bhatia037c9472016-01-14 11:44:43 -05009from core.models.network import *
10from core.models.slice import *
11from core.models.instance import Instance
Scott Baker3586c552016-01-14 15:30:20 -080012from xos.logger import observer_logger as logger
Sapan Bhatiaf0538b82016-01-15 11:05:52 -050013from synchronizers.base.ansible import *
Sapan Bhatia037c9472016-01-14 11:44:43 -050014from openstack.driver import OpenStackDriver
15from xos.config import Config
16import json
17
18import pdb
19
20class SyncControllerNetworks(OpenStackSyncStep):
21 requested_interval = 0
22 provides=[Network]
23 observes=ControllerNetwork
24 playbook='sync_controller_networks.yaml'
25
26 def alloc_subnet(self, uuid):
27 # 16 bits only
28 uuid_masked = uuid & 0xffff
29 a = 10
30 b = uuid_masked >> 8
31 c = uuid_masked & 0xff
32 d = 0
33
34 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
35 return cidr
36
37 def alloc_gateway(self, uuid):
38 # 16 bits only
39 uuid_masked = uuid & 0xffff
40 a = 10
41 b = uuid_masked >> 8
42 c = uuid_masked & 0xff
43 d = 1
44
45 gateway = '%d.%d.%d.%d'%(a,b,c,d)
46 return gateway
47
48
49 def save_controller_network(self, controller_network):
50 network_name = controller_network.network.name
51 subnet_name = '%s-%d'%(network_name,controller_network.pk)
52 cidr = self.alloc_subnet(controller_network.pk)
53 self.cidr=cidr
54 slice = controller_network.network.owner
55
56 network_fields = {'endpoint':controller_network.controller.auth_url,
57 'endpoint_v3': controller_network.controller.auth_url_v3,
58 'admin_user':slice.creator.email,
59 'tenant_name':slice.name,
60 'admin_password':slice.creator.remote_password,
61 'domain': controller_network.controller.domain,
62 'name':network_name,
63 'subnet_name':subnet_name,
64 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
65 'cidr':cidr,
66 'gateway':self.alloc_gateway(controller_network.pk),
67 'use_vtn':getattr(Config(), "networking_use_vtn", False),
68 'delete':False
69 }
70 return network_fields
71
72 def map_sync_outputs(self, controller_network,res):
73 network_id = res[0]['id']
74 subnet_id = res[1]['id']
75 controller_network.net_id = network_id
76 controller_network.subnet = self.cidr
77 controller_network.subnet_id = subnet_id
78 controller_network.backend_status = '1 - OK'
79 controller_network.save()
80
81
82 def map_sync_inputs(self, controller_network):
83 # XXX This check should really be made from booleans, rather than using hardcoded network names
84 if (controller_network.network.template.name not in ['Private', 'Private-Indirect', 'Private-Direct']):
85 logger.info("skipping network controller %s because it is not private" % controller_network)
86 # We only sync private networks
87 return SyncStep.SYNC_WITHOUT_RUNNING
88
89 if not controller_network.controller.admin_user:
90 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
91 return
92
93 if controller_network.network.owner and controller_network.network.owner.creator:
94 return self.save_controller_network(controller_network)
95 else:
96 raise Exception('Could not save network controller %s'%controller_network)
97
98 def map_delete_inputs(self, controller_network):
99 # XXX This check should really be made from booleans, rather than using hardcoded network names
100 if (controller_network.network.template.name not in ['Private', 'Private-Indirect', 'Private-Direct']):
101 # We only sync private networks
102 return
103 try:
104 slice = controller_network.network.owner # XXX: FIXME!!
105 except:
106 raise Exception('Could not get slice for Network %s'%controller_network.network.name)
107
108 network_name = controller_network.network.name
109 subnet_name = '%s-%d'%(network_name,controller_network.pk)
110 cidr = controller_network.subnet
111 network_fields = {'endpoint':controller_network.controller.auth_url,
112 'admin_user':slice.creator.email, # XXX: FIXME
113 'tenant_name':slice.name, # XXX: FIXME
114 'admin_password':slice.creator.remote_password,
115 'name':network_name,
116 'subnet_name':subnet_name,
117 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
118 'cidr':cidr,
119 'delete':True
120 }
121
122 return network_fields
123
124 """
125 driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator,
126 tenant=controller_network.network.owner.name,
127 controller=controller_network.controller.name)
128 if (controller_network.router_id) and (controller_network.subnet_id):
129 driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id)
130 if controller_network.subnet_id:
131 driver.delete_subnet(controller_network.subnet_id)
132 if controller_network.router_id:
133 driver.delete_router(controller_network.router_id)
134 if controller_network.net_id:
135 driver.delete_network(controller_network.net_id)
136 """