Matteo Scandolo | f044103 | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 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 Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 17 | from synchronizers.new_base.modelaccessor import * |
| 18 | from collections import defaultdict |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 19 | from synchronizers.new_base.policy import Policy |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 20 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 21 | class NetworkPolicy(Policy): |
| 22 | model_name = "Network" |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 23 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 24 | def handle_create(self, network): |
| 25 | return self.handle_update(network) |
| 26 | |
| 27 | def handle_update(self, network): |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 28 | # For simplicity, let's assume that a network gets deployed on all controllers. |
| 29 | expected_controllers = Controller.objects.all() |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 30 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 31 | existing_controllers = [] |
| 32 | for cn in ControllerNetwork.objects.all(): |
| 33 | if cn.network.id == network.id: |
Scott Baker | 959cba3 | 2017-06-11 19:41:23 -0700 | [diff] [blame] | 34 | existing_controllers.append(cn.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 35 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 36 | existing_controller_ids = [c.id for c in existing_controllers] |
| 37 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 38 | for expected_controller in expected_controllers: |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 39 | if expected_controller.id not in existing_controller_ids: |
Sapan Bhatia | 99f94c9 | 2017-11-29 14:30:19 -0500 | [diff] [blame] | 40 | lazy_blocked=False |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 41 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 42 | 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() |