Simplify DeviceContextImpl flow counting 81/82581/5
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 20 Jun 2019 10:20:38 +0000 (12:20 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 27 Jun 2019 12:58:23 +0000 (14:58 +0200)
The code to count flows for debugging purposes is a rather arcane
stream. Simplify it by transforming some amount of code from
functional to imperative programming and taking advantage of
nonnullFoo() methods.

Change-Id: Ifa61e38b4a4e4ac786d598ba6199ed2d55304407
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java

index f8fc0f91e87353a64c830e5c09be3cec34c7020a..2e7ee079f7e37e3f3779cae1db73106c0f12c9d2 100644 (file)
@@ -15,7 +15,6 @@ import com.google.common.util.concurrent.MoreExecutors;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import io.netty.util.HashedWheelTimer;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
@@ -729,22 +728,18 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
                 // Count all flows we read from datastore for debugging purposes.
                 // This number do not always represent how many flows were actually added
                 // to DeviceFlowRegistry, because of possible duplicates.
-                long flowCount = Optional.ofNullable(result)
-                        .map(Collections::singleton)
-                        .orElse(Collections.emptySet())
-                        .stream()
-                        .flatMap(Collection::stream)
-                        .filter(Objects::nonNull)
-                        .flatMap(flowCapableNodeOptional
-                            -> com.google.common.base.Optional.fromJavaUtil(flowCapableNodeOptional).asSet().stream())
-                        .filter(Objects::nonNull)
-                        .filter(flowCapableNode -> flowCapableNode.getTable() != null)
-                        .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
-                        .filter(Objects::nonNull)
-                        .filter(table -> table.getFlow() != null)
-                        .flatMap(table -> table.getFlow().stream())
-                        .filter(Objects::nonNull)
-                        .count();
+                long flowCount = 0;
+                if (result != null) {
+                    for (Optional<FlowCapableNode> optNode : result) {
+                        if (optNode.isPresent()) {
+                            flowCount += optNode.get().nonnullTable().stream()
+                                    .filter(Objects::nonNull)
+                                    .flatMap(table -> table.nonnullFlow().stream())
+                                    .filter(Objects::nonNull)
+                                    .count();
+                        }
+                    }
+                }
 
                 LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceInfo);
             }