Cleanup FilterContentValidator 36/96336/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 24 May 2021 12:03:22 +0000 (14:03 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 24 May 2021 17:31:42 +0000 (19:31 +0200)
Make sure we do not mutate our argument and do not access children
until necessary.

Change-Id: Ic2a1a7561fc321b75f2e63a6968eb20507cdda43
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/get/FilterContentValidator.java

index cca0a2b262f7379eef74f2d3f5e5dadf5fcf1a3c..afd31e3cfd7216bff7a4bd04c6e7bf7d5482c5d0 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.netconf.mdsal.connector.ops.get;
 
 import static org.opendaylight.yangtools.yang.data.util.ParserStreamUtils.findSchemaNodeByNameAndNamespace;
 
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -148,14 +149,16 @@ public class FilterContentValidator {
      * @param filterContent filter element
      * @param builder       builder  @return YangInstanceIdentifier
      */
-    private YangInstanceIdentifier getFilterDataRoot(FilterTree tree, final XmlElement filterContent,
+    private YangInstanceIdentifier getFilterDataRoot(final FilterTree tree, final XmlElement filterContent,
                                                      final InstanceIdentifierBuilder builder) {
         builder.node(tree.getName());
         final List<String> path = new ArrayList<>();
-        while (tree.getChildren().size() == 1) {
-            final FilterTree child = tree.getChildren().iterator().next();
+
+        FilterTree current = tree;
+        while (current.size() == 1) {
+            final FilterTree child = current.getChildren().iterator().next();
             if (child.getType() == Type.CHOICE_CASE) {
-                tree = child;
+                current = child;
                 continue;
             }
             builder.node(child.getName());
@@ -164,7 +167,7 @@ public class FilterContentValidator {
                 appendKeyIfPresent(child, filterContent, path, builder);
                 return builder.build();
             }
-            tree = child;
+            current = child;
         }
         return builder.build();
     }
@@ -235,7 +238,7 @@ public class FilterContentValidator {
     /**
      * Class represents tree of QNames as they are present in the filter.
      */
-    private static class FilterTree {
+    private static final class FilterTree {
         private final Map<QName, FilterTree> children = new HashMap<>();
         private final DataSchemaNode schemaNode;
         private final QName name;
@@ -280,6 +283,15 @@ public class FilterContentValidator {
         DataSchemaNode getSchemaNode() {
             return schemaNode;
         }
+
+        int size() {
+            return children.size();
+        }
+
+        @Override
+        public String toString() {
+            return MoreObjects.toStringHelper(this).add("name", name).add("type", type).add("size", size()).toString();
+        }
     }
 
     private static class ValidationException extends Exception {