blob: d9ec215a5654cca9baf957eb8f434c196924ef13 [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 Bakerb63ea792016-08-11 10:24:48 -07007 # network controllers are not visible to users. We must ensure
8 # networks are deployed at all deploymets available to their slices.
Scott Bakera0a688a2017-03-28 11:59:56 -07009
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 Bakerb63ea792016-08-11 10:24:48 -070014 slice_controllers = ControllerSlice.objects.all()
15 slice_deploy_lookup = defaultdict(list)
16 for slice_controller in slice_controllers:
Scott Bakera0a688a2017-03-28 11:59:56 -070017 slice_deploy_lookup[slice_controller.slice.id].append(slice_controller.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070018
19 network_controllers = ControllerNetwork.objects.all()
20 network_deploy_lookup = defaultdict(list)
21 for network_controller in network_controllers:
Scott Bakera0a688a2017-03-28 11:59:56 -070022 network_deploy_lookup[network_controller.network.id].append(network_controller.controller.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070023
Scott Bakera0a688a2017-03-28 11:59:56 -070024 expected_controllers = slice_deploy_lookup[network.owner.id]
Scott Bakerb63ea792016-08-11 10:24:48 -070025 for expected_controller in expected_controllers:
Scott Bakera0a688a2017-03-28 11:59:56 -070026 if network.id not in network_deploy_lookup or expected_controller.id not in network_deploy_lookup[network.id]:
Scott Bakerb63ea792016-08-11 10:24:48 -070027 lazy_blocked=True
28
29 # check and see if some instance already exists
30 for networkslice in network.networkslices.all():
Scott Bakeraf599eb2017-03-21 12:43:26 -070031 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 Bakerb63ea792016-08-11 10:24:48 -070036 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()