blob: 215f537589b5e37fc1466ab96b03a57605f1791b [file] [log] [blame]
Andy Bavier89a95422016-11-02 14:38:39 -04001
Matteo Scandolof5e10332017-08-08 13:05:25 -07002# 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
17
Andy Bavier89a95422016-11-02 14:38:39 -040018def handle(controller):
19 from core.models import Controller, Site, ControllerSite, Slice, ControllerSlice, User, ControllerUser, ControllerImages, ControllerNetwork, Image, Network
20 from collections import defaultdict
21
22 # relations for all sites
23 ctrls_by_site = defaultdict(list)
24 ctrl_sites = ControllerSite.objects.all()
25 for ctrl_site in ctrl_sites:
26 ctrls_by_site[ctrl_site.site].append(ctrl_site.controller)
27 sites = Site.objects.all()
28 for site in sites:
29 if site not in ctrls_by_site or \
30 controller not in ctrls_by_site[site]:
31 controller_site = ControllerSite(controller=controller, site=site)
32 controller_site.save()
33 # relations for all slices
34 ctrls_by_slice = defaultdict(list)
35 ctrl_slices = ControllerSlice.objects.all()
36 for ctrl_slice in ctrl_slices:
37 ctrls_by_slice[ctrl_slice.slice].append(ctrl_slice.controller)
38 slices = Slice.objects.all()
39 for slice in slices:
40 if slice not in ctrls_by_slice or \
41 controller not in ctrls_by_slice[slice]:
42 controller_slice = ControllerSlice(controller=controller, slice=slice)
43 controller_slice.save()
44 # relations for all users
45 ctrls_by_user = defaultdict(list)
46 ctrl_users = ControllerUser.objects.all()
47 for ctrl_user in ctrl_users:
48 ctrls_by_user[ctrl_user.user].append(ctrl_user.controller)
49 users = User.objects.all()
50 for user in users:
51 if user not in ctrls_by_user or \
52 controller not in ctrls_by_user[user]:
53 controller_user = ControllerUser(controller=controller, user=user)
54 controller_user.save()
55 # relations for all networks
56 ctrls_by_network = defaultdict(list)
57 ctrl_networks = ControllerNetwork.objects.all()
58 for ctrl_network in ctrl_networks:
59 ctrls_by_network[ctrl_network.network].append(ctrl_network.controller)
60 networks = Network.objects.all()
61 for network in networks:
62 if network not in ctrls_by_network or \
63 controller not in ctrls_by_network[network]:
64 controller_network = ControllerNetwork(controller=controller, network=network)
65 if network.subnet and network.subnet.strip():
66 controller_network.subnet = network.subnet.strip()
67 controller_network.save()
68 # relations for all images
69 ctrls_by_image = defaultdict(list)
70 ctrl_images = ControllerImages.objects.all()
71 for ctrl_image in ctrl_images:
72 ctrls_by_image[ctrl_image.image].append(ctrl_image.controller)
73 images = Image.objects.all()
74 for image in images:
75 if image not in ctrls_by_image or \
76 controller not in ctrls_by_image[image]:
77 controller_image = ControllerImages(controller=controller, image=image)
78 controller_image.save()