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 | # 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 Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 30 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 31 | #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 Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 35 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 36 | # For simplicity, let's assume that a network gets deployed on all controllers. |
| 37 | expected_controllers = Controller.objects.all() |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 38 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 39 | existing_controllers = [] |
| 40 | for cn in ControllerNetwork.objects.all(): |
| 41 | if cn.network.id == network.id: |
Scott Baker | 959cba3 | 2017-06-11 19:41:23 -0700 | [diff] [blame] | 42 | existing_controllers.append(cn.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 43 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 44 | existing_controller_ids = [c.id for c in existing_controllers] |
| 45 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 46 | for expected_controller in expected_controllers: |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 47 | if expected_controller.id not in existing_controller_ids: |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 48 | lazy_blocked=True |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 49 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 50 | # 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 Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 59 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 60 | 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() |