blob: 1167c5ed119ca9638f8ae98cd3271b03d36f8ea3 [file] [log] [blame]
Scott Bakeraf599eb2017-03-21 12:43:26 -07001from synchronizers.new_base.modelaccessor import *
2from collections import defaultdict
Scott Bakerb63ea792016-08-11 10:24:48 -07003
4def handle(network):
Scott Bakerb63ea792016-08-11 10:24:48 -07005 print "MODEL POLICY: network", network
6
Scott Baker3f9071c2017-06-22 08:36:29 -07007 # For simplicity, let's assume that a network gets deployed on all controllers.
8 expected_controllers = Controller.objects.all()
Scott Bakera0a688a2017-03-28 11:59:56 -07009
Scott Baker3f9071c2017-06-22 08:36:29 -070010 existing_controllers = []
11 for cn in ControllerNetwork.objects.all():
12 if cn.network.id == network.id:
13 existing_controllers.append(cn.controller)
Scott Bakera0a688a2017-03-28 11:59:56 -070014
Scott Baker3f9071c2017-06-22 08:36:29 -070015 existing_controller_ids = [c.id for c in existing_controllers]
Scott Bakerb63ea792016-08-11 10:24:48 -070016
Scott Baker3f9071c2017-06-22 08:36:29 -070017 for expected_controller in expected_controllers:
18 if expected_controller.id not in existing_controller_ids:
Scott Bakerb63ea792016-08-11 10:24:48 -070019 lazy_blocked=True
20
21 # check and see if some instance already exists
22 for networkslice in network.networkslices.all():
Scott Bakeraf599eb2017-03-21 12:43:26 -070023 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 Bakerb63ea792016-08-11 10:24:48 -070028 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()