SEBA-497 delayering, Makefile, and tox for att-workflow-drvier

Change-Id: I5869c36b8b716cab02ae12a343969c4feeacf5fb
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..b8f06bb
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,2 @@
+.tox
+venv-service
diff --git a/.gitignore b/.gitignore
index 9cff527..0d57996 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,7 @@
-.idea/
-*.pyc
-
+.idea
+.tox
 .coverage
-htmlcov
 coverage.xml
-nose2-junit.xml
-
+nose2-results.xml
+venv-service
+*.pyc
diff --git a/Dockerfile.synchronizer b/Dockerfile.synchronizer
index d8e1780..4771fb7 100644
--- a/Dockerfile.synchronizer
+++ b/Dockerfile.synchronizer
@@ -16,8 +16,14 @@
 
 # xosproject/att-workflow-driver-synchronizer
 
-FROM xosproject/xos-synchronizer-base:2.2.18
+FROM xosproject/alpine-grpc-base:0.9.0
 
+# Install pip packages
+COPY requirements.txt /tmp/requirements.txt
+RUN pip install -r /tmp/requirements.txt \
+ && pip freeze > /var/xos/pip_freeze_att_workflow_driver_service_`date -u +%Y%m%dT%H%M%S`
+
+# Copy files
 COPY xos/synchronizer /opt/xos/synchronizers/att-workflow-driver
 COPY VERSION /opt/xos/synchronizers/att-workflow-driver/
 
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..c621e16
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,80 @@
+# 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.
+
+# Configure shell
+SHELL = bash -e -o pipefail
+
+# Variables
+VERSION                  ?= $(shell cat ./VERSION)
+SERVICE_NAME             ?= $(notdir $(abspath .))
+SYNCHRONIZER_NAME        ?= att-workflow-driver-synchronizer
+
+## Docker related
+DOCKER_REGISTRY          ?=
+DOCKER_REPOSITORY        ?=
+DOCKER_BUILD_ARGS        ?=
+DOCKER_TAG               ?= ${VERSION}
+DOCKER_IMAGENAME         := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${SYNCHRONIZER_NAME}:${DOCKER_TAG}
+
+## Docker labels. Only set ref and commit date if committed
+DOCKER_LABEL_VCS_URL     ?= $(shell git remote get-url $(shell git remote))
+DOCKER_LABEL_VCS_REF     ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown")
+DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" )
+DOCKER_LABEL_BUILD_DATE  ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
+
+## Migration related - paths are relative to the xos subdirectory within this repo
+XOS_DIR                  ?= "../../../xos"
+SERVICES_DIR             ?= "../.."
+
+all: test
+
+docker-build:
+	docker build $(DOCKER_BUILD_ARGS) \
+    -t ${DOCKER_IMAGENAME} \
+    --build-arg org_label_schema_version="${VERSION}" \
+    --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
+    --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
+    --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
+    --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
+    -f Dockerfile.synchronizer .
+
+docker-push:
+	docker push ${DOCKER_IMAGENAME}
+
+test: test-unit test-migration
+
+test-unit:
+	tox
+
+venv-service:
+	virtualenv $@;\
+    source ./$@/bin/activate ; set -u ;\
+    pip install -r requirements.txt xosmigrate~=3.0.1
+
+create-migration: venv-service
+	source ./venv-service/bin/activate; set -u;\
+    cd xos; xos-migrate --xos-dir ${XOS_DIR} --services-dir ${SERVICES_DIR} -s ${SERVICE_NAME}
+
+test-migration: venv-service
+	source ./venv-service/bin/activate; set -u;\
+    cd xos; xos-migrate --xos-dir ${XOS_DIR} --services-dir ${SERVICES_DIR} -s ${SERVICE_NAME} --check
+
+clean:
+	find . -name '*.pyc' | xargs rm -f
+	rm -rf \
+    .tox \
+    venv-service \
+    xos/.coverage \
+    xos/coverage.xml \
+    xos/nose2-results.xml
diff --git a/VERSION b/VERSION
index 653f458..cb7e5f6 100644
--- a/VERSION
+++ b/VERSION
@@ -1,2 +1,2 @@
-1.1.0
+1.2.0
 
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..e039eea
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,3 @@
+xossynchronizer~=3.0.1
+xosapi~=3.0.1
+xoskafka~=3.0.1
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..9064a11
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,46 @@
+; 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.
+
+[tox]
+envlist = py27
+; future; envlist = py27,py35,py36,py37
+skip_missing_interpreters = true
+skipsdist = True
+
+[testenv]
+deps =
+  -r requirements.txt
+  requests_mock
+  nose2
+;  flake8
+
+changedir = xos
+commands =
+  nose2 -c ../tox.ini --verbose --junit-xml
+; future: flake8
+
+[flake8]
+max-line-length = 119
+
+[unittest]
+plugins = nose2.plugins.junitxml
+
+[junit-xml]
+path = nose2-results.xml
+
+[coverage]
+always-on = True
+coverage-report =
+  term
+  xml
diff --git a/xos/synchronizer/__init__.py b/xos/synchronizer/__init__.py
index 8bbb6fa..19d1424 100644
--- a/xos/synchronizer/__init__.py
+++ b/xos/synchronizer/__init__.py
@@ -1,5 +1,4 @@
-
-# Copyright 2017-present Open Networking Foundation
+# 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.
@@ -11,4 +10,4 @@
 # 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.
