CORD-1244 Modify model_policies for OpenStack synchronizer

Change-Id: I8e946e3ca665689bd0449b80d87fe4bb29afde99
diff --git a/xos/synchronizer/model_policies/__init__.py b/xos/synchronizer/model_policies/__init__.py
deleted file mode 100644
index 36c6e25..0000000
--- a/xos/synchronizer/model_policies/__init__.py
+++ /dev/null
@@ -1,12 +0,0 @@
-from .model_policy_Slice import *
-from .model_policy_Instance import *
-from .model_policy_User import *
-from .model_policy_Network import *
-from .model_policy_Site import *
-from .model_policy_SitePrivilege import *
-from .model_policy_SlicePrivilege import *
-from .model_policy_ControllerSlice import *
-from .model_policy_ControllerSite import *
-from .model_policy_ControllerUser import *
-from .model_policy_Controller import *
-from .model_policy_Image import *
diff --git a/xos/synchronizer/model_policies/model_policy_Controller.py b/xos/synchronizer/model_policies/model_policy_Controller.py
index 680f252..45b0fae 100644
--- a/xos/synchronizer/model_policies/model_policy_Controller.py
+++ b/xos/synchronizer/model_policies/model_policy_Controller.py
@@ -1,66 +1,73 @@
 from collections import defaultdict
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(controller):
-    # relations for all sites
-    ctrls_by_site = defaultdict(list)
-    ctrl_sites = ControllerSite.objects.all()
-    for ctrl_site in ctrl_sites:
-        ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id)
+class ControllerPolicy(Policy):
+    model_name = "Controller"
 
-    sites = Site.objects.all()
-    for site in sites:
-        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()
+    def handle_create(self, controller):
+        return self.handle_update(controller)
 
-    # 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.id].append(ctrl_slice.controller.id)
+    def handle_update(self, controller):
+        # relations for all sites
+        ctrls_by_site = defaultdict(list)
+        ctrl_sites = ControllerSite.objects.all()
+        for ctrl_site in ctrl_sites:
+            ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id)
 
-    slices = Slice.objects.all()
-    for slice in slices:
-        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()
+        sites = Site.objects.all()
+        for site in sites:
+            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 users
-    ctrls_by_user = defaultdict(list)
-    ctrl_users = ControllerUser.objects.all()
-    for ctrl_user in ctrl_users:
-        ctrls_by_user[ctrl_user.user.id].append(ctrl_user.controller.id)
+        # 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.id].append(ctrl_slice.controller.id)
 
-    users = User.objects.all()
-    for user in users:
-        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()
+        slices = Slice.objects.all()
+        for slice in slices:
+            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 networks
-    ctrls_by_network = defaultdict(list)
-    ctrl_networks = ControllerNetwork.objects.all()
-    for ctrl_network in ctrl_networks:
-        ctrls_by_network[ctrl_network.network.id].append(ctrl_network.controller.id)
+        # 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.id].append(ctrl_user.controller.id)
 
-    networks = Network.objects.all()
-    for network in networks:
-        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()
+        users = User.objects.all()
+        for user in users:
+            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 images
-    ctrls_by_image = defaultdict(list)
-    ctrl_images = ControllerImages.objects.all()
-    for ctrl_image in ctrl_images:
-        ctrls_by_image[ctrl_image.image.id].append(ctrl_image.controller.id)
+        # 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.id].append(ctrl_network.controller.id)
 
-    images = Image.objects.all()
-    for image in images:
-        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()
+        networks = Network.objects.all()
+        for network in networks:
+            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.id].append(ctrl_image.controller.id)
+
+        images = Image.objects.all()
+        for image in images:
+            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_ControllerSite.py b/xos/synchronizer/model_policies/model_policy_ControllerSite.py
index 3b7a037..e0927b4 100644
--- a/xos/synchronizer/model_policies/model_policy_ControllerSite.py
+++ b/xos/synchronizer/model_policies/model_policy_ControllerSite.py
@@ -1,16 +1,19 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(controller_site):
-    try:
+class ControllerSitePolicy(Policy):
+    model_name = "ControllerSite"
+
+    def handle_create(self, controller_site):
+        return self.handle_update(controller_site)
+
+    def handle_update(self, controller_site):
         my_status_code = int(controller_site.backend_status[0])
         try:
             his_status_code = int(controller_site.site.backend_status[0])
         except:
             his_status_code = 0
