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;
private NormalizedNodeWriter normalizedNodeWriter;
private boolean headerWritten;
+ private QName lastLeafSetQName;
AbstractNormalizedNodeDataOutput(final DataOutput output) {
this.output = Preconditions.checkNotNull(output);
}
@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);
}
@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");
+ lastLeafSetQName = name.getNodeType();
startNode(name.getNodeType(), NodeTypes.LEAF_SET);
}
@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");
+ lastLeafSetQName = name.getNodeType();
startNode(name.getNodeType(), NodeTypes.ORDERED_LEAF_SET);
}
@Override
- public void leafSetEntryNode(final Object value) throws IOException, IllegalArgumentException {
+ public void leafSetEntryNode(final QName name, final Object value) throws IOException, IllegalArgumentException {
LOG.debug("Writing a new leaf set entry node");
output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
+
+ // lastLeafSetQName is set if the parent LeafSetNode was previously written. Otherwise this is a
+ // stand alone LeafSetEntryNode so write out it's name here.
+ if(lastLeafSetQName == null) {
+ writeQName(name);
+ }
+
writeObject(value);
}
@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");
}
@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");
}
@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");
}
@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");
}
@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");
}
@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);
}
@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");
}
@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");
}
@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");
}
@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
}
}
+ @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();
}
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);
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());
case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
- YangInstanceIdentifier.NodeWithValue nodeWithValue =
- (YangInstanceIdentifier.NodeWithValue) pathArgument;
+ NodeWithValue<?> nodeWithValue = (NodeWithValue<?>) pathArgument;
writeQName(nodeWithValue.getNodeType());
writeObject(nodeWithValue.getValue());
case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
- YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
- (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
+ AugmentationIdentifier augmentationIdentifier = (AugmentationIdentifier) pathArgument;
// No Qname in augmentation identifier
writeQNameSet(augmentationIdentifier.getPossibleChildNames());