[VOL-4746] removing sub based on programmed data

Fix originally sent by Matteo Scandolo.

Change-Id: Ia62300f17af84d8cac98e3fc18a99eb697d4de15
Signed-off-by: Gustavo Silva <gsilva@furukawalatam.com>
diff --git a/impl/src/main/java/org/opencord/olt/impl/OltFlowService.java b/impl/src/main/java/org/opencord/olt/impl/OltFlowService.java
index a339c87..f303f4a 100644
--- a/impl/src/main/java/org/opencord/olt/impl/OltFlowService.java
+++ b/impl/src/main/java/org/opencord/olt/impl/OltFlowService.java
@@ -111,6 +111,7 @@
 import static org.opencord.olt.impl.OltUtils.flowOpToString;
 import static org.opencord.olt.impl.OltUtils.getPortName;
 import static org.opencord.olt.impl.OltUtils.portWithName;
+import static org.opencord.olt.impl.OltUtils.getProgrammedSubscriber;
 import static org.opencord.olt.impl.OsgiPropertyConstants.*;
 import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_FLOW_DIRECTION;
 import static org.opencord.olt.impl.fttb.FttbUtils.FTTB_FLOW_DOWNSTREAM;
@@ -270,7 +271,7 @@
     public void activate(ComponentContext context) {
         cfgService.registerProperties(getClass());
         appId = coreService.registerApplication(APP_NAME);
-        internalFlowListener = new InternalFlowListener();
+        internalFlowListener = new InternalFlowListener(this);
 
         modified(context);
 
@@ -401,16 +402,13 @@
             cpStatusReadLock.lock();
 
             cpStatus.forEach((sk, status) -> {
-                if (
-                    // not NNI Port
-                        !oltDeviceService.isNniPort(deviceService.getDevice(sk.getPort().connectPoint().deviceId()),
-                                sk.getPort().connectPoint().port()) &&
-                                // not EAPOL flow
-                                !sk.getService().equals(defaultEapolUniTag) &&
-                                !status.subscriberFlowsStatus.equals(OltFlowsStatus.PENDING_REMOVE)
-                                && !status.subscriberFlowsStatus.equals(OltFlowsStatus.REMOVED)
-
-                ) {
+                ConnectPoint cp = sk.getPort().connectPoint();
+                Device device = deviceService.getDevice(cp.deviceId());
+                boolean notNni = !oltDeviceService.isNniPort(device, cp.port());
+                boolean notEapol = !sk.getService().equals(defaultEapolUniTag);
+                boolean hasHsia = status.subscriberFlowsStatus.hasFlow();
+                boolean hasDhcp = status.dhcpStatus.hasFlow();
+                if (notNni && notEapol && (hasHsia || hasDhcp)) {
                     subscribers.put(sk, sk.getService());
                 }
             });
@@ -1601,6 +1599,13 @@
     }
 
     protected class InternalFlowListener implements FlowRuleListener {
+
+        private OltFlowServiceInterface oltFlowService;
+
+        public InternalFlowListener(OltFlowServiceInterface oltFlowService) {
+            this.oltFlowService = oltFlowService;
+        }
+
         @Override
         public void event(FlowRuleEvent event) {
             if (appId.id() != (event.subject().appId())) {
@@ -1728,7 +1733,12 @@
         }
 
         private ServiceKey getSubscriberKeyFromFlowRule(FlowRule flowRule, Port flowPort) {
-            SubscriberAndDeviceInformation si = subsService.get(getPortName(flowPort));
+            AccessDevicePort accessDevicePort = new AccessDevicePort(flowPort);
+            SubscriberAndDeviceInformation si = getProgrammedSubscriber(oltFlowService, accessDevicePort);
+            if (si == null) {
+                log.debug("si not found in programmedSubscribers, getting it from sadis.");
+                si = subsService.get(getPortName(flowPort));
+            }
 
             Boolean isNni = oltDeviceService.isNniPort(deviceService.getDevice(flowRule.deviceId()), flowPort.number());
             if (si == null && !isNni) {