blob: 3f33219918dbc93c90d25548deb5dc6964c077f5 [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 collections import defaultdict
18from synchronizers.new_base.modelaccessor import *
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 ControllerPolicy(Policy):
22 model_name = "Controller"
Scott Bakera0a688a2017-03-28 11:59:56 -070023
Scott Baker46a3ee92017-05-30 16:44:56 -070024 def handle_create(self, controller):
25 return self.handle_update(controller)
Scott Bakera0a688a2017-03-28 11:59:56 -070026
Scott Baker46a3ee92017-05-30 16:44:56 -070027 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 Bakera0a688a2017-03-28 11:59:56 -070033
Scott Baker46a3ee92017-05-30 16:44:56 -070034 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 Bakera0a688a2017-03-28 11:59:56 -070039
Scott Baker46a3ee92017-05-30 16:44:56 -070040 # 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 Bakera0a688a2017-03-28 11:59:56 -070045
Scott Baker46a3ee92017-05-30 16:44:56 -070046 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 Bakera0a688a2017-03-28 11:59:56 -070051
Scott Baker46a3ee92017-05-30 16:44:56 -070052 # 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 Bakera0a688a2017-03-28 11:59:56 -070057
Scott Baker46a3ee92017-05-30 16:44:56 -070058 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 Bakera0a688a2017-03-28 11:59:56 -070063
Scott Baker46a3ee92017-05-30 16:44:56 -070064 # 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 Bakera0a688a2017-03-28 11:59:56 -070069
Scott Baker46a3ee92017-05-30 16:44:56 -070070 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 Bakera0a688a2017-03-28 11:59:56 -070089