SEBA-497 delayering, Makefile, and tox for onos-synchronizer

Change-Id: I849b307330a6fd5a5de5489d8f2db804a02acead
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/helpers.py b/xos/synchronizer/steps/helpers.py
index 9d5f197..2629638 100644
--- a/xos/synchronizer/steps/helpers.py
+++ b/xos/synchronizer/steps/helpers.py
@@ -14,6 +14,7 @@
 
 from xossynchronizer.modelaccessor import Service
 
+
 class Helpers():
     @staticmethod
     def format_url(url):
diff --git a/xos/synchronizer/steps/sync_onos_app.py b/xos/synchronizer/steps/sync_onos_app.py
index d9b0f4c..210f8ce 100644
--- a/xos/synchronizer/steps/sync_onos_app.py
+++ b/xos/synchronizer/steps/sync_onos_app.py
@@ -27,6 +27,7 @@
 log = create_logger(Config().get('logging'))
 log.info("config file", file=Config().get_config_file())
 
+
 class SyncONOSApp(SyncStep):
     provides = [ONOSApp]
     observes = [ONOSApp, ServiceInstanceAttribute]
@@ -44,21 +45,25 @@
         """
         if not deps:
             return True
-        for dep in [x.strip() for x in deps.split(',') if x is not ""]:
+        for dep in [x.strip() for x in deps.split(',') if x != ""]:
             try:
                 app = ONOSApp.objects.get(app_id=dep)
                 if not app.backend_code == 1:
                     # backend_code == 1 means that the app has been pushed
                     return False
-            except IndexError, e:
+            except IndexError:
                 return False
         return True
 
     def add_config(self, o):
         log.info("Adding config %s" % o.name, model=o.tologdict())
         # getting onos url and auth
-        onos_url = "%s:%s" % (Helpers.format_url(o.service_instance.leaf_model.owner.leaf_model.rest_hostname), o.service_instance.leaf_model.owner.leaf_model.rest_port)
-        onos_basic_auth = HTTPBasicAuth(o.service_instance.leaf_model.owner.leaf_model.rest_username, o.service_instance.leaf_model.owner.leaf_model.rest_password)
+        onos_url = "%s:%s" % (Helpers.format_url(
+            o.service_instance.leaf_model.owner.leaf_model.rest_hostname),
+            o.service_instance.leaf_model.owner.leaf_model.rest_port)
+        onos_basic_auth = HTTPBasicAuth(
+            o.service_instance.leaf_model.owner.leaf_model.rest_username,
+            o.service_instance.leaf_model.owner.leaf_model.rest_password)
 
         # push configs (if any)
         url = o.name
@@ -145,10 +150,14 @@
 
         if request.status_code != 200:
             log.error("Request failed", response=request.text)
-            raise Exception("Failed to read application %s from ONOS: %s while checking correct version" % (url, request.text))
+            raise Exception(
+                "Failed to read application %s from ONOS: %s while checking correct version" %
+                (url, request.text))
         else:
             if o.version != request.json()["version"]:
-                raise Exception("The version of %s you installed (%s) is not the same you requested (%s)" % (o.app_id, request.json()["version"], o.version))
+                raise Exception(
+                    "The version of %s you installed (%s) is not the same you requested (%s)" %
+                    (o.app_id, request.json()["version"], o.version))
 
     def sync_record(self, o):
         log.info("Sync'ing", model=o.tologdict())
@@ -156,7 +165,7 @@
             # this is a ServiceInstanceAttribute model just push the config
             if 'ONOSApp' in o.service_instance.leaf_model.class_names:
                 return self.add_config(o)
-            return # if it's not an ONOSApp do nothing
+            return  # if it's not an ONOSApp do nothing
 
         if not self.check_app_dependencies(o.dependencies):
             raise DeferredException('Deferring installation of ONOSApp with id %s as dependencies are not met' % o.id)
@@ -176,8 +185,12 @@
         log.info("Deleting config %s" % o.name)
         # getting onos url and auth
         onos_app = o.service_instance.leaf_model
-        onos_url = "%s:%s" % (Helpers.format_url(onos_app.owner.leaf_model.rest_hostname), onos_app.owner.leaf_model.rest_port)
-        onos_basic_auth = HTTPBasicAuth(onos_app.owner.leaf_model.rest_username, onos_app.owner.leaf_model.rest_password)
+        onos_url = "%s:%s" % (Helpers.format_url(
+            onos_app.owner.leaf_model.rest_hostname),
+            onos_app.owner.leaf_model.rest_port)
+        onos_basic_auth = HTTPBasicAuth(
+            onos_app.owner.leaf_model.rest_username,
+            onos_app.owner.leaf_model.rest_password)
 
         url = o.name
         if url[0] == "/":
@@ -191,7 +204,7 @@
             log.error("Request failed", response=request.text)
             raise Exception("Failed to remove config %s from ONOS:  %s" % (url, request.text))
 
-    def uninstall_app(self,o, onos_url, onos_basic_auth):
+    def uninstall_app(self, o, onos_url, onos_basic_auth):
         log.info("Uninstalling app %s" % o.app_id)
         url = '%s/onos/v1/applications/%s' % (onos_url, o.app_id)
 
@@ -217,7 +230,7 @@
             # this is a ServiceInstanceAttribute model
             if 'ONOSApp' in o.service_instance.leaf_model.class_names:
                 return self.delete_config(o)
-            return # if it's not related to an ONOSApp do nothing
+            return  # if it's not related to an ONOSApp do nothing
 
         # NOTE if it is an ONOSApp we don't care about the ServiceInstanceAttribute
         # as the reaper will delete it
diff --git a/xos/synchronizer/steps/sync_onos_service.py b/xos/synchronizer/steps/sync_onos_service.py
index 8f1550a..fae13e7 100644
--- a/xos/synchronizer/steps/sync_onos_service.py
+++ b/xos/synchronizer/steps/sync_onos_service.py
@@ -26,6 +26,7 @@
 
 log = create_logger(Config().get('logging'))
 
+
 class SyncONOSService(SyncStep):
     provides = [ONOSService]
     observes = [ONOSService, ServiceAttribute]
@@ -41,7 +42,7 @@
             if 'ONOSService' in o.service.leaf_model.class_names:
                 print "sync ONOSService Attribute", o.service.leaf_model
                 return self.sync_record(o.service.leaf_model)
-            return # if it's not related to an ONOSService do nothing
+            return  # if it's not related to an ONOSService do nothing
 
         onos_url = "%s:%s" % (Helpers.format_url(o.rest_hostname), o.rest_port)
         onos_basic_auth = HTTPBasicAuth(o.rest_username, o.rest_password)
@@ -72,7 +73,7 @@
                 # getting onos url and auth
                 onos_service = o.service.leaf_model
                 onos_url = "%s:%s" % (
-                Helpers.format_url(onos_service.rest_hostname), onos_service.rest_port)
+                    Helpers.format_url(onos_service.rest_hostname), onos_service.rest_port)
                 onos_basic_auth = HTTPBasicAuth(onos_service.rest_username,
                                                 onos_service.rest_password)
 
diff --git a/xos/synchronizer/steps/test_sync_onos_app.py b/xos/synchronizer/steps/test_sync_onos_app.py
index fb38a50..4dffeec 100644
--- a/xos/synchronizer/steps/test_sync_onos_app.py
+++ b/xos/synchronizer/steps/test_sync_onos_app.py
@@ -13,25 +13,27 @@
 # limitations under the License.
 
 import unittest
-import json
 import functools
-from mock import patch, call, Mock, PropertyMock
+from mock import patch, Mock
 import requests_mock
 
-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 match_none(req):
-    return req.text == None
+    return req.text is None
+
 
 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 TestSyncOnosApp(unittest.TestCase):
 
     def setUp(self):
@@ -47,11 +49,11 @@
         # END Setting up the config module
 
         from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
-        mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"),])
+        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(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
@@ -62,7 +64,6 @@
         for (k, v) in model_accessor.all_model_classes.items():
             globals()[k] = v
 
-
         self.sync_step = SyncONOSApp
 
         onos = ONOSService()
@@ -110,8 +111,8 @@
         self.onos_app_attribute.service_instance = self.si
         self.onos_app_attribute.name = "/onos/v1/network/configuration/apps/org.opencord.olt"
         self.onos_app_attribute.value = {
-            "kafka" : {
-                "bootstrapServers" : "cord-kafka-kafka.default.svc.cluster.local:9092"
+            "kafka": {
+                "bootstrapServers": "cord-kafka-kafka.default.svc.cluster.local:9092"
             }
         }
 
@@ -132,14 +133,16 @@
         openflow.backend_code = 0
 
         with patch.object(ONOSApp.objects, "get_items") as app_get, \
-            patch.object(ServiceInstance.objects, "get_items") as mock_si, \
-            self.assertRaises(DeferredException) as e:
+                patch.object(ServiceInstance.objects, "get_items") as mock_si, \
+                self.assertRaises(DeferredException) as e:
 
             app_get.return_value = [segment_routing, openflow]
             mock_si.return_value = [self.si]
             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.assertEqual(
+            e.exception.message,
+            'Deferring installation of ONOSApp with id 1 as dependencies are not met')
         self.assertFalse(m.called)
 
     @requests_mock.Mocker()
@@ -153,8 +156,8 @@
                additional_matcher=match_none)
 
         m.get("http://onos-url:8181/onos/v1/applications/org.onosproject.vrouter",
-               status_code=200,
-               json=self.vrouter_app_response)
+              status_code=200,
+              json=self.vrouter_app_response)
 
         self.si.serviceinstanceattribute_dict = {}
 
@@ -177,8 +180,8 @@
                additional_matcher=match_none)
 
         m.get("http://onos-url:8181/onos/v1/applications/org.onosproject.vrouter",
-               status_code=200,
-               json=self.vrouter_app_response)
+              status_code=200,
+              json=self.vrouter_app_response)
 
         self.si.serviceinstanceattribute_dict = {}
 
@@ -260,13 +263,12 @@
                additional_matcher=functools.partial(match_json, expected),
                json=self.vrouter_app_response)
 
-
         m.get("http://onos-url:8181/onos/v1/applications/org.onosproject.vrouter",
               [
                   {"json": self.vrouter_app_response, "status_code": 200},
                   {"json": self.vrouter_app_response_updated, "status_code": 200}
               ]
-        )
+              )
 
         m.delete("http://onos-url:8181/onos/v1/applications/org.onosproject.vrouter",
                  status_code=204)
@@ -308,19 +310,22 @@
         self.si.serviceinstanceattribute_dict = {}
 
         with patch.object(ServiceInstance.objects, "get_items") as mock_si, \
-            self.assertRaises(Exception) as e:
+                self.assertRaises(Exception) as e:
             mock_si.return_value = [self.si]
             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"])
-        self.assertEqual(e.exception.message, "The version of org.onosproject.vrouter you installed (1.13.1) is not the same you requested (1.14.2)")
+        self.assertEqual(
+            e.exception.message,
+            "The version of org.onosproject.vrouter you installed (1.13.1) is not the same you requested (1.14.2)")
 
     @requests_mock.Mocker()
     def test_handle_409(self, m):
         """