- 
+
         if (my_status_code not in [0,his_status_code]):
             controller_site.site.backend_status = controller_site.backend_status
             controller_site.site.save(update_fields = ['backend_status'])
-    except Exception,e:
-        print str(e)	
-        pass
diff --git a/xos/synchronizer/model_policies/model_policy_ControllerSlice.py b/xos/synchronizer/model_policies/model_policy_ControllerSlice.py
index 3c6d54f..867b4c4 100644
--- a/xos/synchronizer/model_policies/model_policy_ControllerSlice.py
+++ b/xos/synchronizer/model_policies/model_policy_ControllerSlice.py
@@ -1,7 +1,13 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(controller_slice):
-    try:
+class ControllerSlicePolicy(Policy):
+    model_name = "ControllerSlice"
+
+    def handle_create(self, controller_slice):
+        return self.handle_update(controller_slice)
+
+    def handle_update(self, controller_slice):
         my_status_code = int(controller_slice.backend_status[0])
         try:
             his_status_code = int(controller_slice.slice.backend_status[0])
@@ -18,8 +24,3 @@
             fields+=['backend_register']
 
         controller_slice.slice.save(update_fields = fields)
-
-        
-    except Exception,e:
-        print str(e)	
-        pass
diff --git a/xos/synchronizer/model_policies/model_policy_ControllerUser.py b/xos/synchronizer/model_policies/model_policy_ControllerUser.py
index 895ebf8..190fb6e 100644
--- a/xos/synchronizer/model_policies/model_policy_ControllerUser.py
+++ b/xos/synchronizer/model_policies/model_policy_ControllerUser.py
@@ -1,7 +1,13 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(controller_user):
-    try:
+class ControllerUserPolicy(Policy):
+    model_name = "ControllerUser"
+
+    def handle_create(self, controller_user):
+        return self.handle_update(controller_user)
+
+    def handle_update(self, controller_user):
         my_status_code = int(controller_user.backend_status[0])
         try:
             his_status_code = int(controller_user.user.backend_status[0])
@@ -11,6 +17,3 @@
         if (my_status_code not in [0,his_status_code]):
             controller_user.user.backend_status = controller_user.backend_status
             controller_user.user.save(update_fields = ['backend_status'])
-    except Exception,e:
-        print str(e)	
-        pass
diff --git a/xos/synchronizer/model_policies/model_policy_Image.py b/xos/synchronizer/model_policies/model_policy_Image.py
index 153b6b3..37936c1 100644
--- a/xos/synchronizer/model_policies/model_policy_Image.py
+++ b/xos/synchronizer/model_policies/model_policy_Image.py
@@ -1,17 +1,24 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(image):
-    if (image.kind == "container"):
-        # container images do not get instantiated
-        return
+class ImagePolicy(Policy):
+    model_name = "Image"
 
-    controller_images = ControllerImages.objects.filter(image_id=image.id)
-    existing_controllers = [cs.controller for cs in controller_images]
-    existing_controller_ids = [c.id for c in existing_controllers]
+    def handle_create(self, image):
+        return self.handle_update(image)
 
-    all_controllers = Controller.objects.all()
-    for controller in all_controllers:
-        if controller.id not in existing_controller_ids:
-            sd = ControllerImages(image=image, controller=controller)
-            sd.save()
+    def handle_update(self, image):
+        if (image.kind == "container"):
+            # container images do not get instantiated
+            return
+
+        controller_images = ControllerImages.objects.filter(image_id=image.id)
+        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.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 db07cf6..53fa9fa 100644
--- a/xos/synchronizer/model_policies/model_policy_Instance.py
+++ b/xos/synchronizer/model_policies/model_policy_Instance.py
@@ -1,59 +1,23 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle_container_on_metal(instance):
-        print "MODEL POLICY: instance", instance, "handle container_on_metal"
+class InstancePolicy(Policy):
+    model_name = "Instance"
 
