blob: a9936bd6312f041444265b55d33bff9db80824b9 [file] [log] [blame]
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -04001
2def handle(slice):
Sapan Bhatia085ed482015-01-17 00:56:36 +00003 from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice,NetworkTemplate, Slice
Tony Macka7dbd422015-01-05 22:48:11 -05004 from collections import defaultdict
Sapan Bhatia085ed482015-01-17 00:56:36 +00005
6 # slice = Slice.get(slice_id)
7
Tony Macka7dbd422015-01-05 22:48:11 -05008 controller_slices = ControllerSlice.objects.filter(slice=slice)
9 existing_controllers = [cs.controller for cs in controller_slices]
Sapan Bhatia85320d32015-01-23 16:11:29 +000010
Tony Macka7dbd422015-01-05 22:48:11 -050011 all_controllers = Controller.objects.all()
12 for controller in all_controllers:
13 if controller not in existing_controllers:
Tony Mack26564362015-01-06 17:49:25 -050014 sd = ControllerSlice(slice=slice, controller=controller)
Tony Macka7dbd422015-01-05 22:48:11 -050015 sd.save()
Sapan Bhatiab3aeb1b2014-09-03 15:39:13 -040016
Tony Macka7dbd422015-01-05 22:48:11 -050017 # make sure slice has at least 1 public and 1 private networkd
18 public_nets = []
19 private_net = None
20 networks = Network.objects.filter(owner=slice)
21 for network in networks:
22 if network.template.name == 'Public dedicated IPv4':
23 public_nets.append(network)
24 elif network.template.name == 'Public shared IPv4':
25 public_nets.append(network)
26 elif network.template.name == 'Private':
27 private_net = network
28 if not public_nets:
Scott Bakercb7649c2014-10-16 00:43:54 -070029 # ensure there is at least one public network, and default it to dedicated
Tony Macka7dbd422015-01-05 22:48:11 -050030 nat_net = Network(
31 name = slice.name+'-nat',
32 template = NetworkTemplate.objects.get(name='Public shared IPv4'),
33 owner = slice
34 )
35 nat_net.save()
36 public_nets.append(nat_net)
Scott Bakercb7649c2014-10-16 00:43:54 -070037
Tony Macka7dbd422015-01-05 22:48:11 -050038 if not private_net:
39 private_net = Network(
40 name = slice.name+'-private',
41 template = NetworkTemplate.objects.get(name='Private'),
42 owner = slice
43 )
44 private_net.save()
45 # create slice networks
46 public_net_slice = None
47 private_net_slice = None
48 net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[private_net]+public_nets)
49 for net_slice in net_slices:
50 if net_slice.network in public_nets:
51 public_net_slice = net_slice
52 elif net_slice.network == private_net:
53 private_net_slice = net_slice
54 if not public_net_slice:
55 public_net_slice = NetworkSlice(slice=slice, network=public_nets[0])
56 public_net_slice.save()
57 if not private_net_slice:
58 private_net_slice = NetworkSlice(slice=slice, network=private_net)
59 private_net_slice.save()
Scott Bakercb7649c2014-10-16 00:43:54 -070060
61
62
63