SEBA-405 Update SimpleExampleService to use synchronizer library

Change-Id: Ibe0d8cd56325cdc2f6f7a4752c4012fad4162121
diff --git a/Dockerfile.synchronizer b/Dockerfile.synchronizer
index d2b1b2a..0137f8b 100644
--- a/Dockerfile.synchronizer
+++ b/Dockerfile.synchronizer
@@ -14,7 +14,7 @@
 
 # xosproject/simpleexampleservice-synchronizer
 
-FROM xosproject/xos-synchronizer-base:2.1.32
+FROM xosproject/xos-synchronizer-base:2.1.38
 
 COPY xos/synchronizer /opt/xos/synchronizers/simpleexampleservice
 COPY VERSION /opt/xos/synchronizers/simpleexampleservice/
diff --git a/VERSION b/VERSION
index e25d8d9..0664a8f 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.1.5
+1.1.6
diff --git a/xos/synchronizer/event_steps/simpleexampleevent.py b/xos/synchronizer/event_steps/simpleexampleevent.py
index a0b1ec4..07374b9 100644
--- a/xos/synchronizer/event_steps/simpleexampleevent.py
+++ b/xos/synchronizer/event_steps/simpleexampleevent.py
@@ -17,8 +17,7 @@
 import json
 import os
 import sys
-from synchronizers.new_base.eventstep import EventStep
-from synchronizers.new_base.modelaccessor import *
+from xossynchronizer.event_steps.eventstep import EventStep
 from xosconfig import Config
 from multistructlog import create_logger
 
@@ -36,7 +35,7 @@
         service_instance_name = value["service_instance"]
         tenant_message = value["tenant_message"]
 
-        objs = SimpleExampleServiceInstance.objects.filter(name=service_instance_name)
+        objs = self.model_accessor.SimpleExampleServiceInstance.objects.filter(name=service_instance_name)
         if not objs:
             raise Exception("failed to find %s" % service_instance_name)
 
diff --git a/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py b/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py
index 63db3ef..169024f 100644
--- a/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py
@@ -16,8 +16,8 @@
 import base64
 import jinja2
 import json
-from synchronizers.new_base.modelaccessor import *
-from synchronizers.new_base.policy import Policy
+import os
+from xossynchronizer.model_policies.policy import Policy
 
 from xosconfig import Config
 from multistructlog import create_logger
@@ -57,8 +57,10 @@
     def handle_update(self, service_instance):
         if not service_instance.compute_instance:
             # TODO: Break dependency
-            compute_service = KubernetesService.objects.first()
-            compute_service_instance_class = Service.objects.get(id=compute_service.id).get_service_instance_class()
+            compute_service = self.model_accessor.KubernetesService.objects.first()
+            compute_service_instance_class = self.model_accessor.Service.objects.get(
+                id=compute_service.id
+            ).get_service_instance_class()
 
             exampleservice = service_instance.owner.leaf_model
 
@@ -74,11 +76,11 @@
 
             # Create a configmap and attach it to the compute instance
             data = {"index.html": self.render_index(service_instance)}
