blob: 367440de8fc1e890a86275de4661c4487b5d5ff1 [file] [log] [blame]
Tony Mack6793de42014-04-08 16:36:52 -04001import os
2import base64
3from collections import defaultdict
4from netaddr import IPAddress, IPNetwork
5from django.db.models import F, Q
6from planetstack.config import Config
7from observer.openstacksyncstep import OpenStackSyncStep
8from core.models.network import *
9from core.models.slice import *
Scott Baker165e5ef2014-05-10 15:38:00 -070010from core.models.sliver import Sliver
Tony Mack6793de42014-04-08 16:36:52 -040011from util.logger import Logger, logging
12
13logger = Logger(level=logging.INFO)
14
15class SyncNetworkDeployments(OpenStackSyncStep):
Scott Bakerf3e71512014-08-12 18:05:04 -070016 requested_interval = 0
Scott Baker165e5ef2014-05-10 15:38:00 -070017 provides=[Network, NetworkDeployments, Sliver]
Scott Bakerf3e71512014-08-12 18:05:04 -070018
Sapan Bhatia881f62e2014-07-23 09:45:23 -040019 def fetch_pending(self, deleted):
20 if (deleted):
21 return NetworkDeployments.deleted_objects.all()
22 else:
23 # network deployments are not visible to users. We must ensure
Scott Bakerf3e71512014-08-12 18:05:04 -070024 # networks are deployed at all deploymets available to their slices.
Sapan Bhatia881f62e2014-07-23 09:45:23 -040025 slice_deployments = SliceDeployments.objects.all()
26 slice_deploy_lookup = defaultdict(list)
27 for slice_deployment in slice_deployments:
28 slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment)
Scott Bakerf3e71512014-08-12 18:05:04 -070029
Sapan Bhatia881f62e2014-07-23 09:45:23 -040030 network_deployments = NetworkDeployments.objects.all()
31 network_deploy_lookup = defaultdict(list)
32 for network_deployment in network_deployments:
33 network_deploy_lookup[network_deployment.network].append(network_deployment.deployment)
Tony Mack6793de42014-04-08 16:36:52 -040034
Sapan Bhatia881f62e2014-07-23 09:45:23 -040035 for network in Network.objects.filter():
Sapan Bhatia881f62e2014-07-23 09:45:23 -040036 expected_deployments = slice_deploy_lookup[network.owner]
37 for expected_deployment in expected_deployments:
38 if network not in network_deploy_lookup or \
39 expected_deployment not in network_deploy_lookup[network]:
40 nd = NetworkDeployments(network=network, deployment=expected_deployment)
41 nd.save()
42 return NetworkDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack6793de42014-04-08 16:36:52 -040043
44 def get_next_subnet(self, deployment=None):
45 # limit ourself to 10.0.x.x for now
46 valid_subnet = lambda net: net.startswith('10.0')
47 driver = self.driver.admin_driver(deployment=deployment)
48 subnets = driver.shell.quantum.list_subnets()['subnets']
49 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
50 if valid_subnet(subnet['cidr'])]
51 ints.sort()
52 if ints:
53 last_ip = IPAddress(ints[-1])
54 else:
Tony Mackb84fccc2014-04-12 10:28:52 -040055 last_ip = IPAddress('10.0.0.0')
Tony Mack6793de42014-04-08 16:36:52 -040056 last_network = IPNetwork(str(last_ip) + "/24")
57 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
58 return next_network
59
60 def save_network_deployment(self, network_deployment):
Scott Baker36f09c02014-08-14 17:33:36 -070061 if (not network_deployment.net_id) and network_deployment.network.template.sharedNetworkName:
Scott Bakerf3e71512014-08-12 18:05:04 -070062 # It's a shared network, try to find the shared network id
63
64 quantum_networks = self.driver.shell.quantum.list_networks(name=network_deployment.network.template.sharedNetworkName)["networks"]
65 if quantum_networks:
66 logger.info("set shared network id %s" % quantum_networks[0]["id"])
67 network_deployment.net_id = quantum_networks[0]["id"]
68 else:
69 logger.info("failed to find shared network id for deployment")
70 return
71
72 # At this point, it must be a private network, so create it if it does
73 # not exist.
Tony Mack6793de42014-04-08 16:36:52 -040074
75 if not network_deployment.net_id:
76 network_name = network_deployment.network.name
77
78 # create network
79 os_network = self.driver.create_network(network_name, shared=True)
80 network_deployment.net_id = os_network['id']
81
82 # create router
Tony Mackeb786e02014-04-18 15:32:45 -040083 #router = self.driver.create_router(network_name)
84 #network_deployment.router_id = router['id']
Tony Mack6793de42014-04-08 16:36:52 -040085
86 # create subnet
87 next_subnet = self.get_next_subnet(deployment=network_deployment.deployment.name)
88 cidr = str(next_subnet.cidr)
89 ip_version = next_subnet.version
90 start = str(next_subnet[2])
91 end = str(next_subnet[-2])
92 subnet = self.driver.create_subnet(name=network_name,
93 network_id = network_deployment.net_id,
94 cidr_ip = cidr,
95 ip_version = ip_version,
96 start = start,
97 end = end)
98 network_deployment.subnet = cidr
99 network_deployment.subnet_id = subnet['id']
100 # add subnet as interface to slice's router
Tony Mackeb786e02014-04-18 15:32:45 -0400101 #self.driver.add_router_interface(router['id'], subnet['id'])
Tony Mack6793de42014-04-08 16:36:52 -0400102 # add external route
Tony Mackeb786e02014-04-18 15:32:45 -0400103 #self.driver.add_external_route(subnet)
Tony Mack6793de42014-04-08 16:36:52 -0400104 logger.info("created private subnet (%s) for network: %s" % (cidr, network_deployment.network))
Scott Bakerf3e71512014-08-12 18:05:04 -0700105
106 # Now, figure out the subnet and subnet_id for the network. This works
107 # for both private and shared networks.
108
109 if (not network_deployment.subnet_id) or (not network_deployment.subnet):
Tony Mack6793de42014-04-08 16:36:52 -0400110 (network_deployment.subnet_id, network_deployment.subnet) = self.driver.get_network_subnet(network_deployment.net_id)
111 logger.info("sync'ed subnet (%s) for network: %s" % (network_deployment.subnet, network_deployment.network))
112
Scott Bakerf3e71512014-08-12 18:05:04 -0700113 if (not network_deployment.subnet):
114 # this will generate a non-null database constraint error
115 # ... which in turn leads to transaction errors
116 # it's probably caused by networks that no longer exist at the
117 # quantum level.
118
119 logger.info("null subnet for network %s, skipping save" % network_deployment.network)
120 return
121
Tony Mack6793de42014-04-08 16:36:52 -0400122 network_deployment.save()
123
124 def sync_record(self, network_deployment):
125 if network_deployment.network.owner and network_deployment.network.owner.creator:
126 try:
127 # update manager context
128 real_driver = self.driver
Scott Bakerf3e71512014-08-12 18:05:04 -0700129 self.driver = self.driver.client_driver(caller=network_deployment.network.owner.creator,
Tony Mack6793de42014-04-08 16:36:52 -0400130 tenant=network_deployment.network.owner.name,
131 deployment=network_deployment.deployment.name)
132 self.save_network_deployment(network_deployment)
133 self.driver = real_driver
134 logger.info("saved network deployment: %s" % (network_deployment))
135 except Exception,e:
136 logger.log_exc("save network deployment failed: %s" % network_deployment)
Scott Bakerf3e71512014-08-12 18:05:04 -0700137 raise e
138
139
140 def delete_record(self, network_deployment):
Sapan Bhatia881f62e2014-07-23 09:45:23 -0400141 driver = OpenStackDriver().client_driver(caller=network_deployment.network.owner.creator,
142 tenant=network_deployment.network.owner.name,
143 deployment=network_deployment.deployment.name)
144 if (network_deployment.router_id) and (network_deployment.subnet_id):
145 driver.delete_router_interface(network_deployment.router_id, network_deployment.subnet_id)
146 if network_deployment.subnet_id:
147 driver.delete_subnet(network_deployment.subnet_id)
148 if network_deployment.router_id:
149 driver.delete_router(network_deployment.router_id)
150 if network_deployment.net_id:
151 driver.delete_network(network_deployment.net_id)