blob: fa907512d0660a181abc56f25eb097d8445c1087 [file] [log] [blame]
Scott Bakeraf599eb2017-03-21 12:43:26 -07001from synchronizers.new_base.modelaccessor import *
2from collections import defaultdict
Scott Baker46a3ee92017-05-30 16:44:56 -07003from synchronizers.new_base.policy import Policy
Scott Bakerb63ea792016-08-11 10:24:48 -07004
Scott Baker46a3ee92017-05-30 16:44:56 -07005class NetworkPolicy(Policy):
6 model_name = "Network"
Scott Bakerb63ea792016-08-11 10:24:48 -07007
Scott Baker46a3ee92017-05-30 16:44:56 -07008 def handle_create(self, network):
9 return self.handle_update(network)
10
11 def handle_update(self, network):
12 # network controllers are not visible to users. We must ensure
13 # networks are deployed at all deploymets available to their slices.
Scott Bakera0a688a2017-03-28 11:59:56 -070014
15 # TODO: should be possible to query only the ControllerSlice objects
16 # associated with network.owner rather than iterating through
17 # all ControllerSlice.
18
Scott Baker46a3ee92017-05-30 16:44:56 -070019 slice_controllers = ControllerSlice.objects.all()
20 slice_deploy_lookup = defaultdict(list)
21 for slice_controller in slice_controllers:
22 slice_deploy_lookup[slice_controller.slice.id].append(slice_controller.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070023
Scott Baker46a3ee92017-05-30 16:44:56 -070024 network_controllers = ControllerNetwork.objects.all()
25 network_deploy_lookup = defaultdict(list)
26 for network_controller in network_controllers:
27 network_deploy_lookup[network_controller.network.id].append(network_controller.controller.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070028
Scott Baker46a3ee92017-05-30 16:44:56 -070029 expected_controllers = slice_deploy_lookup[network.owner.id]
30 for expected_controller in expected_controllers:
31 if network.id not in network_deploy_lookup or expected_controller.id not in network_deploy_lookup[network.id]:
32 lazy_blocked=True
Scott Bakerb63ea792016-08-11 10:24:48 -070033
Scott Baker46a3ee92017-05-30 16:44:56 -070034 # check and see if some instance already exists
35 for networkslice in network.networkslices.all():
36 found = False
37 for instance in networkslice.slice.instances.all():
38 if instance.node.site_deployment.controller.id == expected_controller.id:
39 found = True
40 if found:
41 self.logger.info("MODEL_POLICY: network %s setting lazy_blocked to false because instance on controller already exists" % network)
42 lazy_blocked=False
Scott Bakerb63ea792016-08-11 10:24:48 -070043
Scott Baker46a3ee92017-05-30 16:44:56 -070044 nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked)
45 self.logger.info("MODEL POLICY: network %s create ControllerNetwork %s lazy_blocked %s" % (network, nd, lazy_blocked))
46 if network.subnet:
47 # XXX: Possibly unpredictable behavior if there is
48 # more than one ControllerNetwork and the subnet
49 # is specified.
50 nd.subnet = network.subnet
51 nd.save()