Cleanup FieldsParam 28/98128/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 25 Oct 2021 19:25:52 +0000 (21:25 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 25 Oct 2021 19:26:51 +0000 (21:26 +0200)
We can reconstruct paramName() from the data we have in a rather
straightforward fashion. Do that and clean up various bits and pieces
that changed over the course of development.

JIRA: NETCONF-773
Change-Id: Id037b202fb24290ca710d4aa527d63ad5bbb2a36
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/FieldsParam.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/FieldsParameterParser.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/FieldsParamTest.java [moved from restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/FieldsParameterTest.java with 97% similarity]

index 247fb43084c2ca78125bb75ab24b4e86392492d0..d6b96e1cac50492fd98968191d3a7f68d8c0bb4f 100644 (file)
@@ -41,10 +41,6 @@ public final class FieldsParam implements RestconfQueryParam<FieldsParam> {
             checkArgument(!path.isEmpty(), "At least path segment is required");
         }
 
-        NodeSelector(final ImmutableList<ApiIdentifier> path) {
-            this(path, ImmutableList.of());
-        }
-
         /**
          * Return the path to the selected node. Guaranteed to have at least one element.
          *
@@ -72,6 +68,26 @@ public final class FieldsParam implements RestconfQueryParam<FieldsParam> {
             }
             return helper.toString();
         }
+
+        void appendTo(final StringBuilder sb) {
+            final var it = path.iterator();
+            appendStep(sb, it.next());
+            while (it.hasNext()) {
+                appendStep(sb.append('/'), it.next());
+            }
+
+            if (!subSelectors.isEmpty()) {
+                appendSelectors(sb.append('('), subSelectors).append(')');
+            }
+        }
+
+        private static void appendStep(final StringBuilder sb, final ApiIdentifier step) {
+            final var mod = step.module();
+            if (mod != null) {
+                sb.append(mod).append(':');
+            }
+            sb.append(step.identifier().getLocalName());
+        }
     }
 
     // API consistency: must not be confused with enum constants
@@ -80,12 +96,10 @@ public final class FieldsParam implements RestconfQueryParam<FieldsParam> {
     private static final URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:fields:1.0");
 
     private final ImmutableList<NodeSelector> nodeSelectors;
-    private final String paramValue;
 
-    private FieldsParam(final ImmutableList<NodeSelector> nodeSelectors, final String uriValue) {
+    FieldsParam(final ImmutableList<NodeSelector> nodeSelectors) {
         this.nodeSelectors = requireNonNull(nodeSelectors);
         checkArgument(!nodeSelectors.isEmpty(), "At least one selector is required");
-        paramValue = requireNonNull(uriValue);
     }
 
     /**
@@ -96,7 +110,7 @@ public final class FieldsParam implements RestconfQueryParam<FieldsParam> {
      * @throws ParseException if {@code str} does not represent a valid {@code fields} parameter.
      */
     public static FieldsParam parse(final String str) throws ParseException {
-        return new FieldsParam(new FieldsParameterParser().parseNodeSelectors(str), str);
+        return new FieldsParameterParser().parse(str);
     }
 
     @Override
@@ -124,11 +138,20 @@ public final class FieldsParam implements RestconfQueryParam<FieldsParam> {
 
     @Override
     public String paramValue() {
-        return paramValue;
+        return appendSelectors(new StringBuilder(), nodeSelectors).toString();
     }
 
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this).add("nodeSelectors", nodeSelectors).toString();
     }
+
+    private static StringBuilder appendSelectors(final StringBuilder sb, final ImmutableList<NodeSelector> selectors) {
+        final var it = selectors.iterator();
+        it.next().appendTo(sb);
+        while (it.hasNext()) {
+            it.next().appendTo(sb.append(';'));
+        }
+        return sb;
+    }
 }
index 177c23222b8a9b1c45c641156f40f9eae6629068..45ea6b6d1ae5c7b9edaacf41d610243a8ad8565c 100644 (file)
@@ -66,7 +66,7 @@ final class FieldsParameterParser {
     // parsers instead of ten.
     private Deque<NodeSelectorParser> parsers;
 
-    @NonNull ImmutableList<NodeSelector> parseNodeSelectors(final String str) throws ParseException {
+    @NonNull FieldsParam parse(final String str) throws ParseException {
         final var nodeSelectors = ImmutableList.<NodeSelector>builder();
 
         int idx = 0;
@@ -77,7 +77,7 @@ final class FieldsParameterParser {
 
             if (next == str.length()) {
                 // We have reached the end, we are done
-                return nodeSelectors.build();
+                return new FieldsParam(nodeSelectors.build());
             }
 
             final char ch = str.charAt(next);
similarity index 97%
rename from restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/FieldsParameterTest.java
rename to restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/FieldsParamTest.java
index 8ab7d3dbe3d6d177962ea2549e5235dd99215c3e..4a70f28004e0948f26b5ffaf2b080f6a653f0780 100644 (file)
@@ -16,7 +16,7 @@ import org.junit.Test;
 import org.opendaylight.restconf.nb.rfc8040.ApiPath.ApiIdentifier;
 import org.opendaylight.restconf.nb.rfc8040.FieldsParam.NodeSelector;
 
-public class FieldsParameterTest {
+public class FieldsParamTest {
     // https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.3:
     //    ";" is used to select multiple nodes.  For example, to retrieve only
     //    the "genre" and "year" of an album, use "fields=genre;year".
@@ -186,10 +186,14 @@ public class FieldsParameterTest {
     }
 
     private static List<NodeSelector> assertValidFields(final String str) {
+        final FieldsParam param;
         try {
-            return FieldsParam.parse(str).nodeSelectors();
+            param = FieldsParam.parse(str);
         } catch (ParseException e) {
             throw new AssertionError(e);
         }
+
+        assertEquals(str, param.paramValue());
+        return param.nodeSelectors();
     }
 }