-        if instance.deleted:
-            return
+    def handle_create(self, instance):
+        return self.handle_update(instance)
 
-        if (instance.isolation in ["container"]) and (instance.slice.network not in ["host", "bridged"]):
-            # Our current docker-on-metal network strategy requires that there be some
-            # VM on the server that connects to the networks, so that
-            # the containers can piggyback off of that configuration.
-            if not Instance.objects.filter(slice_id=instance.slice.id, node_id=instance.node.id, isolation="vm").exists():
-                flavors = Flavor.objects.filter(name="m1.small")
-                if not flavors:
-                    raise XOSConfigurationError("No m1.small flavor")
+    def handle_update(self, instance):
+        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)
 
-                images = Image.objects.filter(kind="vm")
+        # a little clumsy because the API ORM doesn't support __in queries
+        network_ids = [x.id for x in networks]
+        controller_networks = [x for x in controller_networks if x.network.id in network_ids]
 
-                companion_instance = Instance(slice = instance.slice,
-                                node = instance.node,
-                                image = images[0],
-                                creator = instance.creator,
-                                deployment = instance.node.site_deployment.deployment,
-                                flavor = flavors[0])
-                companion_instance.save()
-
-                print "MODEL POLICY: instance", instance, "created companion", companion_instance
-
-        # Add the ports for the container
-        for network in instance.slice.networks.all():
-            # hmmm... The NAT ports never become ready, because sync_ports never
-            # instantiates them. Need to think about this.
-            print "MODEL POLICY: instance", instance, "handling network", network
-            if (network.name.endswith("-nat")):
-                continue
-
-            if not Port.objects.filter(network_id=network.id, instance_id=instance.id).exists():
-                port = Port(network = network, instance=instance)
-                port.save()
-                print "MODEL POLICY: instance", instance, "created port", port
-
-def handle(instance):
-    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]
-    controller_networks = [x for x in controller_networks if x.network.id in network_ids]
-
-    for cn in controller_networks:
-        if (cn.lazy_blocked):
-                print "MODEL POLICY: instance", instance, "unblocking network", cn.network
-		cn.lazy_blocked=False
-		cn.backend_register = '{}'
-		cn.save()
-
-    if (instance.isolation in ["container", "container_vm"]):
-        handle_container_on_metal(instance)
+        for cn in controller_networks:
+            if (cn.lazy_blocked):
+                    self.logger.info("MODEL POLICY: instance %s unblocking network %s" % (instance, cn.network))
+            cn.lazy_blocked=False
+            cn.backend_register = '{}'
+            cn.save()
diff --git a/xos/synchronizer/model_policies/model_policy_Network.py b/xos/synchronizer/model_policies/model_policy_Network.py
index d9ec215..fa90751 100644
--- a/xos/synchronizer/model_policies/model_policy_Network.py
+++ b/xos/synchronizer/model_policies/model_policy_Network.py
@@ -1,46 +1,51 @@
 from synchronizers.new_base.modelaccessor import *
 from collections import defaultdict
+from synchronizers.new_base.policy import Policy
 
-def handle(network):
-        print "MODEL POLICY: network", network
+class NetworkPolicy(Policy):
+    model_name = "Network"
 
-	# network controllers are not visible to users. We must ensure
-	# networks are deployed at all deploymets available to their slices.
+    def handle_create(self, network):
+        return self.handle_update(network)
+
+    def handle_update(self, network):
+        # 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.id].append(slice_controller.controller)
+        slice_controllers = ControllerSlice.objects.all()
+        slice_deploy_lookup = defaultdict(list)
+        for slice_controller in slice_controllers:
+            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.id].append(network_controller.controller.id)
+        network_controllers = ControllerNetwork.objects.all()
+        network_deploy_lookup = defaultdict(list)
+        for network_controller in network_controllers:
+            network_deploy_lookup[network_controller.network.id].append(network_controller.controller.id)
 
