Merge "Remove RuntimeGeneratedMappingService from config-manager"
authorTony Tkacik <ttkacik@cisco.com>
Thu, 19 Mar 2015 12:26:19 +0000 (12:26 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 19 Mar 2015 12:26:20 +0000 (12:26 +0000)
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeXmlBodyWriter.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java
opendaylight/netconf/netconf-notifications-impl/src/test/java/org/opendaylight/controller/netconf/notifications/impl/ops/GetTest.java
opendaylight/netconf/netconf-notifications-impl/src/test/java/org/opendaylight/controller/netconf/notifications/impl/ops/NotificationsTransformUtilTest.java

index 4944d96b546933c49637fbb66b4b91ea8add05d1..5c17f2a14ab2558c4acd089e224828a9248530eb 100644 (file)
@@ -66,36 +66,54 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
             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,
@@ -104,9 +122,15 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
         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);
@@ -118,15 +142,9 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
 
     }
 
-    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);
-        }
-    }
-
 }
index f61ec4e1c0941abef0fee706962b6a732af9c858..f6b7027b34a9994d43c2d731a40a11bcf7551bb6 100644 (file)
@@ -18,6 +18,7 @@ import javax.ws.rs.core.MediaType;
 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;
@@ -27,9 +28,7 @@ import org.opendaylight.controller.sal.rest.api.RestconfService;
 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;
@@ -85,38 +84,45 @@ public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<Normalized
         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();
index 75c61d1c196d720401236dba043ec518d38b7fc4..814273ebc0dc6ba1205fa3a7c93dd97152e38034 100644 (file)
@@ -18,6 +18,7 @@ import com.google.common.base.Throwables;
 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;
@@ -74,6 +75,7 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceI
 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;
@@ -168,8 +170,6 @@ public class RestconfImpl implements RestconfService {
 
     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";
@@ -180,10 +180,18 @@ public class RestconfImpl implements RestconfService {
 
     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,
@@ -1325,10 +1333,12 @@ public class RestconfImpl implements RestconfService {
      */
     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;
         }
index 6f38f24f10c02e3e6dac9c7147fb24104f2ba9db..05ed9bf006f79c2cc7937da1e7064f5d76364d6b 100644 (file)
@@ -8,14 +8,9 @@
 
 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;
@@ -41,22 +36,20 @@ public class GetTest {
 
         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 {
index b63e0877b207ab66d55e5cbd92727f08ac73c762..7bb213ab46942578e4fdce191a5c7a3eb0436d79 100644 (file)
@@ -8,24 +8,30 @@
 
 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>";
 
@@ -39,17 +45,22 @@ public class NotificationsTransformUtilTest {
         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
@@ -57,9 +68,7 @@ public class NotificationsTransformUtilTest {
         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