blob: a8d6262a97e3573c2e3656ddb3ee69d99efd9f1c [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 *
10from util.logger import Logger, logging
11
12logger = Logger(level=logging.INFO)
13
14class SyncNetworkDeployments(OpenStackSyncStep):
Tony Mack490f6fa2014-04-10 12:20:33 -040015 requested_interval = 0
Tony Mack6793de42014-04-08 16:36:52 -040016 provides=[NetworkDeployments]
Tony Mack6793de42014-04-08 16:36:52 -040017
18 def fetch_pending(self):
19 # network deployments are not visible to users. We must ensure
20 # networks are deployed at all deploymets available to their slices.
21 slice_deployments = SliceDeployments.objects.all()
22 slice_deploy_lookup = defaultdict(list)
23 for slice_deployment in slice_deployments:
24 slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment)
25
26 network_deployments = NetworkDeployments.objects.all()
27 network_deploy_lookup = defaultdict(list)
28 for network_deployment in network_deployments:
29 network_deploy_lookup[network_deployment.network].append(network_deployment.deployment)
30
31 for network in Network.objects.filter():
32 expected_deployments = slice_deploy_lookup[network.owner]
33 for expected_deployment in expected_deployments:
34 if network not in network_deploy_lookup or \
35 expected_deployment not in network_deploy_lookup[network]:
36 nd = NetworkDeployments(network=network, deployment=expected_deployment)
37 nd.save()
38 return NetworkDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
39
40 def get_next_subnet(self, deployment=None):
41 # limit ourself to 10.0.x.x for now
42 valid_subnet = lambda net: net.startswith('10.0')
43 driver = self.driver.admin_driver(deployment=deployment)
44 subnets = driver.shell.quantum.list_subnets()['subnets']
45 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
46 if valid_subnet(subnet['cidr'])]
47 ints.sort()
48 if ints:
49 last_ip = IPAddress(ints[-1])
50 else:
Tony Mackb84fccc2014-04-12 10:28:52 -040051 last_ip = IPAddress('10.0.0.0')
Tony Mack6793de42014-04-08 16:36:52 -040052 last_network = IPNetwork(str(last_ip) + "/24")
53 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
54 return next_network
55
56 def save_network_deployment(self, network_deployment):
57 if not network_deployment.network_id and network_deployment.network.template.sharedNetworkName:
58 network_deployment.network_id = network_deployment.network.template.sharedNetworkId
59
60 if not network_deployment.net_id:
61 network_name = network_deployment.network.name
62
63 # create network
64 os_network = self.driver.create_network(network_name, shared=True)
65 network_deployment.net_id = os_network['id']
66
67 # create router
68 router = self.driver.create_router(network_name)
69 network_deployment.router_id = router['id']
70
71 # create subnet
72 next_subnet = self.get_next_subnet(deployment=network_deployment.deployment.name)
73 cidr = str(next_subnet.cidr)
74 ip_version = next_subnet.version
75 start = str(next_subnet[2])
76 end = str(next_subnet[-2])
77 subnet = self.driver.create_subnet(name=network_name,
78 network_id = network_deployment.net_id,
79 cidr_ip = cidr,
80 ip_version = ip_version,
81 start = start,
82 end = end)
83 network_deployment.subnet = cidr
84 network_deployment.subnet_id = subnet['id']
85 # add subnet as interface to slice's router
86 self.driver.add_router_interface(router['id'], subnet['id'])
87 # add external route
88 self.driver.add_external_route(subnet)
89 logger.info("created private subnet (%s) for network: %s" % (cidr, network_deployment.network))
90 else:
91 (network_deployment.subnet_id, network_deployment.subnet) = self.driver.get_network_subnet(network_deployment.net_id)
92 logger.info("sync'ed subnet (%s) for network: %s" % (network_deployment.subnet, network_deployment.network))
93
94 network_deployment.save()
95
96 def sync_record(self, network_deployment):
97 if network_deployment.network.owner and network_deployment.network.owner.creator:
98 try:
99 # update manager context
100 real_driver = self.driver
101 self.driver = self.driver.client_driver(caller=network_deployment.network.owner.creator,
102 tenant=network_deployment.network.owner.name,
103 deployment=network_deployment.deployment.name)
104 self.save_network_deployment(network_deployment)
105 self.driver = real_driver
106 logger.info("saved network deployment: %s" % (network_deployment))
107 except Exception,e:
108 logger.log_exc("save network deployment failed: %s" % network_deployment)
109 raise e
110
111
112