[CORD-2634] Fix verify operation in OFDPA pipeliner.

Current code does not detect properly a mismatch
between nextobj and flowobjstore. In particular
it is able to detect only missing chains and
duplicated chains, while falls if allactivekeys
has more chains with respect to the next we want
to verify. I think flapping links or subsequent
events (for example several mcast-join follow by
mcast-delete) can create easily this problem.

Imagine we send a next with two treatments
Next(x,y) where x and y are the output ports.
AllActiveKeys has three chains AllActiveKeys(x,y,z)
where x, y and z are the output ports.
existingPortAndLabel is not able to detect the
extraneous chain related to the port z

Change-Id: I41fa47347a8c1d4188d990d96f48a898a4df59e1
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java
index bb215ae..a3670e6 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2GroupHandler.java
@@ -1597,6 +1597,9 @@
         List<Deque<GroupKey>> allActiveKeys = appKryo.deserialize(next.data());
         List<TrafficTreatment> bucketsToCreate = Lists.newArrayList();
         List<Integer> indicesToRemove = Lists.newArrayList();
+
+        // Iterating over the treatments of the next objective allows
+        // to detect missing buckets and/or duplicate buckets (to be removed)
         for (TrafficTreatment bkt : nextObjective.next()) {
             PortNumber portNumber = readOutPortFromTreatment(bkt);
             int label = readLabelFromTreatment(bkt);
@@ -1621,6 +1624,22 @@
             }
         }
 
+        // Detect situation where the next data has more buckets
+        // (not duplicates) respect to the next objective
+        if (allActiveKeys.size() > nextObjective.next().size()) {
+            log.warn("Mismatch detected between next and flowobjstore for device {}: " +
+                             "nextId:{}, nextObjective-size:{} next-size:{} .. correcting",
+                     deviceId, nextObjective.id(), nextObjective.next().size(), allActiveKeys.size());
+            List<Integer> otherIndices = indicesToRemoveFromNextGroup(allActiveKeys, nextObjective,
+                                                                      groupService, deviceId);
+            // Filter out the indices not present
+            otherIndices = otherIndices.stream()
+                    .filter(index -> !indicesToRemove.contains(index))
+                    .collect(Collectors.toList());
+            // Add all to the final list
+            indicesToRemove.addAll(otherIndices);
+        }
+
         if (!bucketsToCreate.isEmpty()) {
             log.info("creating {} buckets as part of nextId: {} verification",
                      bucketsToCreate.size(), nextObjective.id());