SEBA-405 Update onos-service to use synchronizer library

Change-Id: I75b9e840b63d68432b2cee7b069a61898dd86e74
diff --git a/xos/synchronizer/event_steps/kubernetes_event.py b/xos/synchronizer/event_steps/kubernetes_event.py
index 2f81c04..67c2569 100644
--- a/xos/synchronizer/event_steps/kubernetes_event.py
+++ b/xos/synchronizer/event_steps/kubernetes_event.py
@@ -17,8 +17,8 @@
 import json
 import os
 import sys
-from synchronizers.new_base.eventstep import EventStep
-from synchronizers.new_base.modelaccessor import ONOSApp, ONOSService
+from xossynchronizer.event_steps.eventstep import EventStep
+from xossynchronizer.modelaccessor import ONOSApp, ONOSService
 from xosconfig import Config
 from multistructlog import create_logger
 
diff --git a/xos/synchronizer/event_steps/test_kubernetes_event.py b/xos/synchronizer/event_steps/test_kubernetes_event.py
index ac0d67e..3d81f78 100644
--- a/xos/synchronizer/event_steps/test_kubernetes_event.py
+++ b/xos/synchronizer/event_steps/test_kubernetes_event.py
@@ -20,27 +20,8 @@
 
 import os, sys
 
-# Hack to load synchronizer framework
 test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
-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'))
-# END Hack to load synchronizer framework
 
-# generate model from xproto
-def get_models_fn(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))
-# END generate model from xproto
 
 class TestKubernetesEvent(unittest.TestCase):
 
@@ -48,8 +29,6 @@
         global DeferredException
 
         self.sys_path_save = sys.path
-        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
@@ -58,12 +37,20 @@
         Config.init(config, "synchronizer-config-schema.yaml")
         # END Setting up the config module
 
-        from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-        build_mock_modelaccessor(xos_dir, services_dir, [
-            get_models_fn("onos-service", "onos.xproto")
-        ])
-        import synchronizers.new_base.modelaccessor
-        from synchronizers.new_base.modelaccessor import model_accessor
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"),])
+
+        import xossynchronizer.modelaccessor
+        import mock_modelaccessor
+        reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+        reload(xossynchronizer.modelaccessor)      # in case nose2 loaded it in a previous test
+
+        from kubernetes_event import KubernetesPodDetailsEventStep
+
+        from xossynchronizer.modelaccessor import model_accessor
+
+        self.model_accessor = model_accessor
+
         from mock_modelaccessor import MockObjectList
 
         from kubernetes_event import KubernetesPodDetailsEventStep
@@ -122,7 +109,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(log=self.log, model_accessor=self.model_accessor)
             step.process_event(event)
 
             self.assertEqual(self.onos.backend_code, 0)
@@ -155,7 +142,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(log=self.log, model_accessor=self.model_accessor)
             step.process_event(event)
 
             self.assertEqual(self.onos.backend_code, 1)
@@ -180,7 +167,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(log=self.log, model_accessor=self.model_accessor)
             step.process_event(event)
 
             self.assertEqual(self.onos.backend_code, 1)
@@ -204,7 +191,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(log=self.log, model_accessor=self.model_accessor)
             step.process_event(event)
 
             self.assertEqual(self.onos.backend_code, 1)
diff --git a/xos/synchronizer/onos-synchronizer.py b/xos/synchronizer/onos-synchronizer.py
index 337472d..5f82ca5 100755
--- a/xos/synchronizer/onos-synchronizer.py
+++ b/xos/synchronizer/onos-synchronizer.py
@@ -16,9 +16,8 @@
 
 # This imports and runs ../../xos-observer.py
 
-import importlib
 import os
-import sys
+from xossynchronizer import Synchronizer
 from xosconfig import Config
 
 base_config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/config.yaml')
@@ -29,7 +28,4 @@
 else:
     Config.init(base_config_file, 'synchronizer-config-schema.yaml')
 
-observer_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"../../synchronizers/new_base")
-sys.path.append(observer_path)
-mod = importlib.import_module("xos-synchronizer")
-mod.main()
+Synchronizer().run()
diff --git a/xos/synchronizer/steps/helpers.py b/xos/synchronizer/steps/helpers.py
index 83b9458..9d5f197 100644
--- a/xos/synchronizer/steps/helpers.py
+++ b/xos/synchronizer/steps/helpers.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from synchronizers.new_base.modelaccessor import Service
+from xossynchronizer.modelaccessor import Service
 
 class Helpers():
     @staticmethod
@@ -20,4 +20,4 @@
         if 'http' in url:
             return url
         else:
