Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 1 | |
| 2 | def handle(slice): |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 3 | from core.models import Controller, ControllerSiteDeployments, ControllerSlices,Controller,Network,NetworkSlice,NetworkTemplate |
Sapan Bhatia | 728acce | 2014-09-15 03:48:20 -0400 | [diff] [blame] | 4 | from collections import defaultdict |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 5 | ctrl_site_deployments = ControllerSiteDeployments.objects.all() |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 6 | site_deploy_lookup = defaultdict(list) |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 7 | for ctrl_site_deployment in ctrl_site_deployments: |
| 8 | site_deploy_lookup[ctrl_site_deployment.site_deployment].append(ctrl_site_deployment) |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 9 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 10 | slice_controllers = ControllerSlices.objects.all() |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 11 | slice_deploy_lookup = defaultdict(list) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 12 | for slice_controller in slice_controllers: |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 13 | slice_deploy_lookup[slice_controller.slice].append(slice_controller) |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 14 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 15 | #expected_controllers = site_deploy_lookup[slice.site] |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 16 | all_controllers = Controller.objects.all() |
| 17 | for expected_controller in controllers: |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 18 | if slice not in slice_deploy_lookup or \ |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 19 | expected_controller not in slice_deploy_lookup[slice]: |
| 20 | sd = ControllerSlices(slice=slice, controller=expected_controller) |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 21 | sd.save() |
| 22 | |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 23 | # make sure slice has at least 1 public and 1 private networkd |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 24 | public_nets = [] |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 25 | private_net = None |
| 26 | networks = Network.objects.filter(owner=slice) |
| 27 | for network in networks: |
| 28 | if network.template.name == 'Public dedicated IPv4': |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 29 | public_nets.append(network) |
| 30 | elif network.template.name == 'Public shared IPv4': |
| 31 | public_nets.append(network) |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 32 | elif network.template.name == 'Private': |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 33 | private_net = network |
| 34 | if not public_nets: |
| 35 | # ensure there is at least one public network, and default it to dedicated |
Tony Mack | d0eeed8 | 2014-11-12 10:00:12 -0500 | [diff] [blame] | 36 | nat_net = Network( |
| 37 | name = slice.name+'-nat', |
| 38 | template = NetworkTemplate.objects.get(name='Public shared IPv4'), |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 39 | owner = slice |
| 40 | ) |
Tony Mack | d0eeed8 | 2014-11-12 10:00:12 -0500 | [diff] [blame] | 41 | nat_net.save() |
| 42 | public_nets.append(nat_net) |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 43 | |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 44 | 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 Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 54 | net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[private_net]+public_nets) |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 55 | for net_slice in net_slices: |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 56 | if net_slice.network in public_nets: |
| 57 | public_net_slice = net_slice |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 58 | elif net_slice.network == private_net: |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 59 | private_net_slice = net_slice |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 60 | if not public_net_slice: |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 61 | public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) |
Tony Mack | d2046f3 | 2014-10-08 22:54:02 -0400 | [diff] [blame] | 62 | public_net_slice.save() |
| 63 | if not private_net_slice: |
| 64 | private_net_slice = NetworkSlice(slice=slice, network=private_net) |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 65 | private_net_slice.save() |
| 66 | |
| 67 | |
| 68 | |
| 69 | |