SEBA-405 Update fabric synchronizer to use synchronizer library

Change-Id: I101dce9008bac14bbce95a3f61ace7059b92a4b4
diff --git a/xos/synchronizer/steps/helpers.py b/xos/synchronizer/steps/helpers.py
index 08be81d..2203db1 100644
--- a/xos/synchronizer/steps/helpers.py
+++ b/xos/synchronizer/steps/helpers.py
@@ -12,8 +12,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from synchronizers.new_base.modelaccessor import Service
-
 class Helpers():
     @staticmethod
     def format_url(url):
@@ -22,8 +20,8 @@
         else:
             return 'http://%s' % url
     @staticmethod
-    def get_onos_fabric_service():
+    def get_onos_fabric_service(model_accessor):
         # FIXME do not select by name but follow ServiceDependency
-        fabric_service = Service.objects.get(name="fabric")
+        fabric_service = model_accessor.Service.objects.get(name="fabric")
         onos_fabric_service = fabric_service.provider_services[0].leaf_model
         return onos_fabric_service
diff --git a/xos/synchronizer/steps/sync_fabric_port.py b/xos/synchronizer/steps/sync_fabric_port.py
index 9c37ffc..2ccdbac 100644
--- a/xos/synchronizer/steps/sync_fabric_port.py
+++ b/xos/synchronizer/steps/sync_fabric_port.py
@@ -16,8 +16,8 @@
 import requests
 import urllib
 from requests.auth import HTTPBasicAuth
-from synchronizers.new_base.syncstep import SyncStep, DeferredException, model_accessor
-from synchronizers.new_base.modelaccessor import FabricService, SwitchPort, PortInterface, FabricIpAddress
+from xossynchronizer.steps.syncstep import SyncStep, DeferredException
+from xossynchronizer.modelaccessor import FabricService, SwitchPort, PortInterface, FabricIpAddress, model_accessor
 
 from xosconfig import Config
 from multistructlog import create_logger
@@ -65,7 +65,7 @@
 
         log.debug("Port %s/%s data" % (model.switch.ofId, model.portId), data=data)
 
-        onos = Helpers.get_onos_fabric_service()
+        onos = Helpers.get_onos_fabric_service(self.model_accessor)
 
         url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
 
@@ -81,7 +81,7 @@
                 log.info("Port %s/%s response" % (model.switch.ofId, model.portId), text=r.text)
 
     def delete_netcfg_item(self, partial_url):
-        onos = Helpers.get_onos_fabric_service()
+        onos = Helpers.get_onos_fabric_service(self.model_accessor)
         url = 'http://%s:%s/onos/v1/network/configuration/ports/%s' % (onos.rest_hostname, onos.rest_port, partial_url)
 
         r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
diff --git a/xos/synchronizer/steps/sync_fabric_switch.py b/xos/synchronizer/steps/sync_fabric_switch.py
index 7f5aa17..31c5575 100644
--- a/xos/synchronizer/steps/sync_fabric_switch.py
+++ b/xos/synchronizer/steps/sync_fabric_switch.py
@@ -15,8 +15,8 @@
 
 import requests
 from requests.auth import HTTPBasicAuth
-from synchronizers.new_base.syncstep import SyncStep, model_accessor
-from synchronizers.new_base.modelaccessor import FabricService, Switch
+from xossynchronizer.steps.syncstep import SyncStep
+from xossynchronizer.modelaccessor import FabricService, Switch, model_accessor
 
 from xosconfig import Config
 from multistructlog import create_logger
@@ -51,7 +51,7 @@
           }
         }
 
-        onos = Helpers.get_onos_fabric_service()
+        onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor)
 
         url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
         r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
@@ -67,7 +67,7 @@
 
     def delete_record(self, model):
         log.info("Removing switch %s from onos-fabric" % model.name)
-        onos = Helpers.get_onos_fabric_service()
+        onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor)
         url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % (
         onos.rest_hostname, onos.rest_port, model.ofId)
 
diff --git a/xos/synchronizer/steps/test_sync_fabric_port.py b/xos/synchronizer/steps/test_sync_fabric_port.py
index 8c6e773..97c93c5 100644
--- a/xos/synchronizer/steps/test_sync_fabric_port.py
+++ b/xos/synchronizer/steps/test_sync_fabric_port.py
@@ -22,27 +22,7 @@
 
 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():
@@ -56,8 +36,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
@@ -66,11 +44,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("fabric", "fabric.xproto")])
-        import synchronizers.new_base.modelaccessor
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto")])
 
-        from sync_fabric_port import SyncFabricPort, model_accessor
+        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 xossynchronizer.modelaccessor import model_accessor
+        self.model_accessor = model_accessor
+
+        from sync_fabric_port import SyncFabricPort
 
         # import all class names to globals
         for (k, v) in model_accessor.all_model_classes.items():
@@ -154,7 +139,7 @@
 
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
-            self.sync_step().sync_record(port)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(port)
             self.assertTrue(m.called)
 
     @requests_mock.Mocker()
@@ -173,7 +158,7 @@
 
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
-            self.sync_step().delete_record(port)
+            self.sync_step(model_accessor=self.model_accessor).delete_record(port)
             self.assertTrue(m.called)
 
     @requests_mock.Mocker()
@@ -206,7 +191,7 @@
 
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
-            self.sync_step().delete_record(interface_to_remove)
+            self.sync_step(model_accessor=self.model_accessor).delete_record(interface_to_remove)
             self.assertTrue(m.called)
 
     @requests_mock.Mocker()
@@ -232,5 +217,8 @@
 
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
-            self.sync_step().delete_record(ip_to_remove)
+            self.sync_step(model_accessor=self.model_accessor).delete_record(ip_to_remove)
             self.assertTrue(m.called)
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/xos/synchronizer/steps/test_sync_fabric_switch.py b/xos/synchronizer/steps/test_sync_fabric_switch.py
index 5761b73..2184a6d 100644
--- a/xos/synchronizer/steps/test_sync_fabric_switch.py
+++ b/xos/synchronizer/steps/test_sync_fabric_switch.py
@@ -22,27 +22,7 @@
 
 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():
@@ -56,8 +36,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
@@ -66,11 +44,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("fabric", "fabric.xproto")])
-        import synchronizers.new_base.modelaccessor
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto")])
 
-        from sync_fabric_switch import SyncFabricSwitch, model_accessor
+        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 xossynchronizer.modelaccessor import model_accessor
+        self.model_accessor = model_accessor
+
+        from sync_fabric_switch import SyncFabricSwitch
 
         # import all class names to globals
         for (k, v) in model_accessor.all_model_classes.items():
@@ -142,7 +127,7 @@
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
 
-            self.sync_step().sync_record(self.o)
+            self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
 
             self.assertTrue(m.called)
 
@@ -156,6 +141,9 @@
         with patch.object(Service.objects, "get") as onos_fabric_get:
             onos_fabric_get.return_value = self.fabric
 
-            self.sync_step().delete_record(self.o)
+            self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
 
             self.assertTrue(m.called)
+
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file