blob: 45b0fae4cea93686f36bb7416ab530e62c095c3e [file] [log] [blame]
Scott Bakeraf599eb2017-03-21 12:43:26 -07001from collections import defaultdict
2from synchronizers.new_base.modelaccessor import *
Scott Baker46a3ee92017-05-30 16:44:56 -07003from synchronizers.new_base.policy import Policy
Scott Bakerb63ea792016-08-11 10:24:48 -07004
Scott Baker46a3ee92017-05-30 16:44:56 -07005class ControllerPolicy(Policy):
6 model_name = "Controller"
Scott Bakera0a688a2017-03-28 11:59:56 -07007
Scott Baker46a3ee92017-05-30 16:44:56 -07008 def handle_create(self, controller):
9 return self.handle_update(controller)
Scott Bakera0a688a2017-03-28 11:59:56 -070010
Scott Baker46a3ee92017-05-30 16:44:56 -070011 def handle_update(self, controller):
12 # relations for all sites
13 ctrls_by_site = defaultdict(list)
14 ctrl_sites = ControllerSite.objects.all()
15 for ctrl_site in ctrl_sites:
16 ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id)
Scott Bakera0a688a2017-03-28 11:59:56 -070017
Scott Baker46a3ee92017-05-30 16:44:56 -070018 sites = Site.objects.all()
19 for site in sites:
20 if site.id not in ctrls_by_site or controller.id not in ctrls_by_site[site.id]:
21 controller_site = ControllerSite(controller=controller, site=site)
22 controller_site.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070023
Scott Baker46a3ee92017-05-30 16:44:56 -070024 # relations for all slices
25 ctrls_by_slice = defaultdict(list)
26 ctrl_slices = ControllerSlice.objects.all()
27 for ctrl_slice in ctrl_slices:
28 ctrls_by_slice[ctrl_slice.slice.id].append(ctrl_slice.controller.id)
Scott Bakera0a688a2017-03-28 11:59:56 -070029
Scott Baker46a3ee92017-05-30 16:44:56 -070030 slices = Slice.objects.all()
31 for slice in slices:
32 if slice.id not in ctrls_by_slice or controller.id not in ctrls_by_slice[slice.id]:
33 controller_slice = ControllerSlice(controller=controller, slice=slice)
34 controller_slice.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070035
Scott Baker46a3ee92017-05-30 16:44:56 -070036 # relations for all users
37 ctrls_by_user = defaultdict(list)
38 ctrl_users = ControllerUser.objects.all()
39 for ctrl_user in ctrl_users:
40 ctrls_by_user[ctrl_user.user.id].append(ctrl_user.controller.id)
Scott Bakera0a688a2017-03-28 11:59:56 -070041
Scott Baker46a3ee92017-05-30 16:44:56 -070042 users = User.objects.all()
43 for user in users:
44 if user.id not in ctrls_by_user or controller.id not in ctrls_by_user[user.id]:
45 controller_user = ControllerUser(controller=controller, user=user)
46 controller_user.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070047
Scott Baker46a3ee92017-05-30 16:44:56 -070048 # relations for all networks
49 ctrls_by_network = defaultdict(list)
50 ctrl_networks = ControllerNetwork.objects.all()
51 for ctrl_network in ctrl_networks:
52 ctrls_by_network[ctrl_network.network.id].append(ctrl_network.controller.id)
Scott Bakera0a688a2017-03-28 11:59:56 -070053
Scott Baker46a3ee92017-05-30 16:44:56 -070054 networks = Network.objects.all()
55 for network in networks:
56 if network.id not in ctrls_by_network or controller.id not in ctrls_by_network[network.id]:
57 controller_network = ControllerNetwork(controller=controller, network=network)
58 if network.subnet and network.subnet.strip():
59 controller_network.subnet = network.subnet.strip()
60 controller_network.save()
61
62 # relations for all images
63 ctrls_by_image = defaultdict(list)
64 ctrl_images = ControllerImages.objects.all()
65 for ctrl_image in ctrl_images:
66 ctrls_by_image[ctrl_image.image.id].append(ctrl_image.controller.id)
67
68 images = Image.objects.all()
69 for image in images:
70 if image.id not in ctrls_by_image or controller.id not in ctrls_by_image[image.id]:
71 controller_image = ControllerImages(controller=controller, image=image)
72 controller_image.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070073