Bug 2265: Modified NormalizedNodeOutputStreamWriter to implement yangtools interface
authortpantelis <tpanteli@brocade.com>
Mon, 27 Oct 2014 21:13:58 +0000 (17:13 -0400)
committertpantelis <tpanteli@brocade.com>
Mon, 27 Oct 2014 21:57:37 +0000 (17:57 -0400)
The NormalizedNodeOutputStreamWriter was originally written to implement
a NormalizedNodeStreamWriter interface class that was copied from
yangtools. This was done to get access to the NodeWithValue in the
leafSetEntryNode method which isn't specified in the yangtools
interface. However it turns out we don't actually need the NodeWithValue so
NormalizedNodeOutputStreamWriter was changed to implement the yangtols interface
and the copied NormalizedNodeStreamWriter interface was removed.

Change-Id: I72f0c96359e4fe96e8e183aa7afba9291c5d519d
Signed-off-by: tpantelis <tpanteli@brocade.com>
java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java
java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java
java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamWriter.java [deleted file]

index bdc52bca688a0c354f9c91ad7de6c6b774bcd966..b59a32efc9bd96e0bd4ad157b0faa565dafa948b 100644 (file)
@@ -55,19 +55,22 @@ import java.util.Set;
 
 public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamReader {
 
-    private DataInputStream reader;
-
     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
 
-    private Map<Integer, String> codedStringMap = new HashMap<>();
     private static final String REVISION_ARG = "?revision=";
 
+    private final DataInputStream reader;
+
+    private final Map<Integer, String> codedStringMap = new HashMap<>();
+
+    private QName lastLeafSetQName;
+
     public NormalizedNodeInputStreamReader(InputStream stream) throws IOException {
         Preconditions.checkNotNull(stream);
         reader = new DataInputStream(stream);
     }
 
-
+    @Override
     public NormalizedNode<?, ?> readNormalizedNode() throws IOException {
         NormalizedNode<?, ?> node = null;
 
@@ -89,20 +92,21 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
             node = augmentationBuilder.build();
 
         } else {
-            QName qName = readQName();
-
             if(nodeType == NodeTypes.LEAF_SET_ENTRY_NODE) {
                 LOG.debug("Reading leaf set entry node. Will create NodeWithValue instance identifier");
 
                 // Read the object value
                 Object value = readObject();
 
-                YangInstanceIdentifier.NodeWithValue nodeWithValue = new YangInstanceIdentifier.NodeWithValue(qName, value);
-                node =  Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue).withValue(value).build();
+                YangInstanceIdentifier.NodeWithValue nodeWithValue = new YangInstanceIdentifier.NodeWithValue(
+                        lastLeafSetQName, value);
+                node =  Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue).
+                        withValue(value).build();
 
             } else if(nodeType == NodeTypes.MAP_ENTRY_NODE) {
                 LOG.debug("Reading map entry node. Will create node identifier with predicates.");
 
+                QName qName = readQName();
                 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier =
                     new YangInstanceIdentifier.NodeIdentifierWithPredicates(qName, readKeyValueMap());
                 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder
@@ -114,6 +118,8 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
             } else {
                 LOG.debug("Creating standard node identifier. ");
+
+                QName qName = readQName();
                 YangInstanceIdentifier.NodeIdentifier identifier = new YangInstanceIdentifier.NodeIdentifier(qName);
                 node = readNodeIdentifierDependentNode(nodeType, identifier);
 
@@ -186,7 +192,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
                 LOG.debug("Read leaf set node");
                 ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder =
                     Builders.leafSetBuilder().withNodeIdentifier(identifier);
-                leafSetBuilder = addLeafSetChildren(leafSetBuilder);
+                leafSetBuilder = addLeafSetChildren(identifier.getNodeType(), leafSetBuilder);
                 return leafSetBuilder.build();
 
             default :
@@ -325,11 +331,14 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
         }
     }
 
