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 DataInputStream reader;
60 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
62 private Map<Integer, String> codedStringMap = new HashMap<>();
63 private static final String REVISION_ARG = "?revision=";
65 public NormalizedNodeInputStreamReader(InputStream stream) throws IOException {
66 Preconditions.checkNotNull(stream);
67 reader = new DataInputStream(stream);
71 public NormalizedNode<?, ?> readNormalizedNode() throws IOException {
72 NormalizedNode<?, ?> node = null;
74 // each node should start with a byte
75 byte nodeType = reader.readByte();
77 if(nodeType == NodeTypes.END_NODE) {
78 LOG.debug("End node reached. return");
81 else if(nodeType == NodeTypes.AUGMENTATION_NODE) {
82 LOG.debug("Reading augmentation node. will create augmentation identifier");
84 YangInstanceIdentifier.AugmentationIdentifier identifier =
85 new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
86 DataContainerNodeBuilder<YangInstanceIdentifier.AugmentationIdentifier, AugmentationNode> augmentationBuilder =
87 Builders.augmentationBuilder().withNodeIdentifier(identifier);
88 augmentationBuilder = addDataContainerChildren(augmentationBuilder);
89 node = augmentationBuilder.build();
92 QName qName = readQName();
94 if(nodeType == NodeTypes.LEAF_SET_ENTRY_NODE) {
95 LOG.debug("Reading leaf set entry node. Will create NodeWithValue instance identifier");
97 // Read the object value
98 Object value = readObject();
100 YangInstanceIdentifier.NodeWithValue nodeWithValue = new YangInstanceIdentifier.NodeWithValue(qName, value);
101 node = Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue).withValue(value).build();
103 } else if(nodeType == NodeTypes.MAP_ENTRY_NODE) {
104 LOG.debug("Reading map entry node. Will create node identifier with predicates.");
106 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier =
107 new YangInstanceIdentifier.NodeIdentifierWithPredicates(qName, readKeyValueMap());
108 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder
109 = Builders.mapEntryBuilder().withNodeIdentifier(nodeIdentifier);
111 mapEntryBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates,
112 MapEntryNode>)addDataContainerChildren(mapEntryBuilder);
113 node = mapEntryBuilder.build();
116 LOG.debug("Creating standard node identifier. ");
117 YangInstanceIdentifier.NodeIdentifier identifier = new YangInstanceIdentifier.NodeIdentifier(qName);
118 node = readNodeIdentifierDependentNode(nodeType, identifier);
125 private NormalizedNode<?, ?> readNodeIdentifierDependentNode(byte nodeType, YangInstanceIdentifier.NodeIdentifier identifier)
129 case NodeTypes.LEAF_NODE :
130 LOG.debug("Read leaf node");
131 // Read the object value
132 NormalizedNodeAttrBuilder leafBuilder = Builders.leafBuilder();
133 return leafBuilder.withNodeIdentifier(identifier).withValue(readObject()).build();
135 case NodeTypes.ANY_XML_NODE :
136 LOG.debug("Read xml node");
137 Node value = (Node) readObject();
138 return Builders.anyXmlBuilder().withValue(value).build();
140 case NodeTypes.MAP_NODE :
141 LOG.debug("Read map node");
142 CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder().withNodeIdentifier(identifier);
143 mapBuilder = addMapNodeChildren(mapBuilder);
144 return mapBuilder.build();
146 case NodeTypes.CHOICE_NODE :
147 LOG.debug("Read choice node");
148 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ChoiceNode> choiceBuilder =
149 Builders.choiceBuilder().withNodeIdentifier(identifier);
150 choiceBuilder = addDataContainerChildren(choiceBuilder);
151 return choiceBuilder.build();
153 case NodeTypes.ORDERED_MAP_NODE :
154 LOG.debug("Reading ordered map node");
155 CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapBuilder =
156 Builders.orderedMapBuilder().withNodeIdentifier(identifier);
157 orderedMapBuilder = addMapNodeChildren(orderedMapBuilder);
158 return orderedMapBuilder.build();
160 case NodeTypes.UNKEYED_LIST :
161 LOG.debug("Read unkeyed list node");
162 CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> unkeyedListBuilder =
163 Builders.unkeyedListBuilder().withNodeIdentifier(identifier);
164 unkeyedListBuilder = addUnkeyedListChildren(unkeyedListBuilder);
165 return unkeyedListBuilder.build();
167 case NodeTypes.UNKEYED_LIST_ITEM :
168 LOG.debug("Read unkeyed list item node");
169 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, UnkeyedListEntryNode> unkeyedListEntryBuilder
170 = Builders.unkeyedListEntryBuilder().withNodeIdentifier(identifier);
172 unkeyedListEntryBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, UnkeyedListEntryNode>)
173 addDataContainerChildren(unkeyedListEntryBuilder);
174 return unkeyedListEntryBuilder.build();
176 case NodeTypes.CONTAINER_NODE :
177 LOG.debug("Read container node");
178 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> containerBuilder =
179 Builders.containerBuilder().withNodeIdentifier(identifier);
181 containerBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode>)
182 addDataContainerChildren(containerBuilder);
183 return containerBuilder.build();
185 case NodeTypes.LEAF_SET :
186 LOG.debug("Read leaf set node");
187 ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder =
188 Builders.leafSetBuilder().withNodeIdentifier(identifier);
189 leafSetBuilder = addLeafSetChildren(leafSetBuilder);
190 return leafSetBuilder.build();
197 private QName readQName() throws IOException {
198 // Read in the same sequence of writing
199 String localName = readCodedString();
200 String namespace = readCodedString();
201 String revision = readCodedString();
203 // Not using stringbuilder as compiler optimizes string concatenation of +
204 if(revision != null){
205 qName = "(" + namespace+ REVISION_ARG + revision + ")" +localName;
207 qName = "(" + namespace + ")" +localName;
210 return QNameFactory.create(qName);
214 private String readCodedString() throws IOException {
215 boolean readFromMap = reader.readBoolean();
217 return codedStringMap.get(reader.readInt());
219 String value = reader.readUTF();
221 codedStringMap.put(Integer.valueOf(codedStringMap.size()), value);
227 private Set<QName> readQNameSet() throws IOException{
228 // Read the children count
229 int count = reader.readInt();
230 Set<QName> children = new HashSet<>(count);
231 for(int i = 0; i<count; i++) {
232 children.add(readQName());
237 private Map<QName, Object> readKeyValueMap() throws IOException {
238 int count = reader.readInt();
239 Map<QName, Object> keyValueMap = new HashMap<>(count);
241 for(int i = 0; i<count; i++) {
242 keyValueMap.put(readQName(), readObject());
248 private Object readObject() throws IOException {
249 byte objectType = reader.readByte();
251 case ValueTypes.BITS_TYPE:
254 case ValueTypes.BOOL_TYPE :
255 return reader.readBoolean();
257 case ValueTypes.BYTE_TYPE :
258 return reader.readByte();
260 case ValueTypes.INT_TYPE :
261 return reader.readInt();
263 case ValueTypes.LONG_TYPE :
264 return reader.readLong();
266 case ValueTypes.QNAME_TYPE :
269 case ValueTypes.SHORT_TYPE :
270 return reader.readShort();
272 case ValueTypes.STRING_TYPE :
273 return reader.readUTF();
275 case ValueTypes.BIG_DECIMAL_TYPE :
276 return new BigDecimal(reader.readUTF());
278 case ValueTypes.BIG_INTEGER_TYPE :
279 return new BigInteger(reader.readUTF());
281 case ValueTypes.YANG_IDENTIFIER_TYPE :
282 int size = reader.readInt();
284 List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>(size);
286 for(int i=0; i<size; i++) {
287 pathArguments.add(readPathArgument());
289 return YangInstanceIdentifier.create(pathArguments);
296 private Set<String> readObjSet() throws IOException {
297 int count = reader.readInt();
298 Set<String> children = new HashSet<>(count);
299 for(int i = 0; i<count; i++) {
300 children.add(readCodedString());
305 private YangInstanceIdentifier.PathArgument readPathArgument() throws IOException {
307 int type = reader.readByte();
311 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
312 return new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
314 case PathArgumentTypes.NODE_IDENTIFIER :
315 return new YangInstanceIdentifier.NodeIdentifier(readQName());
317 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES :
318 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(readQName(), readKeyValueMap());
320 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
321 return new YangInstanceIdentifier.NodeWithValue(readQName(), readObject());
328 private ListNodeBuilder<Object, LeafSetEntryNode<Object>> addLeafSetChildren(ListNodeBuilder<Object,
329 LeafSetEntryNode<Object>> builder)
332 LOG.debug("Reading children of leaf set");
333 LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>)readNormalizedNode();
335 while(child != null) {
336 builder.withChild(child);
337 child = (LeafSetEntryNode<Object>)readNormalizedNode();
342 private CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addUnkeyedListChildren(
343 CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder)
346 LOG.debug("Reading children of unkeyed list");
347 UnkeyedListEntryNode child = (UnkeyedListEntryNode)readNormalizedNode();
349 while(child != null) {
350 builder.withChild(child);
351 child = (UnkeyedListEntryNode)readNormalizedNode();
356 private DataContainerNodeBuilder addDataContainerChildren(DataContainerNodeBuilder builder)
358 LOG.debug("Reading data container (leaf nodes) nodes");
360 DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> child =
361 (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>) readNormalizedNode();
363 while(child != null) {
364 builder.withChild(child);
366 (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>) readNormalizedNode();
372 private CollectionNodeBuilder addMapNodeChildren(CollectionNodeBuilder builder)
374 LOG.debug("Reading map node children");
375 MapEntryNode child = (MapEntryNode)readNormalizedNode();
377 while(child != null){
378 builder.withChild(child);
379 child = (MapEntryNode)readNormalizedNode();
387 public void close() throws IOException {