-        A 409 "Application Already installed" response is not an error. This should not happen as we check if the app is installed.
+        A 409 "Application Already installed" response is not an error. This should not happen as we check
+        if the app is installed.
         """
 
         self.onos_app.url = 'http://onf.org/maven/...'
@@ -342,7 +347,7 @@
     @requests_mock.Mocker()
     def test_config_delete(self, m):
         m.delete("http://onos-url:8181%s" % self.onos_app_attribute.name,
-               status_code=204)
+                 status_code=204)
 
         self.sync_step(model_accessor=self.model_accessor).delete_record(self.onos_app_attribute)
         self.assertTrue(m.called)
@@ -351,7 +356,7 @@
     @requests_mock.Mocker()
     def test_app_deactivate(self, m):
         m.delete("http://onos-url:8181/onos/v1/applications/org.onosproject.vrouter/active",
-               status_code=204)
+                 status_code=204)
 
         self.sync_step(model_accessor=self.model_accessor).delete_record(self.onos_app)
         self.assertTrue(m.called)
@@ -370,6 +375,6 @@
         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 ee6daaf..054baea 100644
--- a/xos/synchronizer/steps/test_sync_onos_service.py
+++ b/xos/synchronizer/steps/test_sync_onos_service.py
@@ -15,20 +15,22 @@
 import unittest
 import json
 import functools
-from mock import patch, call, Mock, PropertyMock
+from mock import patch, Mock
 import requests_mock
 
-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 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 TestSyncOnosService(unittest.TestCase):
 
     def setUp(self):
@@ -43,11 +45,11 @@
         # END Setting up the config module
 
         from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
-        mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"),])
+        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(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
@@ -58,7 +60,6 @@
         for (k, v) in model_accessor.all_model_classes.items():
             globals()[k] = v
 
-
         self.sync_step = SyncONOSService
 
         self.onos = Mock(spec=[
@@ -162,14 +163,16 @@
                additional_matcher=functools.partial(match_json, json.loads(expected_conf)))
 
         with self.assertRaises(Exception) as e, \
-            patch.object(Service.objects, "get_items") as service_mock:
+                patch.object(Service.objects, "get_items") as service_mock:
 
             service_mock.return_value = [self.service]
             self.sync_step(model_accessor=self.model_accessor).sync_record(self.onos)
 
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 1)
-        self.assertEqual(e.exception.message, "Failed to add config http://onos-url:8181/onos/v1/network/configuration/apps/org.onosproject.olt in ONOS")
+        self.assertEqual(
+            e.exception.message,
+            "Failed to add config http://onos-url:8181/onos/v1/network/configuration/apps/org.onosproject.olt in ONOS")
 
     @requests_mock.Mocker()
     def test_delete(self, m):
@@ -180,5 +183,6 @@
         self.assertTrue(m.called)
         self.assertEqual(m.call_count, 1)
 
+
 if __name__ == '__main__':
     unittest.main()