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 | 3f9071c | 2017-06-22 08:36:29 -0700 | [diff] [blame] | 7 | # For simplicity, let's assume that a network gets deployed on all controllers. |
| 8 | expected_controllers = Controller.objects.all() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 9 | |
Scott Baker | 3f9071c | 2017-06-22 08:36:29 -0700 | [diff] [blame] | 10 | existing_controllers = [] |
| 11 | for cn in ControllerNetwork.objects.all(): |
| 12 | if cn.network.id == network.id: |
| 13 | existing_controllers.append(cn.controller) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 14 | |
Scott Baker | 3f9071c | 2017-06-22 08:36:29 -0700 | [diff] [blame] | 15 | existing_controller_ids = [c.id for c in existing_controllers] |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 16 | |
Scott Baker | 3f9071c | 2017-06-22 08:36:29 -0700 | [diff] [blame] | 17 | for expected_controller in expected_controllers: |
| 18 | if expected_controller.id not in existing_controller_ids: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 19 | lazy_blocked=True |
| 20 | |
| 21 | # check and see if some instance already exists |
| 22 | for networkslice in network.networkslices.all(): |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 23 | found = False |
| 24 | for instance in networkslice.slice.instances.all(): |
| 25 | if instance.node.site_deployment.controller.id == expected_controller.id: |
| 26 | found = True |
| 27 | if found: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 28 | print "MODEL_POLICY: network, setting lazy_blocked to false because instance on controller already exists" |
| 29 | lazy_blocked=False |
| 30 | |
| 31 | nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked) |
| 32 | print "MODEL POLICY: network", network, "create ControllerNetwork", nd, "lazy_blocked", lazy_blocked |
| 33 | if network.subnet: |
| 34 | # XXX: Possibly unpredictable behavior if there is |
| 35 | # more than one ControllerNetwork and the subnet |
| 36 | # is specified. |
| 37 | nd.subnet = network.subnet |
| 38 | nd.save() |