-	expected_controllers = slice_deploy_lookup[network.owner.id]
-	for expected_controller in expected_controllers:
-		if network.id not in network_deploy_lookup or expected_controller.id not in network_deploy_lookup[network.id]:
-                        lazy_blocked=True
+        expected_controllers = slice_deploy_lookup[network.owner.id]
+        for expected_controller in expected_controllers:
+            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
-                        for networkslice in network.networkslices.all():
-                            found = False
-                            for instance in networkslice.slice.instances.all():
-                               if instance.node.site_deployment.controller.id == expected_controller.id:
-                                   found = True
-                            if found:
-                               print "MODEL_POLICY: network, setting lazy_blocked to false because instance on controller already exists"
-                               lazy_blocked=False
+                # check and see if some instance already exists
+                for networkslice in network.networkslices.all():
+                    found = False
+                    for instance in networkslice.slice.instances.all():
+                       if instance.node.site_deployment.controller.id == expected_controller.id:
+                           found = True
+                    if found:
+                       self.logger.info("MODEL_POLICY: network %s setting lazy_blocked to false because instance on controller already exists" % network)
+                       lazy_blocked=False
 
-			nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked)
-                        print "MODEL POLICY: network", network, "create ControllerNetwork", nd, "lazy_blocked", lazy_blocked
-                        if network.subnet:
-                            # XXX: Possibly unpredictable behavior if there is
-                            # more than one ControllerNetwork and the subnet
-                            # is specified.
-                            nd.subnet = network.subnet
-			nd.save()
+                nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked)
+                self.logger.info("MODEL POLICY: network %s create ControllerNetwork %s lazy_blocked %s" % (network, nd, lazy_blocked))
+                if network.subnet:
+                    # XXX: Possibly unpredictable behavior if there is
+                    # more than one ControllerNetwork and the subnet
+                    # is specified.
+                    nd.subnet = network.subnet
+                nd.save()
diff --git a/xos/synchronizer/model_policies/model_policy_Site.py b/xos/synchronizer/model_policies/model_policy_Site.py
index 07fe6f3..e0e288d 100644
--- a/xos/synchronizer/model_policies/model_policy_Site.py
+++ b/xos/synchronizer/model_policies/model_policy_Site.py
@@ -1,14 +1,22 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(site):
-    # site = Site.get(site_id)
-    # make sure site has a ControllerSite record for each controller
-    ctrl_sites = ControllerSite.objects.filter(site_id=site.id)
-    existing_controllers = [cs.controller for cs in ctrl_sites]
-    existing_controller_ids = [c.id for c in existing_controllers]
+class SitePolicy(Policy):
+    model_name = "Site"
 
-    all_controllers = Controller.objects.all()
-    for ctrl in all_controllers:
-        if ctrl.id not in existing_controller_ids:
-            ctrl_site = ControllerSite(controller=ctrl, site=site)
-            ctrl_site.save() 
+    def handle_create(self, site):
+        return self.handle_update(site)
+
+    def handle_update(self, site):
+        # site = Site.get(site_id)
+        # make sure site has a ControllerSite record for each controller
+        ctrl_sites = ControllerSite.objects.filter(site_id=site.id)
+        existing_controllers = [cs.controller for cs in ctrl_sites]
+        existing_controller_ids = [c.id for c in existing_controllers]
+
+        all_controllers = Controller.objects.all()
+        for ctrl in all_controllers:
+            if ctrl.id not in existing_controller_ids:
+                ctrl_site = ControllerSite(controller=ctrl, site=site)
+                ctrl_site.save()
+
diff --git a/xos/synchronizer/model_policies/model_policy_SitePrivilege.py b/xos/synchronizer/model_policies/model_policy_SitePrivilege.py
index 2218994..10600fb 100644
--- a/xos/synchronizer/model_policies/model_policy_SitePrivilege.py
+++ b/xos/synchronizer/model_policies/model_policy_SitePrivilege.py
@@ -1,15 +1,22 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(site_privilege):
-    # site_privilege = SitePrivilege.get(site_privilege_id)
-    # apply site privilage at all controllers
-    controller_site_privileges = ControllerSitePrivilege.objects.filter(
-        site_privilege_id = site_privilege.id,
-        )
-    existing_controllers = [sp.controller for sp in controller_site_privileges]
-    all_controllers = Controller.objects.all()
-    for controller in all_controllers:
-        if controller not in existing_controllers:
-            ctrl_site_priv = ControllerSitePrivilege(controller=controller, site_privilege=site_privilege)
-            ctrl_site_priv.save()  
+class SitePrivilegePolicy(Policy):
+    model_name = "SitePrivilege"
+
+    def handle_create(self, site_privilege):
+        return self.handle_update(site_privilege)
+
+    def handle_update(self, site_privilege):
+        # site_privilege = SitePrivilege.get(site_privilege_id)
+        # apply site privilage at all controllers
+        controller_site_privileges = ControllerSitePrivilege.objects.filter(
+            site_privilege_id = site_privilege.id,
+            )
+        existing_controllers = [sp.controller for sp in controller_site_privileges]
+        all_controllers = Controller.objects.all()
+        for controller in all_controllers:
+            if controller not in existing_controllers:
+                ctrl_site_priv = ControllerSitePrivilege(controller=controller, site_privilege=site_privilege)
+                ctrl_site_priv.save()
 