-            cfmap = KubernetesConfigMap(name="simpleexampleserviceinstance-map-%s" % service_instance.id,
+            cfmap = self.model_accessor.KubernetesConfigMap(name="simpleexampleserviceinstance-map-%s" % service_instance.id,
                                       trust_domain=slice.trust_domain,
                                       data=json.dumps(data))
             cfmap.save()
-            cfmap_mnt = KubernetesConfigVolumeMount(config=cfmap,
+            cfmap_mnt = self.model_accessor.KubernetesConfigVolumeMount(config=cfmap,
                                                     service_instance=compute_service_instance,
                                                     mount_path="/usr/local/apache2/htdocs")
             cfmap_mnt.save()
@@ -86,11 +88,14 @@
             # Create a secret and attach it to the compute instance
             data = {"service_secret.txt": base64.b64encode(str(exampleservice.service_secret)),
                     "tenant_secret.txt": base64.b64encode(str(service_instance.tenant_secret))}
-            secret = KubernetesSecret(name="simpleexampleserviceinstance-secret-%s" % service_instance.id,
+            secret = self.model_accessor.KubernetesSecret(name="simpleexampleserviceinstance-secret-%s" % service_instance.id,
                                       trust_domain=slice.trust_domain,
                                       data=json.dumps(data))
             secret.save()
-            secret_mnt = KubernetesSecretVolumeMount(secret=secret, service_instance=compute_service_instance, mount_path="/usr/local/apache2/secrets")
+            secret_mnt = self.model_accessor.KubernetesSecretVolumeMount(
+                secret=secret,
+                service_instance=compute_service_instance,
+                mount_path="/usr/local/apache2/secrets")
             secret_mnt.save()
 
             compute_service_instance.no_sync = False
diff --git a/xos/synchronizer/simpleexampleservice-synchronizer.py b/xos/synchronizer/simpleexampleservice-synchronizer.py
index 7caa954..26cc382 100644
--- a/xos/synchronizer/simpleexampleservice-synchronizer.py
+++ b/xos/synchronizer/simpleexampleservice-synchronizer.py
@@ -16,17 +16,18 @@
 
 # Runs the standard XOS synchronizer
 
-import importlib
 import os
-import sys
+from xossynchronizer import Synchronizer
 from xosconfig import Config
 
-config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/simpleexampleservice_config.yaml')
-Config.init(config_file, 'synchronizer-config-schema.yaml')
+base_config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/simpleexampleservice_config.yaml')
+mounted_config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/mounted_config.yaml')
 
-synchronizer_path = os.path.join(os.path.dirname(
-    os.path.realpath(__file__)), "../../synchronizers/new_base")
-sys.path.append(synchronizer_path)
-mod = importlib.import_module("xos-synchronizer")
-mod.main()
+if os.path.isfile(mounted_config_file):
+    Config.init(base_config_file, 'synchronizer-config-schema.yaml', mounted_config_file)
+else:
+    Config.init(base_config_file, 'synchronizer-config-schema.yaml')
+
+Synchronizer().run()
+
 
diff --git a/xos/synchronizer/steps/sync_simpleexampleserviceinstance.py b/xos/synchronizer/steps/sync_simpleexampleserviceinstance.py
index c8e7c52..73c5859 100644
--- a/xos/synchronizer/steps/sync_simpleexampleserviceinstance.py
+++ b/xos/synchronizer/steps/sync_simpleexampleserviceinstance.py
@@ -16,25 +16,16 @@
 
 import os
 import sys
-from synchronizers.new_base.syncstep import SyncStep
-from synchronizers.new_base.modelaccessor import *
+from xossynchronizer.steps.syncstep import SyncStep
 from xosconfig import Config
 from multistructlog import create_logger
 
 log = create_logger(Config().get('logging'))
 
 class SyncSimpleExampleServiceInstance(SyncStep):
-
-    provides = [SimpleExampleServiceInstance]
-
-    observes = SimpleExampleServiceInstance
-
+    observes = "SimpleExampleServiceInstance"
     requested_interval = 0
 
-    template_name = "simpleexampleserviceinstance_playbook.yaml"
-
-    service_key_name = "/opt/xos/synchronizers/exampleservicenew/simpleexampleservice_private_key"
-
     def __init__(self, *args, **kwargs):
         super(SyncSimpleExampleServiceInstance, self).__init__(*args, **kwargs)
 
diff --git a/xos/synchronizer/tests/test_model_policy_simpleexampleserviceinstance.py b/xos/synchronizer/tests/test_model_policy_simpleexampleserviceinstance.py
index 1960d84..12d7ba5 100644
--- a/xos/synchronizer/tests/test_model_policy_simpleexampleserviceinstance.py
+++ b/xos/synchronizer/tests/test_model_policy_simpleexampleserviceinstance.py
@@ -33,6 +33,7 @@
                                                     ("kubernetes-service", "kubernetes.xproto")] )
 
         self.MockObjectList = self.unittest_setup["MockObjectList"]
+        self.model_accessor = self.unittest_setup["model_accessor"]
 
         sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../model_policies"))
 
@@ -66,7 +67,7 @@
                                               owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
             si.embedded_images = self.MockObjectList([])
 
-            step = self.policy_class()
+            step = self.policy_class(model_accessor=self.model_accessor)
 
             desired_data = json.dumps({"index.html": step.render_index(si)})
 
@@ -136,7 +137,7 @@
             si.compute_instance = ksi
             ksi.kubernetes_config_volume_mounts = self.MockObjectList([cfm_mnt])
 
-            step = self.policy_class()
+            step = self.policy_class(model_accessor=self.model_accessor)
 
             desired_data = json.dumps({"index.html": step.render_index(si)})
 
@@ -166,7 +167,7 @@
             k8s_service_objects.return_value = [self.k8s_service]
             service_objects.return_value = [self.k8s_service, self.service]
 
-            step = self.policy_class()
+            step = self.policy_class(model_accessor=self.model_accessor)
 
             si = SimpleExampleServiceInstance(name="test-simple-instance",
                                               id=1112,
@@ -205,7 +206,7 @@
                                             name="simpleexampleserviceinstance-1112")
             si.compute_instance = ksi
 
-            step = self.policy_class()
+            step = self.policy_class(model_accessor=self.model_accessor)
 
             step.handle_delete(si)
 
diff --git a/xos/synchronizer/tests/unit_test_common.py b/xos/synchronizer/tests/unit_test_common.py
index 68f6743..c2cdde8 100644
--- a/xos/synchronizer/tests/unit_test_common.py
+++ b/xos/synchronizer/tests/unit_test_common.py
@@ -18,15 +18,14 @@
 
 def setup_sync_unit_test(test_path, globals_dict, models, config_fn="test_config.yaml"):
     """ Perform the common steps associated with setting up a synchronizer unit test.
-           1) Add synchronizers/new_base to sys.path
-           2) Import xosconfig.Config and set it up to test_config.yaml in the current dir
-           3) Build the mock modelaccessor and import it
-           4) Import all model accessor classes into global space
+           1) Import xosconfig.Config and set it up to test_config.yaml in the current dir
+           2) Build the mock modelaccessor and import it
+           3) Import all model accessor classes into global space
 
         Arguments:
             test_path - path to the test case that is being run
             globals_dict - a dictionary to add global models to
-            models - a list of pairs (service_name, xproto_name,
+            models - a list of pairs (service_name, xproto_name)
             config_fn - filename of config file)
 
         Returns:
@@ -37,39 +36,23 @@
                 xos_dir: xos directory
                 services_dir: services directory
     """
-    def get_models_fn(services_dir, service_name, xproto_name):
-        name = os.path.join(service_name, "xos", xproto_name)
-        if os.path.exists(os.path.join(services_dir, name)):
-            return name
-        else:
-            name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
-            if os.path.exists(os.path.join(services_dir, name)):
-                return name
-        raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
-
     sys_path_save = sys.path
 
-    xos_dir = os.path.join(test_path, "../../..")
-    if not os.path.exists(os.path.join(test_path, "new_base")):
-        xos_dir = os.path.join(test_path, "../../../../../../orchestration/xos/xos")
-        services_dir = os.path.join(xos_dir, "../../xos_services")
-    sys.path.append(xos_dir)
-    sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
-
     # Setting up the config module
     from xosconfig import Config
     config = os.path.join(test_path, config_fn)
     Config.clear()
     Config.init(config, "synchronizer-config-schema.yaml")
 
-    xprotos = []
-    for (service_name, xproto_name) in models:
-        xprotos.append(get_models_fn(services_dir, service_name, xproto_name))
+    from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+    mock_modelaccessor_config(test_path, models)
 
-    from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-    build_mock_modelaccessor(xos_dir, services_dir, xprotos)
-    import synchronizers.new_base.modelaccessor
-    from synchronizers.new_base.modelaccessor import model_accessor
+    import xossynchronizer.modelaccessor
+    reload(xossynchronizer.modelaccessor)  # in case nose2 loaded it in a previous testp
+
+    from xossynchronizer.modelaccessor import model_accessor
+
+    # modelaccessor.py will have ensure mock_modelaccessor is in sys.path
     from mock_modelaccessor import MockObjectList
 
     # import all class names to globals
@@ -79,6 +62,4 @@
     return {"sys_path_save": sys_path_save,
             "model_accessor": model_accessor,
             "Config": Config,
-            "xos_dir": xos_dir,
-            "services_dir": services_dir,
             "MockObjectList": MockObjectList}