3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
11 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.Node;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import java.io.DataInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.math.BigDecimal;
41 import java.math.BigInteger;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.List;
50 * NormalizedNodeInputStreamReader reads the byte stream and constructs the normalized node including its children nodes.
51 * This process goes in recursive manner, where each NodeTypes object signifies the start of the object, except END_NODE.
52 * If a node can have children, then that node's end is calculated based on appearance of END_NODE.
56 public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamReader {
58 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
60 private static final String REVISION_ARG = "?revision=";
62 private final DataInputStream reader;
64 private final Map<Integer, String> codedStringMap = new HashMap<>();
66 private QName lastLeafSetQName;
68 public NormalizedNodeInputStreamReader(InputStream stream) throws IOException {
69 Preconditions.checkNotNull(stream);
70 reader = new DataInputStream(stream);
74 public NormalizedNode<?, ?> readNormalizedNode() throws IOException {
75 NormalizedNode<?, ?> node = null;
77 // each node should start with a byte
78 byte nodeType = reader.readByte();
80 if(nodeType == NodeTypes.END_NODE) {
81 LOG.debug("End node reached. return");
84 else if(nodeType == NodeTypes.AUGMENTATION_NODE) {
85 LOG.debug("Reading augmentation node. will create augmentation identifier");
87 YangInstanceIdentifier.AugmentationIdentifier identifier =
88 new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
89 DataContainerNodeBuilder<YangInstanceIdentifier.AugmentationIdentifier, AugmentationNode> augmentationBuilder =
90 Builders.augmentationBuilder().withNodeIdentifier(identifier);
91 augmentationBuilder = addDataContainerChildren(augmentationBuilder);
92 node = augmentationBuilder.build();
95 if(nodeType == NodeTypes.LEAF_SET_ENTRY_NODE) {
96 LOG.debug("Reading leaf set entry node. Will create NodeWithValue instance identifier");
98 // Read the object value
99 Object value = readObject();
101 YangInstanceIdentifier.NodeWithValue nodeWithValue = new YangInstanceIdentifier.NodeWithValue(
102 lastLeafSetQName, value);
103 node = Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue).
104 withValue(value).build();
106 } else if(nodeType == NodeTypes.MAP_ENTRY_NODE) {
107 LOG.debug("Reading map entry node. Will create node identifier with predicates.");
109 QName qName = readQName();
110 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier =
111 new YangInstanceIdentifier.NodeIdentifierWithPredicates(qName, readKeyValueMap());
112 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder
113 = Builders.mapEntryBuilder().withNodeIdentifier(nodeIdentifier);
115 mapEntryBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates,
116 MapEntryNode>)addDataContainerChildren(mapEntryBuilder);
117 node = mapEntryBuilder.build();
120 LOG.debug("Creating standard node identifier. ");
122 QName qName = readQName();
123 YangInstanceIdentifier.NodeIdentifier identifier = new YangInstanceIdentifier.NodeIdentifier(qName);
124 node = readNodeIdentifierDependentNode(nodeType, identifier);
131 private NormalizedNode<?, ?> readNodeIdentifierDependentNode(byte nodeType, YangInstanceIdentifier.NodeIdentifier identifier)
135 case NodeTypes.LEAF_NODE :
136 LOG.debug("Read leaf node");
137 // Read the object value
138 NormalizedNodeAttrBuilder leafBuilder = Builders.leafBuilder();
139 return leafBuilder.withNodeIdentifier(identifier).withValue(readObject()).build();
141 case NodeTypes.ANY_XML_NODE :
142 LOG.debug("Read xml node");
143 Node<?> value = (Node<?>) readObject();
144 return Builders.anyXmlBuilder().withValue(value).build();
146 case NodeTypes.MAP_NODE :
147 LOG.debug("Read map node");
148 CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder().withNodeIdentifier(identifier);
149 mapBuilder = addMapNodeChildren(mapBuilder);
150 return mapBuilder.build();
152 case NodeTypes.CHOICE_NODE :
153 LOG.debug("Read choice node");
154 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ChoiceNode> choiceBuilder =
155 Builders.choiceBuilder().withNodeIdentifier(identifier);
156 choiceBuilder = addDataContainerChildren(choiceBuilder);
157 return choiceBuilder.build();
159 case NodeTypes.ORDERED_MAP_NODE :
160 LOG.debug("Reading ordered map node");
161 CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapBuilder =
162 Builders.orderedMapBuilder().withNodeIdentifier(identifier);
163 orderedMapBuilder = addMapNodeChildren(orderedMapBuilder);
164 return orderedMapBuilder.build();
166 case NodeTypes.UNKEYED_LIST :
167 LOG.debug("Read unkeyed list node");
168 CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> unkeyedListBuilder =
169 Builders.unkeyedListBuilder().withNodeIdentifier(identifier);
170 unkeyedListBuilder = addUnkeyedListChildren(unkeyedListBuilder);
171 return unkeyedListBuilder.build();
173 case NodeTypes.UNKEYED_LIST_ITEM :
174 LOG.debug("Read unkeyed list item node");
175 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, UnkeyedListEntryNode> unkeyedListEntryBuilder
176 = Builders.unkeyedListEntryBuilder().withNodeIdentifier(identifier);
178 unkeyedListEntryBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, UnkeyedListEntryNode>)
179 addDataContainerChildren(unkeyedListEntryBuilder);
180 return unkeyedListEntryBuilder.build();
182 case NodeTypes.CONTAINER_NODE :
183 LOG.debug("Read container node");
184 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> containerBuilder =
185 Builders.containerBuilder().withNodeIdentifier(identifier);
187 containerBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode>)
188 addDataContainerChildren(containerBuilder);
189 return containerBuilder.build();
191 case NodeTypes.LEAF_SET :
192 LOG.debug("Read leaf set node");
193 ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder =
194 Builders.leafSetBuilder().withNodeIdentifier(identifier);
195 leafSetBuilder = addLeafSetChildren(identifier.getNodeType(), leafSetBuilder);
196 return leafSetBuilder.build();
203 private QName readQName() throws IOException {
204 // Read in the same sequence of writing
205 String localName = readCodedString();
206 String namespace = readCodedString();
207 String revision = readCodedString();
209 // Not using stringbuilder as compiler optimizes string concatenation of +
210 if(revision != null){
211 qName = "(" + namespace+ REVISION_ARG + revision + ")" +localName;
213 qName = "(" + namespace + ")" +localName;
216 return QNameFactory.create(qName);
220 private String readCodedString() throws IOException {
221 boolean readFromMap = reader.readBoolean();
223 return codedStringMap.get(reader.readInt());
225 String value = reader.readUTF();
227 codedStringMap.put(Integer.valueOf(codedStringMap.size()), value);
233 private Set<QName> readQNameSet() throws IOException{
234 // Read the children count
235 int count = reader.readInt();
236 Set<QName> children = new HashSet<>(count);
237 for(int i = 0; i<count; i++) {
238 children.add(readQName());
243 private Map<QName, Object> readKeyValueMap() throws IOException {
244 int count = reader.readInt();
245 Map<QName, Object> keyValueMap = new HashMap<>(count);
247 for(int i = 0; i<count; i++) {
248 keyValueMap.put(readQName(), readObject());
254 private Object readObject() throws IOException {
255 byte objectType = reader.readByte();
257 case ValueTypes.BITS_TYPE:
260 case ValueTypes.BOOL_TYPE :
261 return reader.readBoolean();
263 case ValueTypes.BYTE_TYPE :
264 return reader.readByte();
266 case ValueTypes.INT_TYPE :
267 return reader.readInt();
269 case ValueTypes.LONG_TYPE :
270 return reader.readLong();
272 case ValueTypes.QNAME_TYPE :
275 case ValueTypes.SHORT_TYPE :
276 return reader.readShort();
278 case ValueTypes.STRING_TYPE :
279 return reader.readUTF();
281 case ValueTypes.BIG_DECIMAL_TYPE :
282 return new BigDecimal(reader.readUTF());
284 case ValueTypes.BIG_INTEGER_TYPE :
285 return new BigInteger(reader.readUTF());
287 case ValueTypes.YANG_IDENTIFIER_TYPE :
288 int size = reader.readInt();
290 List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>(size);
292 for(int i=0; i<size; i++) {
293 pathArguments.add(readPathArgument());
295 return YangInstanceIdentifier.create(pathArguments);
302 private Set<String> readObjSet() throws IOException {
303 int count = reader.readInt();
304 Set<String> children = new HashSet<>(count);
305 for(int i = 0; i<count; i++) {
306 children.add(readCodedString());
311 private YangInstanceIdentifier.PathArgument readPathArgument() throws IOException {
313 int type = reader.readByte();
317 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
318 return new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
320 case PathArgumentTypes.NODE_IDENTIFIER :
321 return new YangInstanceIdentifier.NodeIdentifier(readQName());
323 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES :
324 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(readQName(), readKeyValueMap());
326 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
327 return new YangInstanceIdentifier.NodeWithValue(readQName(), readObject());
334 private ListNodeBuilder<Object, LeafSetEntryNode<Object>> addLeafSetChildren(QName nodeType,
335 ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder)
338 LOG.debug("Reading children of leaf set");
340 lastLeafSetQName = nodeType;
342 LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>)readNormalizedNode();
344 while(child != null) {
345 builder.withChild(child);
346 child = (LeafSetEntryNode<Object>)readNormalizedNode();
351 private CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addUnkeyedListChildren(
352 CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder)
355 LOG.debug("Reading children of unkeyed list");
356 UnkeyedListEntryNode child = (UnkeyedListEntryNode)readNormalizedNode();
358 while(child != null) {
359 builder.withChild(child);
360 child = (UnkeyedListEntryNode)readNormalizedNode();
365 private DataContainerNodeBuilder addDataContainerChildren(DataContainerNodeBuilder builder)
367 LOG.debug("Reading data container (leaf nodes) nodes");
369 DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> child =
370 (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>) readNormalizedNode();
372 while(child != null) {
373 builder.withChild(child);
375 (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>) readNormalizedNode();
381 private CollectionNodeBuilder addMapNodeChildren(CollectionNodeBuilder builder)
383 LOG.debug("Reading map node children");
384 MapEntryNode child = (MapEntryNode)readNormalizedNode();
386 while(child != null){
387 builder.withChild(child);
388 child = (MapEntryNode)readNormalizedNode();
396 public void close() throws IOException {