CORD-1010 Fix Controller, Image, Network, and User model_policies
Change-Id: I42f8771048090666acd9d75ca32b6081a72ce880
diff --git a/xos/synchronizer/model_policies/model_policy_Controller.py b/xos/synchronizer/model_policies/model_policy_Controller.py
index 2ff25d4..680f252 100644
--- a/xos/synchronizer/model_policies/model_policy_Controller.py
+++ b/xos/synchronizer/model_policies/model_policy_Controller.py
@@ -6,56 +6,61 @@
ctrls_by_site = defaultdict(list)
ctrl_sites = ControllerSite.objects.all()
for ctrl_site in ctrl_sites:
- ctrls_by_site[ctrl_site.site].append(ctrl_site.controller)
+ ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id)
+
sites = Site.objects.all()
for site in sites:
- if site not in ctrls_by_site or \
- controller not in ctrls_by_site[site]:
+ if site.id not in ctrls_by_site or controller.id not in ctrls_by_site[site.id]:
controller_site = ControllerSite(controller=controller, site=site)
controller_site.save()
+
# relations for all slices
ctrls_by_slice = defaultdict(list)
ctrl_slices = ControllerSlice.objects.all()
for ctrl_slice in ctrl_slices:
- ctrls_by_slice[ctrl_slice.slice].append(ctrl_slice.controller)
+ ctrls_by_slice[ctrl_slice.slice.id].append(ctrl_slice.controller.id)
+
slices = Slice.objects.all()
for slice in slices:
- if slice not in ctrls_by_slice or \
- controller not in ctrls_by_slice[slice]:
+ if slice.id not in ctrls_by_slice or controller.id not in ctrls_by_slice[slice.id]:
controller_slice = ControllerSlice(controller=controller, slice=slice)
controller_slice.save()
+
# relations for all users
ctrls_by_user = defaultdict(list)
ctrl_users = ControllerUser.objects.all()
for ctrl_user in ctrl_users:
- ctrls_by_user[ctrl_user.user].append(ctrl_user.controller)
+ ctrls_by_user[ctrl_user.user.id].append(ctrl_user.controller.id)
+
users = User.objects.all()
for user in users:
- if user not in ctrls_by_user or \
- controller not in ctrls_by_user[user]:
+ if user.id not in ctrls_by_user or controller.id not in ctrls_by_user[user.id]:
controller_user = ControllerUser(controller=controller, user=user)
controller_user.save()
+
# relations for all networks
ctrls_by_network = defaultdict(list)
ctrl_networks = ControllerNetwork.objects.all()
for ctrl_network in ctrl_networks:
- ctrls_by_network[ctrl_network.network].append(ctrl_network.controller)
+ ctrls_by_network[ctrl_network.network.id].append(ctrl_network.controller.id)
+
networks = Network.objects.all()
for network in networks:
- if network not in ctrls_by_network or \
- controller not in ctrls_by_network[network]:
+ if network.id not in ctrls_by_network or controller.id not in ctrls_by_network[network.id]:
controller_network = ControllerNetwork(controller=controller, network=network)
if network.subnet and network.subnet.strip():
controller_network.subnet = network.subnet.strip()
controller_network.save()
+
# relations for all images
ctrls_by_image = defaultdict(list)
ctrl_images = ControllerImages.objects.all()
for ctrl_image in ctrl_images:
- ctrls_by_image[ctrl_image.image].append(ctrl_image.controller)
+ ctrls_by_image[ctrl_image.image.id].append(ctrl_image.controller.id)
+
images = Image.objects.all()
for image in images:
- if image not in ctrls_by_image or \
- controller not in ctrls_by_image[image]:
+ if image.id not in ctrls_by_image or controller.id not in ctrls_by_image[image.id]:
controller_image = ControllerImages(controller=controller, image=image)
controller_image.save()
+
diff --git a/xos/synchronizer/model_policies/model_policy_Image.py b/xos/synchronizer/model_policies/model_policy_Image.py
index 7e1a6f3..153b6b3 100644
--- a/xos/synchronizer/model_policies/model_policy_Image.py
+++ b/xos/synchronizer/model_policies/model_policy_Image.py
@@ -6,11 +6,12 @@
return
controller_images = ControllerImages.objects.filter(image_id=image.id)
- existing_controllers = [cs.controller for cs in controller_images]
-
- all_controllers = Controller.objects.all()
+ existing_controllers = [cs.controller for cs in controller_images]
+ existing_controller_ids = [c.id for c in existing_controllers]
+
+ all_controllers = Controller.objects.all()
for controller in all_controllers:
- if controller not in existing_controllers:
+ if controller.id not in existing_controller_ids:
sd = ControllerImages(image=image, controller=controller)
sd.save()
diff --git a/xos/synchronizer/model_policies/model_policy_Instance.py b/xos/synchronizer/model_policies/model_policy_Instance.py
index c810a96..db07cf6 100644
--- a/xos/synchronizer/model_policies/model_policy_Instance.py
+++ b/xos/synchronizer/model_policies/model_policy_Instance.py
@@ -41,8 +41,8 @@
print "MODEL POLICY: instance", instance, "created port", port
def handle(instance):
- networks = [ns.network for ns in NetworkSlice.objects.filter(slice=instance.slice)]
- controller_networks = ControllerNetwork.objects.filter(controller=instance.node.site_deployment.controller)
+ networks = [ns.network for ns in NetworkSlice.objects.filter(slice_id=instance.slice.id)]
+ controller_networks = ControllerNetwork.objects.filter(controller_id=instance.node.site_deployment.controller.id)
# a little clumsy because the API ORM doesn't support __in queries
network_ids = [x.id for x in networks]
diff --git a/xos/synchronizer/model_policies/model_policy_Network.py b/xos/synchronizer/model_policies/model_policy_Network.py
index b3f45ce..d9ec215 100644
--- a/xos/synchronizer/model_policies/model_policy_Network.py
+++ b/xos/synchronizer/model_policies/model_policy_Network.py
@@ -4,23 +4,26 @@
def handle(network):
print "MODEL POLICY: network", network
- # network = Network.get(network_id)
# network controllers are not visible to users. We must ensure
# networks are deployed at all deploymets available to their slices.
+
+ # TODO: should be possible to query only the ControllerSlice objects
+ # associated with network.owner rather than iterating through
+ # all ControllerSlice.
+
slice_controllers = ControllerSlice.objects.all()
slice_deploy_lookup = defaultdict(list)
for slice_controller in slice_controllers:
- slice_deploy_lookup[slice_controller.slice].append(slice_controller.controller)
+ slice_deploy_lookup[slice_controller.slice.id].append(slice_controller.controller)
network_controllers = ControllerNetwork.objects.all()
network_deploy_lookup = defaultdict(list)
for network_controller in network_controllers:
- network_deploy_lookup[network_controller.network].append(network_controller.controller)
+ network_deploy_lookup[network_controller.network.id].append(network_controller.controller.id)
- expected_controllers = slice_deploy_lookup[network.owner]
+ expected_controllers = slice_deploy_lookup[network.owner.id]
for expected_controller in expected_controllers:
- if network not in network_deploy_lookup or \
- expected_controller not in network_deploy_lookup[network]:
+ if network.id not in network_deploy_lookup or expected_controller.id not in network_deploy_lookup[network.id]:
lazy_blocked=True
# check and see if some instance already exists
diff --git a/xos/synchronizer/model_policies/model_policy_User.py b/xos/synchronizer/model_policies/model_policy_User.py
index cc37db1..448b58f 100644
--- a/xos/synchronizer/model_policies/model_policy_User.py
+++ b/xos/synchronizer/model_policies/model_policy_User.py
@@ -1,13 +1,12 @@
from synchronizers.new_base.modelaccessor import *
def handle(user):
- # user = User.get(user_id)
-
- controller_users = ControllerUser.objects.filter(user=user)
+ controller_users = ControllerUser.objects.filter(user_id=user.id)
existing_controllers = [cu.controller for cu in controller_users]
+ existing_controller_ids = [c.id for c in existing_controllers]
all_controllers = Controller.objects.all()
for controller in all_controllers:
- if controller not in existing_controllers:
+ if controller.id not in existing_controller_ids:
ctrl_user = ControllerUser(controller=controller, user=user)
ctrl_user.save()
diff --git a/xos/synchronizer/steps/sync_instances.py b/xos/synchronizer/steps/sync_instances.py
index bb6c665..640c712 100644
--- a/xos/synchronizer/steps/sync_instances.py
+++ b/xos/synchronizer/steps/sync_instances.py
@@ -88,7 +88,7 @@
# sanity check - make sure model_policy for slice has run
if ((not instance.slice.policed) or (instance.slice.policed < instance.slice.updated)):
- raise DeferredException("Instance %s waiting on Slice %s to execute model policies" % (instance, slice.name))
+ raise DeferredException("Instance %s waiting on Slice %s to execute model policies" % (instance, instance.slice.name))
# sanity check - make sure model_policy for all slice networks have run
for network in instance.slice.ownedNetworks.all():