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 collections import defaultdict |
| 18 | from synchronizers.new_base.modelaccessor import * |
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 ControllerPolicy(Policy): |
| 22 | model_name = "Controller" |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 23 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 24 | def handle_create(self, controller): |
| 25 | return self.handle_update(controller) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 26 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 27 | def handle_update(self, controller): |
| 28 | # relations for all sites |
| 29 | ctrls_by_site = defaultdict(list) |
| 30 | ctrl_sites = ControllerSite.objects.all() |
| 31 | for ctrl_site in ctrl_sites: |
| 32 | ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 33 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 34 | sites = Site.objects.all() |
| 35 | for site in sites: |
| 36 | if site.id not in ctrls_by_site or controller.id not in ctrls_by_site[site.id]: |
| 37 | controller_site = ControllerSite(controller=controller, site=site) |
| 38 | controller_site.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 39 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 40 | # relations for all slices |
| 41 | ctrls_by_slice = defaultdict(list) |
| 42 | ctrl_slices = ControllerSlice.objects.all() |
| 43 | for ctrl_slice in ctrl_slices: |
| 44 | ctrls_by_slice[ctrl_slice.slice.id].append(ctrl_slice.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 45 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 46 | slices = Slice.objects.all() |
| 47 | for slice in slices: |
| 48 | if slice.id not in ctrls_by_slice or controller.id not in ctrls_by_slice[slice.id]: |
| 49 | controller_slice = ControllerSlice(controller=controller, slice=slice) |
| 50 | controller_slice.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 51 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 52 | # relations for all users |
| 53 | ctrls_by_user = defaultdict(list) |
| 54 | ctrl_users = ControllerUser.objects.all() |
| 55 | for ctrl_user in ctrl_users: |
| 56 | ctrls_by_user[ctrl_user.user.id].append(ctrl_user.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 57 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 58 | users = User.objects.all() |
| 59 | for user in users: |
| 60 | if user.id not in ctrls_by_user or controller.id not in ctrls_by_user[user.id]: |
| 61 | controller_user = ControllerUser(controller=controller, user=user) |
| 62 | controller_user.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 63 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 64 | # relations for all networks |
| 65 | ctrls_by_network = defaultdict(list) |
| 66 | ctrl_networks = ControllerNetwork.objects.all() |
| 67 | for ctrl_network in ctrl_networks: |
| 68 | ctrls_by_network[ctrl_network.network.id].append(ctrl_network.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 69 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 70 | networks = Network.objects.all() |
| 71 | for network in networks: |
| 72 | if network.id not in ctrls_by_network or controller.id not in ctrls_by_network[network.id]: |
| 73 | controller_network = ControllerNetwork(controller=controller, network=network) |
| 74 | if network.subnet and network.subnet.strip(): |
| 75 | controller_network.subnet = network.subnet.strip() |
| 76 | controller_network.save() |
| 77 | |
| 78 | # relations for all images |
| 79 | ctrls_by_image = defaultdict(list) |
| 80 | ctrl_images = ControllerImages.objects.all() |
| 81 | for ctrl_image in ctrl_images: |
| 82 | ctrls_by_image[ctrl_image.image.id].append(ctrl_image.controller.id) |
| 83 | |
| 84 | images = Image.objects.all() |
| 85 | for image in images: |
| 86 | if image.id not in ctrls_by_image or controller.id not in ctrls_by_image[image.id]: |
| 87 | controller_image = ControllerImages(controller=controller, image=image) |
| 88 | controller_image.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 89 | |