[CORD-2749] T3: Properly filter groups in output

Change-Id: I857f9e5462b3bf21859596e82d54fada3482eddb
(cherry picked from commit 07167558bc236b4f04452239009c6046cb540c50)
diff --git a/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java b/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java
index 18ba520..b89732d 100644
--- a/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java
+++ b/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java
@@ -887,7 +887,9 @@
                 //to the possible outputs for this packet
                 if (instruction.type().equals(Instruction.Type.OUTPUT)) {
                     buildOutputFromDevice(trace, in, builder, outputPorts,
-                            (OutputInstruction) instruction, groupsForDevice);
+                            (OutputInstruction) instruction, ImmutableList.copyOf(groupsForDevice));
+                    //clearing the groups because we start from the top.
+                    groupsForDevice.clear();
                 } else {
                     builder = translateInstruction(builder, instruction);
                 }
@@ -906,10 +908,15 @@
                 trace.addResultMessage("Null group for Instruction " + instr);
                 break;
             }
-            //add the group to the traversed groups
-            groupsForDevice.add(group);
+
             //Cycle in each of the group's buckets and add them to the groups for this Device.
             for (GroupBucket bucket : group.buckets().buckets()) {
+
+                //add the group to the traversed groups
+                if (!groupsForDevice.contains(group)) {
+                    groupsForDevice.add(group);
+                }
+
                 getGroupsFromInstructions(trace, groupsForDevice, bucket.treatment().allInstructions(),
                         deviceId, builder, outputPorts, in);
             }