Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 1 | from synchronizers.new_base.modelaccessor import * |
| 2 | from collections import defaultdict |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 3 | |
| 4 | def handle(network): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 5 | print "MODEL POLICY: network", network |
| 6 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 7 | # network controllers are not visible to users. We must ensure |
| 8 | # networks are deployed at all deploymets available to their slices. |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 9 | |
| 10 | # TODO: should be possible to query only the ControllerSlice objects |
| 11 | # associated with network.owner rather than iterating through |
| 12 | # all ControllerSlice. |
| 13 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 14 | slice_controllers = ControllerSlice.objects.all() |
| 15 | slice_deploy_lookup = defaultdict(list) |
| 16 | for slice_controller in slice_controllers: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 17 | slice_deploy_lookup[slice_controller.slice.id].append(slice_controller.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 18 | |
| 19 | network_controllers = ControllerNetwork.objects.all() |
| 20 | network_deploy_lookup = defaultdict(list) |
| 21 | for network_controller in network_controllers: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 22 | network_deploy_lookup[network_controller.network.id].append(network_controller.controller.id) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 23 | |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 24 | expected_controllers = slice_deploy_lookup[network.owner.id] |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 25 | for expected_controller in expected_controllers: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 26 | if network.id not in network_deploy_lookup or expected_controller.id not in network_deploy_lookup[network.id]: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 27 | lazy_blocked=True |
| 28 | |
| 29 | # check and see if some instance already exists |
| 30 | for networkslice in network.networkslices.all(): |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 31 | found = False |
| 32 | for instance in networkslice.slice.instances.all(): |
| 33 | if instance.node.site_deployment.controller.id == expected_controller.id: |
| 34 | found = True |
| 35 | if found: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 36 | print "MODEL_POLICY: network, setting lazy_blocked to false because instance on controller already exists" |
| 37 | lazy_blocked=False |
| 38 | |
| 39 | nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked) |
| 40 | print "MODEL POLICY: network", network, "create ControllerNetwork", nd, "lazy_blocked", lazy_blocked |
| 41 | if network.subnet: |
| 42 | # XXX: Possibly unpredictable behavior if there is |
| 43 | # more than one ControllerNetwork and the subnet |
| 44 | # is specified. |
| 45 | nd.subnet = network.subnet |
| 46 | nd.save() |