From 92316b08659db4cac91d2847de038f143b7e42d0 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 28 Oct 2019 21:58:58 +0100 Subject: [PATCH] Clean up public entrypoints This API is centered around NormalizedNodeStreamVersion, which defines versions supported by this artifact. An enum cannot really be rehosted in an API-friendly way, which really makes it an API entrypoint. Embrace that, eliminating NormalizedNodeInputOutput, which acted in the same way. JIRA: YANGTOOLS-1035 Change-Id: I8878bf6df3348981eb9165380844374885176ab1 Signed-off-by: Robert Varga --- .../binfmt/AbstractMagnesiumDataInput.java | 14 +--- .../codec/binfmt/NormalizedNodeDataInput.java | 27 +++++++ .../binfmt/NormalizedNodeDataOutput.java | 7 +- .../binfmt/NormalizedNodeInputOutput.java | 79 ------------------- .../binfmt/NormalizedNodeStreamVersion.java | 69 +++++++++++++++- 5 files changed, 100 insertions(+), 96 deletions(-) delete mode 100644 yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeInputOutput.java diff --git a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractMagnesiumDataInput.java b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractMagnesiumDataInput.java index 0c2ec5611b..21b143286e 100644 --- a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractMagnesiumDataInput.java +++ b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractMagnesiumDataInput.java @@ -63,12 +63,6 @@ abstract class AbstractMagnesiumDataInput extends AbstractNormalizedNodeDataInpu private static final byte @NonNull[] BINARY_0 = new byte[0]; private static final @NonNull AugmentationIdentifier EMPTY_AID = AugmentationIdentifier.create(ImmutableSet.of()); - // FIXME: these should be available as constants - private static final @NonNull Uint8 UINT8_0 = Uint8.valueOf(0); - private static final @NonNull Uint16 UINT16_0 = Uint16.valueOf(0); - private static final @NonNull Uint32 UINT32_0 = Uint32.valueOf(0); - private static final @NonNull Uint64 UINT64_0 = Uint64.valueOf(0); - private final List codedAugments = new ArrayList<>(); private final List codedNodeIdentifiers = new ArrayList<>(); private final List codedModules = new ArrayList<>(); @@ -742,21 +736,21 @@ abstract class AbstractMagnesiumDataInput extends AbstractNormalizedNodeDataInpu case MagnesiumValue.UINT8: return Uint8.fromByteBits(input.readByte()); case MagnesiumValue.UINT8_0: - return UINT8_0; + return Uint8.ZERO; case MagnesiumValue.UINT16: return Uint16.fromShortBits(input.readShort()); case MagnesiumValue.UINT16_0: - return UINT16_0; + return Uint16.ZERO; case MagnesiumValue.UINT32: return Uint32.fromIntBits(input.readInt()); case MagnesiumValue.UINT32_0: - return UINT32_0; + return Uint32.ZERO; case MagnesiumValue.UINT32_2B: return Uint32.fromIntBits(input.readShort() & 0xFFFF); case MagnesiumValue.UINT64: return Uint64.fromLongBits(input.readLong()); case MagnesiumValue.UINT64_0: - return UINT64_0; + return Uint64.ZERO; case MagnesiumValue.UINT64_4B: return Uint64.fromLongBits(input.readInt() & 0xFFFFFFFFL); case MagnesiumValue.BIGDECIMAL: diff --git a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataInput.java b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataInput.java index 2020685090..c15948bda9 100644 --- a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataInput.java +++ b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataInput.java @@ -84,4 +84,31 @@ public interface NormalizedNodeDataInput extends DataInput { default Optional> readOptionalNormalizedNode() throws IOException { return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty(); } + + /** + * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads + * and validates that the input contains a valid NormalizedNode stream. + * + * @param input the DataInput to read from + * @return a new {@link NormalizedNodeDataInput} instance + * @throws InvalidNormalizedNodeStreamException if the stream version is not supported + * @throws IOException if an error occurs reading from the input + */ + static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException { + return new VersionedNormalizedNodeDataInput(input).delegate(); + } + + /** + * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not + * perform any initial validation of the input stream. + * + * @param input the DataInput to read from + * @return a new {@link NormalizedNodeDataInput} instance + * @deprecated Use {@link #newDataInput(DataInput)} instead. + */ + // FIXME: 5.0.0: deprecate for removal + @Deprecated + static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) { + return new VersionedNormalizedNodeDataInput(input); + } } diff --git a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataOutput.java b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataOutput.java index 9bf503d4d1..a8cb9b1368 100644 --- a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataOutput.java +++ b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataOutput.java @@ -10,7 +10,7 @@ package org.opendaylight.yangtools.yang.data.codec.binfmt; import com.google.common.annotations.Beta; import java.io.DataOutput; import java.io.IOException; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -23,10 +23,11 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; * and {@link SchemaPath}s. */ @Beta +@NonNullByDefault public interface NormalizedNodeDataOutput extends AutoCloseable, DataOutput { - void writeQName(@NonNull QName qname) throws IOException; + void writeQName(QName qname) throws IOException; - void writeNormalizedNode(@NonNull NormalizedNode normalizedNode) throws IOException; + void writeNormalizedNode(NormalizedNode normalizedNode) throws IOException; void writePathArgument(PathArgument pathArgument) throws IOException; diff --git a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeInputOutput.java b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeInputOutput.java deleted file mode 100644 index 5428f15d67..0000000000 --- a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeInputOutput.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2015 Cisco Systems, Inc. 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.yangtools.yang.data.codec.binfmt; - -import com.google.common.annotations.Beta; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import org.eclipse.jdt.annotation.NonNull; - -@Beta -public final class NormalizedNodeInputOutput { - private NormalizedNodeInputOutput() { - throw new UnsupportedOperationException(); - } - - /** - * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads - * and validates that the input contains a valid NormalizedNode stream. - * - * @param input the DataInput to read from - * @return a new {@link NormalizedNodeDataInput} instance - * @throws IOException if an error occurs reading from the input - */ - public static NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException { - return new VersionedNormalizedNodeDataInput(input).delegate(); - } - - /** - * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not - * perform any initial validation of the input stream. - * - * @param input the DataInput to read from - * @return a new {@link NormalizedNodeDataInput} instance - */ - public static NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) { - return new VersionedNormalizedNodeDataInput(input); - } - - /** - * Creates a new {@link NormalizedNodeDataOutput} instance that writes to the given output and latest current - * stream version. - * - * @param output the DataOutput to write to - * @return a new {@link NormalizedNodeDataOutput} instance - */ - public static NormalizedNodeDataOutput newDataOutput(final @NonNull DataOutput output) { - return new SodiumSR1DataOutput(output); - } - - /** - * Creates a new {@link NormalizedNodeDataOutput} instance that writes to the given output. - * - * @param output the DataOutput to write to - * @param version Streaming version to use - * @return a new {@link NormalizedNodeDataOutput} instance - */ - public static NormalizedNodeDataOutput newDataOutput(final @NonNull DataOutput output, - final @NonNull NormalizedNodeStreamVersion version) { - switch (version) { - case LITHIUM: - return new LithiumNormalizedNodeOutputStreamWriter(output); - case NEON_SR2: - return new NeonSR2NormalizedNodeOutputStreamWriter(output); - case SODIUM_SR1: - return new SodiumSR1DataOutput(output); - case MAGNESIUM: - return new MagnesiumDataOutput(output); - default: - throw new IllegalStateException("Unhandled version " + version); - } - } - -} diff --git a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeStreamVersion.java b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeStreamVersion.java index e3cba2110f..3bf680b93a 100644 --- a/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeStreamVersion.java +++ b/yang/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeStreamVersion.java @@ -8,7 +8,11 @@ package org.opendaylight.yangtools.yang.data.codec.binfmt; import com.google.common.annotations.Beta; +import java.io.DataOutput; +import java.math.BigInteger; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.opendaylight.yangtools.yang.common.Uint64; +import org.opendaylight.yangtools.yang.data.api.schema.ValueNode; /** * Enumeration of all stream versions this implementation supports on both input and output. @@ -16,8 +20,65 @@ import org.eclipse.jdt.annotation.NonNullByDefault; @Beta @NonNullByDefault public enum NormalizedNodeStreamVersion { - LITHIUM, - NEON_SR2, - SODIUM_SR1, - MAGNESIUM; + /** + * Original stream version, as shipped in OpenDaylight Lithium simultaneous release. The caveat here is that this + * version has augmented in OpenDaylight Oxygen to retrofit a non-null representation of the empty type. + */ + // FIXME: 5.0.0: consider deprecating this version + LITHIUM { + @Override + public NormalizedNodeDataOutput newDataOutput(DataOutput output) { + return new LithiumNormalizedNodeOutputStreamWriter(output); + } + }, + /** + * Updated stream version, as shipped in OpenDaylight Neon SR2 release. Improves identifier encoding over + * {@link #LITHIUM}, so that QName caching is more effective. + */ + NEON_SR2 { + @Override + public NormalizedNodeDataOutput newDataOutput(DataOutput output) { + return new NeonSR2NormalizedNodeOutputStreamWriter(output); + } + }, + /** + * First shipping in Sodium SR1. Improved stream coding to eliminate redundancies present in {@link #NEON_SR2}. + * Supports {code Uint8} et al. as well as {@link BigInteger}. + */ + SODIUM_SR1 { + @Override + public NormalizedNodeDataOutput newDataOutput(DataOutput output) { + return new SodiumSR1DataOutput(output); + } + }, + /** + * First shipping is Magnesium. Does not support {@link BigInteger} mirroring it being superseded by {@link Uint64} + * in {@link ValueNode#getValue()}. + */ + MAGNESIUM { + @Override + public NormalizedNodeDataOutput newDataOutput(DataOutput output) { + return new MagnesiumDataOutput(output); + } + }; + + /** + * Return the current runtime version. Guaranteed to not throw {@link UnsupportedOperationException} from + * {@link #newDataOutput(DataOutput)}. + * + * @return Current runtime version. + */ + public static NormalizedNodeStreamVersion current() { + return MAGNESIUM; + } + + /** + * Creates a new {@link NormalizedNodeDataOutput} instance that writes to the given output. + * + * @param output the DataOutput to write to + * @return a new {@link NormalizedNodeDataOutput} instance + * @throws NullPointerException if {@code output} is null + * @throws UnsupportedOperationException if this version cannot be created in this runtime + */ + public abstract NormalizedNodeDataOutput newDataOutput(DataOutput output); } -- 2.36.6