blob: f9a4bc00156e0cecda79dcefc48313a18c5b18c4 [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():
Tony Mack3a50a6c2014-04-16 07:49:15 -040032 # ignore networks that have
33 # template.visibility = private and template.translation = none
Tony Mack078f6682014-04-17 21:57:16 -040034 if network.template.visibility == 'private' and not network.template.translation == 'none':
35 continue
Tony Mack6793de42014-04-08 16:36:52 -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))
43
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):
61 if not network_deployment.network_id and network_deployment.network.template.sharedNetworkName:
62 network_deployment.network_id = network_deployment.network.template.sharedNetworkId
63
64 if not network_deployment.net_id:
65 network_name = network_deployment.network.name
66
67 # create network
68 os_network = self.driver.create_network(network_name, shared=True)
69 network_deployment.net_id = os_network['id']
70
71 # create router
Tony Mackeb786e02014-04-18 15:32:45 -040072 #router = self.driver.create_router(network_name)
73 #network_deployment.router_id = router['id']
Tony Mack6793de42014-04-08 16:36:52 -040074
75 # create subnet
76 next_subnet = self.get_next_subnet(deployment=network_deployment.deployment.name)
77 cidr = str(next_subnet.cidr)
78 ip_version = next_subnet.version
79 start = str(next_subnet[2])
80 end = str(next_subnet[-2])
81 subnet = self.driver.create_subnet(name=network_name,
82 network_id = network_deployment.net_id,
83 cidr_ip = cidr,
84 ip_version = ip_version,
85 start = start,
86 end = end)
87 network_deployment.subnet = cidr
88 network_deployment.subnet_id = subnet['id']
89 # add subnet as interface to slice's router
Tony Mackeb786e02014-04-18 15:32:45 -040090 #self.driver.add_router_interface(router['id'], subnet['id'])
Tony Mack6793de42014-04-08 16:36:52 -040091 # add external route
Tony Mackeb786e02014-04-18 15:32:45 -040092 #self.driver.add_external_route(subnet)
Tony Mack6793de42014-04-08 16:36:52 -040093 logger.info("created private subnet (%s) for network: %s" % (cidr, network_deployment.network))
94 else:
95 (network_deployment.subnet_id, network_deployment.subnet) = self.driver.get_network_subnet(network_deployment.net_id)
96 logger.info("sync'ed subnet (%s) for network: %s" % (network_deployment.subnet, network_deployment.network))
97
98 network_deployment.save()
99
100 def sync_record(self, network_deployment):
101 if network_deployment.network.owner and network_deployment.network.owner.creator:
102 try:
103 # update manager context
104 real_driver = self.driver
105 self.driver = self.driver.client_driver(caller=network_deployment.network.owner.creator,
106 tenant=network_deployment.network.owner.name,
107 deployment=network_deployment.deployment.name)
108 self.save_network_deployment(network_deployment)
109 self.driver = real_driver
110 logger.info("saved network deployment: %s" % (network_deployment))
111 except Exception,e:
112 logger.log_exc("save network deployment failed: %s" % network_deployment)
113 raise e
114
115
116