diff --git a/xos/synchronizer/model_policies/model_policy_Slice.py b/xos/synchronizer/model_policies/model_policy_Slice.py
index dfc136a..bcda884 100644
--- a/xos/synchronizer/model_policies/model_policy_Slice.py
+++ b/xos/synchronizer/model_policies/model_policy_Slice.py
@@ -1,104 +1,103 @@
-from xos.config import Config
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle_delete(slice):
-    public_nets = []
-    private_net = None
-    networks = Network.objects.filter(owner_id=slice.id)
+class SlicePolicy(Policy):
+    model_name = "Slice"
 
-    for n in networks:
-        n.delete()
+    def handle_create(self, slice):
+        return self.handle_update(slice)
 
-    # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph
+    def handle_update(self, slice):
+        support_nat_net = False # Assume we're using VTN rather than nat-net
 
-def handle(slice):
-    # only create nat_net if not using VTN
-    support_nat_net = not getattr(Config(), "networking_use_vtn", False)
+        # slice = Slice.get(slice_id)
 
-    print "MODEL POLICY: slice", slice
+        controller_slices = ControllerSlice.objects.filter(slice_id=slice.id)
+        existing_controllers = [cs.controller for cs in controller_slices]
+        existing_controllers_ids = [x.id for x in existing_controllers]
 
-    # slice = Slice.get(slice_id)
+        self.logger.info("MODEL POLICY: slice existing_controllers=%s" % existing_controllers)
 
-    controller_slices = ControllerSlice.objects.filter(slice_id=slice.id)
-    existing_controllers = [cs.controller for cs in controller_slices]
-    existing_controllers_ids = [x.id for x in existing_controllers]
+        all_controllers = Controller.objects.all()
+        for controller in all_controllers:
+            if controller.id not in existing_controllers_ids:
+                self.logger.info("MODEL POLICY: slice adding controller %s" % controller)
+                sd = ControllerSlice(slice=slice, controller=controller)
+                sd.save()
 
-    print "MODEL POLICY: slice existing_controllers=", existing_controllers
+        if slice.network in ["host", "bridged"]:
+            # Host and Bridged docker containers need no networks and they will
+            # only get in the way.
+            self.logger.info("MODEL POLICY: Skipping network creation")
+        elif slice.network in ["noauto"]:
+            # do nothing
+            pass
+        else:
+            # make sure slice has at least 1 public and 1 private networkd
+            public_nets = []
+            private_nets = []
+            networks = Network.objects.filter(owner_id=slice.id)
+            for network in networks:
+                if not network.autoconnect:
+                    continue
+                if network.template.name == 'Public dedicated IPv4':
+                    public_nets.append(network)
+                elif network.template.name == 'Public shared IPv4':
+                    public_nets.append(network)
+                elif network.template.name == 'Private':
+                    private_nets.append(network)
+            if support_nat_net and (not public_nets):
+                # ensure there is at least one public network, and default it to dedicated
+                nat_net = Network(
+                        name = slice.name+'-nat',
+                            template = NetworkTemplate.objects.get(name='Public shared IPv4'),
+                        owner = slice
+                        )
+                if slice.exposed_ports:
+                    nat_net.ports = slice.exposed_ports
+                nat_net.save()
+                public_nets.append(nat_net)
+                self.logger.info("MODEL POLICY: slice %s made nat-net" % slice)
 
