BUG-2254 Enable schema-less rpcs invocation in netconf-connector 72/12672/1
authorMaros Marsalek <mmarsale@cisco.com>
Mon, 10 Nov 2014 13:20:52 +0000 (14:20 +0100)
committerMaros Marsalek <mmarsale@cisco.com>
Mon, 10 Nov 2014 13:20:52 +0000 (14:20 +0100)
Change-Id: Id84a2be3db050a546380186c79deadf5c9f7fd28
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java
opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/util/NetconfMessageTransformUtil.java
opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java [new file with mode: 0644]

index 5e61dfb028e33551c72c63e7b4ff89bcf1554119..fccbd715704fc634672edd0f9250628aba511606 100644 (file)
@@ -29,6 +29,7 @@ import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -77,11 +78,16 @@ public class NetconfMessageTransformer implements MessageTransformer<NetconfMess
                     final DataNodeContainer schemaForGetConfig = NetconfMessageTransformUtil.createSchemaForGetConfig(schemaContext.get());
                     w3cPayload = XmlDocumentUtils.toDocument(rpcPayload, schemaForGetConfig, codecProvider);
                 } else {
-                    final DataNodeContainer schemaForGetConfig = NetconfMessageTransformUtil.createSchemaForRpc(rpc, schemaContext.get());
-                    w3cPayload = XmlDocumentUtils.toDocument(rpcPayload, schemaForGetConfig, codecProvider);
+                    final Optional<RpcDefinition> schemaForRpc = NetconfMessageTransformUtil.findSchemaForRpc(rpc, schemaContext.get());
+                    if(schemaForRpc.isPresent()) {
+                        final DataNodeContainer schemaForGetConfig = NetconfMessageTransformUtil.createSchemaForRpc(schemaForRpc.get());
+                        w3cPayload = XmlDocumentUtils.toDocument(rpcPayload, schemaForGetConfig, codecProvider);
+                    } else {
+                        w3cPayload = toRpcRequestWithoutSchema(rpcPayload, codecProvider);
+                    }
                 }
             } else {
-                w3cPayload = XmlDocumentUtils.toDocument(rpcPayload, codecProvider);
+                w3cPayload = toRpcRequestWithoutSchema(rpcPayload, codecProvider);
             }
         } catch (final UnsupportedDataTypeException e) {
             throw new IllegalArgumentException("Unable to create message", e);
@@ -90,6 +96,10 @@ public class NetconfMessageTransformer implements MessageTransformer<NetconfMess
         return new NetconfMessage(w3cPayload);
     }
 
+    private Document toRpcRequestWithoutSchema(final CompositeNodeTOImpl rpcPayload, final XmlCodecProvider codecProvider) {
+        return XmlDocumentUtils.toDocument(rpcPayload, codecProvider);
+    }
+
     @Override
     public synchronized RpcResult<CompositeNode> toRpcResult(final NetconfMessage message, final QName rpc) {
         if(schemaContext.isPresent()) {
index 6117a216cdf1d4a79861ab775ebe985fb3ab2b6c..efc020ecd0044ef4e8d13f081611972e20db1efa 100644 (file)
@@ -7,14 +7,6 @@
  */
 package org.opendaylight.controller.sal.connect.netconf.util;
 
-import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Sets;
-
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -33,9 +25,9 @@ import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
@@ -48,6 +40,15 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Sets;
+
 public class NetconfMessageTransformUtil {
 
     public static final String MESSAGE_ID_ATTR = "message-id";
@@ -326,6 +327,20 @@ public class NetconfMessageTransformUtil {
         return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
     }
 
+
+    public static Optional<RpcDefinition> findSchemaForRpc(final QName rpcName, final SchemaContext schemaContext) {
+        Preconditions.checkNotNull(rpcName);
+        Preconditions.checkNotNull(schemaContext);
+
+        for (final RpcDefinition rpcDefinition : schemaContext.getOperations()) {
+            if(rpcDefinition.getQName().equals(rpcName)) {
+                return Optional.of(rpcDefinition);
+            }
+        }
+
+        return Optional.absent();
+    }
+
     /**
      * Creates artificial schema node for schema defined rpc. This artificial schema looks like:
      * <pre>
@@ -341,18 +356,9 @@ public class NetconfMessageTransformUtil {
      * This makes the translation of schema defined rpc request
      * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
      */
-    public static DataNodeContainer createSchemaForRpc(final QName rpcName, final SchemaContext schemaContext) {
-        Preconditions.checkNotNull(rpcName);
-        Preconditions.checkNotNull(schemaContext);
-
-        for (final RpcDefinition rpcDefinition : schemaContext.getOperations()) {
-            if(rpcDefinition.getQName().equals(rpcName)) {
-                final NodeContainerProxy rpcBodyProxy = new NodeContainerProxy(rpcName, rpcDefinition.getInput().getChildNodes());
-                return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(rpcBodyProxy));
-            }
-        }
-
-        throw new IllegalArgumentException("Rpc " + rpcName + " not found in schema context " + schemaContext + ". Unable to invoke Rpc");
+    public static DataNodeContainer createSchemaForRpc(final RpcDefinition rpcDefinition) {
+        final NodeContainerProxy rpcBodyProxy = new NodeContainerProxy(rpcDefinition.getQName(), rpcDefinition.getInput().getChildNodes());
+        return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(rpcBodyProxy));
     }
 
     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java b/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java
new file mode 100644 (file)
index 0000000..5a2c97c
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * 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.sal.connect.netconf.schema.mapping;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+import org.opendaylight.controller.sal.connect.netconf.NetconfToRpcRequestTest;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class NetconfMessageTransformerTest {
+
+    private static final QName COMMIT_Q_NAME = QName.create("namespace", "2012-12-12", "commit");
+
+    @Test
+    public void testToRpcRequestNoSchemaForRequest() throws Exception {
+        final NetconfMessageTransformer netconfMessageTransformer = getTransformer();
+        final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(COMMIT_Q_NAME,
+                NodeFactory.createImmutableCompositeNode(COMMIT_Q_NAME, null, Collections.<Node<?>>emptyList()));
+        assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<commit"));
+    }
+
+    private NetconfMessageTransformer getTransformer() {
+        final NetconfMessageTransformer netconfMessageTransformer = new NetconfMessageTransformer();
+        netconfMessageTransformer.onGlobalContextUpdated(getSchema());
+        return netconfMessageTransformer;
+    }
+
+    @Test
+    public void testToRpcResultNoSchemaForResult() throws Exception {
+        final NetconfMessageTransformer netconfMessageTransformer = getTransformer();
+        final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
+                "<rpc-reply><ok/></rpc-reply>"
+        ));
+        final RpcResult<CompositeNode> compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, COMMIT_Q_NAME);
+        assertTrue(compositeNodeRpcResult.isSuccessful());
+        assertEquals("ok", compositeNodeRpcResult.getResult().getValue().get(0).getKey().getLocalName());
+    }
+
+    public SchemaContext getSchema() {
+        final List<InputStream> modelsToParse = Collections
+                .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
+        final YangParserImpl parser = new YangParserImpl();
+        final Set<Module> configModules = parser.parseYangModelsFromStreams(modelsToParse);
+        final SchemaContext cfgCtx = parser.resolveSchemaContext(configModules);
+        assertNotNull(cfgCtx);
+        return cfgCtx;
+    }
+}