SEBA-405 Update fabric synchronizer to use synchronizer library

Change-Id: I101dce9008bac14bbce95a3f61ace7059b92a4b4
diff --git a/Dockerfile.synchronizer b/Dockerfile.synchronizer
index 3e2980b..4a2d495 100644
--- a/Dockerfile.synchronizer
+++ b/Dockerfile.synchronizer
@@ -15,7 +15,7 @@
 # docker build -t xosproject/fabric-synchronizer:candidate -f Dockerfile.synchronizer .
 
 # xosproject/fabric-synchronizer
-FROM xosproject/xos-synchronizer-base:2.1.32
+FROM xosproject/xos-synchronizer-base:2.1.38
 
 COPY xos/synchronizer /opt/xos/synchronizers/fabric
 COPY VERSION /opt/xos/synchronizers/fabric/
diff --git a/VERSION b/VERSION
index 8dbb0f2..a39c0b7 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.1.10
+2.1.11
diff --git a/xos/synchronizer/event_steps/kubernetes_event.py b/xos/synchronizer/event_steps/kubernetes_event.py
index 03c17df..507da0d 100644
--- a/xos/synchronizer/event_steps/kubernetes_event.py
+++ b/xos/synchronizer/event_steps/kubernetes_event.py
@@ -17,9 +17,9 @@
 import json
 import os
 import sys
-from synchronizers.new_base.eventstep import EventStep
-from synchronizers.new_base.modelaccessor import model_accessor
-from synchronizers.new_base.modelaccessor import FabricService, Switch, Service
+from xossynchronizer.event_steps.eventstep import EventStep
+from xossynchronizer.modelaccessor import model_accessor
+from xossynchronizer.modelaccessor import FabricService, Switch, Service
 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 22b0c95..ae2915d 100644
--- a/xos/synchronizer/event_steps/test_kubernetes_event.py
+++ b/xos/synchronizer/event_steps/test_kubernetes_event.py
@@ -20,27 +20,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
 
 class TestKubernetesEvent(unittest.TestCase):
 
@@ -48,8 +28,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,18 +36,19 @@
         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"),
-            get_models_fn("onos-service", "onos.xproto"),
-        ])
-        import synchronizers.new_base.mock_modelaccessor
-        reload(synchronizers.new_base.mock_modelaccessor) # in case nose2 loaded it in a previous test        
-        import synchronizers.new_base.modelaccessor
-        reload(synchronizers.new_base.modelaccessor)      # in case nose2 loaded it in a previous test         
-        from kubernetes_event import model_accessor
-        from mock_modelaccessor import MockObjectList
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto"),
+                                              ("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 xossynchronizer.modelaccessor import model_accessor
+        self.model_accessor = model_accessor
+
+        from mock_modelaccessor import MockObjectList
         from kubernetes_event import KubernetesPodDetailsEventStep
 
         # import all class names to globals
@@ -129,7 +108,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
             step.process_event(event)
 
             self.assertEqual(self.switch.backend_code, 0)
@@ -164,7 +143,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
             step.process_event(event)
 
             self.assertEqual(self.switch.backend_code, 1)
@@ -195,7 +174,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
             step.process_event(event)
 
             self.assertEqual(self.switch.backend_code, 1)
@@ -225,7 +204,7 @@
             event = Mock()
             event.value = json.dumps(event_dict)
 
-            step = self.event_step(log=self.log)
+            step = self.event_step(model_accessor=self.model_accessor, log=self.log)
             step.process_event(event)
 
             self.assertEqual(self.switch.backend_code, 1)
diff --git a/xos/synchronizer/fabric-synchronizer.py b/xos/synchronizer/fabric-synchronizer.py
index f9ea235..205bd4a 100755
--- a/xos/synchronizer/fabric-synchronizer.py
+++ b/xos/synchronizer/fabric-synchronizer.py
@@ -18,9 +18,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')
@@ -31,7 +30,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/model_policies/model_policy_compute_nodes.py b/xos/synchronizer/model_policies/model_policy_compute_nodes.py
index 91efd40..a253a12 100644
--- a/xos/synchronizer/model_policies/model_policy_compute_nodes.py
+++ b/xos/synchronizer/model_policies/model_policy_compute_nodes.py
@@ -15,8 +15,8 @@
 
 import ipaddress
 import random
-from synchronizers.new_base.modelaccessor import NodeToSwitchPort, PortInterface, model_accessor
-from synchronizers.new_base.policy import Policy
+from xossynchronizer.modelaccessor import NodeToSwitchPort, PortInterface, model_accessor
+from xossynchronizer.model_policies.policy import Policy
 
 from xosconfig import Config
 from multistructlog import create_logger
diff --git a/xos/synchronizer/model_policies/test_model_policy_compute_node.py b/xos/synchronizer/model_policies/test_model_policy_compute_node.py
index 4586a71..9b17d8d 100644
--- a/xos/synchronizer/model_policies/test_model_policy_compute_node.py
+++ b/xos/synchronizer/model_policies/test_model_policy_compute_node.py
@@ -21,44 +21,30 @@
 import os, sys
 
 test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
-service_dir=os.path.join(test_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")
-
-# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
-# both possible locations...
-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))
 
 class TestComputeNodePolicy(unittest.TestCase):
     def setUp(self):
         global ComputeNodePolicy, MockObjectList
 
         self.sys_path_save = sys.path
-        sys.path.append(xos_dir)
-        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
 
         config = os.path.join(test_path, "../test_config.yaml")
         from xosconfig import Config
         Config.clear()
         Config.init(config, 'synchronizer-config-schema.yaml')
 
-        from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
-        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric", "fabric.xproto")])
+        from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+        mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto")])
 
-        import synchronizers.new_base.modelaccessor
+        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 model_policy_compute_nodes import ComputeNodePolicy, model_accessor
 
+        self.model_accessor = model_accessor
+
         from mock_modelaccessor import MockObjectList
 
         # import all class names to globals
@@ -113,7 +99,6 @@
         test_subnet = self.policy.getPortCidrByIp(test_ip)
 
         with patch.object(PortInterface.objects, "get_items") as get_pi:
-
             get_pi.return_value = [mock_pi]
             vlan = self.policy.getVlanByCidr(test_subnet)
 
@@ -138,7 +123,7 @@
 
     def test_handle_create(self):
 
-        policy = self.policy()
+        policy = self.policy(model_accessor=self.model_accessor)
         with patch.object(policy, "handle_update") as handle_update:
             policy.handle_create(self.model)
             handle_update.assert_called_with(self.model)
@@ -151,7 +136,7 @@
         mock_pi.name = "test_interface"
         mock_pi.ips = str(self.policy.getPortCidrByIp(mock_pi_ip))
 
-        policy = self.policy()
+        policy = self.policy(model_accessor=self.model_accessor)
 
         self.model.port.id = 1
         self.model.node.dataPlaneIntf = "test_interface"
@@ -169,7 +154,7 @@
 
     def test_handle_update(self):
 
-        policy = self.policy()
+        policy = self.policy(model_accessor=self.model_accessor)
 
         self.model.port.id = 1
         self.model.node.dataPlaneIntf = "test_interface"
@@ -194,5 +179,6 @@
 
 
 if __name__ == '__main__':
+    sys.path.append("../steps")  # so we can import helpers from steps directory
     unittest.main()
 
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