-    all_controllers = Controller.objects.all()
-    for controller in all_controllers:
-        if controller.id not in existing_controllers_ids:
-            print "MODEL POLICY: slice adding controller", controller
-            sd = ControllerSlice(slice=slice, controller=controller)
-            sd.save()
-
-    if slice.network in ["host", "bridged"]:
-        # Host and Bridged docker containers need no networks and they will
-        # only get in the way.
-        print "MODEL POLICY: Skipping network creation"
-    elif slice.network in ["noauto"]:
-        # do nothing
-        pass
-    else:
-        # make sure slice has at least 1 public and 1 private networkd
-        public_nets = []
-        private_nets = []
-        networks = Network.objects.filter(owner_id=slice.id)
-        for network in networks:
-            if not network.autoconnect:
-                continue
-            if network.template.name == 'Public dedicated IPv4':
-                public_nets.append(network)
-            elif network.template.name == 'Public shared IPv4':
-                public_nets.append(network)
-            elif network.template.name == 'Private':
-                private_nets.append(network)
-        if support_nat_net and (not public_nets):
-            # ensure there is at least one public network, and default it to dedicated
-            nat_net = Network(
-                    name = slice.name+'-nat',
-                        template = NetworkTemplate.objects.get(name='Public shared IPv4'),
+            if not private_nets:
+                private_net = Network(
+                    name = slice.name+'-private',
+                    template = NetworkTemplate.objects.get(name='Private'),
                     owner = slice
-                    )
-            if slice.exposed_ports:
-                nat_net.ports = slice.exposed_ports
-            nat_net.save()
-            public_nets.append(nat_net)
-            print "MODEL POLICY: slice", slice, "made nat-net"
+                )
+                private_net.save()
+                self.logger.info("MODEL POLICY: slice %s made private net" % slice)
+                private_nets = [private_net]
+            # create slice networks
+            public_net_slice = None
+            private_net_slice = None
 
-        if not private_nets:
-            private_net = Network(
-                name = slice.name+'-private',
-                template = NetworkTemplate.objects.get(name='Private'),
-                owner = slice
-            )
-            private_net.save()
-            print "MODEL POLICY: slice", slice, "made private net"
-            private_nets = [private_net]
-        # create slice networks
-        public_net_slice = None
-        private_net_slice = None
+            public_net_ids = [x.id for x in public_nets]
+            private_net_ids = [x.id for x in private_nets]
+            net_slices = NetworkSlice.objects.filter(slice_id=slice.id)
+            net_slices = [x for x in net_slices if x.network_id in public_net_ids+private_net_ids]
 
-        public_net_ids = [x.id for x in public_nets]
-        private_net_ids = [x.id for x in private_nets]
-        net_slices = NetworkSlice.objects.filter(slice_id=slice.id)
-        net_slices = [x for x in net_slices if x.network_id in public_net_ids+private_net_ids]
+            for net_slice in net_slices:
+                if net_slice.network.id in public_nets_ids:
+                    public_net_slice = net_slice
+                elif net_slice.network.id in private_nets_ids:
+                    private_net_slice = net_slice
+            if support_nat_net and (not public_net_slice):
+                public_net_slice = NetworkSlice(slice=slice, network=public_nets[0])
+                public_net_slice.save()
+                self.logger.info("MODEL POLICY: slice %s made public_net_slice" % slice)
+            if not private_net_slice:
+                private_net_slice = NetworkSlice(slice=slice, network=private_nets[0])
+                private_net_slice.save()
+                self.logger.info("MODEL POLICY: slice %s made private_net_slice" % slice)
 
-        for net_slice in net_slices:
-            if net_slice.network.id in public_nets_ids:
-                public_net_slice = net_slice
-            elif net_slice.network.id in private_nets_ids:
-                private_net_slice = net_slice
-                
-        if support_nat_net and (not public_net_slice):
-            public_net_slice = NetworkSlice(slice=slice, network=public_nets[0])
-            public_net_slice.save()
-            print "MODEL POLICY: slice", slice, "made public_net_slice"
-        if not private_net_slice:
-            private_net_slice = NetworkSlice(slice=slice, network=private_nets[0])
-            private_net_slice.save()
-            print "MODEL POLICY: slice", slice, "made private_net_slice"
+    # TODO: This feels redundant with the reaper
+    def handle_delete(slice):
+        public_nets = []
+        private_net = None
+        networks = Network.objects.filter(owner_id=slice.id)
 