\ No newline at end of file
+# limitations under the License.
diff --git a/xos/synchronizer/att-workflow-driver-synchronizer.py b/xos/synchronizer/att-workflow-driver-synchronizer.py
index 9b420e7..9ea2e25 100644
--- a/xos/synchronizer/att-workflow-driver-synchronizer.py
+++ b/xos/synchronizer/att-workflow-driver-synchronizer.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python
 
 # Copyright 2017-present Open Networking Foundation
 #
@@ -13,9 +14,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
-#!/usr/bin/env python
-
 # This imports and runs ../../xos-observer.py
 
 import os
@@ -31,4 +29,4 @@
 else:
     Config.init(base_config_file, 'synchronizer-config-schema.yaml')
 
-Synchronizer().run()
\ No newline at end of file
+Synchronizer().run()
diff --git a/xos/synchronizer/event_steps/__init__.py b/xos/synchronizer/event_steps/__init__.py
new file mode 100644
index 0000000..19d1424
--- /dev/null
+++ b/xos/synchronizer/event_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/event_steps/auth_event.py b/xos/synchronizer/event_steps/auth_event.py
index 2d9f4d7..b7124a9 100644
--- a/xos/synchronizer/event_steps/auth_event.py
+++ b/xos/synchronizer/event_steps/auth_event.py
@@ -14,12 +14,10 @@
 # limitations under the License.
 
 import json
-import time
-import os
-import sys
 from xossynchronizer.event_steps.eventstep import EventStep
 from helpers import AttHelpers
 
+
 class SubscriberAuthEventStep(EventStep):
     topics = ["authentication.events"]
     technology = "kafka"
@@ -33,10 +31,12 @@
         onu_sn = AttHelpers.get_onu_sn(self.model_accessor, self.log, value)
         si = AttHelpers.get_si_by_sn(self.model_accessor, self.log, onu_sn)
         if not si:
-            self.log.exception("authentication.events: Cannot find att-workflow-driver service instance for this event", kafka_event=value)
+            self.log.exception(
+                "authentication.events: Cannot find att-workflow-driver service instance for this event",
+                kafka_event=value)
             raise Exception("authentication.events: Cannot find att-workflow-driver service instance for this event")
 
         self.log.info("authentication.events: Got event for subscriber", event_value=value, onu_sn=onu_sn, si=si)
 
-        si.authentication_state = value["authenticationState"];
+        si.authentication_state = value["authenticationState"]
         si.save_changed_fields(always_update_timestamp=True)
diff --git a/xos/synchronizer/event_steps/dhcp_event.py b/xos/synchronizer/event_steps/dhcp_event.py
index ab411f7..06abc5a 100644
--- a/xos/synchronizer/event_steps/dhcp_event.py
+++ b/xos/synchronizer/event_steps/dhcp_event.py
@@ -14,12 +14,10 @@
 # limitations under the License.
 
 import json