-            return 'http://%s' % url
\ No newline at end of file
+            return 'http://%s' % url
diff --git a/xos/synchronizer/steps/sync_onos_app.py b/xos/synchronizer/steps/sync_onos_app.py
index 82e24e3..d9b0f4c 100644
--- a/xos/synchronizer/steps/sync_onos_app.py
+++ b/xos/synchronizer/steps/sync_onos_app.py
@@ -15,8 +15,9 @@
 import json
 import requests
 from requests.auth import HTTPBasicAuth
-from synchronizers.new_base.syncstep import SyncStep, DeferredException, model_accessor
-from synchronizers.new_base.modelaccessor import ONOSApp, ServiceInstance, ServiceInstanceAttribute
+from xossynchronizer.steps.syncstep import SyncStep, DeferredException
+from xossynchronizer.modelaccessor import model_accessor
+from xossynchronizer.modelaccessor import ONOSApp, ServiceInstance, ServiceInstanceAttribute
 
 from xosconfig import Config
 from multistructlog import create_logger
diff --git a/xos/synchronizer/steps/sync_onos_service.py b/xos/synchronizer/steps/sync_onos_service.py
index 010fc33..8f1550a 100644
--- a/xos/synchronizer/steps/sync_onos_service.py
+++ b/xos/synchronizer/steps/sync_onos_service.py
@@ -16,8 +16,8 @@
 import json
 import requests
 from requests.auth import HTTPBasicAuth
-from synchronizers.new_base.syncstep import SyncStep, model_accessor
-from synchronizers.new_base.modelaccessor import ONOSService, Service, ServiceAttribute
+from xossynchronizer.steps.syncstep import SyncStep
+from xossynchronizer.modelaccessor import ONOSService, Service, ServiceAttribute, model_accessor
 
 from xosconfig import Config
 from multistructlog import create_logger
diff --git a/xos/synchronizer/steps/test_sync_onos_app.py b/xos/synchronizer/steps/test_sync_onos_app.py
index ffde435..fb38a50 100644
--- a/xos/synchronizer/steps/test_sync_onos_app.py
+++ b/xos/synchronizer/steps/test_sync_onos_app.py
@@ -20,27 +20,8 @@
 
 import os, sys
 
-# Hack to load synchronizer framework
 test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
-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'))
-# END Hack to load synchronizer framework
 
-# generate model from xproto
-def get_models_fn(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))
-# END generate model from xproto
 
 def match_none(req):
     return req.text == None
@@ -57,8 +38,6 @@
         global DeferredException
 
         self.sys_path_save = sys.path
-        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
@@ -67,14 +46,18 @@
         Config.init(config, "synchronizer-config-schema.yaml")
         # END Setting up the config module
 
-        from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-        build_mock_modelaccessor(xos_dir, services_dir, [
-            get_models_fn("onos-service", "onos.xproto")
-        ])
-        import synchronizers.new_base.modelaccessor
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"),])
+
+        import xossynchronizer.modelaccessor
+        import mock_modelaccessor
+        reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+        reload(xossynchronizer.modelaccessor)      # in case nose2 loaded it in a previous test
 
         from sync_onos_app import SyncONOSApp, DeferredException, model_accessor
 
+        self.model_accessor = model_accessor
+
         # import all class names to globals
         for (k, v) in model_accessor.all_model_classes.items():
             globals()[k] = v
@@ -154,7 +137,7 @@
 
             app_get.return_value = [segment_routing, openflow]
             mock_si.return_value = [self.si]
-            self.sync_step().sync_record(self.onos_app)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_app)
 
         self.assertEqual(e.exception.message, 'Deferring installation of ONOSApp with id 1 as dependencies are not met')
         self.assertFalse(m.called)
@@ -177,7 +160,7 @@
 
         with patch.object(ServiceInstance.objects, "get_items") as mock_si:
             mock_si.return_value = [self.si]
-            self.sync_step().sync_record(self.onos_app)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_app)
 
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 2)
@@ -201,7 +184,7 @@
 
         with patch.object(ServiceInstance.objects, "get_items") as mock_si:
             mock_si.return_value = [self.si]
-            self.sync_step().sync_record(self.onos_app)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_app)
 
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 2)
@@ -220,7 +203,7 @@
 
         with patch.object(ServiceInstance.objects, "get_items") as mock_si:
             mock_si.return_value = [self.si]
-            self.sync_step().sync_record(self.onos_app)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_app)
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 2)
         self.assertEqual(self.onos_app.version, self.vrouter_app_response["version"])
@@ -254,7 +237,7 @@
 
         with patch.object(ServiceInstance.objects, "get_items") as mock_si:
             mock_si.return_value = [self.si]
-            self.sync_step().sync_record(self.onos_app)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_app)
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 3)
         self.assertEqual(self.onos_app.app_id, self.vrouter_app_response["name"])
@@ -292,7 +275,7 @@
 
         with patch.object(ServiceInstance.objects, "get_items") as mock_si:
             mock_si.return_value = [self.si]
