blob: 606fec87e7255afab457f41707617f357238dfd6 [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):
Scott Bakerb5fa8a22017-06-07 17:31:15 -070012 # if (not network.owner.policed) or (network.owner.policed<network.owner.updated):
13 # raise Exception("Cannot apply network policies until slice policies have completed")
Scott Bakera0a688a2017-03-28 11:59:56 -070014
Scott Bakerb5fa8a22017-06-07 17:31:15 -070015 #expected_controllers = []
16 #for slice_controller in ControllerSlice.objects.all():
17 # if slice_controller.slice.id == network.owner.id:
18 # expected_controllers.append(slice_controller.controller)
Scott Bakera0a688a2017-03-28 11:59:56 -070019
Scott Bakerb5fa8a22017-06-07 17:31:15 -070020 # For simplicity, let's assume that a network gets deployed on all controllers.
21 expected_controllers = Controller.objects.all()
Scott Bakerb63ea792016-08-11 10:24:48 -070022
Scott Bakerb5fa8a22017-06-07 17:31:15 -070023 existing_controllers = []
24 for cn in ControllerNetwork.objects.all():
25 if cn.network.id == network.id:
Scott Baker959cba32017-06-11 19:41:23 -070026 existing_controllers.append(cn.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070027
Scott Bakerb5fa8a22017-06-07 17:31:15 -070028 existing_controller_ids = [c.id for c in existing_controllers]
29
Scott Baker46a3ee92017-05-30 16:44:56 -070030 for expected_controller in expected_controllers:
Scott Bakerb5fa8a22017-06-07 17:31:15 -070031 if expected_controller.id not in existing_controller_ids:
Scott Baker46a3ee92017-05-30 16:44:56 -070032 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()