X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FNormalizedNodeInputStreamReader.java;h=3f6cbb743bc26568583b248a7276b87a11b3efa3;hp=2246b51a3e1428ddca70220c1608a368f9174956;hb=70a1d4634bd92bf970077e269dc4433d1719f6fb;hpb=81d25b3c5f4a040b5f11475752a26712b46f5c0b diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java index 2246b51a3e..3f6cbb743b 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java @@ -1,11 +1,9 @@ /* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. * - * Copyright (c) 2014 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 - * + * 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.controller.cluster.datastore.node.utils.stream; @@ -18,6 +16,7 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -49,7 +48,7 @@ import org.slf4j.LoggerFactory; * */ -public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamReader { +public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput { private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class); @@ -64,20 +63,31 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead private NormalizedNodeAttrBuilder> leafBuilder; - private NormalizedNodeAttrBuilder> leafSetEntryBuilder; + private NormalizedNodeAttrBuilder> leafSetEntryBuilder; private final StringBuilder reusableStringBuilder = new StringBuilder(50); private boolean readSignatureMarker = true; - public NormalizedNodeInputStreamReader(InputStream stream) throws IOException { - Preconditions.checkNotNull(stream); - input = new DataInputStream(stream); + /** + * @deprecated Use {@link #NormalizedNodeInputStreamReader(DataInput)} instead. + */ + @Deprecated + public NormalizedNodeInputStreamReader(final InputStream stream) throws IOException { + this((DataInput) new DataInputStream(Preconditions.checkNotNull(stream))); + } + + /** + * @deprecated Use {@link NormalizedNodeInputOutput#newDataInput(DataInput)} instead. + */ + @Deprecated + public NormalizedNodeInputStreamReader(final DataInput input) { + this(input, false); } - public NormalizedNodeInputStreamReader(DataInput input) { + NormalizedNodeInputStreamReader(final DataInput input, final boolean versionChecked) { this.input = Preconditions.checkNotNull(input); + readSignatureMarker = !versionChecked; } @Override @@ -90,13 +100,16 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead if(readSignatureMarker) { readSignatureMarker = false; - byte marker = input.readByte(); - if(marker != NormalizedNodeOutputStreamWriter.SIGNATURE_MARKER) { + final byte marker = input.readByte(); + if (marker != TokenTypes.SIGNATURE_MARKER) { throw new InvalidNormalizedNodeStreamException(String.format( "Invalid signature marker: %d", marker)); } - input.readShort(); // read the version - not currently used/needed. + final short version = input.readShort(); + if (version != TokenTypes.LITHIUM_VERSION) { + throw new InvalidNormalizedNodeStreamException(String.format("Unhandled stream version %s", version)); + } } } @@ -120,8 +133,13 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead withNodeIdentifier(augIdentifier)).build(); case NodeTypes.LEAF_SET_ENTRY_NODE : + QName name = lastLeafSetQName; + if(name == null) { + name = readQName(); + } + Object value = readObject(); - NodeWithValue leafIdentifier = new NodeWithValue(lastLeafSetQName, value); + NodeWithValue leafIdentifier = new NodeWithValue<>(name, value); LOG.debug("Reading leaf set entry node {}, value {}", leafIdentifier, value); @@ -159,7 +177,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead return leafSetEntryBuilder; } - private NormalizedNode readNodeIdentifierDependentNode(byte nodeType, NodeIdentifier identifier) + private NormalizedNode readNodeIdentifierDependentNode(final byte nodeType, final NodeIdentifier identifier) throws IOException { switch(nodeType) { @@ -207,6 +225,13 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead return addLeafSetChildren(identifier.getNodeType(), Builders.leafSetBuilder().withNodeIdentifier(identifier)).build(); + case NodeTypes.ORDERED_LEAF_SET: + LOG.debug("Read ordered leaf set node {}", identifier); + ListNodeBuilder> orderedLeafSetBuilder = + Builders.orderedLeafSetBuilder().withNodeIdentifier(identifier); + orderedLeafSetBuilder = addLeafSetChildren(identifier.getNodeType(), orderedLeafSetBuilder); + return orderedLeafSetBuilder.build(); + default : return null; } @@ -234,9 +259,9 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead private String readCodedString() throws IOException { byte valueType = input.readByte(); - if(valueType == NormalizedNodeOutputStreamWriter.IS_CODE_VALUE) { + if(valueType == TokenTypes.IS_CODE_VALUE) { return codedStringMap.get(input.readInt()); - } else if(valueType == NormalizedNodeOutputStreamWriter.IS_STRING_VALUE) { + } else if(valueType == TokenTypes.IS_STRING_VALUE) { String value = input.readUTF().intern(); codedStringMap.put(Integer.valueOf(codedStringMap.size()), value); return value; @@ -293,6 +318,9 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead case ValueTypes.STRING_TYPE : return input.readUTF(); + case ValueTypes.STRING_BYTES_TYPE: + return readStringBytes(); + case ValueTypes.BIG_DECIMAL_TYPE : return new BigDecimal(input.readUTF()); @@ -312,6 +340,13 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead } } + private String readStringBytes() throws IOException { + byte[] bytes = new byte[input.readInt()]; + input.readFully(bytes); + return new String(bytes, StandardCharsets.UTF_8); + } + + @Override public YangInstanceIdentifier readYangInstanceIdentifier() throws IOException { readSignatureMarkerAndVersionIfNeeded(); return readYangInstanceIdentifierInternal(); @@ -337,6 +372,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead return children; } + @Override public PathArgument readPathArgument() throws IOException { // read Type int type = input.readByte(); @@ -353,7 +389,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead return new NodeIdentifierWithPredicates(readQName(), readKeyValueMap()); case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE : - return new NodeWithValue(readQName(), readObject()); + return new NodeWithValue<>(readQName(), readObject()); default : return null; @@ -361,8 +397,8 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead } @SuppressWarnings("unchecked") - private ListNodeBuilder> addLeafSetChildren(QName nodeType, - ListNodeBuilder> builder) throws IOException { + private ListNodeBuilder> addLeafSetChildren(final QName nodeType, + final ListNodeBuilder> builder) throws IOException { LOG.debug("Reading children of leaf set"); @@ -379,7 +415,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead @SuppressWarnings({ "unchecked", "rawtypes" }) private NormalizedNodeContainerBuilder addDataContainerChildren( - NormalizedNodeContainerBuilder builder) throws IOException { + final NormalizedNodeContainerBuilder builder) throws IOException { LOG.debug("Reading data container (leaf nodes) nodes"); NormalizedNode child = readNormalizedNodeInternal(); @@ -390,4 +426,94 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead } return builder; } + + @Override + public void readFully(final byte[] b) throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + input.readFully(b); + } + + @Override + public void readFully(final byte[] b, final int off, final int len) throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + input.readFully(b, off, len); + } + + @Override + public int skipBytes(final int n) throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.skipBytes(n); + } + + @Override + public boolean readBoolean() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readBoolean(); + } + + @Override + public byte readByte() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readByte(); + } + + @Override + public int readUnsignedByte() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readUnsignedByte(); + } + + @Override + public short readShort() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readShort(); + } + + @Override + public int readUnsignedShort() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readUnsignedShort(); + } + + @Override + public char readChar() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readChar(); + } + + @Override + public int readInt() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readInt(); + } + + @Override + public long readLong() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readLong(); + } + + @Override + public float readFloat() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readFloat(); + } + + @Override + public double readDouble() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readDouble(); + } + + @Override + public String readLine() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readLine(); + } + + @Override + public String readUTF() throws IOException { + readSignatureMarkerAndVersionIfNeeded(); + return input.readUTF(); + } }