blob: a4a19b54ae271a641c82a9a6722578365f1a4bb3 [file] [log] [blame]
Matteo Scandolof0441032017-08-08 13:05:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Bakeraf599eb2017-03-21 12:43:26 -070017from synchronizers.new_base.modelaccessor import *
18from collections import defaultdict
Scott Baker46a3ee92017-05-30 16:44:56 -070019from synchronizers.new_base.policy import Policy
Scott Bakerb63ea792016-08-11 10:24:48 -070020
Scott Baker46a3ee92017-05-30 16:44:56 -070021class NetworkPolicy(Policy):
22 model_name = "Network"
Scott Bakerb63ea792016-08-11 10:24:48 -070023
Scott Baker46a3ee92017-05-30 16:44:56 -070024 def handle_create(self, network):
25 return self.handle_update(network)
26
27 def handle_update(self, network):
Scott Bakerb5fa8a22017-06-07 17:31:15 -070028 # if (not network.owner.policed) or (network.owner.policed<network.owner.updated):
29 # raise Exception("Cannot apply network policies until slice policies have completed")
Scott Bakera0a688a2017-03-28 11:59:56 -070030
Scott Bakerb5fa8a22017-06-07 17:31:15 -070031 #expected_controllers = []
32 #for slice_controller in ControllerSlice.objects.all():
33 # if slice_controller.slice.id == network.owner.id:
34 # expected_controllers.append(slice_controller.controller)
Scott Bakera0a688a2017-03-28 11:59:56 -070035
Scott Bakerb5fa8a22017-06-07 17:31:15 -070036 # For simplicity, let's assume that a network gets deployed on all controllers.
37 expected_controllers = Controller.objects.all()
Scott Bakerb63ea792016-08-11 10:24:48 -070038
Scott Bakerb5fa8a22017-06-07 17:31:15 -070039 existing_controllers = []
40 for cn in ControllerNetwork.objects.all():
41 if cn.network.id == network.id:
Scott Baker959cba32017-06-11 19:41:23 -070042 existing_controllers.append(cn.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070043
Scott Bakerb5fa8a22017-06-07 17:31:15 -070044 existing_controller_ids = [c.id for c in existing_controllers]
45
Scott Baker46a3ee92017-05-30 16:44:56 -070046 for expected_controller in expected_controllers:
Scott Bakerb5fa8a22017-06-07 17:31:15 -070047 if expected_controller.id not in existing_controller_ids:
Scott Baker46a3ee92017-05-30 16:44:56 -070048 lazy_blocked=True
Scott Bakerb63ea792016-08-11 10:24:48 -070049
Scott Baker46a3ee92017-05-30 16:44:56 -070050 # check and see if some instance already exists
51 for networkslice in network.networkslices.all():
52 found = False
53 for instance in networkslice.slice.instances.all():
54 if instance.node.site_deployment.controller.id == expected_controller.id:
55 found = True
56 if found:
57 self.logger.info("MODEL_POLICY: network %s setting lazy_blocked to false because instance on controller already exists" % network)
58 lazy_blocked=False
Scott Bakerb63ea792016-08-11 10:24:48 -070059
Scott Baker46a3ee92017-05-30 16:44:56 -070060 nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked)
61 self.logger.info("MODEL POLICY: network %s create ControllerNetwork %s lazy_blocked %s" % (network, nd, lazy_blocked))
62 if network.subnet:
63 # XXX: Possibly unpredictable behavior if there is
64 # more than one ControllerNetwork and the subnet
65 # is specified.
66 nd.subnet = network.subnet
67 nd.save()