Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 1 | |
| 2 | def handle(slice): |
Tony Mack | 0f69d9e | 2015-01-03 20:04:54 -0500 | [diff] [blame] | 3 | from core.models import Controller,ControllerSlices,Controller,Network,NetworkSlice,NetworkTemplate |
Sapan Bhatia | 0e5be03 | 2014-09-15 03:48:20 -0400 | [diff] [blame] | 4 | from collections import defaultdict |
Tony Mack | 0f69d9e | 2015-01-03 20:04:54 -0500 | [diff] [blame] | 5 | ctrl_site_deployments = SiteDeployments.objects.all() |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 6 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 7 | slice_controllers = ControllerSlices.objects.all() |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 8 | slice_deploy_lookup = defaultdict(list) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 9 | for slice_controller in slice_controllers: |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 10 | slice_deploy_lookup[slice_controller.slice].append(slice_controller) |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 11 | |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 12 | all_controllers = Controller.objects.all() |
Sapan Bhatia | e5e2f2e | 2014-12-22 01:38:00 -0500 | [diff] [blame] | 13 | for expected_controller in all_controllers: |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 14 | if slice not in slice_deploy_lookup or \ |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 15 | expected_controller not in slice_deploy_lookup[slice]: |
| 16 | sd = ControllerSlices(slice=slice, controller=expected_controller) |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 17 | sd.save() |
| 18 | |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 19 | # make sure slice has at least 1 public and 1 private networkd |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 20 | public_nets = [] |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 21 | private_net = None |
| 22 | networks = Network.objects.filter(owner=slice) |
| 23 | for network in networks: |
| 24 | if network.template.name == 'Public dedicated IPv4': |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 25 | public_nets.append(network) |
| 26 | elif network.template.name == 'Public shared IPv4': |
| 27 | public_nets.append(network) |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 28 | elif network.template.name == 'Private': |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 29 | private_net = network |
| 30 | if not public_nets: |
| 31 | # ensure there is at least one public network, and default it to dedicated |
Tony Mack | c696df8 | 2014-11-12 10:00:12 -0500 | [diff] [blame] | 32 | nat_net = Network( |
| 33 | name = slice.name+'-nat', |
| 34 | template = NetworkTemplate.objects.get(name='Public shared IPv4'), |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 35 | owner = slice |
| 36 | ) |
Tony Mack | c696df8 | 2014-11-12 10:00:12 -0500 | [diff] [blame] | 37 | nat_net.save() |
| 38 | public_nets.append(nat_net) |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 39 | |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 40 | if not private_net: |
| 41 | private_net = Network( |
| 42 | name = slice.name+'-private', |
| 43 | template = NetworkTemplate.objects.get(name='Private'), |
| 44 | owner = slice |
| 45 | ) |
| 46 | private_net.save() |
| 47 | # create slice networks |
| 48 | public_net_slice = None |
| 49 | private_net_slice = None |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 50 | net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[private_net]+public_nets) |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 51 | for net_slice in net_slices: |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 52 | if net_slice.network in public_nets: |
| 53 | public_net_slice = net_slice |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 54 | elif net_slice.network == private_net: |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 55 | private_net_slice = net_slice |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 56 | if not public_net_slice: |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 57 | public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) |
Tony Mack | bd7fb11 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 58 | public_net_slice.save() |
| 59 | if not private_net_slice: |
| 60 | private_net_slice = NetworkSlice(slice=slice, network=private_net) |
Scott Baker | 0cbdcea | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 61 | private_net_slice.save() |
| 62 | |
| 63 | |
| 64 | |
| 65 | |