CORD-1010 refactor openstack synchronizer to use new_base

Change-Id: I4eb4e5131275faf69e1da05efc81a3d8d0d12296
diff --git a/xos/synchronizer/steps/sync_instances.py b/xos/synchronizer/steps/sync_instances.py
index 395b9b1..3682fd3 100644
--- a/xos/synchronizer/steps/sync_instances.py
+++ b/xos/synchronizer/steps/sync_instances.py
@@ -1,16 +1,13 @@
 import os
 import base64
 import socket
-from django.db.models import F, Q
 from xos.config import Config
 from xos.settings import RESTAPI_HOSTNAME, RESTAPI_PORT
 from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
-from core.models.instance import Instance
-from core.models.slice import Slice, SlicePrivilege, ControllerSlice
-from core.models.network import Network, NetworkSlice, ControllerNetwork
-from synchronizers.base.ansible_helper import *
-from synchronizers.base.syncstep import *
+from synchronizers.new_base.ansible_helper import *
+from synchronizers.new_base.syncstep import *
 from xos.logger import observer_logger as logger
+from synchronizers.new_base.modelaccessor import *
 
 def escape(s):
     s = s.replace('\n',r'\n').replace('"',r'\"')
@@ -105,7 +102,7 @@
             if tag.name.startswith("sysctl-"):
                 metadata_update[tag.name] = tag.value
 
-	slice_memberships = SlicePrivilege.objects.filter(slice=instance.slice)
+	slice_memberships = SlicePrivilege.objects.filter(slice_id=instance.slice.id)
         pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
         if instance.creator.public_key:
             pubkeys.add(instance.creator.public_key)
@@ -120,23 +117,25 @@
 
         # handle ports the were created by the user
         port_ids=[]
-        for port in Port.objects.filter(instance=instance):
+        for port in Port.objects.filter(instance_id=instance.id):
             if not port.port_id:
                 raise DeferredException("Instance %s waiting on port %s" % (instance, port))
             nics.append({"kind": "port", "value": port.port_id, "network": port.network})
 
         # we want to exclude from 'nics' any network that already has a Port
-        existing_port_networks = [port.network for port in Port.objects.filter(instance=instance)]
+        existing_port_networks = [port.network for port in Port.objects.filter(instance_id=instance.id)]
+        existing_port_network_ids = [x.id for x in existing_port_networks]
 
-        networks = [ns.network for ns in NetworkSlice.objects.filter(slice=instance.slice) if ns.network not in existing_port_networks]
-        controller_networks = ControllerNetwork.objects.filter(network__in=networks,
-                                                                controller=instance.node.site_deployment.controller)
+        networks = [ns.network for ns in NetworkSlice.objects.filter(slice_id=instance.slice.id) if ns.network.id not in existing_port_network_ids]
+        networks_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.id in networks_ids]
+
 
         for network in networks:
-           if not ControllerNetwork.objects.filter(network=network, controller=instance.node.site_deployment.controller).exists():
+           if not ControllerNetwork.objects.filter(network_id=network.id, controller_id=instance.node.site_deployment.controller.id).exists():
               raise DeferredException("Instance %s Private Network %s lacks ControllerNetwork object" % (instance, network.name))
 
-        #controller_networks = self.sort_controller_networks(controller_networks)
         for controller_network in controller_networks:
             # Lenient exception - causes slow backoff
             if controller_network.network.template.translation == 'none':
@@ -148,7 +147,6 @@
         network_templates = [network.template.shared_network_name for network in networks \
                              if network.template.shared_network_name]
 
-        #driver = self.driver.client_driver(caller=instance.creator, tenant=instance.slice.name, controller=instance.controllerNetwork)
         driver = self.driver.admin_driver(tenant='admin', controller=instance.node.site_deployment.controller)
         nets = driver.shell.neutron.list_networks()['networks']
         for net in nets:
@@ -163,7 +161,8 @@
         nics = self.sort_nics(nics)
 
         image_name = None
-        controller_images = instance.image.controllerimages.filter(controller=instance.node.site_deployment.controller)
+        controller_images = instance.image.controllerimages.all()
+        controller_images = [x for x in controller_images if x.controller_id==instance.node.site_deployment.controller.id]
         if controller_images:
             image_name = controller_images[0].image.name
             logger.info("using image from ControllerImage object: " + str(image_name))