-import time
-import os
-import sys
 from xossynchronizer.event_steps.eventstep import EventStep
 from helpers import AttHelpers
 
+
 class SubscriberDhcpEventStep(EventStep):
     topics = ["dhcp.events"]
     technology = "kafka"
@@ -34,7 +32,9 @@
         si = AttHelpers.get_si_by_sn(self.model_accessor, self.log, onu_sn)
 
         if not si:
-            self.log.exception("dhcp.events: Cannot find att-workflow-driver service instance for this event", kafka_event=value)
+            self.log.exception(
+                "dhcp.events: Cannot find att-workflow-driver service instance for this event",
+                kafka_event=value)
             raise Exception("dhcp.events: Cannot find att-workflow-driver service instance for this event")
 
         self.log.info("dhcp.events: Got event for subscriber", event_value=value, onu_sn=onu_sn, si=si)
diff --git a/xos/synchronizer/event_steps/onu_event.py b/xos/synchronizer/event_steps/onu_event.py
index bae496d..0db61df 100644
--- a/xos/synchronizer/event_steps/onu_event.py
+++ b/xos/synchronizer/event_steps/onu_event.py
@@ -17,6 +17,7 @@
 import json
 from xossynchronizer.event_steps.eventstep import EventStep
 
+
 class ONUEventStep(EventStep):
     topics = ["onu.events"]
     technology = "kafka"
@@ -28,18 +29,21 @@
 
     def get_att_si(self, event):
         try:
-            att_si = self.model_accessor.AttWorkflowDriverServiceInstance.objects.get(serial_number=event["serial_number"])
-            att_si.no_sync = False;
+            att_si = self.model_accessor.AttWorkflowDriverServiceInstance.objects.get(
+                serial_number=event["serial_number"])
+            att_si.no_sync = False
             att_si.uni_port_id = event["uni_port_id"]
             att_si.of_dpid = event["of_dpid"]
             self.log.debug("onu.events: Found existing AttWorkflowDriverServiceInstance", si=att_si)
         except IndexError:
-            # create an AttWorkflowDriverServiceInstance, the validation will be triggered in the corresponding sync step
+            # create an AttWorkflowDriverServiceInstance, the validation will be
+            # triggered in the corresponding sync step
             att_si = self.model_accessor.AttWorkflowDriverServiceInstance(
                 serial_number=event["serial_number"],
                 of_dpid=event["of_dpid"],
                 uni_port_id=event["uni_port_id"],
-                owner=self.model_accessor.AttWorkflowDriverService.objects.first() # we assume there is only one AttWorkflowDriverService
+                # we assume there is only one AttWorkflowDriverService
+                owner=self.model_accessor.AttWorkflowDriverService.objects.first()
             )
             self.log.debug("onu.events: Created new AttWorkflowDriverServiceInstance", si=att_si)
         return att_si
diff --git a/xos/synchronizer/event_steps/test_auth_event.py b/xos/synchronizer/event_steps/test_auth_event.py
index 35f8350..e875c43 100644
--- a/xos/synchronizer/event_steps/test_auth_event.py
+++ b/xos/synchronizer/event_steps/test_auth_event.py
@@ -13,12 +13,14 @@
 # limitations under the License.
 
 import unittest
-from mock import patch, call, Mock, PropertyMock
+from mock import patch, Mock
 import json
 
-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__)))
+
 
 class TestSubscriberAuthEvent(unittest.TestCase):
 
@@ -42,7 +44,7 @@
 
         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
@@ -67,7 +69,6 @@
         self.hippie_si.serial_number = "BRCM1234"
         self.hippie_si.save = Mock()
 
-
     def tearDown(self):
         sys.path = self.sys_path_save
 
@@ -80,8 +81,8 @@
         })
 
         with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \
-            patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as hippie_si_mock, \
-            patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
+                patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as hippie_si_mock, \
+                patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
 
             volt_service_mock.return_value = [self.volt]
             get_onu_sn.return_value = "BRCM1234"
@@ -89,9 +90,12 @@
 
             self.event_step.process_event(self.event)
 
