*/
package org.opendaylight.yangtools.yang.data.codec.binfmt;
-import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
* Utility serialization/deserialization for {@link DataTreeCandidate}. Note that this utility does not maintain
* before-image information across serialization.
*/
-@Beta
public final class DataTreeCandidateInputOutput {
private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateInputOutput.class);
private static final byte DELETE = 0;
private static final byte DISAPPEARED = 5;
private DataTreeCandidateInputOutput() {
-
+ // Hidden on purpose
}
public static @NonNull DataTreeCandidate readDataTreeCandidate(final NormalizedNodeDataInput in)
public static @NonNull DataTreeCandidate readDataTreeCandidate(final NormalizedNodeDataInput in,
final ReusableStreamReceiver receiver) throws IOException {
- final YangInstanceIdentifier rootPath = in.readYangInstanceIdentifier();
+ final var rootPath = in.readYangInstanceIdentifier();
final byte type = in.readByte();
- final DataTreeCandidateNode rootNode;
- switch (type) {
- case APPEARED:
- rootNode = ModifiedDataTreeCandidateNode.create(ModificationType.APPEARED, readChildren(in, receiver));
- break;
- case DELETE:
- rootNode = DeletedDataTreeCandidateNode.create();
- break;
- case DISAPPEARED:
- rootNode = ModifiedDataTreeCandidateNode.create(ModificationType.DISAPPEARED,
- readChildren(in, receiver));
- break;
- case SUBTREE_MODIFIED:
- rootNode = ModifiedDataTreeCandidateNode.create(ModificationType.SUBTREE_MODIFIED,
- readChildren(in, receiver));
- break;
- case WRITE:
- rootNode = DataTreeCandidateNodes.written(in.readNormalizedNode(receiver));
- break;
- case UNMODIFIED:
- rootNode = UnmodifiedRootDataTreeCandidateNode.INSTANCE;
- break;
- default:
- throw unhandledNodeType(type);
- }
-
+ final var rootNode = switch (type) {
+ case APPEARED -> ModifiedDataTreeCandidateNode.create(
+ ModificationType.APPEARED, readChildren(in, receiver));
+ case DELETE -> DeletedDataTreeCandidateNode.create();
+ case DISAPPEARED -> ModifiedDataTreeCandidateNode.create(
+ ModificationType.DISAPPEARED, readChildren(in, receiver));
+ case SUBTREE_MODIFIED -> ModifiedDataTreeCandidateNode.create(
+ ModificationType.SUBTREE_MODIFIED, readChildren(in, receiver));
+ case WRITE -> DataTreeCandidateNodes.written(in.readNormalizedNode(receiver));
+ case UNMODIFIED -> UnmodifiedRootDataTreeCandidateNode.INSTANCE;
+ default -> throw unhandledNodeType(type);
+ };
return DataTreeCandidates.newDataTreeCandidate(rootPath, rootNode);
}
throws IOException {
out.writeYangInstanceIdentifier(candidate.getRootPath());
- final DataTreeCandidateNode node = candidate.getRootNode();
+ final var node = candidate.getRootNode();
switch (node.getModificationType()) {
- case APPEARED:
+ case APPEARED -> {
out.writeByte(APPEARED);
writeChildren(out, node.getChildNodes());
- break;
- case DELETE:
- out.writeByte(DELETE);
- break;
- case DISAPPEARED:
+ }
+ case DELETE -> out.writeByte(DELETE);
+ case DISAPPEARED -> {
out.writeByte(DISAPPEARED);
writeChildren(out, node.getChildNodes());
- break;
- case SUBTREE_MODIFIED:
+ }
+ case SUBTREE_MODIFIED -> {
out.writeByte(SUBTREE_MODIFIED);
writeChildren(out, node.getChildNodes());
- break;
- case UNMODIFIED:
- out.writeByte(UNMODIFIED);
- break;
- case WRITE:
+ }
+ case UNMODIFIED -> out.writeByte(UNMODIFIED);
+ case WRITE -> {
out.writeByte(WRITE);
out.writeNormalizedNode(node.getDataAfter().get());
- break;
- default:
- throw unhandledNodeType(node);
+ }
+ default -> throw unhandledNodeType(node);
}
}
private static DataTreeCandidateNode readModifiedNode(final ModificationType type, final NormalizedNodeDataInput in,
final ReusableStreamReceiver receiver) throws IOException {
- final PathArgument identifier = in.readPathArgument();
- final Collection<DataTreeCandidateNode> children = readChildren(in, receiver);
+ final var identifier = in.readPathArgument();
+ final var children = readChildren(in, receiver);
if (children.isEmpty()) {
LOG.debug("Modified node {} does not have any children, not instantiating it", identifier);
return null;
return ImmutableList.of();
}
- final Collection<DataTreeCandidateNode> ret = new ArrayList<>(size);
+ final var ret = new ArrayList<DataTreeCandidateNode>(size);
for (int i = 0; i < size; ++i) {
- final DataTreeCandidateNode child = readNode(in, receiver);
+ final var child = readNode(in, receiver);
if (child != null) {
ret.add(child);
}
private static DataTreeCandidateNode readNode(final NormalizedNodeDataInput in,
final ReusableStreamReceiver receiver) throws IOException {
final byte type = in.readByte();
- switch (type) {
- case APPEARED:
- return readModifiedNode(ModificationType.APPEARED, in, receiver);
- case DELETE:
- return DeletedDataTreeCandidateNode.create(in.readPathArgument());
- case DISAPPEARED:
- return readModifiedNode(ModificationType.DISAPPEARED, in, receiver);
- case SUBTREE_MODIFIED:
- return readModifiedNode(ModificationType.SUBTREE_MODIFIED, in, receiver);
- case UNMODIFIED:
- return null;
- case WRITE:
- return DataTreeCandidateNodes.written(in.readNormalizedNode(receiver));
- default:
- throw unhandledNodeType(type);
- }
+ return switch (type) {
+ case APPEARED -> readModifiedNode(ModificationType.APPEARED, in, receiver);
+ case DELETE -> DeletedDataTreeCandidateNode.create(in.readPathArgument());
+ case DISAPPEARED -> readModifiedNode(ModificationType.DISAPPEARED, in, receiver);
+ case SUBTREE_MODIFIED -> readModifiedNode(ModificationType.SUBTREE_MODIFIED, in, receiver);
+ case UNMODIFIED -> null;
+ case WRITE -> DataTreeCandidateNodes.written(in.readNormalizedNode(receiver));
+ default -> throw unhandledNodeType(type);
+ };
}
private static void writeChildren(final NormalizedNodeDataOutput out,
final Collection<DataTreeCandidateNode> children) throws IOException {
out.writeInt(children.size());
- for (DataTreeCandidateNode child : children) {
+ for (var child : children) {
writeNode(out, child);
}
}
private static void writeNode(final NormalizedNodeDataOutput out, final DataTreeCandidateNode node)
throws IOException {
switch (node.getModificationType()) {
- case APPEARED:
+ case APPEARED -> {
out.writeByte(APPEARED);
out.writePathArgument(node.getIdentifier());
writeChildren(out, node.getChildNodes());
- break;
- case DELETE:
+ }
+ case DELETE -> {
out.writeByte(DELETE);
out.writePathArgument(node.getIdentifier());
- break;
- case DISAPPEARED:
+ }
+ case DISAPPEARED -> {
out.writeByte(DISAPPEARED);
out.writePathArgument(node.getIdentifier());
writeChildren(out, node.getChildNodes());
- break;
- case SUBTREE_MODIFIED:
+ }
+ case SUBTREE_MODIFIED -> {
out.writeByte(SUBTREE_MODIFIED);
out.writePathArgument(node.getIdentifier());
writeChildren(out, node.getChildNodes());
- break;
- case WRITE:
+ }
+ case WRITE -> {
out.writeByte(WRITE);
out.writeNormalizedNode(node.getDataAfter().get());
- break;
- case UNMODIFIED:
+ }
+ case UNMODIFIED -> {
out.writeByte(UNMODIFIED);
- break;
- default:
- throw unhandledNodeType(node);
+ }
+ default -> throw unhandledNodeType(node);
}
}
*/
package org.opendaylight.yangtools.yang.data.codec.binfmt;
-import com.google.common.annotations.Beta;
import java.io.DataInput;
import java.io.IOException;
import java.util.Optional;
* Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
* and {@link SchemaNodeIdentifier}s.
*/
-@Beta
public interface NormalizedNodeDataInput extends QNameAwareDataInput {
/**
* Interpret current stream position as a NormalizedNode, stream its events into a NormalizedNodeStreamWriter.
* @param writer Writer to emit events to
* @throws IOException if an error occurs
* @throws IllegalStateException if the dictionary has been detached
- * @throws NullPointerException if {@code writer} is null
+ * @throws NullPointerException if {@code writer} is {@code null}
*/
void streamNormalizedNode(NormalizedNodeStreamWriter writer) throws IOException;
* @return Next node from the stream, or null if end of stream has been reached.
* @throws IOException if an error occurs
* @throws IllegalStateException if the dictionary has been detached
+ * @throws NullPointerException if {@code receiver} is {@code null}
*/
default NormalizedNode readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
try {
* @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
+ * @throws NullPointerException if {@code input} is {@code null}
*/
static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
return new VersionedNormalizedNodeDataInput(input).delegate();
*/
package org.opendaylight.yangtools.yang.data.codec.binfmt;
-import com.google.common.annotations.Beta;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
* Interface for emitting {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
* and {@link SchemaNodeIdentifier}s.
*/
-@Beta
@NonNullByDefault
public interface NormalizedNodeDataOutput extends AutoCloseable, QNameAwareDataOutput {
-
+ /**
+ * Write a {@link NormalizedNode}.
+ *
+ * @param normalizedNode NormalizedNode to write
+ * @throws IOException if an error occurs
+ * @throws NullPointerException if {@code normalizedNode} is {@code null}
+ */
void writeNormalizedNode(NormalizedNode normalizedNode) throws IOException;
+ /**
+ * Write a {@link PathArgument}.
+ *
+ * @param pathArgument PathArgument to write
+ * @throws IOException if an error occurs
+ * @throws NullPointerException if {@code pathArgument} is {@code null}
+ */
void writePathArgument(PathArgument pathArgument) throws IOException;
+ /**
+ * Write a {@link YangInstanceIdentifier}.
+ *
+ * @param identifier YangInstanceIdentifier to write
+ * @throws IOException if an error occurs
+ * @throws NullPointerException if {@code identifier} is {@code null}
+ */
void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException;
+ /**
+ * Write a {@link SchemaNodeIdentifier}.
+ *
+ * @param path SchemaNodeIdentifier to write
+ * @throws IOException if an error occurs
+ * @throws NullPointerException if {@code path} is {@code null}
+ */
void writeSchemaNodeIdentifier(SchemaNodeIdentifier path) throws IOException;
- @Override
- void close() throws IOException;
-
+ /**
+ * Write a {@link NormalizedNode} or {@code null} value.
+ *
+ * @param normalizedNode NormalizedNode to write, perhapss {@code null}, which will be restored on read.
+ * @throws IOException if an error occurs
+ * @throws NullPointerException if {@code normalizedNode} is {@code null}
+ */
default void writeOptionalNormalizedNode(final @Nullable NormalizedNode normalizedNode) throws IOException {
if (normalizedNode != null) {
writeBoolean(true);
writeBoolean(false);
}
}
+
+ /**
+ * {@inheritDoc}
+ * @throws IOException if an error occurs
+ */
+ @Override
+ void close() throws IOException;
}