SEBA-612 Port SEBA-571 changes to tt-workflow-driver

Change-Id: I8d6933805fa0106979e9f2e4bd6ac68be0f51e6c
diff --git a/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_serviceinstance.py b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_serviceinstance.py
index 95081c8..f9628e3 100644
--- a/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_serviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_serviceinstance.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 
 
-
+from helpers import TtHelpers
 from xossynchronizer.model_policies.policy import Policy
 
 import os
@@ -23,7 +23,6 @@
 sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
 sys.path.append(sync_path)
 
-from helpers import TtHelpers
 
 class DeferredException(Exception):
     pass
@@ -36,7 +35,7 @@
         self.handle_update(si)
 
     def handle_update(self, si):
-        self.logger.debug("MODEL_POLICY: handle_update for TtWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.onu_state)
+        self.logger.debug("MODEL_POLICY: handle_update for TtWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.admin_onu_state)
 
         self.process_onu_state(si)
         self.process_dhcp_state(si)
@@ -51,28 +50,22 @@
 
         si.save_changed_fields()
 
+    # Check the whitelist to see if the ONU is valid.  If it is, make sure that it's enabled.
     def process_onu_state(self, si):
         [valid, message] = TtHelpers.validate_onu(self.model_accessor, self.logger, si)
-        if si.onu_state == "AWAITING" or si.onu_state == "ENABLED":
-            si.status_message = message
-            if valid:
-                si.onu_state = "ENABLED"
-                self.update_onu(si.serial_number, "ENABLED")
-            else:
-                si.onu_state = "DISABLED"
-                self.update_onu(si.serial_number, "DISABLED")
-        else: # DISABLED
-            if not valid:
-                si.status_message = message
-            else:
-                si.status_message = "ONU has been disabled"
+        si.status_message = message
+        if valid:
+            si.admin_onu_state = "ENABLED"
+            self.update_onu(si.serial_number, "ENABLED")
+        else:
+            si.admin_onu_state = "DISABLED"
             self.update_onu(si.serial_number, "DISABLED")
 
 
     # The DhcpL2Relay ONOS app generates events that update the fields below.
     # It only sends events when it processes DHCP packets.  It keeps no internal state.
     def process_dhcp_state(self, si):
-        if si.onu_state in ["AWAITING", "DISABLED"]:
+        if si.admin_onu_state in ["AWAITING", "DISABLED"]:
             si.ip_address = ""
             si.mac_address = ""
             si.dhcp_state = "AWAITING"
@@ -87,17 +80,20 @@
     # ENABLED   | *
     # DISABLED  | AWAITING
     def validate_states(self, si):
-        if (si.onu_state == "AWAITING" or si.onu_state == "DISABLED") and si.dhcp_state == "AWAITING":
+        if (si.admin_onu_state == "AWAITING" or si.admin_onu_state == "DISABLED") and si.dhcp_state == "AWAITING":
             return
-        if si.onu_state == "ENABLED":
+        if si.admin_onu_state == "ENABLED":
             return
-        self.logger.warning("MODEL_POLICY (validate_states): invalid state combination", onu_state=si.onu_state, dhcp_state=si.dhcp_state)
+        self.logger.warning("MODEL_POLICY (validate_states): invalid state combination", onu_state=si.admin_onu_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
@@ -105,10 +101,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):
@@ -119,10 +118,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,
@@ -136,25 +140,32 @@
                 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):
         cur_status = subscriber.status
         # Don't change state if someone has disabled the subscriber
         if subscriber.status != "disabled":
-            if si.onu_state == "ENABLED":
+            if si.admin_onu_state == "ENABLED":
                 subscriber.status = "enabled"
-            elif si.onu_state == "DISABLED":
+            elif si.admin_onu_state == "DISABLED":
                 subscriber.status = "awaiting-auth"
 
         # NOTE we save the subscriber only if:
         # - 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, subscriber_status=subscriber.status)
+            self.logger.debug(
+                "MODEL_POLICY: updating subscriber",
+                onu_device=subscriber.onu_device,
+                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_tt_workflow_driver_whitelistentry.py b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_whitelistentry.py
index b16642a..3d5f34c 100644
--- a/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_whitelistentry.py
+++ b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_whitelistentry.py
@@ -34,12 +34,14 @@
     def validate_onu_state(self, si):
         [valid, message] = TtHelpers.validate_onu(self.model_accessor, self.logger, si)
         if valid:
-            si.onu_state = "ENABLED"
+            si.admin_onu_state = "ENABLED"
         else:
-            si.onu_state = "DISABLED"
+            si.admin_onu_state = "DISABLED"
 
         self.logger.debug(
-            "MODEL_POLICY: activating TtWorkflowDriverServiceInstance because of change in the whitelist", si=si, onu_state=si.onu_state)
+            "MODEL_POLICY: activating TtWorkflowDriverServiceInstance because of change in the whitelist",
+            si=si,
+            onu_state=si.admin_onu_state)
         si.save_changed_fields(always_update_timestamp=True)
 
     def handle_update(self, whitelist):
diff --git a/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_serviceinstance.py b/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_serviceinstance.py
index a067779..2496f9d 100644
--- a/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_serviceinstance.py
+++ b/xos/synchronizer/model_policies/test_model_policy_tt_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 TestModelPolicyTtWorkflowDriverServiceInstance(unittest.TestCase):
@@ -46,14 +47,12 @@
         from model_policy_tt_workflow_driver_serviceinstance import TtWorkflowDriverServiceInstancePolicy, TtHelpers
         self.TtHelpers = TtHelpers
 
-        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()
 
 
@@ -72,7 +71,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 +79,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.TtHelpers, "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 +97,7 @@
 
     def test_disable_onu(self):
         with patch.object(self.TtHelpers, "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,24 +113,26 @@
         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
 
-            self.si.onu_state = "AWAITING"
+            self.si.admin_onu_state = "AWAITING"
+            self.si.oper_onu_status = "AWAITING"
             self.policy.handle_update(self.si)
             process_onu_state.assert_called_with(self.si)
 
-            self.si.onu_state = "ENABLED"
+            self.si.admin_onu_state = "ENABLED"
+            self.si.oper_onu_status = "ENABLED"
             self.policy.handle_update(self.si)
             process_onu_state.assert_called_with(self.si)
 
-            self.si.onu_state = "DISABLED"
+            self.si.admin_onu_state = "DISABLED"
+            self.si.oper_onu_status = "DISABLED"
             self.policy.handle_update(self.si)
             process_onu_state.assert_called_with(self.si)
 
-
     def test_get_subscriber(self):
 
         sub = RCORDSubscriber(
@@ -160,7 +160,7 @@
         self.si.status_message = "some content"
 
         with patch.object(sub, "save") as sub_save:
-            self.si.onu_state = "ENABLED"
+            self.si.admin_onu_state = "ENABLED"
             sub.status = "awaiting-auth"
             self.policy.update_subscriber(sub, self.si)
             self.assertEqual(sub.status, "enabled")
@@ -168,7 +168,7 @@
             sub_save.reset_mock()
             sub.status = None
 
-            self.si.onu_state = "DISABLED"
+            self.si.admin_onu_state = "DISABLED"
             sub.status = "enabled"
             self.policy.update_subscriber(sub, self.si)
             self.assertEqual(sub.status, "awaiting-auth")
@@ -183,12 +183,12 @@
 
         with patch.object(sub, "save") as sub_save:
             sub.status = "enabled"
-            self.si.onu_state = "ENABLED"
+            self.si.admin_onu_state = "ENABLED"
             self.policy.update_subscriber(sub, self.si)
             sub_save.assert_not_called()
 
             sub.status = "awaiting-auth"
-            self.si.onu_state = "DISABLED"
+            self.si.admin_onu_state = "DISABLED"
             self.policy.update_subscriber(sub, self.si)
             sub_save.assert_not_called()
 
@@ -208,8 +208,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 = []
@@ -231,7 +231,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 = []
 
@@ -245,19 +245,19 @@
             self.assertEqual(saved_ip.description, "DHCP Assigned IP Address")
 
     def test_handle_update_subscriber(self):
-        self.si.onu_state = "DISABLED"
+        self.si.admin_onu_state = "DISABLED"
 
         sub = RCORDSubscriber(
             onu_device="BRCM1234"
         )
 
         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
@@ -266,5 +266,5 @@
 
 
 if __name__ == '__main__':
+    sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
     unittest.main()
-
diff --git a/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_whitelistentry.py b/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_whitelistentry.py
index bee8971..a439cab 100644
--- a/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_whitelistentry.py
+++ b/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_whitelistentry.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 TestModelPolicyTtWorkflowDriverWhiteListEntry(unittest.TestCase):
     def setUp(self):
@@ -37,7 +38,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,8 +52,8 @@
         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 = TtWorkflowDriverWhiteListEntryPolicy(model_accessor=model_accessor)
@@ -67,39 +68,44 @@
     def test_enable_onu(self):
         si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
         with patch.object(self.TtHelpers, "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=[
+                    'admin_onu_state', 'serial_number', 'updated'])
 
     def test_disable_onu(self):
         si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
         with patch.object(self.TtHelpers, "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=[
+                    'admin_onu_state', 'serial_number', 'updated'])
 
     def test_whitelist_update(self):
         si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id)
         wle = TtWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service)
         with patch.object(TtWorkflowDriverServiceInstance.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 = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id)
@@ -113,7 +119,11 @@
 
             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'])
-if __name__ == '__main__':
-    unittest.main()
+            wle_save.assert_called_with(
+                always_update_timestamp=False, update_fields=[
+                    'backend_need_reap', 'owner', 'serial_number'])
 
+
+if __name__ == '__main__':
+    sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
+    unittest.main()