-            self.hippie_si.save.assert_called_with(always_update_timestamp=True, update_fields=['authentication_state', 'serial_number', 'updated'])
+            self.hippie_si.save.assert_called_with(
+                always_update_timestamp=True, update_fields=[
+                    'authentication_state', 'serial_number', 'updated'])
             self.assertEqual(self.hippie_si.authentication_state, 'APPROVED')
 
+
 if __name__ == '__main__':
-    sys.path.append("..") # for import of helpers.py
-    unittest.main()
\ No newline at end of file
+    sys.path.append("..")  # for import of helpers.py
+    unittest.main()
diff --git a/xos/synchronizer/event_steps/test_dhcp_event.py b/xos/synchronizer/event_steps/test_dhcp_event.py
index 62fcaee..7176ba9 100644
--- a/xos/synchronizer/event_steps/test_dhcp_event.py
+++ b/xos/synchronizer/event_steps/test_dhcp_event.py
@@ -13,12 +13,14 @@
 # limitations under the License.
 
 import unittest
-from mock import patch, call, Mock, PropertyMock
+from mock import patch, Mock
 import json
 
-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__)))
+
 
 class TestSubscriberAuthEvent(unittest.TestCase):
 
@@ -42,7 +44,7 @@
 
         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
@@ -74,23 +76,22 @@
         self.si.serial_number = "BRCM1234"
         self.si.save = Mock()
 
-
     def tearDown(self):
         sys.path = self.sys_path_save
 
     def test_dhcp_subscriber(self):
 
         self.event.value = json.dumps({
-            "deviceId" : "of:0000000000000001",
-            "portNumber" : "1",
-            "macAddress" : self.mac_address,
-            "ipAddress" : self.ip_address,
+            "deviceId": "of:0000000000000001",
+            "portNumber": "1",
+            "macAddress": self.mac_address,
+            "ipAddress": self.ip_address,
             "messageType": "DHCPREQUEST"
         })
 
         with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \
-            patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as si_mock, \
-            patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
+                patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as si_mock, \
+                patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
 
             self.assertTrue(VOLTService.objects.first() is not None)
 
@@ -105,6 +106,7 @@
             self.assertEqual(self.si.mac_address, self.mac_address)
             self.assertEqual(self.si.ip_address, self.ip_address)
 
+
 if __name__ == '__main__':
-    sys.path.append("..") # for import of helpers.py
-    unittest.main()
\ No newline at end of file
+    sys.path.append("..")  # for import of helpers.py
+    unittest.main()
diff --git a/xos/synchronizer/event_steps/test_onu_events.py b/xos/synchronizer/event_steps/test_onu_events.py
index 4e939b8..4543357 100644
--- a/xos/synchronizer/event_steps/test_onu_events.py
+++ b/xos/synchronizer/event_steps/test_onu_events.py
@@ -13,12 +13,14 @@
 # limitations under the License.
 
 import unittest
-from mock import patch, call, Mock, PropertyMock
+from mock import patch, Mock
 import json
 
-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__)))
+
 
 class TestSyncOLTDevice(unittest.TestCase):
 
@@ -40,7 +42,7 @@
 
         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,12 +71,11 @@
     def tearDown(self):
         sys.path = self.sys_path_save
 
-
     def test_create_instance(self):
 
-        with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
-            patch.object(AttWorkflowDriverService.objects, "get_items") as service_mock, \
-            patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
+        with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \
+                patch.object(AttWorkflowDriverService.objects, "get_items") as service_mock, \
+                patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
 
             att_si_mock.return_value = []
             service_mock.return_value = [self.att]
@@ -98,8 +99,8 @@
             uni_port_id="foo"
         )
 
-        with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \
-            patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
+        with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \
+                patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
 
             att_si_mock.return_value = [si]
 
@@ -128,8 +129,8 @@
             self.event_step.process_event(self.event)
 
             self.assertEqual(mock_save.call_count, 0)
-            
+
 
 if __name__ == '__main__':
     sys.path.append("..")  # for import of helpers.py
