--- /dev/null
+/*
+ * 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;
+ }
+}
// 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;
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);
}
return withDefaults;
}
- public boolean prettyPrint() {
+ public @Nullable PrettyPrintParam prettyPrint() {
return prettyPrint;
}
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();
}
}
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;
DepthParam depth = null;
FieldsParam fields = null;
WithDefaultsParam withDefaults = null;
+ PrettyPrintParam prettyPrint = null;
boolean tagged = false;
for (Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
}
}
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) {
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;
.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())
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());
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) {
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;
return params.depth();
}
+ public @Nullable PrettyPrintParam prettyPrint() {
+ return params.prettyPrint();
+ }
public @Nullable List<Set<QName>> fields() {
return fields;
public @Nullable List<YangInstanceIdentifier> fieldPaths() {
return fieldPaths;
}
-
- public boolean prettyPrint() {
- return params.prettyPrint();
- }
}
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")));
}