[CORD-3226] Adding support for dual stack IPs

Change-Id: I75919b9a16d4e8e7f03fdce9b7294b1c43ce0ecd
diff --git a/VERSION b/VERSION
index 238d6e8..3a17d48 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.0.7
+1.0.8-dev
diff --git a/xos/synchronizer/event_steps/dhcp_event.py b/xos/synchronizer/event_steps/dhcp_event.py
index ddd1e31..74a7696 100644
--- a/xos/synchronizer/event_steps/dhcp_event.py
+++ b/xos/synchronizer/event_steps/dhcp_event.py
@@ -43,16 +43,5 @@
         si.dhcp_state = value["messageType"];
         si.ip_address = value["ipAddress"];
         si.mac_address = value["macAddress"];
+
         si.save_changed_fields(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/model_policies/model_policy_att_workflow_driver_serviceinstance.py b/xos/synchronizer/model_policies/model_policy_att_workflow_driver_serviceinstance.py
index d3195e0..3fb05dd 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
@@ -15,7 +15,7 @@
 
 
 
-from synchronizers.new_base.modelaccessor import RCORDSubscriber, ONUDevice, model_accessor
+from synchronizers.new_base.modelaccessor import RCORDSubscriber, RCORDIpAddress, ONUDevice, model_accessor
 from synchronizers.new_base.policy import Policy
 
 import os
@@ -83,6 +83,25 @@
             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):
+        # TODO check if the subscriber has an IP and update it,
+        # or create a new one
+        try:
+            ip = RCORDIpAddress.objects.filter(
+                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)
+            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)
+            ip = RCORDIpAddress(
+                subscriber_id=subscriber.id,
+                ip=ip,
+                description="DHCP Assigned IP Address"
+            )
+            ip.save()
+
     def update_subscriber(self, subscriber, si):
         cur_status = subscriber.status
         if si.authentication_state == "AWAITING":
@@ -107,7 +126,8 @@
         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)
             if si.ip_address and si.mac_address:
-                subscriber.ip_address = si.ip_address
+                # subscriber.ip_address = si.ip_address
+                self.update_subscriber_ip(subscriber, si.ip_address)
                 subscriber.mac_address = si.mac_address
             subscriber.save_changed_fields(always_update_timestamp=True)
         else:
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 306094c..be5461f 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
@@ -240,22 +240,57 @@
             self.policy.update_subscriber(sub, self.si)
             sub_save.assert_not_called()
 
-    def test_update_subscriber_dhcp(self):
+    def test_update_subscriber_dhcp_with_exiting_ip(self):
         sub = RCORDSubscriber(
+            id=10,
+            onu_device="BRCM1234"
+        )
+
+        ip = RCORDIpAddress(
+            subscriber_id=sub.id,
+            ip='10.11.2.23'
+        )
+
+        self.si.dhcp_state = "DHCPACK"
+        self.si.ip_address = "10.11.2.23"
+        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:
+
+            get_ips.return_value = [ip]
+            ip_mock.return_value = []
+
+            self.policy.update_subscriber(sub, self.si)
+            sub_save.assert_called()
+            self.assertEqual(sub.mac_address, self.si.mac_address)
+
+            ip_mock.assert_called_with()
+
+    def test_update_subscriber_dhcp_with_new_ip(self):
+        sub = RCORDSubscriber(
+            id=10,
             onu_device="BRCM1234"
         )
 
         self.si.dhcp_state = "DHCPACK"
-        self.si.ip_address = "1234"
+        self.si.ip_address = "10.11.2.23"
         self.si.mac_address = "4321"
 
-        with patch.object(sub, "save") as sub_save:
+        with patch.object(sub, "save") as sub_save, \
+            patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
+
+            ip_mock.return_value = []
+
             self.policy.update_subscriber(sub, self.si)
             sub_save.assert_called()
             self.assertEqual(sub.mac_address, self.si.mac_address)
-            self.assertEqual(sub.ip_address, self.si.ip_address)
 
-
+            saved_ip = ip_mock.call_args[0][0]
+            self.assertEqual(saved_ip.ip, self.si.ip_address)
+            self.assertEqual(saved_ip.subscriber_id, sub.id)
+            self.assertEqual(saved_ip.description, "DHCP Assigned IP Address")
 
     def test_handle_update_subscriber(self):
         self.si.onu_state = "DISABLED"