BUG-7594: Expand NormalizedNodeData{Input,Output} to handle SchemaPath
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / AbstractNormalizedNodeDataOutput.java
index abe1f8c588fb53fc9108c00afb1de9134805d66c..f308575932e66521a1a1c6793acc30c988752669 100644 (file)
@@ -11,15 +11,27 @@ import com.google.common.base.Preconditions;
 import java.io.DataOutput;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.StringWriter;
 import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+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.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -147,7 +159,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void leafNode(final YangInstanceIdentifier.NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
+    public void leafNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Writing a new leaf node");
         startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
@@ -156,7 +168,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startLeafSet(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Starting a new leaf set");
 
@@ -165,7 +177,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startOrderedLeafSet(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Starting a new ordered leaf set");
 
@@ -189,7 +201,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startContainerNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
 
         LOG.debug("Starting a new container node");
@@ -198,7 +210,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startYangModeledAnyXmlNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
 
         LOG.debug("Starting a new yang modeled anyXml node");
@@ -207,7 +219,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startUnkeyedList(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Starting a new unkeyed list");
 
@@ -215,7 +227,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startUnkeyedListItem(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalStateException {
+    public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalStateException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Starting a new unkeyed list item");
 
@@ -223,7 +235,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startMapNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Starting a new map node");
 
@@ -231,7 +243,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startMapEntryNode(final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(identifier, "Node identifier should not be null");
         LOG.debug("Starting a new map entry node");
         startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
@@ -241,7 +253,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startOrderedMapNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Starting a new ordered map node");
 
@@ -249,7 +261,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startChoiceNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
+    public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
         LOG.debug("Starting a new choice node");
 
@@ -257,7 +269,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void startAugmentationNode(final YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
+    public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(identifier, "Node identifier should not be null");
         LOG.debug("Starting a new augmentation node");
 
@@ -266,13 +278,19 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     @Override
-    public void anyxmlNode(final YangInstanceIdentifier.NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
+    public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
         Preconditions.checkNotNull(name, "Node identifier should not be null");
-        LOG.debug("Writing a new xml node");
+        LOG.debug("Writing any xml node");
 
         startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
 
-        writeObject(value);
+        try {
+            StreamResult xmlOutput = new StreamResult(new StringWriter());
+            TransformerFactory.newInstance().newTransformer().transform((DOMSource)value, xmlOutput);
+            writeObject(xmlOutput.getWriter().toString());
+        } catch (TransformerException | TransformerFactoryConfigurationError e) {
+            throw new IOException("Error writing anyXml", e);
+        }
     }
 
     @Override
@@ -316,6 +334,18 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
         }
     }
 
+    @Override
+    public void writeSchemaPath(final SchemaPath path) throws IOException {
+        ensureHeaderWritten();
+        output.writeBoolean(path.isAbsolute());
+
+        final Collection<QName> qnames = path.getPath();
+        output.writeInt(qnames.size());
+        for (QName qname : qnames) {
+            writeQName(qname);
+        }
+    }
+
     @Override
     public void writeYangInstanceIdentifier(final YangInstanceIdentifier identifier) throws IOException {
         ensureHeaderWritten();
@@ -323,16 +353,16 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
     }
 
     private void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
-        Collection<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
+        Collection<PathArgument> pathArguments = identifier.getPathArguments();
         output.writeInt(pathArguments.size());
 
-        for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
+        for(PathArgument pathArgument : pathArguments) {
             writePathArgument(pathArgument);
         }
     }
 
     @Override
-    public void writePathArgument(final YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
+    public void writePathArgument(final PathArgument pathArgument) throws IOException {
 
         byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
 
@@ -341,16 +371,15 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
         switch(type) {
             case PathArgumentTypes.NODE_IDENTIFIER:
 
-                YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
-                    (YangInstanceIdentifier.NodeIdentifier) pathArgument;
+                NodeIdentifier nodeIdentifier = (NodeIdentifier) pathArgument;
 
                 writeQName(nodeIdentifier.getNodeType());
                 break;
 
             case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
 
-                YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
-                    (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
+                NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
+                    (NodeIdentifierWithPredicates) pathArgument;
                 writeQName(nodeIdentifierWithPredicates.getNodeType());
 
                 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
@@ -358,8 +387,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
 
             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
 
-                YangInstanceIdentifier.NodeWithValue nodeWithValue =
-                    (YangInstanceIdentifier.NodeWithValue) pathArgument;
+                NodeWithValue<?> nodeWithValue = (NodeWithValue<?>) pathArgument;
 
                 writeQName(nodeWithValue.getNodeType());
                 writeObject(nodeWithValue.getValue());
@@ -367,8 +395,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
 
             case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
 
-                YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
-                    (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
+                AugmentationIdentifier augmentationIdentifier = (AugmentationIdentifier) pathArgument;
 
                 // No Qname in augmentation identifier
                 writeQNameSet(augmentationIdentifier.getPossibleChildNames());