-    unittest.main()
\ No newline at end of file
+    unittest.main()
diff --git a/xos/synchronizer/model_policies/__init__.py b/xos/synchronizer/model_policies/__init__.py
new file mode 100644
index 0000000..19d1424
--- /dev/null
+++ b/xos/synchronizer/model_policies/__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/model_policies/model_policy_att_workflow_driver_serviceinstance.py b/xos/synchronizer/model_policies/model_policy_att_workflow_driver_serviceinstance.py
index 5172830..3b7b74e 100644
--- a/xos/synchronizer/model_policies/model_policy_att_workflow_driver_serviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_att_workflow_driver_serviceinstance.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 
-
+from helpers import AttHelpers
 from xossynchronizer.model_policies.policy import Policy
 
 import os
@@ -23,11 +23,11 @@
 sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
 sys.path.append(sync_path)
 
-from helpers import AttHelpers
 
 class DeferredException(Exception):
     pass
 
+
 class AttWorkflowDriverServiceInstancePolicy(Policy):
     model_name = "AttWorkflowDriverServiceInstance"
 
@@ -36,7 +36,8 @@
         self.handle_update(si)
 
     def handle_update(self, si):
-        self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.onu_state, authentication_state=si.authentication_state)
+        self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " %
+                          (si.id), onu_state=si.onu_state, authentication_state=si.authentication_state)
 
         # Changing ONU state can change auth state
         # Changing auth state can change DHCP state
@@ -65,7 +66,7 @@
             else:
                 si.onu_state = "DISABLED"
                 self.update_onu(si.serial_number, "DISABLED")
-        else: # DISABLED
+        else:  # DISABLED
             if not valid:
                 si.status_message = message
             else:
@@ -118,17 +119,24 @@
     # ENABLED   | APPROVED | *
     # DISABLED  | AWAITING | AWAITING
     def validate_states(self, si):
-        if (si.onu_state == "AWAITING" or si.onu_state == "DISABLED") and si.authentication_state == "AWAITING" and si.dhcp_state == "AWAITING":
+        if (si.onu_state == "AWAITING" or si.onu_state ==
+                "DISABLED") and si.authentication_state == "AWAITING" and si.dhcp_state == "AWAITING":
             return
         if si.onu_state == "ENABLED" and (si.authentication_state == "APPROVED" or si.dhcp_state == "AWAITING"):
             return
-        self.logger.warning("MODEL_POLICY (validate_states): invalid state combination", onu_state=si.onu_state, auth_state=si.authentication_state, dhcp_state=si.dhcp_state)
-
+        self.logger.warning(
+            "MODEL_POLICY (validate_states): invalid state combination",
+            onu_state=si.onu_state,
+            auth_state=si.authentication_state,
+            dhcp_state=si.dhcp_state)
 
     def update_onu(self, serial_number, admin_state):
-        onu = [onu for onu in self.model_accessor.ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0]
+        onu = [onu for onu in self.model_accessor.ONUDevice.objects.all() if onu.serial_number.lower()
+               == serial_number.lower()][0]
         if onu.admin_state == admin_state:
-            self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
+            self.logger.debug(
+                "MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" %
+                (serial_number, admin_state))
         else:
             self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
             onu.admin_state = admin_state
@@ -136,10 +144,13 @@
 
     def get_subscriber(self, serial_number):
         try:
-            return [s for s in self.model_accessor.RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
+            return [s for s in self.model_accessor.RCORDSubscriber.objects.all() if s.onu_device.lower()
+                    == serial_number.lower()][0]
         except IndexError:
             # If the subscriber doesn't exist we don't do anything
-            self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
+            self.logger.debug(
+                "MODEL_POLICY: subscriber does not exists for this SI, doing nothing",
+                onu_device=serial_number)
             return None
 
     def update_subscriber_ip(self, subscriber, ip):
@@ -150,10 +161,15 @@
                 subscriber_id=subscriber.id,
                 ip=ip
             )[0]
-            self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
+            self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber",
+                              onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
             ip.save_changed_fields()
         except IndexError:
-            self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
+            self.logger.debug(
+                "MODEL_POLICY: Creating new RCORDIpAddress for subscriber",
+                onu_device=subscriber.onu_device,
+                subscriber_status=subscriber.status,
+                ip=ip)
             ip = self.model_accessor.RCORDIpAddress(
                 subscriber_id=subscriber.id,
                 ip=ip,
@@ -167,9 +183,13 @@
                 subscriber_id=subscriber.id,
                 ip=ip
             )[0]
-            self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
+            self.logger.debug(
+                "MODEL_POLICY: delete RCORDIpAddress for subscriber",
+                onu_device=subscriber.onu_device,
+                subscriber_status=subscriber.status,
+                ip=ip)
             ip.delete()
-        except:
+        except BaseException:
             self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip)
 
     def update_subscriber(self, subscriber, si):
@@ -187,7 +207,11 @@
         # - the status has changed
         # - we get a DHCPACK event
         if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
-            self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status)
+            self.logger.debug(
+                "MODEL_POLICY: updating subscriber",
+                onu_device=subscriber.onu_device,
+                authentication_state=si.authentication_state,
+                subscriber_status=subscriber.status)
             if subscriber.status == "awaiting-auth":
                 self.delete_subscriber_ip(subscriber, si.ip_address)
                 subscriber.mac_address = ""
diff --git a/xos/synchronizer/model_policies/model_policy_att_workflow_driver_whitelistentry.py b/xos/synchronizer/model_policies/model_policy_att_workflow_driver_whitelistentry.py
index 2cb0b97..7a31116 100644
--- a/xos/synchronizer/model_policies/model_policy_att_workflow_driver_whitelistentry.py
+++ b/xos/synchronizer/model_policies/model_policy_att_workflow_driver_whitelistentry.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 
 
+from helpers import AttHelpers
 from xossynchronizer.model_policies.policy import Policy
 import os
 import sys
@@ -21,7 +22,6 @@
 sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
 sys.path.append(sync_path)
 
-from helpers import AttHelpers
 
 class AttWorkflowDriverWhiteListEntryPolicy(Policy):
     model_name = "AttWorkflowDriverWhiteListEntry"
@@ -39,7 +39,10 @@
             si.onu_state = "DISABLED"
 
         self.logger.debug(
-            "MODEL_POLICY: activating AttWorkflowDriverServiceInstance because of change in the whitelist", si=si, onu_state=si.onu_state, authentication_state=si.authentication_state)
+            "MODEL_POLICY: activating AttWorkflowDriverServiceInstance because of change in the whitelist",
+            si=si,
+            onu_state=si.onu_state,
+            authentication_state=si.authentication_state)
         si.save_changed_fields(always_update_timestamp=True)
 
     def handle_update(self, whitelist):
@@ -55,11 +58,15 @@
 
             self.validate_onu_state(si)
 
-        whitelist.backend_need_delete_policy=True
+        whitelist.backend_need_delete_policy = True
         whitelist.save_changed_fields()
 
     def handle_delete(self, whitelist):
-        self.logger.debug("MODEL_POLICY: handle_delete for AttWorkflowDriverWhiteListEntry", serial_number=whitelist.serial_number, pon_port=whitelist.pon_port_id, device=whitelist.device_id)
+        self.logger.debug(
+            "MODEL_POLICY: handle_delete for AttWorkflowDriverWhiteListEntry",
+            serial_number=whitelist.serial_number,
+            pon_port=whitelist.pon_port_id,
+            device=whitelist.device_id)
 
         # BUG: Sometimes the delete policy is not called, because the reaper deletes
 
@@ -71,5 +78,5 @@
         for si in sis:
             self.validate_onu_state(si)
 
-        whitelist.backend_need_reap=True
+        whitelist.backend_need_reap = True
         whitelist.save_changed_fields()
diff --git a/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_serviceinstance.py b/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_serviceinstance.py
index 8f803f6..d784258 100644
--- a/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_serviceinstance.py
+++ b/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_serviceinstance.py
@@ -15,11 +15,12 @@
 
 
 import unittest
-from mock import patch, call, Mock, PropertyMock
+from mock import patch
 
-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__)))
 
 
 class TestModelPolicyAttWorkflowDriverServiceInstance(unittest.TestCase):
