Moving DHCP event handling in model policy, update the subscriber only when needed

Change-Id: I77ec7c05d802a5902d96a8216b932d7a703af4e9
diff --git a/xos/synchronizer/event_steps/auth_event.py b/xos/synchronizer/event_steps/auth_event.py
index 9842406..6786c98 100644
--- a/xos/synchronizer/event_steps/auth_event.py
+++ b/xos/synchronizer/event_steps/auth_event.py
@@ -18,7 +18,8 @@
 import os
 import sys
 from synchronizers.new_base.eventstep import EventStep
-from synchronizers.new_base.modelaccessor import VOLTService, AttWorkflowDriverServiceInstance, model_accessor
+from synchronizers.new_base.modelaccessor import model_accessor
+from helpers import AttHelpers
 
 class SubscriberAuthEventStep(EventStep):
     topics = ["authentication.events"]
@@ -27,31 +28,14 @@
     def __init__(self, *args, **kwargs):
         super(SubscriberAuthEventStep, self).__init__(*args, **kwargs)
 
-    def get_onu_sn(self, event):
-        olt_service = VOLTService.objects.first()
-        onu_sn = olt_service.get_onu_sn_from_openflow(event["deviceId"], event["portNumber"])
-        if not onu_sn or onu_sn is None:
-            self.log.exception("authentication.events: Cannot find onu serial number for this event", kafka_event=event)
-            raise Exception("authentication.events: Cannot find onu serial number for this event")
-
-        return onu_sn
-
-    def get_si_by_sn(self, serial_number):
-        try:
-            return AttWorkflowDriverServiceInstance.objects.get(serial_number=serial_number)
-        except IndexError:
-            self.log.exception("authentication.events: Cannot find hippie-oss service instance for this event", kafka_event=value)
-            raise Exception("authentication.events: Cannot find hippie-oss service instance for this event")
-
     def process_event(self, event):
         value = json.loads(event.value)
 
-        onu_sn = self.get_onu_sn(value)
-        si = self.get_si_by_sn(onu_sn)
+        onu_sn = AttHelpers.get_onu_sn(value)
+        si = AttHelpers.get_si_by_sn(onu_sn)
         if not si:
-            self.log.exception("authentication.events: Cannot find hippie-oss service instance for this event", kafka_event=value)
-            raise Exception("authentication.events: Cannot find hippie-oss service instance for this event")
+            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")
 
         si.authentication_state = value["authenticationState"];
-        si.no_sync = True
-        si.save(update_fields=["authentication_state", "no_sync", "updated"], always_update_timestamp=True)
+        si.save(update_fields=["authentication_state", "updated"], always_update_timestamp=True)
diff --git a/xos/synchronizer/event_steps/dhcp_event.py b/xos/synchronizer/event_steps/dhcp_event.py
index b41f803..3d04e96 100644
--- a/xos/synchronizer/event_steps/dhcp_event.py
+++ b/xos/synchronizer/event_steps/dhcp_event.py
@@ -19,6 +19,7 @@
 import sys
 from synchronizers.new_base.eventstep import EventStep
 from synchronizers.new_base.modelaccessor import VOLTService, RCORDSubscriber, model_accessor
+from helpers import AttHelpers
 
 class SubscriberDhcpEventStep(EventStep):
     topics = ["dhcp.events"]
@@ -27,30 +28,31 @@
     def __init__(self, *args, **kwargs):
         super(SubscriberDhcpEventStep, self).__init__(*args, **kwargs)
 
-    def get_onu_sn(self, event):
-        olt_service = VOLTService.objects.first()
-        onu_sn = olt_service.get_onu_sn_from_openflow(event["deviceId"], event["portNumber"])
-        if not onu_sn or onu_sn is None:
-            self.log.exception("dhcp.events: Cannot find onu serial number for this event", kafka_event=event)
-            raise Exception("dhcp.events: Cannot find onu serial number for this event")
-
-        return onu_sn
-
     def process_event(self, event):
         value = json.loads(event.value)
 
-        onu_sn = self.get_onu_sn(value)
+        onu_sn = AttHelpers.get_onu_sn(value)
+        si = AttHelpers.get_si_by_sn(onu_sn)
 
