Add support for odl-pretty-print 36/98136/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 26 Oct 2021 11:37:48 +0000 (13:37 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 26 Oct 2021 11:39:04 +0000 (13:39 +0200)
Somehow we did not have plumbing for this simple usability extension,
make sure we have feature parity with bierman02.

JIRA: NETCONF-825
Change-Id: I011e7369826ca7e07fcb5df1abdada726fa532d6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/PrettyPrintParam.java [new file with mode: 0644]
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/ReadDataParams.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/databind/jaxrs/QueryParams.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandler.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonNormalizedNodeBodyWriter.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlNormalizedNodeBodyWriter.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/legacy/QueryParameters.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandlerTest.java

diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/PrettyPrintParam.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/PrettyPrintParam.java
new file mode 100644 (file)
index 0000000..80457a0
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.restconf.nb.rfc8040;
+
+import java.net.URI;
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * OpenDaylight extension parameter. When used as {@code odl-pretty-print=true}, it will instruct outbound XML/JSON
+ * formatters to make the output easier for humans to understand.
+ */
+public final class PrettyPrintParam implements RestconfQueryParam<PrettyPrintParam> {
+    // API consistency: must not be confused with enum constants
+    @SuppressWarnings("checkstyle:ConstantName")
+    public static final String uriName = "odl-pretty-print";
+
+    private static final @NonNull URI CAPABILITY =
+        URI.create("urn:opendaylight:params:restconf:capability:pretty-print:1.0");
+    private static final @NonNull PrettyPrintParam FALSE = new PrettyPrintParam(false);
+    private static final @NonNull PrettyPrintParam TRUE = new PrettyPrintParam(true);
+
+    private final boolean value;
+
+    private PrettyPrintParam(final boolean value) {
+        this.value = value;
+    }
+
+    public static @NonNull PrettyPrintParam of(final boolean value) {
+        return value ? TRUE : FALSE;
+    }
+
+    public static @NonNull PrettyPrintParam forUriValue(final String uriValue) {
+        switch (uriValue) {
+            case "false":
+                return FALSE;
+            case "true":
+                return TRUE;
+            default:
+                throw new IllegalArgumentException("Value can be 'false' or 'true', not '" + uriValue + "'");
+        }
+    }
+
+    @Override
+    public Class<@NonNull PrettyPrintParam> javaClass() {
+        return PrettyPrintParam.class;
+    }
+
+    @Override
+    public String paramName() {
+        return uriName;
+    }
+
+    @Override
+    public String paramValue() {
+        return String.valueOf(value);
+    }
+
+    public boolean value() {
+        return value;
+    }
+
+    public static @NonNull URI capabilityUri() {
+        return CAPABILITY;
+    }
+}
index e1faefad39da3ef66af21cf79ed6cf09b19109b9..8a4a5417be4fc287fe81c3f0a8487a39be13cc98 100644 (file)
@@ -22,17 +22,17 @@ import org.opendaylight.yangtools.concepts.Immutable;
 // FIXME: this should be a record once we have JDK17+
 public final class ReadDataParams implements Immutable {
     private static final @NonNull ReadDataParams EMPTY =
-        new ReadDataParams(ContentParam.ALL, null, null, null, false, false);
+        new ReadDataParams(ContentParam.ALL, null, null, null, false, null);
 
     private final @NonNull ContentParam content;
     private final WithDefaultsParam withDefaults;
+    private final PrettyPrintParam prettyPrint;
     private final FieldsParam fields;
     private final DepthParam depth;
-    private final boolean prettyPrint;
     private final boolean tagged;
 
     private ReadDataParams(final ContentParam content,  final DepthParam depth, final FieldsParam fields,
-            final WithDefaultsParam withDefaults, final boolean tagged, final boolean prettyPrint) {
+            final WithDefaultsParam withDefaults, final boolean tagged, final PrettyPrintParam prettyPrint) {
         this.content = requireNonNull(content);
         this.depth = depth;
         this.fields = fields;
@@ -47,7 +47,7 @@ public final class ReadDataParams implements Immutable {
 
     public static @NonNull ReadDataParams of(final ContentParam content,  final DepthParam depth,
             final FieldsParam fields, final WithDefaultsParam withDefaults, final boolean tagged,
-            final boolean prettyPrint) {
+            final PrettyPrintParam prettyPrint) {
         return new ReadDataParams(content, depth, fields, withDefaults, tagged, prettyPrint);
     }
 
@@ -67,7 +67,7 @@ public final class ReadDataParams implements Immutable {
         return withDefaults;
     }
 
-    public boolean prettyPrint() {
+    public @Nullable PrettyPrintParam prettyPrint() {
         return prettyPrint;
     }
 
@@ -88,6 +88,10 @@ public final class ReadDataParams implements Immutable {
         if (withDefaults != null) {
             helper.add("withDefaults", withDefaults.paramValue());
         }
-        return helper.add("tagged", tagged).add("prettyPrint", prettyPrint).toString();
+        helper.add("tagged", tagged);
+        if (prettyPrint != null) {
+            helper.add("prettyPrint", prettyPrint.value());
+        }
+        return helper.toString();
     }
 }
index 4848b11e97de8cdcd3db5d60141cac0d18bfac0c..9d7b7b459b022e0900c1c2c0fa48f5d8828215fd 100644 (file)
@@ -32,6 +32,7 @@ import org.opendaylight.restconf.nb.rfc8040.InsertParam;
 import org.opendaylight.restconf.nb.rfc8040.LeafNodesOnlyParam;
 import org.opendaylight.restconf.nb.rfc8040.NotificationQueryParams;
 import org.opendaylight.restconf.nb.rfc8040.PointParam;
+import org.opendaylight.restconf.nb.rfc8040.PrettyPrintParam;
 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
 import org.opendaylight.restconf.nb.rfc8040.SkipNotificationDataParam;
 import org.opendaylight.restconf.nb.rfc8040.StartTimeParam;
@@ -133,6 +134,7 @@ public final class QueryParams {
         DepthParam depth = null;
         FieldsParam fields = null;
         WithDefaultsParam withDefaults = null;
+        PrettyPrintParam prettyPrint = null;
         boolean tagged = false;
 
         for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
@@ -194,13 +196,15 @@ public final class QueryParams {
                         }
                     }
                     break;
+                case PrettyPrintParam.uriName:
+                    prettyPrint = optionalParam(PrettyPrintParam::forUriValue, paramName, paramValues);
+                    break;
                 default:
-                    // FIXME: recognize pretty-print here
                     throw unhandledParam("read", paramName);
             }
         }
 
-        return ReadDataParams.of(content, depth, fields, withDefaults, tagged, false);
+        return ReadDataParams.of(content, depth, fields, withDefaults, tagged, prettyPrint);
     }
 
     public static @NonNull WriteDataParams newWriteDataParams(final UriInfo uriInfo) {
index 495eb9e3401531de9012ac26c63b31f371197b63..8b0cbf1cf6b90b40a7bf22b7dc1788df1447e5f4 100644 (file)
@@ -33,6 +33,7 @@ import org.opendaylight.restconf.nb.rfc8040.DepthParam;
 import org.opendaylight.restconf.nb.rfc8040.FieldsParam;
 import org.opendaylight.restconf.nb.rfc8040.FilterParam;
 import org.opendaylight.restconf.nb.rfc8040.LeafNodesOnlyParam;
+import org.opendaylight.restconf.nb.rfc8040.PrettyPrintParam;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
 import org.opendaylight.restconf.nb.rfc8040.SkipNotificationDataParam;
 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParam;
@@ -185,6 +186,7 @@ public class SchemaContextHandler implements EffectiveModelContextListener, Auto
                     .withChildValue(FilterParam.capabilityUri().toString())
                     .withChildValue(AbstractReplayParam.capabilityUri().toString())
                     .withChildValue(WithDefaultsParam.capabilityUri().toString())
+                    .withChildValue(PrettyPrintParam.capabilityUri().toString())
                     .withChildValue(LeafNodesOnlyParam.capabilityUri().toString())
                     .withChildValue(SkipNotificationDataParam.capabilityUri().toString())
                     .build())
index 604f7a3c6aa071e8636602ad4df0877f30cf15d7..15c4a829a13240b4565e9676062b06f6a5dffaf5 100644 (file)
@@ -67,8 +67,9 @@ public class JsonNormalizedNodeBodyWriter extends AbstractNormalizedNodeBodyWrit
         final InstanceIdentifierContext<SchemaNode> identifierCtx =
                 (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
         final SchemaPath path = identifierCtx.getSchemaNode().getPath();
+        final var pretty = context.getWriterParameters().prettyPrint();
 
-        try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().prettyPrint())) {
+        try (JsonWriter jsonWriter = createJsonWriter(entityStream, pretty == null ? false : pretty.value())) {
             jsonWriter.beginObject();
             writeNormalizedNode(jsonWriter, path, identifierCtx, data,
                     context.getWriterParameters().depth(), context.getWriterParameters().fields());
index 74ef222971e5da7264e0027fc96bca181d50ed6e..b5c085ca679df8856e9fae5e6d9b0578b4237e1a 100644 (file)
@@ -74,7 +74,9 @@ public class XmlNormalizedNodeBodyWriter extends AbstractNormalizedNodeBodyWrite
         XMLStreamWriter xmlWriter;
         try {
             xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
-            if (context.getWriterParameters().prettyPrint()) {
+
+            final var prettyPrint = context.getWriterParameters().prettyPrint();
+            if (prettyPrint != null && prettyPrint.value()) {
                 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
             }
         } catch (final XMLStreamException | FactoryConfigurationError e) {
index 52a72c8cbbcf0e19c196514609ae2f9858c39f8a..e30b18b44e5e9e01e779f5e4437ee26f89fe5430 100644 (file)
@@ -15,6 +15,7 @@ import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.restconf.nb.rfc8040.DepthParam;
+import org.opendaylight.restconf.nb.rfc8040.PrettyPrintParam;
 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -65,6 +66,9 @@ public final class QueryParameters {
         return params.depth();
     }
 
+    public @Nullable PrettyPrintParam prettyPrint() {
+        return params.prettyPrint();
+    }
 
     public @Nullable List<Set<QName>> fields() {
         return fields;
@@ -73,8 +77,4 @@ public final class QueryParameters {
     public @Nullable List<YangInstanceIdentifier> fieldPaths() {
         return fieldPaths;
     }
-
-    public boolean prettyPrint() {
-        return params.prettyPrint();
-    }
 }
index 68b40980e60b9bb08b4cd7d89ef37aabf2f10b42..87c12f7c8d53a98c92a25c71a35d918eb2bdba4e 100644 (file)
@@ -154,6 +154,7 @@ public class SchemaContextHandlerTest {
                 equalTo("urn:ietf:params:restconf:capability:filter:1.0"),
                 equalTo("urn:ietf:params:restconf:capability:replay:1.0"),
                 equalTo("urn:ietf:params:restconf:capability:with-defaults:1.0"),
+                equalTo("urn:opendaylight:params:restconf:capability:pretty-print:1.0"),
                 equalTo("urn:opendaylight:params:restconf:capability:leaf-nodes-only:1.0"),
                 equalTo("urn:opendaylight:params:restconf:capability:skip-notification-data:1.0")));
     }