-    print "MODEL POLICY: slice", slice, "DONE"
+        for n in networks:
+            n.delete()
 
-
+        # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph
diff --git a/xos/synchronizer/model_policies/model_policy_SlicePrivilege.py b/xos/synchronizer/model_policies/model_policy_SlicePrivilege.py
index f4fab58..428c7e3 100644
--- a/xos/synchronizer/model_policies/model_policy_SlicePrivilege.py
+++ b/xos/synchronizer/model_policies/model_policy_SlicePrivilege.py
@@ -1,15 +1,22 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(slice_privilege):
-    # slice_privilege = SlicePrivilege.get(slice_privilege_id)
-    # apply slice privilage at all controllers
-    controller_slice_privileges = ControllerSlicePrivilege.objects.filter(
-        slice_privilege = slice_privilege,
-        )
-    existing_controllers = [sp.controller for sp in controller_slice_privileges]
-    all_controllers = Controller.objects.all()
-    for controller in all_controllers:
-        if controller not in existing_controllers:
-            ctrl_slice_priv = ControllerSlicePrivilege(controller=controller, slice_privilege=slice_privilege)
-            ctrl_slice_priv.save()  
+class SlicePrivilegePolicy(Policy):
+    model_name = "SlicePrivilege"
+
+    def handle_create(self, slice_privilege):
+        return self.handle_update(slice_privilege)
+
+    def handle_update(self, slice_privilege):
+        # slice_privilege = SlicePrivilege.get(slice_privilege_id)
+        # apply slice privilage at all controllers
+        controller_slice_privileges = ControllerSlicePrivilege.objects.filter(
+            slice_privilege = slice_privilege,
+            )
+        existing_controllers = [sp.controller for sp in controller_slice_privileges]
+        all_controllers = Controller.objects.all()
+        for controller in all_controllers:
+            if controller not in existing_controllers:
+                ctrl_slice_priv = ControllerSlicePrivilege(controller=controller, slice_privilege=slice_privilege)
+                ctrl_slice_priv.save()
 
diff --git a/xos/synchronizer/model_policies/model_policy_Sliver.py b/xos/synchronizer/model_policies/model_policy_Sliver.py
deleted file mode 100644
index 2f1f203..0000000
--- a/xos/synchronizer/model_policies/model_policy_Sliver.py
+++ /dev/null
@@ -1,13 +0,0 @@
-from synchronizers.new_base.modelaccessor import *
-
-def handle(instance):
-    networks = [ns.network for ns in NetworkSlice.objects.filter(slice_id=instance.slice.id)]
-    network_ids = [x.id for x in networks]
-    controller_networks = ControllerNetwork.objects.filter(controller_id=instance.node.site_deployment.controller.id)
-    controller_networks = [x for x in controller_networks if x.network_id in network_ids]
-
-    for cn in controller_networks:
-        if (cn.lazy_blocked):	
-		cn.lazy_blocked=False
-		cn.backend_register = '{}'
-		cn.save()
diff --git a/xos/synchronizer/model_policies/model_policy_User.py b/xos/synchronizer/model_policies/model_policy_User.py
index 448b58f..79c0112 100644
--- a/xos/synchronizer/model_policies/model_policy_User.py
+++ b/xos/synchronizer/model_policies/model_policy_User.py
@@ -1,12 +1,19 @@
 from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.policy import Policy
 
-def handle(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.id not in existing_controller_ids:
-            ctrl_user = ControllerUser(controller=controller, user=user)
-            ctrl_user.save()  
+class UserPolicy(Policy):
+    model_name = "User"
+
+    def handle_create(self, user):
+        return self.handle_update(user)
+
+    def handle_update(self, 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.id not in existing_controller_ids:
+                ctrl_user = ControllerUser(controller=controller, user=user)
+                ctrl_user.save()