-    private ListNodeBuilder<Object, LeafSetEntryNode<Object>> addLeafSetChildren(ListNodeBuilder<Object,
-        LeafSetEntryNode<Object>> builder)
+    private ListNodeBuilder<Object, LeafSetEntryNode<Object>> addLeafSetChildren(QName nodeType,
+            ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder)
         throws IOException {
 
         LOG.debug("Reading children of leaf set");
+
+        lastLeafSetQName = nodeType;
+
         LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>)readNormalizedNode();
 
         while(child != null) {
index 05a47a0401a18c2774caae0c7cc85fa6f07b9ebe..cbd7bf885373b331fb71a9d6899efc755b85f767 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.Iterables;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,11 +42,11 @@ import java.util.Set;
 
 public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWriter{
 
-    private DataOutputStream writer;
-
     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
 
-    private Map<String, Integer> stringCodeMap = new HashMap<>();
+    private final DataOutputStream writer;
+
+    private final Map<String, Integer> stringCodeMap = new HashMap<>();
 
     public NormalizedNodeOutputStreamWriter(OutputStream stream) throws IOException {
         Preconditions.checkNotNull(stream);
@@ -70,12 +71,10 @@ public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWri
     }
 
     @Override
-    public void leafSetEntryNode(YangInstanceIdentifier.NodeWithValue name, Object value) throws IOException, IllegalArgumentException {
-        Preconditions.checkNotNull(name, "Node identifier should not be null");
-
+    public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
         LOG.debug("Writing a new leaf set entry node");
-        startNode(name.getNodeType(), NodeTypes.LEAF_SET_ENTRY_NODE);
 
+        writer.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
         writeObject(value);
     }
 
diff --git a/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamWriter.java b/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamWriter.java
deleted file mode 100644 (file)
index af95b61..0000000
+++ /dev/null
@@ -1,219 +0,0 @@
-
-/*
- * 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
- */
-
-package org.opendaylight.controller.cluster.datastore.node.utils.stream;
-
-import java.io.Closeable;
-import java.io.Flushable;
-import java.io.IOException;
-
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
-/**
- * Event Stream Writer based on Normalized Node tree representation
- *
- * <h3>Writing Event Stream</h3>
- *
- * <ul>
- * <li><code>container</code> - Container node representation, start event is
- * emitted using {@link #startContainerNode(YangInstanceIdentifier.NodeIdentifier, int)}
- * and node end event is
- * emitted using {@link #endNode()}. Container node is implementing
- * {@link org.opendaylight.yangtools.yang.binding.DataObject} interface.
- *
- * <li><code>list</code> - YANG list statement has two representation in event
- * stream - unkeyed list and map. Unkeyed list is YANG list which did not
- * specify key.</li>
- *
- * <ul>
- * <li><code>Map</code> - Map start event is emitted using
- * {@link #startMapNode(YangInstanceIdentifier.NodeIdentifier, int)}
- * and is ended using {@link #endNode()}. Each map entry start is emitted using
- * {@link #startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates, int)}
- * with Map of keys
- * and finished using {@link #endNode()}.</li>
- *
- * <li><code>UnkeyedList</code> - Unkeyed list represent list without keys,
- * unkeyed list start is emitted using
- * {@link #startUnkeyedList(YangInstanceIdentifier.NodeIdentifier, int)} list
- * end is emitted using {@link #endNode()}. Each list item is emitted using
- * {@link #startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier, int)}
- * and ended using {@link #endNode()}.</li>
- * </ul>
- *
- * <li><code>leaf</code> - Leaf node event is emitted using
- * {@link #leafNode(YangInstanceIdentifier.NodeIdentifier, Object)}.
- * {@link #endNode()} MUST NOT BE emitted for
- * leaf node.</li>
- *
- * <li><code>leaf-list</code> - Leaf list start is emitted using
- * {@link #startLeafSet(YangInstanceIdentifier.NodeIdentifier, int)}.
- * Leaf list end is emitted using
- * {@link #endNode()}. Leaf list entries are emitted using
- * {@link #leafSetEntryNode(YangInstanceIdentifier.NodeWithValue name, Object).
- *
- * <li><code>anyxml - Anyxml node event is emitted using
- * {@link #leafNode(YangInstanceIdentifier.NodeIdentifier, Object)}. {@link #endNode()} MUST NOT BE emitted
- * for anyxml node.</code></li>
- *
- *
- * <li><code>choice</code> Choice node event is emmited by
- * {@link #startChoiceNode(YangInstanceIdentifier.NodeIdentifier, int)} event and
- * finished by invoking {@link #endNode()}
- * <li>
- * <code>augment</code> - Represents augmentation, augmentation node is started
- * by invoking {@link #startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier)} and
- * finished by invoking {@link #endNode()}.</li>
- *
- * </ul>
- *
- * <h3>Implementation notes</h3>
- *
- * <p>
- * Implementations of this interface must not hold user suppled objects
- * and resources needlessly.
- *
- */
-
-public interface NormalizedNodeStreamWriter extends Closeable, Flushable {
-
-    public final int UNKNOWN_SIZE = -1;
-
-    /**
-     * Write the leaf node identifier and value to the stream.
-     * @param name
-     * @param value
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void leafNode(YangInstanceIdentifier.NodeIdentifier name, Object value)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing leaf Set node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param name
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Write the leaf Set Entry Node object to the stream with identifier and value.
-     * @param name
-     * @param value
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void leafSetEntryNode(YangInstanceIdentifier.NodeWithValue name, Object value)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing container node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param name
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing unkeyed list node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param name
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startUnkeyedList(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing unkeyed list item. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param name
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalStateException
-     */
-    void startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint)
-        throws IOException, IllegalStateException;
-
-    /**
-     * Start writing map node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param name
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing map entry node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param identifier
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, int childSizeHint)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing ordered map node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param name
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startOrderedMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing choice node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param name
-     * @param childSizeHint is the estimated children count. Usage is optional in implementation.
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startChoiceNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Start writing augmentation node. You must call {@link #endNode()} once you are done writing all of its children.
-     * @param identifier
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier identifier)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * Write any xml node identifier and value to the stream
-     * @param name
-     * @param value
-     * @throws IOException
-     * @throws IllegalArgumentException
-     */
-    void anyxmlNode(YangInstanceIdentifier.NodeIdentifier name, Object value)
-        throws IOException, IllegalArgumentException;
-
-    /**
-     * This method should be used to add end symbol/identifier of node in the stream.
-     * @throws IOException
-     * @throws IllegalStateException
-     */
-    void endNode() throws IOException, IllegalStateException;
-
-    @Override
-    void close() throws IOException;
-
-    @Override
-    void flush() throws IOException;
-}