SEBA-497 delayering, Makefile, and tox for fabric-crossconnect-synchronizer

Change-Id: Ibcf8fd06b9ad94d3f198887c18be7adfa4d5a954
diff --git a/xos/synchronizer/steps/__init__.py b/xos/synchronizer/steps/__init__.py
new file mode 100644
index 0000000..19d1424
--- /dev/null
+++ b/xos/synchronizer/steps/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2019-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py b/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
index 2481af8..5814c19 100644
--- a/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
+++ b/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
@@ -13,11 +13,13 @@
 # limitations under the License.
 
 from xossynchronizer.steps.syncstep import SyncStep, DeferredException
-from xossynchronizer.modelaccessor import model_accessor, FabricCrossconnectServiceInstance, ServiceInstance, BNGPortMapping
+from xossynchronizer.modelaccessor import model_accessor, \
+    FabricCrossconnectServiceInstance, \
+    ServiceInstance, \
+    BNGPortMapping
 
 from xosconfig import Config
 from multistructlog import create_logger
-import urllib
 import requests
 from requests.auth import HTTPBasicAuth
 
@@ -50,10 +52,12 @@
         fabric_onos = fabric_onos[0]
 
         return {
-            'url': SyncFabricCrossconnectServiceInstance.format_url("%s:%s" % (fabric_onos.rest_hostname, fabric_onos.rest_port)),
+            'url': SyncFabricCrossconnectServiceInstance.format_url(
+                "%s:%s" %
+                (fabric_onos.rest_hostname,
+                 fabric_onos.rest_port)),
             'user': fabric_onos.rest_username,
-            'pass': fabric_onos.rest_password
-        }
+            'pass': fabric_onos.rest_password}
 
     def make_handle(self, s_tag, switch_datapath_id):
         # Generate a backend_handle that uniquely identifies the cross connect. ONOS doesn't provide us a handle, so
@@ -62,24 +66,24 @@
         return "%d/%s" % (s_tag, switch_datapath_id)
 
     def extract_handle(self, backend_handle):
-        (s_tag, switch_datapath_id) = backend_handle.split("/",1)
+        (s_tag, switch_datapath_id) = backend_handle.split("/", 1)
         s_tag = int(s_tag)
         return (s_tag, switch_datapath_id)
 
     def range_matches(self, value, pattern):
-        value=int(value)
+        value = int(value)
         for this_range in pattern.split(","):
             this_range = this_range.strip()
             if "-" in this_range:
                 (first, last) = this_range.split("-")
                 first = int(first.strip())
                 last = int(last.strip())
-                if (value>=first) and (value<=last):
+                if (value >= first) and (value <= last):
                     return True
-            elif this_range.lower()=="any":
+            elif this_range.lower() == "any":
                 return True
             else:
-                if (value==int(this_range)):
+                if (value == int(this_range)):
                     return True
         return False
 
@@ -95,7 +99,7 @@
         # See if there are any ranges or "any" that match
         for bng_mapping in BNGPortMapping.objects.all():
             if self.range_matches(s_tag, bng_mapping.s_tag):
-                 return bng_mapping
+                return bng_mapping
 
         return None
 
@@ -107,7 +111,7 @@
 
         onos = self.get_fabric_onos_info(o)
 
-        si = ServiceInstance.objects.get(id=o.id)
+        ServiceInstance.objects.get(id=o.id)
 
         if (o.s_tag is None):
             raise Exception("Cannot sync FabricCrossconnectServiceInstance if s_tag is None on fcsi %s" % o.id)
@@ -116,16 +120,18 @@
             raise Exception("Cannot sync FabricCrossconnectServiceInstance if source_port is None on fcsi %s" % o.id)
 
         if (not o.switch_datapath_id):
-            raise Exception("Cannot sync FabricCrossconnectServiceInstance if switch_datapath_id is unset on fcsi %s" % o.id)
+            raise Exception(
+                "Cannot sync FabricCrossconnectServiceInstance if switch_datapath_id is unset on fcsi %s" %
+                o.id)
 
-        bng_mapping = self.find_bng(s_tag = o.s_tag)
+        bng_mapping = self.find_bng(s_tag=o.s_tag)
         if not bng_mapping:
             raise Exception("Unable to determine BNG port for s_tag %s" % o.s_tag)
         east_port = bng_mapping.switch_port
 
-        data = { "deviceId": o.switch_datapath_id,
-                 "vlanId": o.s_tag,
-                 "ports": [ int(o.source_port), int(east_port) ] }
+        data = {"deviceId": o.switch_datapath_id,
+                "vlanId": o.s_tag,
+                "ports": [int(o.source_port), int(east_port)]}
 
         url = onos['url'] + '/onos/segmentrouting/xconnect'
 
@@ -155,8 +161,8 @@
             # backend_handle has everything we need in it to delete this entry.
             (s_tag, switch_datapath_id) = self.extract_handle(o.backend_handle)
 
-            data = { "deviceId": switch_datapath_id,
-                     "vlanId": s_tag }
+            data = {"deviceId": switch_datapath_id,
+                    "vlanId": s_tag}
 
             url = onos['url'] + '/onos/segmentrouting/xconnect'
 
diff --git a/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py b/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
index 62c8d5b..44f3b91 100644
--- a/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
+++ b/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
@@ -15,24 +15,26 @@
 import unittest
 
 import functools
-from mock import patch, call, Mock, PropertyMock, MagicMock
+from mock import patch, Mock
 import requests_mock
-import multistructlog
-from multistructlog import create_logger
 
-import os, sys
+import os
+import sys
 
-test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
 
 def mock_get_westbound_service_instance_properties(props, prop):
     return props[prop]
 
