[SEBA-199] Fixed OLT Deletion and reading delete OLTs from VOLTHA

Change-Id: I93f742daef035ccae1a76bf6436642d315900200
diff --git a/xos/synchronizer/model_policies/test_config.yaml b/xos/synchronizer/model_policies/test_config.yaml
index 270d0a9..1560049 100644
--- a/xos/synchronizer/model_policies/test_config.yaml
+++ b/xos/synchronizer/model_policies/test_config.yaml
@@ -28,3 +28,4 @@
     'multistructlog':
       handlers:
           - console
+#      level: DEBUG
diff --git a/xos/synchronizer/pull_steps/pull_olts.py b/xos/synchronizer/pull_steps/pull_olts.py
index 7db872a..f603da6 100644
--- a/xos/synchronizer/pull_steps/pull_olts.py
+++ b/xos/synchronizer/pull_steps/pull_olts.py
@@ -54,7 +54,7 @@
         raise Exception("Can't find a logical device for device id: %s" % o.device_id)
 
     def pull_records(self):
-        log.info("pulling OLT devices from VOLTHA")
+        log.info("[OLT pull step] pulling OLT devices from VOLTHA")
 
         try:
             self.volt_service = VOLTService.objects.all()[0]
@@ -69,23 +69,24 @@
             r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port))
 
             if r.status_code != 200:
-                log.info("It was not possible to fetch devices from VOLTHA")
+                log.info("[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"]]
 
-            log.debug("received devices", olts=devices)
+            log.debug("[OLT pull step] received devices", olts=devices)
 
-            # TODO
-            # [ ] delete OLTS as OLTDevice.objects.all() - updated OLTs
 
             olts_in_voltha = self.create_or_update_olts(devices)
 
+            self.delete_olts(olts_in_voltha)
+
+
         except ConnectionError, e:
-            log.warn("It was not possible to connect to VOLTHA", reason=e)
+            log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e)
             return
         except InvalidURL, e:
-            log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
+            log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
             return
 
     def create_or_update_olts(self, olts):
@@ -98,17 +99,19 @@
                     [host, port] = ["172.17.0.1", "50060"]
                 else:
                     [host, port] = olt["host_and_port"].split(":")
+
                 model = OLTDevice.objects.filter(device_type=olt["type"], host=host, port=port)[0]
 
-                log.debug("OLTDevice already exists, updating it", device_type=olt["type"], host=host, port=port)
+                log.debug("[OLT pull step] OLTDevice already exists, updating it", device_type=olt["type"], host=host, port=port)
 
                 if model.enacted < model.updated:
-                    log.info("Skipping pull on OLTDevice %s as enacted < updated" % model.name, name=model.name, id=model.id, enacted=model.enacted, updated=model.updated)
+                    log.info("[OLT pull step] Skipping pull on OLTDevice %s as enacted < updated" % model.name, name=model.name, id=model.id, enacted=model.enacted, updated=model.updated)
                     # if we are not updating the device we still need to pull ports
                     self.fetch_olt_ports(model)
-                    return
+                    continue
 
             except IndexError:
+
                 model = OLTDevice()
                 model.device_type = olt["type"]
 
@@ -120,7 +123,7 @@
                     model.host = host
                     model.port = int(port)
 
-                log.debug("OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port)
+                log.debug("[OLT pull step] OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port)
 
             # Adding feedback state to the device
             model.device_id = olt["id"]
@@ -149,19 +152,19 @@
             r = requests.get("%s:%s/api/v1/devices/%s/ports" % (voltha_url, voltha_port, olt.device_id))
 
             if r.status_code != 200:
-                log.info("It was not possible to fetch ports from VOLTHA for device %s" % olt.device_id)
+                log.info("[OLT pull step] It was not possible to fetch ports from VOLTHA for device %s" % olt.device_id)
 
             ports = r.json()['items']
 
-            log.debug("received ports", ports=ports, olt=olt.device_id)
+            log.debug("[OLT pull step] received ports", ports=ports, olt=olt.device_id)
 
             self.create_or_update_ports(ports, olt)
 
         except ConnectionError, e:
-            log.warn("It was not possible to connect to VOLTHA", reason=e)
+            log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e)
             return
         except InvalidURL, e:
-            log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
+            log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
             return
         return
 
@@ -179,13 +182,13 @@
         for port in pon_ports:
             try:
                 model = PONPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0]
