[SEBA-367] olt,onu synchronization fix

Change-Id: If7a5709537bda3b634e8aa1f710ce619886e26f7
diff --git a/VERSION b/VERSION
index ccbccc3..924d548 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1,2 @@
-2.2.0
+2.2.1.dev1
+
diff --git a/xos/synchronizer/pull_steps/pull_olts.py b/xos/synchronizer/pull_steps/pull_olts.py
index d44f4ce..33e89fb 100644
--- a/xos/synchronizer/pull_steps/pull_olts.py
+++ b/xos/synchronizer/pull_steps/pull_olts.py
@@ -74,16 +74,22 @@
             if r.status_code != 200:
                 log.debug("[OLT pull step] It was not possible to fetch devices from VOLTHA")
 
-            # keeping only OLTs
-            devices = [d for d in r.json()["items"] if "olt" in d["type"]]
+            # [SEBA-367] Handling blank response received from Voltha, Scenario occurs when voltha api is called while vcore service is re-starting
+            elif r.text:
+                # keeping only OLTs
+                devices = [d for d in r.json()["items"] if "olt" in d["type"]]
 
-            log.debug("[OLT pull step] received devices", olts=devices)
+                log.debug("[OLT pull step] received devices", olts=devices)
 
-            olts_in_voltha = self.create_or_update_olts(devices)
+                olts_in_voltha = self.create_or_update_olts(devices)
 
-            self.delete_olts(olts_in_voltha)
+                self.delete_olts(olts_in_voltha)
+            else:
+                log.debug("[OLT pull step] Blank response received")
 
-
+        except (ValueError, TypeError), e:
+            log.warn("[OLT pull step] Invalid Json received in response from VOLTHA", reason=e)
+            return
         except ConnectionError, e:
             log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e)
             return
diff --git a/xos/synchronizer/pull_steps/pull_onus.py b/xos/synchronizer/pull_steps/pull_onus.py
index da85248..6920f5c 100644
--- a/xos/synchronizer/pull_steps/pull_onus.py
+++ b/xos/synchronizer/pull_steps/pull_onus.py
@@ -51,16 +51,24 @@
             if r.status_code != 200:
                 log.warn("It was not possible to fetch devices from VOLTHA")
 
-            # keeping only ONUs
-            devices = [d for d in r.json()["items"] if "onu" in d["type"]]
+            # [SEBA-367] Handling blank response received from Voltha, Scenario occurs when voltha api is called while vcore service is re-starting
 
-            log.debug("received devices", onus=devices)
+            elif r.text:
+                # keeping only ONUs
+                devices = [d for d in r.json()["items"] if "onu" in d["type"]]
 
-            # TODO
-            # [ ] delete ONUS as ONUDevice.objects.all() - updated ONUs
+                log.debug("received devices", onus=devices)
 
-            onus_in_voltha = self.create_or_update_onus(devices)
+                # TODO
+                # [ ] delete ONUS as ONUDevice.objects.all() - updated ONUs
 
+                onus_in_voltha = self.create_or_update_onus(devices)
+            else:
+                log.debug("[ONU pull step] Blank response received")
+
+        except (ValueError, TypeError), e:
+            log.warn("[ONU pull step] Invalid Json received in response from VOLTHA", reason=e)
+            return
         except ConnectionError, e:
             log.warn("It was not possible to connect to VOLTHA", reason=e)
             return
diff --git a/xos/synchronizer/pull_steps/test_pull_olts.py b/xos/synchronizer/pull_steps/test_pull_olts.py
index 2fb4a49..7197f8d 100644
--- a/xos/synchronizer/pull_steps/test_pull_olts.py
+++ b/xos/synchronizer/pull_steps/test_pull_olts.py
@@ -217,6 +217,46 @@
 
             mock_olt_delete.assert_called()
 