@@ -39,24 +40,21 @@
 
         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
         from model_policy_att_workflow_driver_serviceinstance import AttWorkflowDriverServiceInstancePolicy, AttHelpers
         self.AttHelpers = AttHelpers
 
-        from mock_modelaccessor import MockObjectList
-
         # import all class names to globals
         for (k, v) in model_accessor.all_model_classes.items():
             globals()[k] = v
 
-        # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
-        # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
+        # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to
+        # creation of tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
         model_accessor.reset_all_object_stores()
 
-
         self.policy = AttWorkflowDriverServiceInstancePolicy(model_accessor=model_accessor)
         self.si = AttWorkflowDriverServiceInstance()
         self.si.owner = AttWorkflowDriverService()
@@ -72,7 +70,7 @@
             admin_state="ENABLED"
         )
         with patch.object(ONUDevice.objects, "get_items") as get_onu, \
-            patch.object(onu, "save") as onu_save:
+                patch.object(onu, "save") as onu_save:
             get_onu.return_value = [onu]
 
             self.policy.update_onu("brcm1234", "ENABLED")
@@ -80,13 +78,13 @@
 
             self.policy.update_onu("brcm1234", "DISABLED")
             self.assertEqual(onu.admin_state, "DISABLED")
-            onu_save.assert_called_with(always_update_timestamp=True, update_fields=['admin_state', 'serial_number', 'updated'])
-
+            onu_save.assert_called_with(
+                always_update_timestamp=True, update_fields=[
+                    'admin_state', 'serial_number', 'updated'])
 
     def test_enable_onu(self):
         with patch.object(self.AttHelpers, "validate_onu") as validate_onu, \
-            patch.object(self.policy, "update_onu") as update_onu, \
-            patch.object(self.si, "save") as save_si:
+                patch.object(self.policy, "update_onu") as update_onu:
             validate_onu.return_value = [True, "valid onu"]
 
             self.policy.process_onu_state(self.si)
@@ -98,8 +96,7 @@
 
     def test_disable_onu(self):
         with patch.object(self.AttHelpers, "validate_onu") as validate_onu, \
-                patch.object(self.policy, "update_onu") as update_onu, \
-                patch.object(self.si, "save") as save_si:
+                patch.object(self.policy, "update_onu") as update_onu:
             validate_onu.return_value = [False, "invalid onu"]
 
             self.policy.process_onu_state(self.si)
