blob: 24ec0ddc16e6b8dcd4096161a7acddfbf1488952 [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 # For simplicity, let's assume that a network gets deployed on all controllers.
29 expected_controllers = Controller.objects.all()
Scott Bakerb63ea792016-08-11 10:24:48 -070030
Scott Bakerb5fa8a22017-06-07 17:31:15 -070031 existing_controllers = []
32 for cn in ControllerNetwork.objects.all():
33 if cn.network.id == network.id:
Scott Baker959cba32017-06-11 19:41:23 -070034 existing_controllers.append(cn.controller)
Scott Bakerb63ea792016-08-11 10:24:48 -070035
Scott Bakerb5fa8a22017-06-07 17:31:15 -070036 existing_controller_ids = [c.id for c in existing_controllers]
37
Scott Baker46a3ee92017-05-30 16:44:56 -070038 for expected_controller in expected_controllers:
Scott Bakerb5fa8a22017-06-07 17:31:15 -070039 if expected_controller.id not in existing_controller_ids:
Sapan Bhatia99f94c92017-11-29 14:30:19 -050040 lazy_blocked=False
Scott Bakerb63ea792016-08-11 10:24:48 -070041
Scott Baker46a3ee92017-05-30 16:44:56 -070042 nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked)
43 self.logger.info("MODEL POLICY: network %s create ControllerNetwork %s lazy_blocked %s" % (network, nd, lazy_blocked))
44 if network.subnet:
45 # XXX: Possibly unpredictable behavior if there is
46 # more than one ControllerNetwork and the subnet
47 # is specified.
48 nd.subnet = network.subnet
49 nd.save()