-            self.sync_step().sync_record(self.onos_app)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_app)
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 4)
         self.assertEqual(self.onos_app.app_id, self.vrouter_app_response_updated["name"])
@@ -327,7 +310,7 @@
         with patch.object(ServiceInstance.objects, "get_items") as mock_si, \
             self.assertRaises(Exception) as e:
             mock_si.return_value = [self.si]
-            self.sync_step().sync_record(self.onos_app)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_app)
 
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 3)
@@ -347,7 +330,7 @@
         m.post("/onos/v1/applications",
                status_code=409)
 
-        step = self.sync_step()
+        step = self.sync_step(model_accessor=self.model_accessor)
         with patch.object(step, "check_app_installed") as mock_check_installed:
             mock_check_installed.return_value = False
 
@@ -361,7 +344,7 @@
         m.delete("http://onos-url:8181%s" % self.onos_app_attribute.name,
                status_code=204)
 
-        self.sync_step().delete_record(self.onos_app_attribute)
+        self.sync_step(model_accessor=self.model_accessor).delete_record(self.onos_app_attribute)
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 1)
 
@@ -370,7 +353,7 @@
         m.delete("http://onos-url:8181/onos/v1/applications/org.onosproject.vrouter/active",
                status_code=204)
 
-        self.sync_step().delete_record(self.onos_app)
+        self.sync_step(model_accessor=self.model_accessor).delete_record(self.onos_app)
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 1)
 
@@ -383,9 +366,10 @@
         m.delete("http://onos-url:8181/onos/v1/applications/org.onosproject.vrouter",
                  status_code=204)
 
-        self.sync_step().delete_record(self.onos_app)
+        self.sync_step(model_accessor=self.model_accessor).delete_record(self.onos_app)
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 1)
 
-
+if __name__ == '__main__':
+    unittest.main()
 
diff --git a/xos/synchronizer/steps/test_sync_onos_service.py b/xos/synchronizer/steps/test_sync_onos_service.py
index 314a0c1..ee6daaf 100644
--- a/xos/synchronizer/steps/test_sync_onos_service.py
+++ b/xos/synchronizer/steps/test_sync_onos_service.py
@@ -20,27 +20,8 @@
 
 import os, sys
 
-# Hack to load synchronizer framework
 test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
-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'))
-# END Hack to load synchronizer framework
 
-# generate model from xproto
-def get_models_fn(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))
-# END generate model from xproto
 
 def match_json(desired, req):
     if desired!=req.json():
@@ -53,8 +34,6 @@
     def setUp(self):
 
         self.sys_path_save = sys.path
-        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
@@ -63,14 +42,18 @@
         Config.init(config, "synchronizer-config-schema.yaml")
         # END Setting up the config module
 
-        from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-        build_mock_modelaccessor(xos_dir, services_dir, [
-            get_models_fn("onos-service", "onos.xproto")
-        ])
-        import synchronizers.new_base.modelaccessor
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"),])
+
+        import xossynchronizer.modelaccessor
+        import mock_modelaccessor
+        reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+        reload(xossynchronizer.modelaccessor)      # in case nose2 loaded it in a previous test
 
         from sync_onos_service import SyncONOSService, model_accessor
 
+        self.model_accessor = model_accessor
+
         # import all class names to globals
         for (k, v) in model_accessor.all_model_classes.items():
             globals()[k] = v
@@ -122,7 +105,7 @@
     def test_sync_no_service_attributes(self, m):
         with patch.object(Service.objects, "get_items") as service_mock:
             service_mock.return_value = [self.service]
-            self.sync_step().sync_record(self.onos)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos)
         self.assertFalse(m.called)
 
     @requests_mock.Mocker()
@@ -144,7 +127,7 @@
 
         with patch.object(Service.objects, "get_items") as service_mock:
             service_mock.return_value = [self.service]
-            self.sync_step().sync_record(self.onos)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos)
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 2)
 
@@ -160,7 +143,7 @@
 
         with patch.object(Service.objects, "get_items") as service_mock:
             service_mock.return_value = [self.service]
-            self.sync_step().sync_record(self.onos_service_attribute)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos_service_attribute)
 
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 1)
@@ -182,7 +165,7 @@
             patch.object(Service.objects, "get_items") as service_mock:
 
             service_mock.return_value = [self.service]
-            self.sync_step().sync_record(self.onos)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos)
 
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 1)
@@ -193,6 +176,9 @@
         m.delete("http://onos-url:8181%s" % self.onos_service_attribute.name,
                  status_code=204)
 
-        self.sync_step().delete_record(self.onos_service_attribute)
+        self.sync_step(model_accessor=self.model_accessor).delete_record(self.onos_service_attribute)
         self.assertTrue(m.called)
-        self.assertEqual(m.call_count, 1)
\ No newline at end of file
+        self.assertEqual(m.call_count, 1)
+
+if __name__ == '__main__':
+    unittest.main()