Clean up Potassium{Node,PathArgument} a bit 26/106026/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 18 May 2023 09:00:08 +0000 (11:00 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 18 May 2023 10:36:46 +0000 (10:36 +0000)
Potassium does not have AugmentationIdentifier nor MountPointIdentifier.
Remove these constants and all their users. Also ensure we split the
readLegacyPathArgument implementations.

JIRA: YANGTOOLS-568
Change-Id: I8e743b248723aec77a5e01c3523a41ac8d86dcf4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractLegacyDataInput.java [new file with mode: 0644]
codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractLithiumDataInput.java
codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractMagnesiumDataInput.java
codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/NormalizedNodeDataInput.java
codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/PotassiumDataInput.java
codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/PotassiumNode.java
codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/PotassiumPathArgument.java

diff --git a/codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractLegacyDataInput.java b/codec/yang-data-codec-binfmt/src/main/java/org/opendaylight/yangtools/yang/data/codec/binfmt/AbstractLegacyDataInput.java
new file mode 100644 (file)
index 0000000..97b96a0
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2023 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.yangtools.yang.data.codec.binfmt;
+
+import java.io.DataInput;
+import java.io.IOException;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+
+/**
+ * Abstract base class for versions which can produce legacy versions of PathArgument.
+ */
+abstract class AbstractLegacyDataInput extends AbstractNormalizedNodeDataInput {
+    AbstractLegacyDataInput(final DataInput input) {
+        super(input);
+    }
+
+    @Override
+    public final PathArgument readPathArgument() throws IOException {
+        final var legacy = readLegacyPathArgument();
+        if (legacy.isFirst()) {
+            return legacy.getFirst();
+        }
+        throw new InvalidNormalizedNodeStreamException(legacy.getSecond() + " does not have a representation");
+    }
+}
index 0dbb73b56702e54aa96f7f587d93cb96d6f9ae9a..0f621e7ea998b2bc06efb9e96fe7c9970d451745 100644 (file)
@@ -48,8 +48,7 @@ import org.xml.sax.SAXException;
  * nodes. This process goes in recursive manner, where each NodeTypes object signifies the start of the object, except
  * END_NODE. If a node can have children, then that node's end is calculated based on appearance of END_NODE.
  */
-abstract class AbstractLithiumDataInput extends AbstractNormalizedNodeDataInput {
-
+abstract class AbstractLithiumDataInput extends AbstractLegacyDataInput {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractLithiumDataInput.class);
 
     private final List<String> codedStringMap = new ArrayList<>();
index 804636707f2297d600ed7ddb98c25620b1a886d8..b933b256ebe3200f1ef91f6b0e457f6ab4764c61 100644 (file)
@@ -51,7 +51,7 @@ import org.xml.sax.SAXException;
  * Abstract base class for NormalizedNodeDataInput based on {@link MagnesiumNode}, {@link MagnesiumPathArgument} and
  * {@link MagnesiumValue}.
  */
-abstract class AbstractMagnesiumDataInput extends AbstractNormalizedNodeDataInput {
+abstract class AbstractMagnesiumDataInput extends AbstractLegacyDataInput {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractMagnesiumDataInput.class);
 
     // Known singleton objects
index 4543e155f818e829d6cabc684ab240fb0a96a680..0107f38a65649cb36eb7feff71ee76fec68135e0 100644 (file)
@@ -79,13 +79,7 @@ public interface NormalizedNodeDataInput extends QNameAwareDataInput {
      * @return A PathArgument
      * @throws IOException if an error occurs
      */
-    default @NonNull PathArgument readPathArgument() throws IOException {
-        final var legacy = readLegacyPathArgument();
-        if (legacy.isFirst()) {
-            return legacy.getFirst();
-        }
-        throw new InvalidNormalizedNodeStreamException(legacy.getSecond() + " does not have a representation");
-    }
+    @NonNull PathArgument readPathArgument() throws IOException;
 
     /**
      * Read a {@link PathArgument} or a {@link LegacyPathArgument} from the reader.
index 73550a9fb8054d714caa4852b7c359ab05b722a5..054ecde62d34f39af473f67ebc9e58a2bde2ee25 100644 (file)
@@ -60,10 +60,7 @@ final class PotassiumDataInput extends AbstractNormalizedNodeDataInput {
     private static final @NonNull Integer INT32_0 = 0;
     private static final @NonNull Long INT64_0 = 0L;
     private static final byte @NonNull[] BINARY_0 = new byte[0];
-    private static final @NonNull LegacyAugmentationIdentifier EMPTY_AID =
-        new LegacyAugmentationIdentifier(ImmutableSet.of());
 
-    private final List<LegacyAugmentationIdentifier> codedAugments = new ArrayList<>();
     private final List<NodeIdentifier> codedNodeIdentifiers = new ArrayList<>();
     private final List<QNameModule> codedModules = new ArrayList<>();
     private final List<String> codedStrings = new ArrayList<>();
@@ -109,9 +106,6 @@ final class PotassiumDataInput extends AbstractNormalizedNodeDataInput {
             case PotassiumNode.NODE_CHOICE:
                 streamChoice(writer, nodeHeader);
                 break;
-            case PotassiumNode.NODE_AUGMENTATION:
-                streamAugmentation(writer, nodeHeader);
-                break;
             case PotassiumNode.NODE_ANYXML:
                 streamAnyxml(writer, nodeHeader);
                 break;
@@ -140,15 +134,6 @@ final class PotassiumDataInput extends AbstractNormalizedNodeDataInput {
         }
     }
 
-    private void streamAugmentation(final NormalizedNodeStreamWriter writer, final byte nodeHeader) throws IOException {
-        final var augIdentifier = decodeAugmentationIdentifier(nodeHeader);
-        LOG.trace("Streaming augmentation node {}", augIdentifier);
-        for (byte nodeType = input.readByte(); nodeType != PotassiumNode.NODE_END; nodeType = input.readByte()) {
-            // FIXME: not just null, but augmentation identifier for debug
-            streamNormalizedNode(writer, null, nodeType);
-        }
-    }
-
     private void streamChoice(final NormalizedNodeStreamWriter writer, final byte nodeHeader) throws IOException {
         final NodeIdentifier identifier = decodeNodeIdentifier(nodeHeader);
         LOG.trace("Streaming choice node {}", identifier);
@@ -319,29 +304,6 @@ final class PotassiumDataInput extends AbstractNormalizedNodeDataInput {
         }
     }
 
-    private LegacyAugmentationIdentifier decodeAugmentationIdentifier(final byte nodeHeader) throws IOException {
-        final int index;
-        switch (nodeHeader & PotassiumNode.ADDR_MASK) {
-            case PotassiumNode.ADDR_DEFINE:
-                return readAugmentationIdentifier();
-            case PotassiumNode.ADDR_LOOKUP_1B:
-                index = input.readUnsignedByte();
-                break;
-            case PotassiumNode.ADDR_LOOKUP_4B:
-                index = input.readInt();
-                break;
-            default:
-                throw new InvalidNormalizedNodeStreamException(
-                    "Unexpected augmentation identifier addressing in header " + nodeHeader);
-        }
-
-        try {
-            return codedAugments.get(index);
-        } catch (IndexOutOfBoundsException e) {
-            throw new InvalidNormalizedNodeStreamException("Invalid augmentation identifier reference " + index, e);
-        }
-    }
-
     @Override
     public YangInstanceIdentifier readYangInstanceIdentifier() throws IOException {
         final byte type = input.readByte();
@@ -382,57 +344,26 @@ final class PotassiumDataInput extends AbstractNormalizedNodeDataInput {
     }
 
     @Override
-    @Deprecated(since = "11.0.0", forRemoval = true)
-    public Either<PathArgument, LegacyPathArgument> readLegacyPathArgument() throws IOException {
+    public PathArgument readPathArgument() throws IOException {
         final byte header = input.readByte();
         return switch (header & PotassiumPathArgument.TYPE_MASK) {
-            case PotassiumPathArgument.AUGMENTATION_IDENTIFIER -> Either.ofSecond(readAugmentationIdentifier(header));
             case PotassiumPathArgument.NODE_IDENTIFIER -> {
                 verifyPathIdentifierOnly(header);
-                yield Either.ofFirst(readNodeIdentifier(header));
+                yield readNodeIdentifier(header);
             }
-            case PotassiumPathArgument.NODE_IDENTIFIER_WITH_PREDICATES ->
-                Either.ofFirst(readNodeIdentifierWithPredicates(header));
+            case PotassiumPathArgument.NODE_IDENTIFIER_WITH_PREDICATES -> readNodeIdentifierWithPredicates(header);
             case PotassiumPathArgument.NODE_WITH_VALUE -> {
                 verifyPathIdentifierOnly(header);
-                yield Either.ofFirst(readNodeWithValue(header));
-            }
-            case PotassiumPathArgument.MOUNTPOINT_IDENTIFIER -> {
-                verifyPathIdentifierOnly(header);
-                yield Either.ofSecond(new LegacyMountPointIdentifier(readNodeIdentifier(header).getNodeType()));
+                yield readNodeWithValue(header);
             }
             default -> throw new InvalidNormalizedNodeStreamException("Unexpected PathArgument header " + header);
         };
     }
 
-    private @NonNull LegacyAugmentationIdentifier readAugmentationIdentifier() throws IOException {
-        final var result = readAugmentationIdentifier(input.readInt());
-        codedAugments.add(result);
-        return result;
-    }
-
-    private @NonNull LegacyAugmentationIdentifier readAugmentationIdentifier(final byte header) throws IOException {
-        final byte count = mask(header, PotassiumPathArgument.AID_COUNT_MASK);
-        return switch (count) {
-            case PotassiumPathArgument.AID_COUNT_1B -> readAugmentationIdentifier(input.readUnsignedByte());
-            case PotassiumPathArgument.AID_COUNT_2B -> readAugmentationIdentifier(input.readUnsignedShort());
-            case PotassiumPathArgument.AID_COUNT_4B -> readAugmentationIdentifier(input.readInt());
-            default -> readAugmentationIdentifier(rshift(count, PotassiumPathArgument.AID_COUNT_SHIFT));
-        };
-    }
-
-    private @NonNull LegacyAugmentationIdentifier readAugmentationIdentifier(final int size) throws IOException {
-        if (size > 0) {
-            final var qnames = ImmutableSet.<QName>builderWithExpectedSize(size);
-            for (int i = 0; i < size; ++i) {
-                qnames.add(readQName());
-            }
-            return new LegacyAugmentationIdentifier(qnames.build());
-        } else if (size == 0) {
-            return EMPTY_AID;
-        } else {
-            throw new InvalidNormalizedNodeStreamException("Invalid augmentation identifier size " + size);
-        }
+    @Override
+    @Deprecated(since = "11.0.0", forRemoval = true)
+    public Either<PathArgument, LegacyPathArgument> readLegacyPathArgument() throws IOException {
+        return Either.ofFirst(readPathArgument());
     }
 
     private @NonNull NodeIdentifier readNodeIdentifier() throws IOException {
index da3d31181bc8c4a1f7ae5df5339b497705113d9f..95f25d6171956c8fe970f8b0ccefe2a1fcf7d876 100644 (file)
@@ -73,7 +73,7 @@ final class PotassiumNode {
     static final byte NODE_LEAFSET         = 0x06;
     static final byte NODE_LEAFSET_ORDERED = 0x07;
     static final byte NODE_CHOICE          = 0x08;
-    static final byte NODE_AUGMENTATION    = 0x09;
+    // static final byte NODE_AUGMENTATION    = 0x09;
     static final byte NODE_ANYXML          = 0x0A;
     static final byte NODE_LIST_ENTRY      = 0x0B;
     static final byte NODE_LEAFSET_ENTRY   = 0x0C;
index 2bc40bb9dd9e2f79d821e16b31fedbe92f28d5fb..9a0dac5bf8361c4b2d8806c82058ad14b37fee70 100644 (file)
@@ -16,27 +16,15 @@ package org.opendaylight.yangtools.yang.data.codec.binfmt;
  *  |         | Type|
  *  +-+-+-+-+-+-+-+-+
  * </pre>
- * There are five type defined:
+ * There are three type defined:
  * <ul>
- *   <li>{@link #AUGMENTATION_IDENTIFIER}, which additionally holds the number of QName elements encoded:
- *     <pre>
- *        7 6 5 4 3 2 1 0
- *       +-+-+-+-+-+-+-+-+
- *       |  Count  |0 0 0|
- *       +-+-+-+-+-+-+-+-+
- *     </pre>
- *     Where count is coded as an unsigned integer, with {@link #AID_COUNT_1B} and {@link #AID_COUNT_2B} and
- *     {@link #AID_COUNT_4B} indicating extended coding with up to 4 additional bytes. This byte is followed by
- *     {@code count} {@link PotassiumValue} QNames.
+ *   <li>{@link #NODE_IDENTIFIER}, which encodes a QName:
  *     <pre>
  *       7 6 5 4 3 2 1 0
  *      +-+-+-+-+-+-+-+-+
  *      |0 0 0| Q |0 0 1|
  *      +-+-+-+-+-+-+-+-+
  *     </pre>
- *     Where QName coding is achieved via {@link #QNAME_DEF}, {@link #QNAME_REF_1B}, {@link #QNAME_REF_2B} and
- *     {@link #QNAME_REF_4B}.
- *   </li>
  *   <li>{@link #NODE_IDENTIFIER_WITH_PREDICATES}, which encodes a QName same way NodeIdentifier does:
  *     <pre>
  *       7 6 5 4 3 2 1 0
@@ -61,40 +49,25 @@ package org.opendaylight.yangtools.yang.data.codec.binfmt;
  *     </pre>
  *     but is additionally followed by a single encoded value, as per {@link PotassiumValue}.
  *   </li>
- *   <li>{@link #MOUNTPOINT_IDENTIFIER}, which encodes a QName same way NodeIdentifier does:
- *     <pre>
- *       7 6 5 4 3 2 1 0
- *      +-+-+-+-+-+-+-+-+
- *      |0 0 0| Q |1 0 0|
- *      +-+-+-+-+-+-+-+-+
- *     </pre>
  *   </li>
  * </ul>
  */
 final class PotassiumPathArgument {
     // 3 bits reserved for type...
-    static final byte AUGMENTATION_IDENTIFIER         = 0x00;
     static final byte NODE_IDENTIFIER                 = 0x01;
     static final byte NODE_IDENTIFIER_WITH_PREDICATES = 0x02;
     static final byte NODE_WITH_VALUE                 = 0x03;
-    static final byte MOUNTPOINT_IDENTIFIER           = 0x04;
 
-    // ... leaving three values currently unused
+    // ... leaving five values currently unused
+    // FIXME: this means we can use just two bits for type encoding
+    // 0x00 reserved
+    // 0x04 reserved
     // 0x05 reserved
     // 0x06 reserved
     // 0x07 reserved
 
     static final byte TYPE_MASK                       = 0x07;
 
-    // In case of AUGMENTATION_IDENTIFIER, top 5 bits are used to encode the number of path arguments, except last three
-    // values. This means that up to AugmentationIdentifiers with up to 28 components have this length encoded inline,
-    // otherwise we encode them in following 1 (unsigned), 2 (unsigned) or 4 (signed) bytes
-    static final byte AID_COUNT_1B                    = (byte) 0xE8;
-    static final byte AID_COUNT_2B                    = (byte) 0xF0;
-    static final byte AID_COUNT_4B                    = (byte) 0xF8;
-    static final byte AID_COUNT_MASK                  = AID_COUNT_4B;
-    static final byte AID_COUNT_SHIFT                 = 3;
-
     // For normal path path arguments we can either define a QName reference or follow a 1-4 byte reference.
     static final byte QNAME_DEF                       = 0x00;
     static final byte QNAME_REF_1B                    = 0x08; // Unsigned