+
 def match_json(desired, req):
-    if desired!=req.json():
+    if desired != req.json():
         raise Exception("Got request %s, but body is not matching" % req.url)
         return False
     return True
 
+
 class TestSyncFabricCrossconnectServiceInstance(unittest.TestCase):
 
     def setUp(self):
@@ -48,11 +50,11 @@
         # END Setting up the config module
 
         from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
-        mock_modelaccessor_config(test_path, [("fabric-crossconnect", "fabric-crossconnect.xproto"),])
+        mock_modelaccessor_config(test_path, [("fabric-crossconnect", "fabric-crossconnect.xproto"), ])
 
         import xossynchronizer.modelaccessor
         import mock_modelaccessor
-        reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+        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
@@ -69,14 +71,14 @@
         self.sync_step.log = Mock()
 
         # mock onos-fabric
-        self.onos_fabric = Service(name = "onos-fabric",
-                              rest_hostname = "onos-fabric",
-                              rest_port = "8181",
-                              rest_username = "onos",
-                              rest_password = "rocks")
+        self.onos_fabric = Service(name="onos-fabric",
+                                   rest_hostname="onos-fabric",
+                                   rest_port="8181",
+                                   rest_username="onos",
+                                   rest_password="rocks")
 
-        self.service = FabricCrossconnectService(name = "fcservice",
-                                                 provider_services = [self.onos_fabric])
+        self.service = FabricCrossconnectService(name="fcservice",
+                                                 provider_services=[self.onos_fabric])
 
     def mock_westbound(self, fsi, s_tag, switch_datapath_id, switch_port):
         # Mock out a ServiceInstance so the syncstep can call get_westbound_service_instance_properties on it
@@ -163,8 +165,8 @@
     @requests_mock.Mocker()
     def test_sync(self, m):
         with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
-            patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \
-            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+                patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \
+                patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
 
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111, source_port=3,
                                                     switch_datapath_id="of:0000000000000201", updated=1, policed=2)
@@ -175,8 +177,8 @@
             bng_objects.return_value = [bngmapping]
 
             desired_data = {"deviceId": "of:0000000000000201",
-                    "vlanId": 111,
-                    "ports": [3, 4]}
+                            "vlanId": 111,
+                            "ports": [3, 4]}
 
             m.post("http://onos-fabric:8181/onos/segmentrouting/xconnect",
                    status_code=200,
@@ -189,8 +191,7 @@
             fcsi_save.assert_called()
 
     def test_sync_no_bng_mapping(self):
-        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
-            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects:
 
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111, source_port=3,
                                                     switch_datapath_id="of:0000000000000201", updated=1, policed=2)
@@ -203,8 +204,7 @@
             self.assertEqual(e.exception.message, "Unable to determine BNG port for s_tag 111")
 
     def test_sync_not_policed(self):
-        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
-            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects:
 
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3,
                                                     switch_datapath_id="of:0000000000000201", updated=1, policed=0)
@@ -217,8 +217,7 @@
             self.assertEqual(e.exception.message, "Waiting for model_policy to run on fcsi 7777")
 
     def test_sync_no_s_tag(self):
-        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
-            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects:
 
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3,
                                                     switch_datapath_id="of:0000000000000201", updated=1, policed=2)
@@ -228,11 +227,11 @@
             with self.assertRaises(Exception) as e:
                 self.sync_step(model_accessor=self.model_accessor).sync_record(fsi)
 
-            self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if s_tag is None on fcsi 7777")
+            self.assertEqual(e.exception.message,
+                             "Cannot sync FabricCrossconnectServiceInstance if s_tag is None on fcsi 7777")
 
     def test_sync_no_switch_datapath_id(self):
-        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
-            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects:
 
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3, s_tag=111,
                                                     updated=1, policed=2)
@@ -242,11 +241,12 @@
             with self.assertRaises(Exception) as e:
                 self.sync_step(model_accessor=self.model_accessor).sync_record(fsi)
 
-            self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if switch_datapath_id is unset on fcsi 7777")
+            self.assertEqual(
+                e.exception.message,
+                "Cannot sync FabricCrossconnectServiceInstance if switch_datapath_id is unset on fcsi 7777")
 
     def test_sync_no_source_port(self):
-        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
-            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects:
 
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111,
                                                     switch_datapath_id="of:0000000000000201", updated=1, policed=2)
@@ -256,24 +256,24 @@
             with self.assertRaises(Exception) as e:
                 self.sync_step(model_accessor=self.model_accessor).sync_record(fsi)
 
-            self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if source_port is None on fcsi 7777")
+            self.assertEqual(e.exception.message,
+                             "Cannot sync FabricCrossconnectServiceInstance if source_port is None on fcsi 7777")
 
     @requests_mock.Mocker()
     def test_delete(self, m):
-        with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects, \
-                patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+        with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects:
             fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service,
                                                     backend_handle="111/of:0000000000000201",
                                                     enacted=True)
 
-            fcsi_objects.return_value=[fsi]
+            fcsi_objects.return_value = [fsi]
 
             desired_data = {"deviceId": "of:0000000000000201",
                             "vlanId": 111}
 
             m.delete("http://onos-fabric:8181/onos/segmentrouting/xconnect",
-                   status_code=204,
-                   additional_matcher=functools.partial(match_json, desired_data))
+                     status_code=204,
+                     additional_matcher=functools.partial(match_json, desired_data))
 
             self.sync_step(model_accessor=self.model_accessor).delete_record(fsi)
             self.assertTrue(m.called)
@@ -282,5 +282,6 @@
         self.o = None
         sys.path = self.sys_path_save
 
+
 if __name__ == '__main__':
     unittest.main()