Fix for empty flooding groups due to bucket add/remove commands being reordered, and
triggered by port up/down events very close to each other.

Change-Id: Ibbd7cc6eb6df35396175619d95439acd6da904b8
diff --git a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index 09808d5..5a06bc2 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -984,7 +984,7 @@
                 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
                 return;
             }
-            log.debug("updating groupHandlerMap with new config for device: {}",
+            log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
                     deviceId);
             groupHandlerMap.put(deviceId, groupHandler);
         }
diff --git a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java
index 31ed338..0867fab 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/grouphandler/DefaultGroupHandler.java
@@ -412,7 +412,7 @@
         if (nextId == -1) {
             if (portUp) {
                 log.debug("**Creating flooding group for first port enabled in"
-                        + " subnet {} on dev {} port {}", vlanId, deviceId, port);
+                        + " vlan {} on dev {} port {}", vlanId, deviceId, port);
                 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
             } else {
                 log.warn("Could not find flooding group for subnet {} on dev:{} when"
@@ -830,8 +830,8 @@
             );
         NextObjective nextObj = nextObjBuilder.add(context);
         flowObjectiveService.next(deviceId, nextObj);
-        log.debug("createBcastGroupFromVlan: Submited next objective {} in device {}",
-                  nextId, deviceId);
+        log.debug("createBcastGroupFromVlan: Submitted next objective {} "
+                + "for vlan: {} in device {}", nextId, vlanId, deviceId);
 
         vlanNextObjStore.put(key, nextId);
     }
diff --git a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
index 5eaaebb..240d09b 100644
--- a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
+++ b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.flowobjective.impl;
 
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import org.apache.felix.scr.annotations.Activate;
@@ -133,7 +134,7 @@
     // instance, via the DistributedFlowObjectiveStore.
     private final Map<Integer, Set<PendingFlowObjective>> pendingForwards =
             Maps.newConcurrentMap();
-    private final Map<Integer, Set<PendingFlowObjective>> pendingNexts =
+    private final Map<Integer, List<PendingFlowObjective>> pendingNexts =
             Maps.newConcurrentMap();
 
     // local store to track which nextObjectives were sent to which device
@@ -309,7 +310,7 @@
                 pendingNexts.compute(next.id(), (id, pending) -> {
                     PendingFlowObjective pendfo = new PendingFlowObjective(deviceId, next);
                     if (pending == null) {
-                        return Sets.newHashSet(pendfo);
+                        return Lists.newArrayList(pendfo);
                     } else {
                         pending.add(pendfo);
                         return pending;
@@ -505,17 +506,18 @@
                 }
 
                 // now check for pending next-objectives
+                List<PendingFlowObjective> pendNexts;
                 synchronized (pendingNexts) {
                     // needs to be synchronized for queueObjective lookup
-                    pending = pendingNexts.remove(event.subject());
+                    pendNexts = pendingNexts.remove(event.subject());
                 }
-                if (pending == null) {
+                if (pendNexts == null) {
                     log.debug("No next objectives pending for this "
                             + "obj event {}", event);
                 } else {
                     log.debug("Processing {} pending next objectives for nextId {}",
-                              pending.size(), event.subject());
-                    pending.forEach(p -> getDevicePipeliner(p.deviceId())
+                              pendNexts.size(), event.subject());
+                    pendNexts.forEach(p -> getDevicePipeliner(p.deviceId())
                                     .next((NextObjective) p.flowObjective()));
                 }
             }
@@ -621,7 +623,7 @@
         }
 
         for (Integer nextId : pendingNexts.keySet()) {
-            Set<PendingFlowObjective> pnext = pendingNexts.get(nextId);
+            List<PendingFlowObjective> pnext = pendingNexts.get(nextId);
             StringBuilder pend = new StringBuilder();
             pend.append("NextId: ")
                     .append(nextId);
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
index 8c64ded..10009d7 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
@@ -635,6 +635,9 @@
             TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
             if (matchInPortTmacTable()) {
                 selector.matchInPort(pnum);
+            } else {
+                log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
+                        "ignoring the IN_PORT criteria");
             }
             if (requireVlanExtensions()) {
                 selector.extension(ofdpaMatchVlanVid, deviceId);
@@ -658,6 +661,9 @@
             treatment = DefaultTrafficTreatment.builder();
             if (matchInPortTmacTable()) {
                 selector.matchInPort(pnum);
+            } else {
+                log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
+                        "ignoring the IN_PORT criteria");
             }
             if (requireVlanExtensions()) {
                 selector.extension(ofdpaMatchVlanVid, deviceId);
@@ -683,6 +689,9 @@
             treatment = DefaultTrafficTreatment.builder();
             if (matchInPortTmacTable()) {
                 selector.matchInPort(pnum);
+            } else {
+                log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
+                        "ignoring the IN_PORT criteria");
             }
             if (requireVlanExtensions()) {
                 selector.extension(ofdpaMatchVlanVid, deviceId);