blob: 04f3270a0175f1c96c0652eedb37fbc88479e927 [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):
Tony Mack490f6fa2014-04-10 12:20:33 -040016 requested_interval = 0
Scott Baker165e5ef2014-05-10 15:38:00 -070017 provides=[Network, NetworkDeployments, Sliver]
Tony Mack6793de42014-04-08 16:36:52 -040018
19 def fetch_pending(self):
20 # network deployments are not visible to users. We must ensure
21 # networks are deployed at all deploymets available to their slices.
22 slice_deployments = SliceDeployments.objects.all()
23 slice_deploy_lookup = defaultdict(list)
24 for slice_deployment in slice_deployments:
25 slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment)
26
27 network_deployments = NetworkDeployments.objects.all()
28 network_deploy_lookup = defaultdict(list)
29 for network_deployment in network_deployments:
30 network_deploy_lookup[network_deployment.network].append(network_deployment.deployment)
31
32 for network in Network.objects.filter():
Tony Mack3a50a6c2014-04-16 07:49:15 -040033 # ignore networks that have
Scott Baker165e5ef2014-05-10 15:38:00 -070034 # template.visibility = private and translation = none
Tony Mack078f6682014-04-17 21:57:16 -040035 if network.template.visibility == 'private' and not network.template.translation == 'none':
Scott Baker165e5ef2014-05-10 15:38:00 -070036 continue
Tony Mack6793de42014-04-08 16:36:52 -040037 expected_deployments = slice_deploy_lookup[network.owner]
38 for expected_deployment in expected_deployments:
39 if network not in network_deploy_lookup or \
40 expected_deployment not in network_deploy_lookup[network]:
41 nd = NetworkDeployments(network=network, deployment=expected_deployment)
42 nd.save()
43 return NetworkDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
44
45 def get_next_subnet(self, deployment=None):
46 # limit ourself to 10.0.x.x for now
47 valid_subnet = lambda net: net.startswith('10.0')
48 driver = self.driver.admin_driver(deployment=deployment)
49 subnets = driver.shell.quantum.list_subnets()['subnets']
50 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
51 if valid_subnet(subnet['cidr'])]
52 ints.sort()
53 if ints:
54 last_ip = IPAddress(ints[-1])
55 else:
Tony Mackb84fccc2014-04-12 10:28:52 -040056 last_ip = IPAddress('10.0.0.0')
Tony Mack6793de42014-04-08 16:36:52 -040057 last_network = IPNetwork(str(last_ip) + "/24")
58 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
59 return next_network
60
61 def save_network_deployment(self, network_deployment):
62 if not network_deployment.network_id and network_deployment.network.template.sharedNetworkName:
63 network_deployment.network_id = network_deployment.network.template.sharedNetworkId
64
65 if not network_deployment.net_id:
66 network_name = network_deployment.network.name
67
68 # create network
69 os_network = self.driver.create_network(network_name, shared=True)
70 network_deployment.net_id = os_network['id']
71
72 # create router
Tony Mackeb786e02014-04-18 15:32:45 -040073 #router = self.driver.create_router(network_name)
74 #network_deployment.router_id = router['id']
Tony Mack6793de42014-04-08 16:36:52 -040075
76 # create subnet
77 next_subnet = self.get_next_subnet(deployment=network_deployment.deployment.name)
78 cidr = str(next_subnet.cidr)
79 ip_version = next_subnet.version
80 start = str(next_subnet[2])
81 end = str(next_subnet[-2])
82 subnet = self.driver.create_subnet(name=network_name,
83 network_id = network_deployment.net_id,
84 cidr_ip = cidr,
85 ip_version = ip_version,
86 start = start,
87 end = end)
88 network_deployment.subnet = cidr
89 network_deployment.subnet_id = subnet['id']
90 # add subnet as interface to slice's router
Tony Mackeb786e02014-04-18 15:32:45 -040091 #self.driver.add_router_interface(router['id'], subnet['id'])
Tony Mack6793de42014-04-08 16:36:52 -040092 # add external route
Tony Mackeb786e02014-04-18 15:32:45 -040093 #self.driver.add_external_route(subnet)
Tony Mack6793de42014-04-08 16:36:52 -040094 logger.info("created private subnet (%s) for network: %s" % (cidr, network_deployment.network))
95 else:
96 (network_deployment.subnet_id, network_deployment.subnet) = self.driver.get_network_subnet(network_deployment.net_id)
97 logger.info("sync'ed subnet (%s) for network: %s" % (network_deployment.subnet, network_deployment.network))
98
99 network_deployment.save()
100
101 def sync_record(self, network_deployment):
102 if network_deployment.network.owner and network_deployment.network.owner.creator:
103 try:
104 # update manager context
105 real_driver = self.driver
106 self.driver = self.driver.client_driver(caller=network_deployment.network.owner.creator,
107 tenant=network_deployment.network.owner.name,
108 deployment=network_deployment.deployment.name)
109 self.save_network_deployment(network_deployment)
110 self.driver = real_driver
111 logger.info("saved network deployment: %s" % (network_deployment))
112 except Exception,e:
113 logger.log_exc("save network deployment failed: %s" % network_deployment)
114 raise e
115
116
117