[SEBA-571] Reset subscriber status when ONU is disabled

Change-Id: Ic05484e0bbf52ab82ee712b895638cbe4e4bacf9
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 3b7b74e..833adf9 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
@@ -37,7 +37,7 @@
 
     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)
+                          (si.id), onu_state=si.admin_onu_state, authentication_state=si.authentication_state)
 
         # Changing ONU state can change auth state
         # Changing auth state can change DHCP state
@@ -56,21 +56,15 @@
 
         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] = AttHelpers.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")
 
     # If the ONU has been disabled then we force re-authentication when it
@@ -87,7 +81,7 @@
             "APPROVED": " - Authentication succeeded",
             "DENIED": " - Authentication denied"
         }
-        if si.onu_state == "DISABLED":
+        if si.admin_onu_state == "DISABLED" or si.oper_onu_status == "DISABLED":
             si.authentication_state = "AWAITING"
         else:
             si.status_message += auth_msgs[si.authentication_state]
@@ -119,14 +113,14 @@
     # ENABLED   | APPROVED | *
     # DISABLED  | AWAITING | AWAITING
     def validate_states(self, si):
-        if (si.onu_state == "AWAITING" or si.onu_state ==
+        if (si.admin_onu_state == "AWAITING" or si.admin_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"):
+        if si.admin_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,
+            onu_state=si.admin_onu_state,
             auth_state=si.authentication_state,
             dhcp_state=si.dhcp_state)
 
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 7a31116..348af76 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
@@ -34,14 +34,14 @@
     def validate_onu_state(self, si):
         [valid, message] = AttHelpers.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 AttWorkflowDriverServiceInstance because of change in the whitelist",
             si=si,
-            onu_state=si.onu_state,
+            onu_state=si.admin_onu_state,
             authentication_state=si.authentication_state)
         si.save_changed_fields(always_update_timestamp=True)
 
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 d784258..7de68cb 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
@@ -117,15 +117,18 @@
             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)
 
@@ -277,7 +280,7 @@
             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"
@@ -296,6 +299,24 @@
             self.policy.handle_update(self.si)
             update_subscriber.assert_called_with(sub, self.si)
 
+    def test_process_auth_state(self):
+        # testing change in admin_onu_state
+        self.si.admin_onu_state = "DISABLED"
+        self.si.oper_onu_status = "ENABLED"
+        self.si.authentication_state, "APPROVED"
+
+        self.policy.process_auth_state(self.si)
+        self.assertEqual(self.si.authentication_state, "AWAITING")
+
+        # testing change in oper_onu_status
+        self.si.admin_onu_state = "ENABLED"
+        self.si.oper_onu_status = "DISABLED"
+        self.si.authentication_state, "APPROVED"
+
+        self.policy.process_auth_state(self.si)
+        self.assertEqual(self.si.authentication_state, "AWAITING")
+
 
 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_att_workflow_driver_whitelistentry.py b/xos/synchronizer/model_policies/test_model_policy_att_workflow_driver_whitelistentry.py
index 7bc24b4..8015a6b 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
@@ -76,7 +76,7 @@
             save_si.assert_called_once()
             save_si.assert_called_with(
                 always_update_timestamp=True, update_fields=[
-                    'onu_state', 'serial_number', 'updated'])
+                    'admin_onu_state', 'serial_number', 'updated'])
 
     def test_disable_onu(self):
         si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
@@ -89,7 +89,7 @@
             save_si.assert_called_once()
             save_si.assert_called_with(
                 always_update_timestamp=True, update_fields=[
-                    'onu_state', 'serial_number', 'updated'])
+                    'admin_onu_state', 'serial_number', 'updated'])
 
     def test_whitelist_update(self):
         si = AttWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id)
@@ -125,4 +125,5 @@
 
 
 if __name__ == '__main__':
+    sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
     unittest.main()