return;
}
+ @SuppressWarnings("unchecked")
final InstanceIdentifierContext<SchemaNode> context = (InstanceIdentifierContext<SchemaNode>) t.getInstanceIdentifierContext();
SchemaPath path = context.getSchemaNode().getPath();
- boolean isDataRoot = false;
+ final JsonWriter jsonWriter = createJsonWriter(entityStream);
+ jsonWriter.beginObject();
+ writeNormalizedNode(jsonWriter,path,context,data);
+ jsonWriter.endObject();
+ jsonWriter.flush();
+ }
+
+ private void writeNormalizedNode(JsonWriter jsonWriter, SchemaPath path,
+ InstanceIdentifierContext<SchemaNode> context, NormalizedNode<?, ?> data) throws IOException {
+ final NormalizedNodeWriter nnWriter;
if (SchemaPath.ROOT.equals(path)) {
- isDataRoot = true;
+ /*
+ * Creates writer without initialNs and we write children of root data container
+ * which is not visible in restconf
+ */
+ nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
+ writeChildren(nnWriter,(ContainerNode) data);
} else if (context.getSchemaNode() instanceof RpcDefinition) {
- isDataRoot = true;
+ /*
+ * RpcDefinition is not supported as initial codec in JSONStreamWriter,
+ * so we need to emit initial output declaratation..
+ */
path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath();
+ nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
+ jsonWriter.name("output");
+ jsonWriter.beginObject();
+ writeChildren(nnWriter, (ContainerNode) data);
+ jsonWriter.endObject();
} else {
path = path.getParent();
- // FIXME: Add proper handling of reading root.
- }
- final JsonWriter jsonWriter = createJsonWriter(entityStream);
- final NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
-
- jsonWriter.beginObject();
- if(isDataRoot) {
- writeDataRoot(nnWriter,(ContainerNode) data);
- } else {
if(data instanceof MapEntryNode) {
data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
}
+ nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
nnWriter.write(data);
}
-
nnWriter.flush();
- jsonWriter.endObject();
- jsonWriter.flush();
+ }
+
+ private void writeChildren(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
+ for(final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
+ nnWriter.write(child);
+ }
}
private NormalizedNodeWriter createNormalizedNodeWriter(final InstanceIdentifierContext<SchemaNode> context,
final SchemaNode schema = context.getSchemaNode();
final JSONCodecFactory codecs = getCodecFactory(context);
- URI initialNs = null;
- if ( ! (schema instanceof RpcDefinition) && (!((DataSchemaNode)schema).isAugmenting() && !(schema instanceof SchemaContext))) {
+ final URI initialNs;
+ if ((schema instanceof DataSchemaNode)
+ && !((DataSchemaNode)schema).isAugmenting()
+ && !(schema instanceof SchemaContext)) {
+ initialNs = schema.getQName().getNamespace();
+ } else if (schema instanceof RpcDefinition) {
initialNs = schema.getQName().getNamespace();
+ } else {
+ initialNs = null;
}
final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
return NormalizedNodeWriter.forStreamWriter(streamWriter);
}
- private JSONCodecFactory getCodecFactory(final InstanceIdentifierContext context) {
+ private JSONCodecFactory getCodecFactory(final InstanceIdentifierContext<?> context) {
// TODO: Performance: Cache JSON Codec factory and schema context
return JSONCodecFactory.create(context.getSchemaContext());
}
- private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
- for(final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
- nnWriter.write(child);
- }
- }
-
}
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
+import javax.xml.XMLConstants;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
NormalizedNode<?, ?> data = t.getData();
SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
- boolean isDataRoot = false;
+
+
+ writeNormalizedNode(xmlWriter,schemaPath,pathContext,data);
+ }
+
+ private void writeNormalizedNode(XMLStreamWriter xmlWriter, SchemaPath schemaPath,InstanceIdentifierContext<?> pathContext, NormalizedNode<?, ?> data) throws IOException {
+ final NormalizedNodeWriter nnWriter;
+ final SchemaContext schemaCtx = pathContext.getSchemaContext();
if (SchemaPath.ROOT.equals(schemaPath)) {
- isDataRoot = true;
+ nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath);
+ writeElements(xmlWriter, nnWriter, (ContainerNode) data);
} else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
- isDataRoot = true;
- schemaPath = ((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath();
- } else {
- schemaPath = schemaPath.getParent();
- }
-
- final NormalizedNodeStreamWriter jsonWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
- pathContext.getSchemaContext(), schemaPath);
- final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
- if (isDataRoot) {
- writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
+ nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, ((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath());
+ writeElements(xmlWriter, nnWriter, (ContainerNode) data);
} else {
+ nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath.getParent());
if (data instanceof MapEntryNode) {
// Restconf allows returning one list item. We need to wrap it
// in map node in order to serialize it properly
data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
}
nnWriter.write(data);
- nnWriter.flush();
}
+ nnWriter.flush();
+ }
+
+ private NormalizedNodeWriter createNormalizedNodeWriter(XMLStreamWriter xmlWriter,
+ SchemaContext schemaContext, SchemaPath schemaPath) {
+ NormalizedNodeStreamWriter xmlStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext, schemaPath);
+ return NormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
}
- private void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data)
+ private void writeElements(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data)
throws IOException {
try {
- final QName name = SchemaContext.NAME;
- xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
- for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
+ final QName name = data.getNodeType();
+ xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, name.getLocalName(), name.getNamespace().toString());
+ xmlWriter.writeDefaultNamespace(name.getNamespace().toString());
+ for(NormalizedNode<?,?> child : data.getValue()) {
nnWriter.write(child);
}
nnWriter.flush();
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import java.math.BigInteger;
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.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
private static final URI NAMESPACE_EVENT_SUBSCRIPTION_AUGMENT = URI.create("urn:sal:restconf:event:subscription");
- private static final Date EVENT_SUBSCRIPTION_AUGMENT_REVISION;
-
private static final String DATASTORE_PARAM_NAME = "datastore";
private static final String SCOPE_PARAM_NAME = "scope";
private static final QName NETCONF_BASE_QNAME;
+ private static final QNameModule SAL_REMOTE_AUGMENT;
+
+ private static final YangInstanceIdentifier.AugmentationIdentifier SAL_REMOTE_AUG_IDENTIFIER;
+
static {
try {
- EVENT_SUBSCRIPTION_AUGMENT_REVISION = new SimpleDateFormat("yyyy-MM-dd").parse("2014-07-08");
+ final Date eventSubscriptionAugRevision = new SimpleDateFormat("yyyy-MM-dd").parse("2014-07-08");
NETCONF_BASE_QNAME = QName.create(QNameModule.create(new URI(NETCONF_BASE), null), NETCONF_BASE_PAYLOAD_NAME );
+ SAL_REMOTE_AUGMENT = QNameModule.create(NAMESPACE_EVENT_SUBSCRIPTION_AUGMENT,
+ eventSubscriptionAugRevision);
+ SAL_REMOTE_AUG_IDENTIFIER = new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(QName.create(SAL_REMOTE_AUGMENT, "scope"),
+ QName.create(SAL_REMOTE_AUGMENT, "datastore")));
} catch (final ParseException e) {
throw new RestconfDocumentedException(
"It wasn't possible to convert revision date of sal-remote-augment to date", ErrorType.APPLICATION,
*/
private <T> T parseEnumTypeParameter(final ContainerNode value, final Class<T> classDescriptor,
final String paramName) {
- final QNameModule salRemoteAugment = QNameModule.create(NAMESPACE_EVENT_SUBSCRIPTION_AUGMENT,
- EVENT_SUBSCRIPTION_AUGMENT_REVISION);
- final Optional<DataContainerChild<? extends PathArgument, ?>> enumNode = value.getChild(new NodeIdentifier(
- QName.create(salRemoteAugment, paramName)));
+ final Optional<DataContainerChild<? extends PathArgument, ?>> augNode = value.getChild(SAL_REMOTE_AUG_IDENTIFIER);
+ if (!augNode.isPresent() && !(augNode instanceof AugmentationNode)) {
+ return null;
+ }
+ final Optional<DataContainerChild<? extends PathArgument, ?>> enumNode =
+ ((AugmentationNode) augNode.get()).getChild(new NodeIdentifier(QName.create(SAL_REMOTE_AUGMENT, paramName)));
if (!enumNode.isPresent()) {
return null;
}
package org.opendaylight.controller.netconf.notifications.impl.ops;
-import static org.junit.Assert.assertTrue;
-
import com.google.common.collect.Lists;
import java.io.IOException;
-import org.custommonkey.xmlunit.Diff;
-import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
-import org.opendaylight.controller.netconf.notifications.impl.ops.Get;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
final Document response = getBlankResponse();
Get.serializeStreamsSubtree(response, streams);
- final Diff diff = XMLUnit.compareXML(XmlUtil.toString(response),
+ NotificationsTransformUtilTest.compareXml(XmlUtil.toString(response),
"<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
- "<data>\n" +
- "<netconf xmlns=\"urn:ietf:params:xml:ns:netmod:notification\">\n" +
- "<streams>\n" +
- "<stream>\n" +
- "<name>base</name>\n" +
- "<description>description</description>\n" +
- "<replaySupport>false</replaySupport>\n" +
- "</stream>\n" +
- "</streams>\n" +
- "</netconf>\n" +
- "</data>\n" +
- "</rpc-reply>\n");
-
- assertTrue(diff.toString(), diff.identical());
+ "<data>\n" +
+ "<netconf xmlns=\"urn:ietf:params:xml:ns:netmod:notification\">\n" +
+ "<streams>\n" +
+ "<stream>\n" +
+ "<name>base</name>\n" +
+ "<description>description</description>\n" +
+ "<replaySupport>false</replaySupport>\n" +
+ "</stream>\n" +
+ "</streams>\n" +
+ "</netconf>\n" +
+ "</data>\n" +
+ "</rpc-reply>\n");
}
private Document getBlankResponse() throws IOException, SAXException {
package org.opendaylight.controller.netconf.notifications.impl.ops;
+import static org.junit.Assert.assertTrue;
+
import com.google.common.collect.Lists;
+import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
+import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
+import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
import org.junit.Test;
import org.opendaylight.controller.netconf.notifications.NetconfNotification;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
+import org.xml.sax.SAXException;
public class NotificationsTransformUtilTest {
private static final Date DATE = new Date();
private static final String innerNotification = "<netconf-capability-change xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-notifications\">" +
- "<deleted-capability>uri3</deleted-capability>" +
"<deleted-capability>uri4</deleted-capability>" +
+ "<deleted-capability>uri3</deleted-capability>" +
"<added-capability>uri1</added-capability>" +
"</netconf-capability-change>";
final NetconfCapabilityChangeBuilder netconfCapabilityChangeBuilder = new NetconfCapabilityChangeBuilder();
netconfCapabilityChangeBuilder.setAddedCapability(Lists.newArrayList(new Uri("uri1"), new Uri("uri1")));
- netconfCapabilityChangeBuilder.setDeletedCapability(Lists.newArrayList(new Uri("uri3"), new Uri("uri4")));
+ netconfCapabilityChangeBuilder.setDeletedCapability(Lists.newArrayList(new Uri("uri4"), new Uri("uri3")));
final NetconfCapabilityChange capabilityChange = netconfCapabilityChangeBuilder.build();
final NetconfNotification transform = NotificationsTransformUtil.transform(capabilityChange, DATE);
final String serialized = XmlUtil.toString(transform.getDocument());
+ compareXml(expectedNotification, serialized);
+ }
+
+ static void compareXml(final String expected, final String actual) throws SAXException, IOException {
XMLUnit.setIgnoreWhitespace(true);
- final Diff diff = XMLUnit.compareXML(expectedNotification, serialized);
- // FIXME the diff is unreliable, provide a proper comparison of XML
-// assertTrue(diff.toString(), diff.similar());
+ final Diff diff = new Diff(expected, actual);
+ final DetailedDiff detailedDiff = new DetailedDiff(diff);
+ detailedDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
+ assertTrue(detailedDiff.toString(), detailedDiff.similar());
}
@Test
final NetconfNotification netconfNotification = new NetconfNotification(XmlUtil.readXmlToDocument(innerNotification), DATE);
XMLUnit.setIgnoreWhitespace(true);
- final Diff diff = XMLUnit.compareXML(expectedNotification, netconfNotification.toString());
- // FIXME the diff is unreliable, provide a proper comparison of XML
-// assertTrue(diff.toString(), diff.similar());
+ compareXml(expectedNotification, netconfNotification.toString());
}
}
\ No newline at end of file