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=cde338179ba727903e35d4513195cbbddd9b1b1c;hp=bdc52bca688a0c354f9c91ad7de6c6b774bcd966;hb=0717c782e9621409f9623f43ba9f6173e4382a44;hpb=d0825a4c573ca648bf509916864645d2ad65dfda 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 bdc52bca68..cde338179b 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 @@ -11,29 +11,8 @@ package org.opendaylight.controller.cluster.datastore.node.utils.stream; import com.google.common.base.Preconditions; -import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; -import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode; -import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; -import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; -import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; -import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; -import org.opendaylight.yangtools.yang.data.api.schema.MapNode; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode; -import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode; -import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode; -import org.opendaylight.yangtools.yang.data.impl.schema.Builders; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.google.common.base.Strings; +import java.io.DataInput; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; @@ -45,6 +24,23 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.Node; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; +import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; +import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * NormalizedNodeInputStreamReader reads the byte stream and constructs the normalized node including its children nodes. @@ -55,139 +51,140 @@ import java.util.Set; public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamReader { - private DataInputStream reader; - private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class); - private Map codedStringMap = new HashMap<>(); private static final String REVISION_ARG = "?revision="; + private final DataInput input; + + private final Map codedStringMap = new HashMap<>(); + + private QName lastLeafSetQName; + + private NormalizedNodeAttrBuilder> leafBuilder; + + private NormalizedNodeAttrBuilder> leafSetEntryBuilder; + + private final StringBuilder reusableStringBuilder = new StringBuilder(50); + public NormalizedNodeInputStreamReader(InputStream stream) throws IOException { Preconditions.checkNotNull(stream); - reader = new DataInputStream(stream); + input = new DataInputStream(stream); } + public NormalizedNodeInputStreamReader(DataInput input) throws IOException { + this.input = Preconditions.checkNotNull(input); + } + @Override public NormalizedNode readNormalizedNode() throws IOException { - NormalizedNode node = null; - // each node should start with a byte - byte nodeType = reader.readByte(); + byte nodeType = input.readByte(); if(nodeType == NodeTypes.END_NODE) { LOG.debug("End node reached. return"); return null; } - else if(nodeType == NodeTypes.AUGMENTATION_NODE) { - LOG.debug("Reading augmentation node. will create augmentation identifier"); - YangInstanceIdentifier.AugmentationIdentifier identifier = - new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet()); - DataContainerNodeBuilder augmentationBuilder = - Builders.augmentationBuilder().withNodeIdentifier(identifier); - augmentationBuilder = addDataContainerChildren(augmentationBuilder); - node = augmentationBuilder.build(); + switch(nodeType) { + case NodeTypes.AUGMENTATION_NODE : + YangInstanceIdentifier.AugmentationIdentifier augIdentifier = + new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet()); - } else { - QName qName = readQName(); + LOG.debug("Reading augmentation node {} ", augIdentifier); - if(nodeType == NodeTypes.LEAF_SET_ENTRY_NODE) { - LOG.debug("Reading leaf set entry node. Will create NodeWithValue instance identifier"); + return addDataContainerChildren(Builders.augmentationBuilder(). + withNodeIdentifier(augIdentifier)).build(); - // Read the object value + case NodeTypes.LEAF_SET_ENTRY_NODE : Object value = readObject(); + NodeWithValue leafIdentifier = new NodeWithValue(lastLeafSetQName, value); + + LOG.debug("Reading leaf set entry node {}, value {}", leafIdentifier, value); + + return leafSetEntryBuilder().withNodeIdentifier(leafIdentifier).withValue(value).build(); - YangInstanceIdentifier.NodeWithValue nodeWithValue = new YangInstanceIdentifier.NodeWithValue(qName, value); - node = Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue).withValue(value).build(); + case NodeTypes.MAP_ENTRY_NODE : + NodeIdentifierWithPredicates entryIdentifier = new NodeIdentifierWithPredicates( + readQName(), readKeyValueMap()); - } else if(nodeType == NodeTypes.MAP_ENTRY_NODE) { - LOG.debug("Reading map entry node. Will create node identifier with predicates."); + LOG.debug("Reading map entry node {} ", entryIdentifier); - YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier = - new YangInstanceIdentifier.NodeIdentifierWithPredicates(qName, readKeyValueMap()); - DataContainerNodeAttrBuilder mapEntryBuilder - = Builders.mapEntryBuilder().withNodeIdentifier(nodeIdentifier); + return addDataContainerChildren(Builders.mapEntryBuilder(). + withNodeIdentifier(entryIdentifier)).build(); - mapEntryBuilder = (DataContainerNodeAttrBuilder)addDataContainerChildren(mapEntryBuilder); - node = mapEntryBuilder.build(); + default : + return readNodeIdentifierDependentNode(nodeType, new NodeIdentifier(readQName())); + } + } + + private NormalizedNodeAttrBuilder> leafBuilder() { + if(leafBuilder == null) { + leafBuilder = Builders.leafBuilder(); + } - } else { - LOG.debug("Creating standard node identifier. "); - YangInstanceIdentifier.NodeIdentifier identifier = new YangInstanceIdentifier.NodeIdentifier(qName); - node = readNodeIdentifierDependentNode(nodeType, identifier); + return leafBuilder; + } - } + private NormalizedNodeAttrBuilder> leafSetEntryBuilder() { + if(leafSetEntryBuilder == null) { + leafSetEntryBuilder = Builders.leafSetEntryBuilder(); } - return node; + + return leafSetEntryBuilder; } - private NormalizedNode readNodeIdentifierDependentNode(byte nodeType, YangInstanceIdentifier.NodeIdentifier identifier) + private NormalizedNode readNodeIdentifierDependentNode(byte nodeType, NodeIdentifier identifier) throws IOException { switch(nodeType) { case NodeTypes.LEAF_NODE : - LOG.debug("Read leaf node"); + LOG.debug("Read leaf node {}", identifier); // Read the object value - NormalizedNodeAttrBuilder leafBuilder = Builders.leafBuilder(); - return leafBuilder.withNodeIdentifier(identifier).withValue(readObject()).build(); + return leafBuilder().withNodeIdentifier(identifier).withValue(readObject()).build(); case NodeTypes.ANY_XML_NODE : LOG.debug("Read xml node"); - Node value = (Node) readObject(); - return Builders.anyXmlBuilder().withValue(value).build(); + return Builders.anyXmlBuilder().withValue((Node) readObject()).build(); case NodeTypes.MAP_NODE : - LOG.debug("Read map node"); - CollectionNodeBuilder mapBuilder = Builders.mapBuilder().withNodeIdentifier(identifier); - mapBuilder = addMapNodeChildren(mapBuilder); - return mapBuilder.build(); + LOG.debug("Read map node {}", identifier); + return addDataContainerChildren(Builders.mapBuilder(). + withNodeIdentifier(identifier)).build(); case NodeTypes.CHOICE_NODE : - LOG.debug("Read choice node"); - DataContainerNodeBuilder choiceBuilder = - Builders.choiceBuilder().withNodeIdentifier(identifier); - choiceBuilder = addDataContainerChildren(choiceBuilder); - return choiceBuilder.build(); + LOG.debug("Read choice node {}", identifier); + return addDataContainerChildren(Builders.choiceBuilder(). + withNodeIdentifier(identifier)).build(); case NodeTypes.ORDERED_MAP_NODE : - LOG.debug("Reading ordered map node"); - CollectionNodeBuilder orderedMapBuilder = - Builders.orderedMapBuilder().withNodeIdentifier(identifier); - orderedMapBuilder = addMapNodeChildren(orderedMapBuilder); - return orderedMapBuilder.build(); + LOG.debug("Reading ordered map node {}", identifier); + return addDataContainerChildren(Builders.orderedMapBuilder(). + withNodeIdentifier(identifier)).build(); case NodeTypes.UNKEYED_LIST : - LOG.debug("Read unkeyed list node"); - CollectionNodeBuilder unkeyedListBuilder = - Builders.unkeyedListBuilder().withNodeIdentifier(identifier); - unkeyedListBuilder = addUnkeyedListChildren(unkeyedListBuilder); - return unkeyedListBuilder.build(); + LOG.debug("Read unkeyed list node {}", identifier); + return addDataContainerChildren(Builders.unkeyedListBuilder(). + withNodeIdentifier(identifier)).build(); case NodeTypes.UNKEYED_LIST_ITEM : - LOG.debug("Read unkeyed list item node"); - DataContainerNodeAttrBuilder unkeyedListEntryBuilder - = Builders.unkeyedListEntryBuilder().withNodeIdentifier(identifier); - - unkeyedListEntryBuilder = (DataContainerNodeAttrBuilder) - addDataContainerChildren(unkeyedListEntryBuilder); - return unkeyedListEntryBuilder.build(); + LOG.debug("Read unkeyed list item node {}", identifier); + return addDataContainerChildren(Builders.unkeyedListEntryBuilder(). + withNodeIdentifier(identifier)).build(); case NodeTypes.CONTAINER_NODE : - LOG.debug("Read container node"); - DataContainerNodeAttrBuilder containerBuilder = - Builders.containerBuilder().withNodeIdentifier(identifier); - - containerBuilder = (DataContainerNodeAttrBuilder) - addDataContainerChildren(containerBuilder); - return containerBuilder.build(); + LOG.debug("Read container node {}", identifier); + return addDataContainerChildren(Builders.containerBuilder(). + withNodeIdentifier(identifier)).build(); case NodeTypes.LEAF_SET : - LOG.debug("Read leaf set node"); - ListNodeBuilder> leafSetBuilder = - Builders.leafSetBuilder().withNodeIdentifier(identifier); - leafSetBuilder = addLeafSetChildren(leafSetBuilder); - return leafSetBuilder.build(); + LOG.debug("Read leaf set node {}", identifier); + return addLeafSetChildren(identifier.getNodeType(), + Builders.leafSetBuilder().withNodeIdentifier(identifier)).build(); default : return null; @@ -199,46 +196,49 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead String localName = readCodedString(); String namespace = readCodedString(); String revision = readCodedString(); + String qName; - // Not using stringbuilder as compiler optimizes string concatenation of + - if(revision != null){ - qName = "(" + namespace+ REVISION_ARG + revision + ")" +localName; + if(!Strings.isNullOrEmpty(revision)) { + qName = reusableStringBuilder.append('(').append(namespace).append(REVISION_ARG). + append(revision).append(')').append(localName).toString(); } else { - qName = "(" + namespace + ")" +localName; + qName = reusableStringBuilder.append('(').append(namespace).append(')'). + append(localName).toString(); } + reusableStringBuilder.delete(0, reusableStringBuilder.length()); return QNameFactory.create(qName); } private String readCodedString() throws IOException { - boolean readFromMap = reader.readBoolean(); - if(readFromMap) { - return codedStringMap.get(reader.readInt()); - } else { - String value = reader.readUTF(); - if(value != null) { - codedStringMap.put(Integer.valueOf(codedStringMap.size()), value); - } + byte valueType = input.readByte(); + if(valueType == NormalizedNodeOutputStreamWriter.IS_CODE_VALUE) { + return codedStringMap.get(input.readInt()); + } else if(valueType == NormalizedNodeOutputStreamWriter.IS_STRING_VALUE) { + String value = input.readUTF().intern(); + codedStringMap.put(Integer.valueOf(codedStringMap.size()), value); return value; } + + return null; } private Set readQNameSet() throws IOException{ // Read the children count - int count = reader.readInt(); + int count = input.readInt(); Set children = new HashSet<>(count); - for(int i = 0; i readKeyValueMap() throws IOException { - int count = reader.readInt(); + int count = input.readInt(); Map keyValueMap = new HashMap<>(count); - for(int i = 0; i pathArguments = new ArrayList<>(size); + case ValueTypes.BINARY_TYPE : + byte[] bytes = new byte[input.readInt()]; + input.readFully(bytes); + return bytes; - for(int i=0; i pathArguments = new ArrayList<>(size); + + for(int i = 0; i < size; i++) { + pathArguments.add(readPathArgument()); + } + return YangInstanceIdentifier.create(pathArguments); + } + private Set readObjSet() throws IOException { - int count = reader.readInt(); + int count = input.readInt(); Set children = new HashSet<>(count); - for(int i = 0; i> addLeafSetChildren(ListNodeBuilder> builder) - throws IOException { + @SuppressWarnings("unchecked") + private ListNodeBuilder> addLeafSetChildren(QName nodeType, + ListNodeBuilder> builder) throws IOException { LOG.debug("Reading children of leaf set"); - LeafSetEntryNode child = (LeafSetEntryNode)readNormalizedNode(); - - while(child != null) { - builder.withChild(child); - child = (LeafSetEntryNode)readNormalizedNode(); - } - return builder; - } - private CollectionNodeBuilder addUnkeyedListChildren( - CollectionNodeBuilder builder) - throws IOException{ + lastLeafSetQName = nodeType; - LOG.debug("Reading children of unkeyed list"); - UnkeyedListEntryNode child = (UnkeyedListEntryNode)readNormalizedNode(); + LeafSetEntryNode child = (LeafSetEntryNode)readNormalizedNode(); while(child != null) { builder.withChild(child); - child = (UnkeyedListEntryNode)readNormalizedNode(); + child = (LeafSetEntryNode)readNormalizedNode(); } return builder; } - private DataContainerNodeBuilder addDataContainerChildren(DataContainerNodeBuilder builder) - throws IOException { + @SuppressWarnings({ "unchecked", "rawtypes" }) + private NormalizedNodeContainerBuilder addDataContainerChildren( + NormalizedNodeContainerBuilder builder) throws IOException { LOG.debug("Reading data container (leaf nodes) nodes"); - DataContainerChild child = - (DataContainerChild) readNormalizedNode(); + NormalizedNode child = readNormalizedNode(); while(child != null) { - builder.withChild(child); - child = - (DataContainerChild) readNormalizedNode(); + builder.addChild(child); + child = readNormalizedNode(); } return builder; } - - - private CollectionNodeBuilder addMapNodeChildren(CollectionNodeBuilder builder) - throws IOException { - LOG.debug("Reading map node children"); - MapEntryNode child = (MapEntryNode)readNormalizedNode(); - - while(child != null){ - builder.withChild(child); - child = (MapEntryNode)readNormalizedNode(); - } - - return builder; - } - - - @Override - public void close() throws IOException { - reader.close(); - } - }