+#[SEBA-367] Unit test for blank response recieved from Voltha
+
+    @requests_mock.Mocker()
+    def test_blank_response_received(self, m):
+
+        m.get("http://voltha_url:1234/api/v1/devices", status_code=200, text="")
+        with patch.object(VOLTService.objects, "all") as olt_service_mock, \
+        patch.object(PONPort, "save") as mock_pon_save, \
+                patch.object(NNIPort, "save") as mock_nni_save, \
+                patch.object(OLTDevice.objects, "get_items") as mock_get:
+
+            olt_service_mock.return_value = [self.volt_service]
+
+            self.sync_step(model_accessor=self.model_accessor).pull_records()
+
+            olt_service_mock.assert_called()
+            mock_pon_save.assert_not_called()
+            mock_nni_save.assert_not_called()
+            mock_get.assert_not_called()
+
+#[SEBA-367] Unit test for invalid json received from Voltha
+
+    @requests_mock.Mocker()
+    def test_invalid_json(self, m):
+
+        m.get("http://voltha_url:1234/api/v1/devices", status_code=200, text="{\"items\" : [host_and_port }")
+        with patch.object(VOLTService.objects, "all") as olt_service_mock, \
+        patch.object(PONPort, "save") as mock_pon_save, \
+                patch.object(NNIPort, "save") as mock_nni_save, \
+                patch.object(OLTDevice.objects, "get_items") as mock_get:
+
+            olt_service_mock.return_value =  [self.volt_service]
+
+            self.sync_step(model_accessor=self.model_accessor).pull_records()
+
+            olt_service_mock.assert_called()
+            mock_pon_save.assert_not_called()
+            mock_nni_save.assert_not_called()
+            mock_get.assert_not_called()
 
 if __name__ == "__main__":
-    unittest.main()
\ No newline at end of file
+    unittest.main()
+
diff --git a/xos/synchronizer/pull_steps/test_pull_onus.py b/xos/synchronizer/pull_steps/test_pull_onus.py
index de9870c..611e084 100644
--- a/xos/synchronizer/pull_steps/test_pull_onus.py
+++ b/xos/synchronizer/pull_steps/test_pull_onus.py
@@ -258,6 +258,45 @@
 
             self.assertEqual(mock_save.call_count, 1)
 
+#[SEBA-367] Unit test for blank response recieved from Voltha
+
+    @requests_mock.Mocker()
+    def test_blank_response_received(self, m):
+
+        m.get("http://voltha_url:1234/api/v1/devices", status_code=200, text="")
+        with patch.object(VOLTService.objects, "all") as olt_service_mock, \
+        patch.object(PONPort.objects, "get") as mock_pon_port, \
+                patch.object(OLTDevice.objects, "get") as mock_get, \
+                patch.object(ONUDevice, "save", autospec=True) as mock_save:
+
+            olt_service_mock.return_value = [self.volt_service]
+
+            self.sync_step(model_accessor=self.model_accessor).pull_records()
+
+            olt_service_mock.assert_called()
+            mock_pon_port.assert_not_called()
+            mock_get.assert_not_called()
+            self.assertEqual(mock_save.call_count, 0)
+
+#[SEBA-367] Unit test for invalid json received from Voltha
+
+    @requests_mock.Mocker()
+    def test_invalid_json(self, m):
+
+        m.get("http://voltha_url:1234/api/v1/devices", status_code=200, text="{\"items\" : [host_and_port}")
+        with patch.object(VOLTService.objects, "all") as olt_service_mock, \
+        patch.object(PONPort.objects, "get") as mock_pon_port, \
+                patch.object(OLTDevice.objects, "get") as mock_get, \
+                patch.object(ONUDevice, "save", autospec=True) as mock_save:
+
+            olt_service_mock.return_value = [self.volt_service]
+
+            self.sync_step(model_accessor=self.model_accessor).pull_records()
+
+            olt_service_mock.assert_called()
+            mock_pon_port.assert_not_called()
+            mock_get.assert_not_called()
+            self.assertEqual(mock_save.call_count, 0)
 
 if __name__ == "__main__":
-    unittest.main()
\ No newline at end of file
+    unittest.main()