[VOL-4669] Ignoring port events on disconnected device

Change-Id: I74f236b635ca015a6a51aa279672f48074f6981f
diff --git a/impl/src/main/java/org/opencord/olt/impl/Olt.java b/impl/src/main/java/org/opencord/olt/impl/Olt.java
index 607b146..ace976b 100644
--- a/impl/src/main/java/org/opencord/olt/impl/Olt.java
+++ b/impl/src/main/java/org/opencord/olt/impl/Olt.java
@@ -758,7 +758,6 @@
                     case DEVICE_ADDED:
                         return;
                     case PORT_ADDED:
-                    case PORT_UPDATED:
                     case PORT_REMOVED:
                         if (!isOlt) {
                             log.trace("Ignoring event {}, this is not an OLT device", deviceId);
@@ -772,6 +771,25 @@
                         // is enabled or not
                         handleOltPort(event.type(), event.subject(), event.port());
                         return;
+                    case PORT_UPDATED:
+                        if (!isOlt) {
+                            log.trace("Ignoring event {}, this is not an OLT device", deviceId);
+                            return;
+                        }
+                        // port updated are handled only when the device is available, makes not sense otherwise.
+                        // this also solves an issue with port events and device disconnection events handled
+                        // in sparse order causing failures in the ofagent disconnect test
+                        // (see https://jira.opencord.org/browse/VOL-4669)
+                        if (!deviceService.isAvailable(deviceId)) {
+                            log.debug("Ignoring port event {} on {} as it is disconnected", event, deviceId);
+                            return;
+                        }
+                        if (!oltDeviceService.isLocalLeader(deviceId)) {
+                            log.trace("Device {} is not local to this node", deviceId);
+                            return;
+                        }
+                        handleOltPort(event.type(), event.subject(), event.port());
+                        return;
                     case DEVICE_AVAILABILITY_CHANGED:
                         if (!isOlt) {
                             log.trace("Ignoring event {}, this is not an OLT device", deviceId);
diff --git a/impl/src/test/java/org/opencord/olt/impl/OltDeviceListenerTest.java b/impl/src/test/java/org/opencord/olt/impl/OltDeviceListenerTest.java
index b3b20e6..576053a 100644
--- a/impl/src/test/java/org/opencord/olt/impl/OltDeviceListenerTest.java
+++ b/impl/src/test/java/org/opencord/olt/impl/OltDeviceListenerTest.java
@@ -208,6 +208,7 @@
         si.setUniTagList(uniTagInformationList);
         doReturn(si).when(olt.subsService).get("uni-1");
 
+        doReturn(true).when(olt.deviceService).isAvailable(any());
         oltDeviceListener.event(uniAddedDisabledEvent);
         LinkedBlockingQueue<DiscoveredSubscriber> q = olt.eventsQueues.get(cp);
         assert !q.isEmpty();