@@ -115,8 +112,8 @@
         when necessary
         """
         with patch.object(self.policy, "process_onu_state") as process_onu_state, \
-            patch.object(self.policy, "update_onu") as update_onu, \
-            patch.object(self.policy, "get_subscriber") as get_subscriber:
+                patch.object(self.policy, "update_onu") as update_onu, \
+                patch.object(self.policy, "get_subscriber") as get_subscriber:
             update_onu.return_value = None
             get_subscriber.return_value = None
 
@@ -132,7 +129,6 @@
             self.policy.handle_update(self.si)
             process_onu_state.assert_called_with(self.si)
 
-
     def test_get_subscriber(self):
 
         sub = RCORDSubscriber(
@@ -243,8 +239,8 @@
         self.si.mac_address = "4321"
 
         with patch.object(sub, "save") as sub_save, \
-            patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
-            patch.object(ip, "save_changed_fields") as ip_mock:
+                patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
+                patch.object(ip, "save_changed_fields") as ip_mock:
 
             get_ips.return_value = [ip]
             ip_mock.return_value = []
@@ -267,7 +263,7 @@
         self.si.mac_address = "4321"
 
         with patch.object(sub, "save") as sub_save, \
-            patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
+                patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
 
             ip_mock.return_value = []
 
@@ -288,12 +284,12 @@
         )
 
         with patch.object(self.policy, "get_subscriber") as get_subscriber, \
-            patch.object(self.policy, "update_onu") as update_onu, \
-            patch.object(self.policy, "update_subscriber") as update_subscriber:
+                patch.object(self.policy, "update_onu") as update_onu, \
+                patch.object(self.policy, "update_subscriber") as update_subscriber:
 
             get_subscriber.return_value = None
             self.policy.handle_update(self.si)
-            update_onu.assert_called_with(sub.onu_device, "DISABLED");
+            update_onu.assert_called_with(sub.onu_device, "DISABLED")
             self.assertEqual(update_subscriber.call_count, 0)
 
             get_subscriber.return_value = sub
@@ -303,4 +299,3 @@
 
 if __name__ == '__main__':
     unittest.main()
-
diff --git a/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_whitelistentry.py b/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_whitelistentry.py
index 9477258..7bc24b4 100644
--- a/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_whitelistentry.py
+++ b/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_whitelistentry.py
@@ -15,11 +15,13 @@
 
 
 import unittest
-from mock import patch, call, Mock, PropertyMock
+from mock import patch
 
-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__)))
+
 
 class TestModelPolicyAttWorkflowDriverWhiteListEntry(unittest.TestCase):
     def setUp(self):
@@ -37,7 +39,7 @@
 
         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
@@ -51,15 +53,14 @@
         for (k, v) in model_accessor.all_model_classes.items():
             globals()[k] = v
 
-        # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
-        # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
+        # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to
+        # creation of tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
         model_accessor.reset_all_object_stores()
 
         self.policy = AttWorkflowDriverWhiteListEntryPolicy(model_accessor=model_accessor)
 
         self.service = AttWorkflowDriverService()
 
-
     def tearDown(self):
         sys.path = self.sys_path_save
         self.service = None
@@ -67,39 +68,44 @@
     def test_enable_onu(self):
         si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
         with patch.object(self.AttHelpers, "validate_onu") as validate_onu, \
-            patch.object(si, "save") as save_si:
+                patch.object(si, "save") as save_si:
             validate_onu.return_value = [True, "valid onu"]
 
             self.policy.validate_onu_state(si)
 
             save_si.assert_called_once()
-            save_si.assert_called_with(always_update_timestamp=True, update_fields=['onu_state', 'serial_number', 'updated'])
+            save_si.assert_called_with(
+                always_update_timestamp=True, update_fields=[
+                    'onu_state', 'serial_number', 'updated'])
 
     def test_disable_onu(self):
         si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
         with patch.object(self.AttHelpers, "validate_onu") as validate_onu, \
-            patch.object(si, "save") as save_si:
+                patch.object(si, "save") as save_si:
             validate_onu.return_value = [False, "invalid onu"]
 
             self.policy.validate_onu_state(si)
 
             save_si.assert_called_once()
-            save_si.assert_called_with(always_update_timestamp=True, update_fields=['onu_state', 'serial_number', 'updated'])
+            save_si.assert_called_with(
+                always_update_timestamp=True, update_fields=[
+                    'onu_state', 'serial_number', 'updated'])
 
     def test_whitelist_update(self):
         si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id)
         wle = AttWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service)
         with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \
-            patch.object(self.policy, "validate_onu_state") as validate_onu_state, \
-            patch.object(wle, "save") as wle_save:
+                patch.object(self.policy, "validate_onu_state") as validate_onu_state, \
+                patch.object(wle, "save") as wle_save:
             oss_si_items.return_value = [si]
 
-
             self.policy.handle_update(wle)
 
             validate_onu_state.assert_called_with(si)
             self.assertTrue(wle.backend_need_delete_policy)
-            wle_save.assert_called_with(always_update_timestamp=False, update_fields=['backend_need_delete_policy', 'owner', 'serial_number'])
+            wle_save.assert_called_with(
+                always_update_timestamp=False, update_fields=[
+                    'backend_need_delete_policy', 'owner', 'serial_number'])
 
     def test_whitelist_delete(self):
         si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id)
@@ -113,7 +119,10 @@
 
             validate_onu_state.assert_called_with(si)
             self.assertTrue(wle.backend_need_reap)
-            wle_save.assert_called_with(always_update_timestamp=False, update_fields=['backend_need_reap', 'owner', 'serial_number'])
+            wle_save.assert_called_with(
+                always_update_timestamp=False, update_fields=[
+                    'backend_need_reap', 'owner', 'serial_number'])
+
+
 if __name__ == '__main__':
     unittest.main()
-