blob: c6a74a854693dd7dfb70edba7a847b11802c9f67 [file] [log] [blame]
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -04001
2def handle(slice):
Tony Mackd14d48f2014-12-05 17:13:08 -05003 from core.models import Controller, ControllerSiteDeployments, ControllerSlices,Controller,Network,NetworkSlice,NetworkTemplate
Sapan Bhatia0e5be032014-09-15 03:48:20 -04004 from collections import defaultdict
Tony Mackd14d48f2014-12-05 17:13:08 -05005 ctrl_site_deployments = ControllerSiteDeployments.objects.all()
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -04006 site_deploy_lookup = defaultdict(list)
Tony Mackd14d48f2014-12-05 17:13:08 -05007 for ctrl_site_deployment in ctrl_site_deployments:
8 site_deploy_lookup[ctrl_site_deployment.site_deployment].append(ctrl_site_deployment)
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -04009
Tony Mack06c8e472014-11-30 15:53:08 -050010 slice_controllers = ControllerSlices.objects.all()
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -040011 slice_deploy_lookup = defaultdict(list)
Tony Mack06c8e472014-11-30 15:53:08 -050012 for slice_controller in slice_controllers:
Tony Mackd14d48f2014-12-05 17:13:08 -050013 slice_deploy_lookup[slice_controller.slice].append(slice_controller)
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -040014
Tony Mack06c8e472014-11-30 15:53:08 -050015 #expected_controllers = site_deploy_lookup[slice.site]
Tony Mackd14d48f2014-12-05 17:13:08 -050016 all_controllers = Controller.objects.all()
Sapan Bhatiae5e2f2e2014-12-22 01:38:00 -050017 for expected_controller in all_controllers:
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -040018 if slice not in slice_deploy_lookup or \
Tony Mack06c8e472014-11-30 15:53:08 -050019 expected_controller not in slice_deploy_lookup[slice]:
20 sd = ControllerSlices(slice=slice, controller=expected_controller)
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -040021 sd.save()
22
Tony Mackbd7fb112014-10-08 22:54:02 -040023 # make sure slice has at least 1 public and 1 private networkd
Scott Baker0cbdcea2014-10-16 00:43:54 -070024 public_nets = []
Tony Mackbd7fb112014-10-08 22:54:02 -040025 private_net = None
26 networks = Network.objects.filter(owner=slice)
27 for network in networks:
28 if network.template.name == 'Public dedicated IPv4':
Scott Baker0cbdcea2014-10-16 00:43:54 -070029 public_nets.append(network)
30 elif network.template.name == 'Public shared IPv4':
31 public_nets.append(network)
Tony Mackbd7fb112014-10-08 22:54:02 -040032 elif network.template.name == 'Private':
Scott Baker0cbdcea2014-10-16 00:43:54 -070033 private_net = network
34 if not public_nets:
35 # ensure there is at least one public network, and default it to dedicated
Tony Mackc696df82014-11-12 10:00:12 -050036 nat_net = Network(
37 name = slice.name+'-nat',
38 template = NetworkTemplate.objects.get(name='Public shared IPv4'),
Scott Baker0cbdcea2014-10-16 00:43:54 -070039 owner = slice
40 )
Tony Mackc696df82014-11-12 10:00:12 -050041 nat_net.save()
42 public_nets.append(nat_net)
Scott Baker0cbdcea2014-10-16 00:43:54 -070043
Tony Mackbd7fb112014-10-08 22:54:02 -040044 if not private_net:
45 private_net = Network(
46 name = slice.name+'-private',
47 template = NetworkTemplate.objects.get(name='Private'),
48 owner = slice
49 )
50 private_net.save()
51 # create slice networks
52 public_net_slice = None
53 private_net_slice = None
Scott Baker0cbdcea2014-10-16 00:43:54 -070054 net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[private_net]+public_nets)
Tony Mackbd7fb112014-10-08 22:54:02 -040055 for net_slice in net_slices:
Scott Baker0cbdcea2014-10-16 00:43:54 -070056 if net_slice.network in public_nets:
57 public_net_slice = net_slice
Tony Mackbd7fb112014-10-08 22:54:02 -040058 elif net_slice.network == private_net:
Scott Baker0cbdcea2014-10-16 00:43:54 -070059 private_net_slice = net_slice
Tony Mackbd7fb112014-10-08 22:54:02 -040060 if not public_net_slice:
Scott Baker0cbdcea2014-10-16 00:43:54 -070061 public_net_slice = NetworkSlice(slice=slice, network=public_nets[0])
Tony Mackbd7fb112014-10-08 22:54:02 -040062 public_net_slice.save()
63 if not private_net_slice:
64 private_net_slice = NetworkSlice(slice=slice, network=private_net)
Scott Baker0cbdcea2014-10-16 00:43:54 -070065 private_net_slice.save()
66
67
68
69