-        subscriber = RCORDSubscriber.objects.get(onu_device=onu_sn)
+        if not si:
+            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", subscriber=subscriber, event_value=value, onu_sn=onu_sn)
+        self.log.info("dhcp.events: Got event for subscriber", event_value=value, onu_sn=onu_sn, si=si)
 
-        # NOTE it will be better to update the SI and use the model policy to update the subscriber,
-        # if this fails for any reason the event is lost
-        if subscriber.ip_address != value["ipAddress"] or \
-            subscriber.mac_address != value["macAddress"]:
-
-            # FIXME apparently it's always saving
-            subscriber.ip_address = value["ipAddress"]
-            subscriber.mac_address = value["macAddress"]
-            subscriber.save()
+        si.dhcp_state = value["messageType"];
+        si.ip_address = value["ipAddress"];
+        si.mac_address = value["macAddress"];
+        si.save(update_fields=["dhcp_state", "ip_address", "mac_address", "updated"], always_update_timestamp=True)
+        # subscriber = RCORDSubscriber.objects.get(onu_device=onu_sn)
+        #
+        #
+        # # NOTE it will be better to update the SI and use the model policy to update the subscriber,
+        # # if this fails for any reason the event is lost
+        #
+        # if value["messageType"] == "DHCPACK":
+        #
+        #     # FIXME apparently it's always saving
+        #     subscriber.ip_address = value["ipAddress"]
+        #     subscriber.mac_address = value["macAddress"]
+        #     subscriber.save()
diff --git a/xos/synchronizer/event_steps/test_auth_event.py b/xos/synchronizer/event_steps/test_auth_event.py
index 08887dd..e04f70b 100644
--- a/xos/synchronizer/event_steps/test_auth_event.py
+++ b/xos/synchronizer/event_steps/test_auth_event.py
@@ -107,5 +107,5 @@
 
             self.event_step.process_event(self.event)
 
-            self.hippie_si.save.assert_called_with(always_update_timestamp=True, update_fields=['authentication_state', 'no_sync', 'updated'])
+            self.hippie_si.save.assert_called_with(always_update_timestamp=True, update_fields=['authentication_state', 'updated'])
             self.assertEqual(self.hippie_si.authentication_state, 'APPROVED')
diff --git a/xos/synchronizer/event_steps/test_dhcp_event.py b/xos/synchronizer/event_steps/test_dhcp_event.py
index a13b2b3..a31bf3f 100644
--- a/xos/synchronizer/event_steps/test_dhcp_event.py
+++ b/xos/synchronizer/event_steps/test_dhcp_event.py
@@ -81,13 +81,17 @@
         self.volt.name = "vOLT"
         self.volt.leaf_model = Mock()
 
-        self.subscriber = RCORDSubscriber()
-        self.subscriber.onu_device = "BRCM1234"
-        self.subscriber.save = Mock()
+        # self.subscriber = RCORDSubscriber()
+        # self.subscriber.onu_device = "BRCM1234"
+        # self.subscriber.save = Mock()
 
         self.mac_address = "00:AA:00:00:00:01"
         self.ip_address = "192.168.3.5"
 
+        self.si = AttWorkflowDriverServiceInstance()
+        self.si.serial_number = "BRCM1234"
+        self.si.save = Mock()
+
 
     def tearDown(self):
         sys.path = self.sys_path_save
@@ -98,19 +102,21 @@
             "deviceId" : "of:0000000000000001",
             "portNumber" : "1",
             "macAddress" : self.mac_address,
-            "ipAddress" : self.ip_address
+            "ipAddress" : self.ip_address,
+            "messageType": "DHCPREQUEST"
         })
 
         with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \
-            patch.object(RCORDSubscriber.objects, "get_items") as subscriber_mock, \
+            patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as 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"
-            subscriber_mock.return_value = [self.subscriber]
+            si_mock.return_value = [self.si]
 
             self.event_step.process_event(self.event)
 
-            self.subscriber.save.assert_called()
-            self.assertEqual(self.subscriber.mac_address, self.mac_address)
-            self.assertEqual(self.subscriber.ip_address, self.ip_address)
+            self.si.save.assert_called()
+            self.assertEqual(self.si.dhcp_state, "DHCPREQUEST")
+            self.assertEqual(self.si.mac_address, self.mac_address)
+            self.assertEqual(self.si.ip_address, self.ip_address)