-                log.debug("PONPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id)
+                log.debug("[OLT pull step] PONPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id)
             except IndexError:
                 model = PONPort()
                 model.port_no = port["port_no"]
                 model.olt_device_id = olt.id
                 model.name = port["label"]
-                log.debug("PONPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id)
+                log.debug("[OLT pull step] PONPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id)
 
             model.admin_state = port["admin_state"]
             model.oper_status = port["oper_status"]
@@ -200,14 +203,14 @@
             try:
                 model = NNIPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0]
                 model.xos_managed = False
-                log.debug("NNIPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id)
+                log.debug("[OLT pull step] NNIPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id)
             except IndexError:
                 model = NNIPort()
                 model.port_no = port["port_no"]
                 model.olt_device_id = olt.id
                 model.name = port["label"]
                 model.xos_managed = False
-                log.debug("NNIPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id)
+                log.debug("[OLT pull step] NNIPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id)
 
             model.admin_state = port["admin_state"]
             model.oper_status = port["oper_status"]
@@ -215,7 +218,20 @@
             update_ports.append(model)
         return update_ports
 
+    def delete_olts(self, olts_in_voltha):
+        olts_id_in_voltha = [m.device_id for m in olts_in_voltha]
 
+        xos_olts = OLTDevice.objects.all()
 
+        deleted_in_voltha = [o for o in xos_olts if o.device_id not in olts_id_in_voltha]
 
+        for model in deleted_in_voltha:
 
+            if model.enacted < model.updated:
+                # DO NOT delete a model that is being processed
+                log.info("[OLT pull step] device is not present in VOLTHA, skipping deletion as sync is in progress", device_id=o.device_id,
+                         name=o.name)
+                continue
+
+            log.info("[OLT pull step] deleting device as it's not present in VOLTHA", device_id=o.device_id, name=o.name)
+            model.delete()
diff --git a/xos/synchronizer/pull_steps/pull_onus.py b/xos/synchronizer/pull_steps/pull_onus.py
index 69d9ef8..8c9efe9 100644
--- a/xos/synchronizer/pull_steps/pull_onus.py
+++ b/xos/synchronizer/pull_steps/pull_onus.py
@@ -162,7 +162,7 @@
                 log.info("It was not possible to fetch ports from VOLTHA for logical_device %s" % logical_device_id)
 
             logical_ports = r.json()['items']
-            log.info("logical device ports for ONUDevice %s" % onu.device_id, logical_ports=logical_ports)
+            log.debug("logical device ports for ONUDevice %s" % onu.device_id, logical_ports=logical_ports)
 
             ports = [p['ofp_port']['port_no'] for p in logical_ports if p['device_id'] == onu.device_id]
             # log.info("Port_id for port %s on ONUDevice %s: %s" % (port['label'], onu.device_id, ports), logical_ports=logical_ports)
diff --git a/xos/synchronizer/pull_steps/test_pull_olts.py b/xos/synchronizer/pull_steps/test_pull_olts.py
index 91b64e3..0e8d262 100644
--- a/xos/synchronizer/pull_steps/test_pull_olts.py
+++ b/xos/synchronizer/pull_steps/test_pull_olts.py
@@ -215,5 +215,26 @@
             mock_pon_save.assert_called()
             mock_nni_save.assert_called()
 
+    @requests_mock.Mocker()
+    def test_pull_deleted_object(self, m):
+        existing_olt = Mock()
+        existing_olt.enacted = 2
+        existing_olt.updated = 1
+        existing_olt.device_id = "test_id"
+
+        m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json={"items": []})
+
+        with patch.object(VOLTService.objects, "all") as olt_service_mock, \
+                patch.object(OLTDevice.objects, "get_items") as mock_get, \
+                patch.object(existing_olt, "delete") as mock_olt_delete:
+
+            olt_service_mock.return_value = [self.volt_service]
+            mock_get.return_value = [existing_olt]
+
+            self.sync_step().pull_records()
+
+            mock_olt_delete.assert_called()
+
+
 if __name__ == "__main__":
     unittest.main()
\ No newline at end of file
diff --git a/xos/synchronizer/steps/sync_olt_device.py b/xos/synchronizer/steps/sync_olt_device.py
index 056a62c..b65dfef 100644
--- a/xos/synchronizer/steps/sync_olt_device.py
+++ b/xos/synchronizer/steps/sync_olt_device.py
@@ -124,41 +124,6 @@
 
         return model
 
-    def configure_onos(self, model):
-
-        log.info("Adding OLT device in onos-voltha", object=str(model), **model.tologdict())
-
-        onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
-        onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
-
-        # Add device info to onos-voltha
-        data = {
-          "devices": {
-            model.dp_id: {
-              "basic": {
-                "driver": model.driver
-              },
-              "accessDevice": {
-                "uplink": model.uplink,
-                "vlan": 1
-              }
-            }
-          }
-        }
-
-        url = "%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port'])
-        request = requests.post(url, json=data, auth=onos_voltha_basic_auth)
-
-        if request.status_code != 200:
-            log.error(request.text)
-            raise Exception("Failed to add OLT device %s into ONOS" % model.name)
-        else:
-            try:
-                print request.json()
-            except Exception:
-                print request.text
-        return model
-
     def sync_record(self, model):
         log.info("Synching device", object=str(model), **model.tologdict())
 
@@ -172,19 +137,10 @@
         else:
             log.info("OLT device already exists in VOLTHA", object=str(model), **model.tologdict())
 
-        # TODO configure onos only if we have: Switch datapath id, Switch port, Uplink
-        if model.switch_datapath_id and model.switch_port and model.uplink:
-            log.info("Pushing OLT device to ONOS-VOLTHA", object=str(model), **model.tologdict())
-            self.configure_onos(model)
-        else:
-            raise DeferredException("Not pushing OLTDevice (%s) to ONOS-VOLTHA as parameters are missing" %  model.id)
-
     def delete_record(self, model):
         log.info("Deleting OLT device", object=str(model), **model.tologdict())
 
         voltha = Helpers.get_voltha_info(model.volt_service)
-        onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
-        onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
 
         if not model.device_id:
             log.error("OLTDevice %s has no device_id" % model.name)
@@ -202,11 +158,3 @@
             if request.status_code != 200:
                 log.error("Failed to delete OLT device from VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code)
                 raise Exception("Failed to delete OLT device from VOLTHA")
-
-            # Remove the device from ONOS
-            request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % (
-                onos_voltha['url'], onos_voltha['port'], model.of_id), auth=onos_voltha_basic_auth)
-
-            if request.status_code != 204:
-                log.error("Failed to remove OLT device from ONOS: %s - %s" % (model.name, model.of_id), rest_response=request.text, rest_status_code=request.status_code)
-                raise Exception("Failed to remove OLT device from ONOS")
diff --git a/xos/synchronizer/steps/test_sync_olt_device.py b/xos/synchronizer/steps/test_sync_olt_device.py
index 04cdd0b..670c49c 100644
--- a/xos/synchronizer/steps/test_sync_olt_device.py
+++ b/xos/synchronizer/steps/test_sync_olt_device.py
@@ -94,10 +94,6 @@
         o.volt_service.voltha_port = 1234
         o.volt_service.voltha_user = "voltha_user"
         o.volt_service.voltha_pass = "voltha_pass"
-        o.volt_service.onos_voltha_url = "onos_voltha_url"
-        o.volt_service.onos_voltha_port = 4321
-        o.volt_service.onos_voltha_user = "onos_voltha_user"
-        o.volt_service.onos_voltha_pass = "onos_voltha_pass"
 
         o.device_type = "ponsim_olt"
         o.host = "172.17.0.1"
@@ -200,8 +196,6 @@
         }
         m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
 
-        m.post("http://onos_voltha_url:4321/onos/v1/network/configuration/", status_code = 200, additional_matcher=match_onos_req, json={})
-
         self.sync_step().sync_record(self.o)
         self.assertEqual(self.o.admin_state, "ENABLED")
         self.assertEqual(self.o.oper_status, "ACTIVE")
@@ -235,9 +229,6 @@
         }
         m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
 
-        m.post("http://onos_voltha_url:4321/onos/v1/network/configuration/", status_code=200,
-               additional_matcher=match_onos_req, json={})
-
         self.sync_step().sync_record(self.o)
         self.assertEqual(self.o.admin_state, "ENABLED")
         self.assertEqual(self.o.oper_status, "ACTIVE")
@@ -271,9 +262,6 @@
         }
         m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
 
-        m.post("http://onos_voltha_url:4321/onos/v1/network/configuration/", status_code=200,
-               additional_matcher=match_onos_req, json={})
-
         with self.assertRaises(Exception) as e:
             self.sync_step().sync_record(self.o)
 
@@ -291,8 +279,6 @@
         self.o.dp_id = "of:0000000ce2314000"
         self.o.of_id = "0001000ce2314000"
 
-        m.post("http://onos_voltha_url:4321/onos/v1/network/configuration/", status_code = 200, additional_matcher=match_onos_req, json={})
-
         self.sync_step().sync_record(self.o)
         self.o.save.assert_not_called()
 
@@ -301,7 +287,6 @@
         self.o.of_id = "0001000ce2314000"
         self.o.device_id = "123"
 
-        m.delete("http://onos_voltha_url:4321/onos/v1/network/configuration/devices/0001000ce2314000", status_code=204)
         m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200)
         m.delete("http://voltha_url:1234/api/v1/devices/123/delete", status_code=200)