Merge "Remove unused isMaster variable in ClusteredNetconfDevice"
authorTomas Cere <tcere@cisco.com>
Thu, 16 Jun 2016 13:51:45 +0000 (13:51 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 16 Jun 2016 13:51:45 +0000 (13:51 +0000)
48 files changed:
netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/AbstractNetconfTopology.java
netconf/netconf-topology/src/test/java/org/opendaylight/netconf/topology/AbstractNetconfTopologyTest.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDevice.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfStateSchemas.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/SchemalessNetconfDevice.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/SchemalessNetconfDeviceRpc.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/ReadOnlyTx.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/NetconfRemoteSchemaYangSourceProvider.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/BaseRpcSchemalessTransformer.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/BaseSchema.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/SchemalessMessageTransformer.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfBaseOps.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfMessageTransformUtil.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfRpcStructureTransformer.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/RpcStructureTransformer.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/SchemalessRpcStructureTransformer.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/main/yang/netconf-node-topology.yang
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfStateSchemasTest.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/SchemalessNetconfDeviceRpcTest.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/NetconfDeviceWriteOnlyTxTest.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/SchemalessRpcStructureTransformerTest.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/data/container.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list-bad-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list-compound-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/container.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list-bad-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list-compound-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/filter/container.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list-bad-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list-compound-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/container.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list-bad-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list-compound-key.xml [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list.xml [new file with mode: 0644]
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/Main.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/MdsalOperationProvider.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolNegotiationFactory.java
netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfDocumentedException.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfError.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/parser/builder/YangInstanceIdentifierDeserializer.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/parser/builder/YangInstanceIdentifierSerializer.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/parser/builder/ParserBuilderConstants.java

index c09dca9162846b272b48845bd36a25502fc9c769..477c3b661f36c544873cafb06c529ccf0cf12151 100644 (file)
@@ -35,6 +35,7 @@ import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 import org.opendaylight.controller.sal.core.api.Broker;
 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
 import org.opendaylight.controller.sal.core.api.Provider;
+import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.client.NetconfClientDispatcher;
 import org.opendaylight.netconf.client.NetconfClientSessionListener;
 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
@@ -42,11 +43,13 @@ import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurati
 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
+import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
 import org.opendaylight.netconf.sal.connect.netconf.LibraryModulesSchemas;
 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceBuilder;
 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemas;
+import org.opendaylight.netconf.sal.connect.netconf.SchemalessNetconfDevice;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
@@ -308,14 +311,18 @@ public abstract class AbstractNetconfTopology implements NetconfTopology, Bindin
         }
 
         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = setupSchemaCacheDTO(nodeId, node);
-
-        final NetconfDevice device = new NetconfDeviceBuilder()
-                .setReconnectOnSchemasChange(reconnectOnChangedSchema)
-                .setSchemaResourcesDTO(schemaResourcesDTO)
-                .setGlobalProcessingExecutor(processingExecutor.getExecutor())
-                .setId(remoteDeviceId)
-                .setSalFacade(salFacade)
-                .build();
+        final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> device;
+        if (node.isSchemaless()) {
+            device = new SchemalessNetconfDevice(remoteDeviceId, salFacade);
+        } else {
+            device = new NetconfDeviceBuilder()
+                    .setReconnectOnSchemasChange(reconnectOnChangedSchema)
+                    .setSchemaResourcesDTO(schemaResourcesDTO)
+                    .setGlobalProcessingExecutor(processingExecutor.getExecutor())
+                    .setId(remoteDeviceId)
+                    .setSalFacade(salFacade)
+                    .build();
+        }
 
         final Optional<NetconfSessionPreferences> userCapabilities = getUserCapabilities(node);
 
index 2e16cca1cb51e9b53e22b84191988e95d4782fed..6043e261e903db9bfbab014b39ea5d40296cc8b1 100644 (file)
@@ -111,6 +111,7 @@ public class AbstractNetconfTopologyTest {
                 .setReconnectOnChangedSchema(true)
                 .setDefaultRequestTimeoutMillis(1000L)
                 .setBetweenAttemptsTimeoutMillis(100)
+                .setSchemaless(false)
                 .build();
 
         AbstractNetconfTopology.NetconfConnectorDTO connectorDTO = topology.createDeviceCommunicator(NODE_ID, testingNode);
@@ -126,6 +127,7 @@ public class AbstractNetconfTopologyTest {
                 .setDefaultRequestTimeoutMillis(1000L)
                 .setBetweenAttemptsTimeoutMillis(100)
                 .setKeepaliveDelay(1L)
+                .setSchemaless(false)
                 .build();
 
         AbstractNetconfTopology.NetconfConnectorDTO connectorDTO = topology.createDeviceCommunicator(NODE_ID, testingNode);
@@ -222,6 +224,7 @@ public class AbstractNetconfTopologyTest {
                 .setBetweenAttemptsTimeoutMillis(100)
                 .setKeepaliveDelay(1000L)
                 .setTcpOnly(true)
+                .setSchemaless(false)
                 .setCredentials(new LoginPasswordBuilder().setUsername("testuser").setPassword("testpassword").build())
                 .build();
         Node nd = mock(Node.class);
@@ -240,6 +243,7 @@ public class AbstractNetconfTopologyTest {
                 .setBetweenAttemptsTimeoutMillis(100)
                 .setKeepaliveDelay(1000L)
                 .setTcpOnly(true)
+                .setSchemaless(false)
                 .setCredentials(new LoginPasswordBuilder().setUsername("testuser").setPassword("testpassword").build())
                 .build();
         Node nd = mock(Node.class);
index c5e36cfc44182490b078e0a6d171a0f10fb63511..4070c96750519ba3ae9f71bd7a720c5653db3861 100644 (file)
@@ -40,6 +40,7 @@ import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommun
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
+import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
@@ -93,9 +94,9 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
      * Create rpc implementation capable of handling RPC for monitoring and notifications even before the schemas of remote device are downloaded
      */
     static NetconfDeviceRpc getRpcForInitialization(final NetconfDeviceCommunicator listener, final boolean notificationSupport) {
-        NetconfMessageTransformer.BaseSchema baseSchema = notificationSupport ?
-                NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS :
-                NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX;
+        BaseSchema baseSchema = notificationSupport ?
+                BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS :
+                BaseSchema.BASE_NETCONF_CTX;
 
         return new NetconfDeviceRpc(baseSchema.getSchemaContext(), listener, new NetconfMessageTransformer(baseSchema.getSchemaContext(), false, baseSchema));
     }
@@ -192,10 +193,10 @@ public class NetconfDevice implements RemoteDevice<NetconfSessionPreferences, Ne
     }
 
     protected void handleSalInitializationSuccess(final SchemaContext result, final NetconfSessionPreferences remoteSessionCapabilities, final DOMRpcService deviceRpc) {
-        NetconfMessageTransformer.BaseSchema baseSchema =
+        BaseSchema baseSchema =
                 remoteSessionCapabilities.isNotificationsSupported() ?
-                NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS :
-                NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX;
+                BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS :
+                BaseSchema.BASE_NETCONF_CTX;
         messageTransformer = new NetconfMessageTransformer(result, true, baseSchema);
 
         updateTransformer(messageTransformer);
index 32d22be32f96afc7d3bbd5c7de7aea67651933d8..b738ed69de782c7daa767f04b312dffc0f3f282a 100644 (file)
@@ -27,7 +27,7 @@ import java.util.concurrent.ExecutionException;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
-import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
+import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
@@ -81,7 +81,7 @@ public final class NetconfStateSchemas {
     private static final ContainerNode GET_SCHEMAS_RPC;
     static {
         final DataContainerChild<?, ?> filter = NetconfMessageTransformUtil.toFilterStructure(STATE_SCHEMAS_IDENTIFIER,
-                NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext());
+                BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext());
         GET_SCHEMAS_RPC
                 = Builders.containerBuilder().withNodeIdentifier(toId(NETCONF_GET_QNAME)).withChild(filter).build();
     }
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/SchemalessNetconfDevice.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/SchemalessNetconfDevice.java
new file mode 100644 (file)
index 0000000..74e6435
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf;
+
+import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
+import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
+import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
+import org.opendaylight.netconf.sal.connect.netconf.sal.SchemalessNetconfDeviceRpc;
+import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
+import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
+
+public class SchemalessNetconfDevice implements
+        RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
+
+    private RemoteDeviceId id;
+    private RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
+
+    public SchemalessNetconfDevice(final RemoteDeviceId id,
+                                   final RemoteDeviceHandler<NetconfSessionPreferences> salFacade) {
+        this.id = id;
+        this.salFacade = salFacade;
+    }
+
+    @Override public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
+                                            final NetconfDeviceCommunicator netconfDeviceCommunicator) {
+
+        final SchemalessNetconfDeviceRpc schemalessNetconfDeviceRpc = new SchemalessNetconfDeviceRpc(id,
+                netconfDeviceCommunicator);
+
+        salFacade.onDeviceConnected(BaseSchema.BASE_NETCONF_CTX.getSchemaContext(),
+                remoteSessionCapabilities, schemalessNetconfDeviceRpc);
+
+    }
+
+    @Override public void onRemoteSessionDown() {
+        salFacade.onDeviceDisconnected();
+    }
+
+    @Override public void onRemoteSessionFailed(final Throwable throwable) {
+        salFacade.onDeviceFailed(throwable);
+    }
+
+    @Override public void onNotification(final NetconfMessage notification) {
+        // TODO support for notifications
+    }
+}
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/SchemalessNetconfDeviceRpc.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/SchemalessNetconfDeviceRpc.java
new file mode 100644 (file)
index 0000000..d610904
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf.sal;
+
+import com.google.common.base.Function;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
+import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
+import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
+import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
+import org.opendaylight.netconf.sal.connect.util.MessageCounter;
+import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
+ */
+public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
+
+    private final RemoteDeviceCommunicator<NetconfMessage> listener;
+    private final BaseRpcSchemalessTransformer baseRpcTransformer;
+    private final SchemalessMessageTransformer schemalessTransformer;
+    private final RemoteDeviceId deviceId;
+
+    public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId,
+                                      final RemoteDeviceCommunicator<NetconfMessage> listener) {
+        this.deviceId = deviceId;
+        this.listener = listener;
+        final MessageCounter counter = new MessageCounter();
+        baseRpcTransformer = new BaseRpcSchemalessTransformer(counter);
+        schemalessTransformer = new SchemalessMessageTransformer(counter);
+    }
+
+    @Nonnull
+    @Override
+    public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
+                                                                  @Nullable final NormalizedNode<?, ?> input) {
+        final MessageTransformer<NetconfMessage> transformer;
+        if (input instanceof AnyXmlNode) {
+            transformer = schemalessTransformer;
+        } else if (isBaseRpc(type)) {
+            transformer = baseRpcTransformer;
+        } else {
+            return Futures.immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("Unable to invoke rpc %s", type));
+        }
+        return handleRpc(type, input, transformer);
+    }
+
+    private CheckedFuture<DOMRpcResult, DOMRpcException> handleRpc(@Nonnull final SchemaPath type,
+                                                                   @Nullable final NormalizedNode<?, ?> input,
+                                                                   final MessageTransformer<NetconfMessage> transformer) {
+        final NetconfMessage netconfMessage = transformer.toRpcRequest(type, input);
+        final ListenableFuture<RpcResult<NetconfMessage>> rpcResultListenableFuture = listener.sendRequest(netconfMessage, type.getLastComponent());
+
+        final ListenableFuture<DOMRpcResult> transformed = Futures.transform(rpcResultListenableFuture, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
+            @Override
+            public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
+                if (input.isSuccessful()) {
+                    return transformer.toRpcResult(input.getResult(), type);
+                } else {
+                    return new DefaultDOMRpcResult(input.getErrors());
+                }
+            }
+        });
+
+        return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
+            @Nullable
+            @Override
+            public DOMRpcException apply(@Nullable final Exception e) {
+                return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s on device %s", type, deviceId);
+            }
+        });
+    }
+
+
+    private boolean isBaseRpc(final SchemaPath type) {
+        return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getLastComponent().getNamespace());
+    }
+
+    @Nonnull
+    @Override
+    public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
+        throw new UnsupportedOperationException("Not available for netconf 1.0");
+    }
+
+}
index dda54851094e612bf97a86c3020349427a384e8f..8087fde8b709a95dc9c4d8e1f22c24a98033fc35 100644 (file)
@@ -8,9 +8,7 @@
 
 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
@@ -21,14 +19,10 @@ import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
-import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-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.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -66,50 +60,16 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
 
     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
             final YangInstanceIdentifier path) {
-        final ListenableFuture<DOMRpcResult> configRunning = netconfOps.getConfigRunning(loggingCallback, Optional.fromNullable(path));
+        final ListenableFuture<Optional<NormalizedNode<?, ?>>> configRunning = netconfOps.getConfigRunningData(loggingCallback, Optional.fromNullable(path));
 
-        final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configRunning, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
-            @Override
-            public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
-                checkReadSuccess(result, path);
-
-                final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
-                return NormalizedNodes.findNode(dataNode, path.getPathArguments());
-            }
-        });
-
-        return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
-    }
-
-    private DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> findDataNode(final DOMRpcResult result) {
-        return ((ContainerNode) result.getResult()).getChild(NetconfMessageTransformUtil.toId(NetconfMessageTransformUtil.NETCONF_DATA_QNAME)).get();
-    }
-
-    private void checkReadSuccess(final DOMRpcResult result, final YangInstanceIdentifier path) {
-        try {
-            Preconditions.checkArgument(AbstractWriteTx.isSuccess(result), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
-        } catch (final IllegalArgumentException e) {
-            LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
-            throw e;
-        }
+        return MappingCheckedFuture.create(configRunning, ReadFailedException.MAPPER);
     }
 
     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
             final YangInstanceIdentifier path) {
-        final ListenableFuture<DOMRpcResult> configCandidate = netconfOps.get(loggingCallback, Optional.fromNullable(path));
-
-        // Find data node and normalize its content
-        final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configCandidate, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
-            @Override
-            public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
-                checkReadSuccess(result, path);
-
-                final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
-                return NormalizedNodes.findNode(dataNode, path.getPathArguments());
-            }
-        });
+        final ListenableFuture<Optional<NormalizedNode<?, ?>>> configCandidate = netconfOps.getData(loggingCallback, Optional.fromNullable(path));
 
-        return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
+        return MappingCheckedFuture.create(configCandidate, ReadFailedException.MAPPER);
     }
 
     @Override
index 3a65ce74324f2b7c80e277085e2a86f4279e991a..bd29ef4395439992c9e77a498a36fd2c465d39d7 100644 (file)
@@ -31,6 +31,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.mon
 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
@@ -67,26 +68,23 @@ public final class NetconfRemoteSchemaYangSourceProvider implements SchemaSource
         this.rpc = Preconditions.checkNotNull(rpc);
     }
 
-    public static ContainerNode createGetSchemaRequest(final String moduleName, final Optional<String> revision) {
-        final QName identifierQName = QName.cachedReference(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "identifier"));
-        final YangInstanceIdentifier.NodeIdentifier identifierId = new YangInstanceIdentifier.NodeIdentifier(identifierQName);
-        final LeafNode<String> identifier = Builders.<String>leafBuilder().withNodeIdentifier(identifierId).withValue(moduleName).build();
-
-        final QName formatQName = QName.cachedReference(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "format"));
-        final YangInstanceIdentifier.NodeIdentifier formatId = new YangInstanceIdentifier.NodeIdentifier(formatQName);
-        final LeafNode<QName> format = Builders.<QName>leafBuilder().withNodeIdentifier(formatId).withValue(Yang.QNAME).build();
+    private static final NodeIdentifier FORMAT_PATHARG = new NodeIdentifier(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "format").intern());
+    private static final NodeIdentifier GET_SCHEMA_PATHARG = new NodeIdentifier(NetconfMessageTransformUtil.GET_SCHEMA_QNAME);
+    private static final NodeIdentifier IDENTIFIER_PATHARG = new NodeIdentifier(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "identifier").intern());
+    private static final NodeIdentifier VERSION_PATHARG = new NodeIdentifier(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "version").intern());
 
-        final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder = Builders.containerBuilder();
+    private static final LeafNode<?> FORMAT_LEAF = Builders.leafBuilder().withNodeIdentifier(FORMAT_PATHARG).withValue(Yang.QNAME).build();
 
-        builder.withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.GET_SCHEMA_QNAME))
-                .withChild(identifier).withChild(format);
+    private static final QName NETCONF_DATA = QName.create(GET_SCHEMA_QNAME, NETCONF_DATA_QNAME.getLocalName()).intern();
+    private static final NodeIdentifier NETCONF_DATA_PATHARG = toId(NETCONF_DATA);
 
-        if(revision.isPresent()) {
-            final QName revisionQName = QName.cachedReference(QName.create(NetconfMessageTransformUtil.GET_SCHEMA_QNAME, "version"));
-            final YangInstanceIdentifier.NodeIdentifier revisionId = new YangInstanceIdentifier.NodeIdentifier(revisionQName);
-            final LeafNode<String> revisionNode = Builders.<String>leafBuilder().withNodeIdentifier(revisionId).withValue(revision.get()).build();
+    public static ContainerNode createGetSchemaRequest(final String moduleName, final Optional<String> revision) {
+        final LeafNode<?> identifier = Builders.leafBuilder().withNodeIdentifier(IDENTIFIER_PATHARG).withValue(moduleName).build();
+        final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> builder = Builders.containerBuilder()
+                .withNodeIdentifier(GET_SCHEMA_PATHARG).withChild(identifier).withChild(FORMAT_LEAF);
 
-            builder.withChild(revisionNode);
+        if (revision.isPresent()) {
+            builder.withChild(Builders.leafBuilder().withNodeIdentifier(VERSION_PATHARG).withValue(revision.get()).build());
         }
 
         return builder.build();
@@ -97,12 +95,11 @@ public final class NetconfRemoteSchemaYangSourceProvider implements SchemaSource
             return Optional.absent();
         }
 
-        final QName schemaWrapperNode = QName.cachedReference(QName.create(GET_SCHEMA_QNAME, NETCONF_DATA_QNAME.getLocalName()));
-        final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = ((ContainerNode) result).getChild(toId(schemaWrapperNode));
+        final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = ((ContainerNode) result).getChild(NETCONF_DATA_PATHARG);
 
         Preconditions.checkState(child.isPresent() && child.get() instanceof AnyXmlNode,
                 "%s Unexpected response to get-schema, expected response with one child %s, but was %s", id,
-                schemaWrapperNode, result);
+                NETCONF_DATA, result);
 
         final DOMSource wrappedNode = ((AnyXmlNode) child.get()).getValue();
         Preconditions.checkNotNull(wrappedNode.getNode());
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/BaseRpcSchemalessTransformer.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/BaseRpcSchemalessTransformer.java
new file mode 100644 (file)
index 0000000..0448c70
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf.schema.mapping;
+
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.util.Map;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import org.opendaylight.controller.config.util.xml.XmlElement;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
+import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
+import org.opendaylight.netconf.sal.connect.util.MessageCounter;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Transforms base netconf rpcs
+ */
+public class BaseRpcSchemalessTransformer implements MessageTransformer<NetconfMessage> {
+
+    private static final Map<QName, RpcDefinition> mappedRpcs = BaseSchema.BASE_NETCONF_CTX.getMappedRpcs();
+    private static final SchemaContext schemaContext = BaseSchema.BASE_NETCONF_CTX.getSchemaContext();
+
+    private final MessageCounter counter;
+
+    public BaseRpcSchemalessTransformer(final MessageCounter counter) {
+        this.counter = counter;
+    }
+
+    @Override
+    public DOMNotification toNotification(final NetconfMessage message) {
+        throw new UnsupportedOperationException("Notifications not supported.");
+    }
+
+    @Override
+    public NetconfMessage toRpcRequest(final SchemaPath rpc, final NormalizedNode<?, ?> payload) {
+        // In case no input for rpc is defined, we can simply construct the payload here
+        final QName rpcQName = rpc.getLastComponent();
+
+        Preconditions.checkNotNull(mappedRpcs.get(rpcQName), "Unknown rpc %s, available rpcs: %s", rpcQName, mappedRpcs.keySet());
+        final DOMResult domResult = NetconfMessageTransformUtil.prepareDomResultForRpcRequest(rpcQName, counter);
+        if(mappedRpcs.get(rpcQName).getInput() == null) {
+            return new NetconfMessage(domResult.getNode().getOwnerDocument());
+        }
+
+        Preconditions.checkNotNull(payload, "Transforming an rpc with input: %s, payload cannot be null", rpcQName);
+        Preconditions.checkArgument(payload instanceof ContainerNode,
+                "Transforming an rpc with input: %s, payload has to be a container, but was: %s", rpcQName, payload);
+
+        // Set the path to the input of rpc for the payload stream writer
+        final SchemaPath inputPath = rpc.createChild(QName.create(rpcQName, "input").intern());
+        final DOMResult result = domResult;
+
+        try {
+            NetconfMessageTransformUtil.writeNormalizedRpc(((ContainerNode) payload), result, inputPath, schemaContext);
+        } catch (final XMLStreamException | IOException | IllegalStateException e) {
+            throw new IllegalStateException("Unable to serialize " + inputPath, e);
+        }
+
+        final Document node = result.getNode().getOwnerDocument();
+
+        return new NetconfMessage(node);
+    }
+
+    @Override
+    public DOMRpcResult toRpcResult(final NetconfMessage message, final SchemaPath rpc) {
+        final NormalizedNode<?, ?> normalizedNode;
+        final QName rpcQName = rpc.getLastComponent();
+        if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpcQName)) {
+            final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
+            final Document data = XmlUtil.newDocument();
+            data.appendChild(data.importNode(xmlData, true));
+            AnyXmlNode xmlDataNode = Builders.anyXmlBuilder()
+                    .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_QNAME))
+                    .withValue(new DOMSource(data))
+                    .build();
+
+            normalizedNode = Builders.containerBuilder()
+                    .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
+                    .withChild(xmlDataNode).build();
+        } else {
+            //other base rpcs don't have any output, we can simply construct the payload here
+            Preconditions.checkArgument(isOkPresent(message.getDocument()),
+                    "Unexpected content in response of rpc: %s, %s", rpc.getLastComponent(), message);
+            normalizedNode = null;
+
+        }
+        return new DefaultDOMRpcResult(normalizedNode);
+    }
+
+    // FIXME this should go to some util class
+    static boolean isOkPresent(final Document doc) {
+        return XmlElement.fromDomDocument(doc).getOnlyChildElementWithSameNamespaceOptionally("ok").isPresent();
+    }
+}
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/BaseSchema.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/BaseSchema.java
new file mode 100644 (file)
index 0000000..10ec99e
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf.schema.mapping;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import java.util.List;
+import java.util.Map;
+import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
+import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public enum BaseSchema {
+
+    BASE_NETCONF_CTX(
+            Lists.newArrayList(
+                    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance(),
+                    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.$YangModuleInfoImpl.getInstance()
+            )
+    ),
+    BASE_NETCONF_CTX_WITH_NOTIFICATIONS(
+            Lists.newArrayList(
+                    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.$YangModuleInfoImpl.getInstance(),
+                    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.$YangModuleInfoImpl.getInstance(),
+                    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance(),
+                    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.$YangModuleInfoImpl.getInstance()
+            )
+    );
+
+    private final Map<QName, RpcDefinition> mappedRpcs;
+    private final SchemaContext schemaContext;
+
+    BaseSchema(final List<YangModuleInfo> modules) {
+        try {
+            final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
+            moduleInfoBackedContext.addModuleInfos(modules);
+            schemaContext = moduleInfoBackedContext.tryToCreateSchemaContext().get();
+            mappedRpcs = Maps.uniqueIndex(schemaContext.getOperations(), RpcDefinition::getQName);
+        } catch (final RuntimeException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+    }
+
+    Map<QName, RpcDefinition> getMappedRpcs() {
+        return mappedRpcs;
+    }
+
+    public SchemaContext getSchemaContext() {
+        return schemaContext;
+    }
+
+}
index 2a9b53e6c29dc0a7aa4d2dbeb03ea63785f0558a..bf8249fa8474b1cbe149b9401a2500e2358b7a2c 100644 (file)
@@ -8,13 +8,11 @@
 package org.opendaylight.netconf.sal.connect.netconf.schema.mapping;
 
 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.IETF_NETCONF_NOTIFICATIONS;
-import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RPC_QNAME;
 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_URI;
 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
 
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
@@ -23,15 +21,12 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
-import java.util.List;
 import java.util.Map;
 import javax.annotation.Nonnull;
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.dom.DOMResult;
 import org.opendaylight.controller.config.util.xml.MissingNameSpaceException;
 import org.opendaylight.controller.config.util.xml.XmlElement;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
@@ -40,19 +35,12 @@ import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
-import org.opendaylight.netconf.util.NetconfUtil;
-import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
-import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 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.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
-import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
@@ -67,51 +55,7 @@ import org.w3c.dom.Element;
 
 public class NetconfMessageTransformer implements MessageTransformer<NetconfMessage> {
 
-    public enum BaseSchema {
-
-        BASE_NETCONF_CTX(
-                Lists.newArrayList(
-                        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance(),
-                        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.$YangModuleInfoImpl.getInstance()
-                )
-        ),
-        BASE_NETCONF_CTX_WITH_NOTIFICATIONS(
-                Lists.newArrayList(
-                        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.$YangModuleInfoImpl.getInstance(),
-                        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.$YangModuleInfoImpl.getInstance(),
-                        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl.getInstance(),
-                        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.$YangModuleInfoImpl.getInstance()
-                )
-        );
-
-        private final Map<QName, RpcDefinition> mappedRpcs;
-        private final SchemaContext schemaContext;
-
-        BaseSchema(List<YangModuleInfo> modules) {
-            try {
-                final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
-                moduleInfoBackedContext.addModuleInfos(modules);
-                schemaContext = moduleInfoBackedContext.tryToCreateSchemaContext().get();
-                mappedRpcs = Maps.uniqueIndex(schemaContext.getOperations(), QNAME_FUNCTION);
-            } catch (final RuntimeException e) {
-                LOG.error("Unable to prepare schema context for base netconf ops", e);
-                throw new ExceptionInInitializerError(e);
-            }
-        }
-
-        private Map<QName, RpcDefinition> getMappedRpcs() {
-            return mappedRpcs;
-        }
-
-        public SchemaContext getSchemaContext() {
-            return schemaContext;
-        }
-    }
-
-    public static final String MESSAGE_ID_PREFIX = "m";
-
-    private static final Logger LOG= LoggerFactory.getLogger(NetconfMessageTransformer.class);
-
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageTransformer.class);
 
     private static final Function<SchemaNode, QName> QNAME_FUNCTION = new Function<SchemaNode, QName>() {
         @Override
@@ -196,7 +140,7 @@ public class NetconfMessageTransformer implements MessageTransformer<NetconfMess
 
         Preconditions.checkNotNull(currentMappedRpcs.get(rpcQName), "Unknown rpc %s, available rpcs: %s", rpcQName, currentMappedRpcs.keySet());
         if(currentMappedRpcs.get(rpcQName).getInput() == null) {
-            return new NetconfMessage(prepareDomResultForRpcRequest(rpcQName).getNode().getOwnerDocument());
+            return new NetconfMessage(NetconfMessageTransformUtil.prepareDomResultForRpcRequest(rpcQName, counter).getNode().getOwnerDocument());
         }
 
         Preconditions.checkNotNull(payload, "Transforming an rpc with input: %s, payload cannot be null", rpcQName);
@@ -205,13 +149,13 @@ public class NetconfMessageTransformer implements MessageTransformer<NetconfMess
 
         // Set the path to the input of rpc for the node stream writer
         rpc = rpc.createChild(QName.create(rpcQName, "input").intern());
-        final DOMResult result = prepareDomResultForRpcRequest(rpcQName);
+        final DOMResult result = NetconfMessageTransformUtil.prepareDomResultForRpcRequest(rpcQName, counter);
 
         try {
             // If the schema context for netconf device does not contain model for base netconf operations, use default pre build context with just the base model
             // This way operations like lock/unlock are supported even if the source for base model was not provided
             SchemaContext ctx = needToUseBaseCtx ? baseSchema.getSchemaContext() : schemaContext;
-            writeNormalizedRpc(((ContainerNode) payload), result, rpc, ctx);
+            NetconfMessageTransformUtil.writeNormalizedRpc(((ContainerNode) payload), result, rpc, ctx);
         } catch (final XMLStreamException | IOException | IllegalStateException e) {
             throw new IllegalStateException("Unable to serialize " + rpc, e);
         }
@@ -227,38 +171,6 @@ public class NetconfMessageTransformer implements MessageTransformer<NetconfMess
                 rpc.getNamespace().equals(NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME.getNamespace());
     }
 
-    private DOMResult prepareDomResultForRpcRequest(final QName rpcQName) {
-        final Document document = XmlUtil.newDocument();
-        final Element rpcNS = document.createElementNS(NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName());
-        // set msg id
-        rpcNS.setAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX));
-        final Element elementNS = document.createElementNS(rpcQName.getNamespace().toString(), rpcQName.getLocalName());
-        rpcNS.appendChild(elementNS);
-        document.appendChild(rpcNS);
-        return new DOMResult(elementNS);
-    }
-
-    private static void writeNormalizedRpc(final ContainerNode normalized, final DOMResult result,
-            final SchemaPath schemaPath, final SchemaContext baseNetconfCtx) throws IOException, XMLStreamException {
-        final XMLStreamWriter writer = NetconfUtil.XML_FACTORY.createXMLStreamWriter(result);
-        try {
-            try (final NormalizedNodeStreamWriter normalizedNodeStreamWriter =
-                    XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath)) {
-                try (final SchemaOrderedNormalizedNodeWriter normalizedNodeWriter =
-                        new SchemaOrderedNormalizedNodeWriter(normalizedNodeStreamWriter, baseNetconfCtx, schemaPath)) {
-                    Collection<DataContainerChild<?, ?>> value = normalized.getValue();
-                    normalizedNodeWriter.write(value);
-                    normalizedNodeWriter.flush();
-                }
-            }
-        } finally {
-            try {
-                writer.close();
-            } catch (final Exception e) {
-                LOG.warn("Unable to close resource properly", e);
-            }
-        }
-    }
 
     @Override
     public synchronized DOMRpcResult toRpcResult(final NetconfMessage message, final SchemaPath rpc) {
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/SchemalessMessageTransformer.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/schema/mapping/SchemalessMessageTransformer.java
new file mode 100644 (file)
index 0000000..04c5023
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf.schema.mapping;
+
+import javax.xml.transform.dom.DOMSource;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
+import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
+import org.opendaylight.netconf.sal.connect.util.MessageCounter;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Transforms anyxml rpcs for schemaless netconf devices.
+ */
+public class SchemalessMessageTransformer implements MessageTransformer<NetconfMessage> {
+
+    private static final YangInstanceIdentifier.NodeIdentifier REPLY_ID =
+            new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME);
+
+    private final MessageCounter counter;
+
+    public SchemalessMessageTransformer(final MessageCounter counter) {
+        this.counter = counter;
+    }
+
+    @Override
+    public DOMNotification toNotification(final NetconfMessage message) {
+        //TODO add support for notifications
+        throw new UnsupportedOperationException("Notifications not supported.");
+    }
+
+    @Override
+    public NetconfMessage toRpcRequest(final SchemaPath rpc, final NormalizedNode<?, ?> input) {
+        final DOMSource payload = (DOMSource) input.getValue();
+        wrapPayload((Document) payload.getNode());
+        return new NetconfMessage(((Document) ((AnyXmlNode) input).getValue().getNode()));
+    }
+
+    /**
+     * Transforms reply message to anyXml node. In case, that rpc-reply doesn't contain data and contains only &lt;ok/&gt;
+     * element, returns null.
+     * @param rpcReply reply message
+     * @return anyxml
+     */
+    @Override
+    public DOMRpcResult toRpcResult(final NetconfMessage rpcReply, final SchemaPath rpc) {
+        final Document document = rpcReply.getDocument();
+        final AnyXmlNode result;
+        if(BaseRpcSchemalessTransformer.isOkPresent(document)) {
+            result =  null;
+        } else {
+            result = Builders.anyXmlBuilder()
+                    .withNodeIdentifier(REPLY_ID)
+                    .withValue(new DOMSource(rpcReply.getDocument()))
+                    .build();
+        }
+        return new DefaultDOMRpcResult(result);
+    }
+
+    private void wrapPayload(final Document doc) {
+        final Element payload = doc.getDocumentElement();
+        doc.removeChild(payload);
+        final Element rpcNS = doc.createElementNS(NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getNamespace().toString(),
+                NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getLocalName());
+        // set msg id
+        rpcNS.setAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR, counter.getNewMessageId(NetconfMessageTransformUtil.MESSAGE_ID_PREFIX));
+        rpcNS.appendChild(payload);
+        doc.appendChild(rpcNS);
+    }
+}
index 3362f0607fd1ef1df21ea61c74f1d0e9cda868ba..de61ee9073e467a57601d88001869a58adb3a4ad 100644 (file)
@@ -27,6 +27,7 @@ import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTr
 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
 
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.FutureCallback;
@@ -34,11 +35,14 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.netconf.sal.connect.netconf.sal.SchemalessNetconfDeviceRpc;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.copy.config.input.target.ConfigTarget;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.edit.config.input.EditContent;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.get.config.input.source.ConfigSource;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
 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.NormalizedNode;
@@ -54,10 +58,16 @@ public final class NetconfBaseOps {
 
     private final DOMRpcService rpc;
     private final SchemaContext schemaContext;
+    private final RpcStructureTransformer transformer;
 
     public NetconfBaseOps(final DOMRpcService rpc, final SchemaContext schemaContext) {
         this.rpc = rpc;
         this.schemaContext = schemaContext;
+        if (rpc instanceof SchemalessNetconfDeviceRpc) {
+            this.transformer = new SchemalessRpcStructureTransformer();
+        } else {
+            this.transformer = new NetconfRpcStructureTransformer(schemaContext);
+        }
     }
 
     public ListenableFuture<DOMRpcResult> lock(final FutureCallback<DOMRpcResult> callback, final QName datastore) {
@@ -157,7 +167,7 @@ public final class NetconfBaseOps {
 
         final ListenableFuture<DOMRpcResult> future;
         if (isFilterPresent(filterPath)) {
-            final DataContainerChild<?, ?> node = toFilterStructure(filterPath.get(), schemaContext);
+            final DataContainerChild<?, ?> node = transformer.toFilterStructure(filterPath.get());
             future = rpc.invokeRpc(toPath(NETCONF_GET_CONFIG_QNAME),
                             NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, getSourceNode(datastore), node));
         } else {
@@ -169,6 +179,31 @@ public final class NetconfBaseOps {
         return future;
     }
 
+    public ListenableFuture<Optional<NormalizedNode<?, ?>>> getConfigRunningData(final FutureCallback<DOMRpcResult> callback,
+                                                                                 final Optional<YangInstanceIdentifier> filterPath) {
+        final ListenableFuture<DOMRpcResult> configRunning = getConfigRunning(callback, filterPath);
+        return extractData(filterPath, configRunning);
+    }
+
+    public ListenableFuture<Optional<NormalizedNode<?, ?>>> getData(final FutureCallback<DOMRpcResult> callback,
+                                                                    final Optional<YangInstanceIdentifier> filterPath) {
+        final ListenableFuture<DOMRpcResult> configRunning = get(callback, filterPath);
+        return extractData(filterPath, configRunning);
+    }
+
+    private ListenableFuture<Optional<NormalizedNode<?, ?>>> extractData(final Optional<YangInstanceIdentifier> path,
+                                                                         final ListenableFuture<DOMRpcResult> configRunning) {
+        return Futures.transform(configRunning, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
+            @Override
+            public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
+                Preconditions.checkArgument(result.getErrors().isEmpty(), "Unable to read data: %s, errors: %s", path, result.getErrors());
+                final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode =
+                        ((ContainerNode) result.getResult()).getChild(NetconfMessageTransformUtil.toId(NetconfMessageTransformUtil.NETCONF_DATA_QNAME)).get();
+                return transformer.selectFromDataStructure(dataNode, path.get());
+            }
+        });
+    }
+
     public ListenableFuture<DOMRpcResult> getConfigRunning(final FutureCallback<DOMRpcResult> callback, final Optional<YangInstanceIdentifier> filterPath) {
         return getConfig(callback, NETCONF_RUNNING_QNAME, filterPath);
     }
@@ -222,7 +257,8 @@ public final class NetconfBaseOps {
     }
 
     public DataContainerChild<?, ?> createEditConfigStrcture(final Optional<NormalizedNode<?, ?>> lastChild, final Optional<ModifyAction> operation, final YangInstanceIdentifier dataPath) {
-        return NetconfMessageTransformUtil.createEditConfigStructure(schemaContext, dataPath, operation, lastChild);
+        final AnyXmlNode configContent = transformer.createEditConfigStructure(lastChild, dataPath, operation);
+        return Builders.choiceBuilder().withNodeIdentifier(toId(EditContent.QNAME)).withChild(configContent).build();
     }
 
     private ContainerNode getEditConfigContent(final QName datastore, final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation, final boolean rollback) {
index aa60efe08b8bea9bd6b7166698e619c7c3c0b08e..cebcfb53f1c13b54fa96db82b76d6877dc9c0519 100644 (file)
@@ -16,12 +16,14 @@ import java.net.URI;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.AbstractMap;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
@@ -30,6 +32,7 @@ import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.notifications.NetconfNotification;
+import org.opendaylight.netconf.sal.connect.util.MessageCounter;
 import org.opendaylight.netconf.util.NetconfUtil;
 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.edit.config.input.EditContent;
@@ -42,12 +45,17 @@ import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
 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.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
@@ -60,11 +68,12 @@ import org.w3c.dom.Element;
 
 public class NetconfMessageTransformUtil {
 
-    private static final Logger LOG= LoggerFactory.getLogger(NetconfMessageTransformUtil.class);
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageTransformUtil.class);
 
+    public static final String MESSAGE_ID_PREFIX = "m";
     public static final String MESSAGE_ID_ATTR = "message-id";
 
-    public static final QName CREATE_SUBSCRIPTION_RPC_QNAME = QName.cachedReference(QName.create(CreateSubscriptionInput.QNAME, "create-subscription"));
+    public static final QName CREATE_SUBSCRIPTION_RPC_QNAME = QName.create(CreateSubscriptionInput.QNAME, "create-subscription").intern();
     private static final String SUBTREE = "subtree";
 
     // Blank document used for creation of new DOM nodes
@@ -73,41 +82,41 @@ public class NetconfMessageTransformUtil {
 
     private NetconfMessageTransformUtil() {}
 
-    public static final QName IETF_NETCONF_MONITORING = QName.create(NetconfState.QNAME, "ietf-netconf-monitoring");
-    public static final QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data");
-    public static final QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema");
-    public static final QName IETF_NETCONF_MONITORING_SCHEMA_FORMAT = QName.create(IETF_NETCONF_MONITORING, "format");
-    public static final QName IETF_NETCONF_MONITORING_SCHEMA_LOCATION = QName.create(IETF_NETCONF_MONITORING, "location");
-    public static final QName IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER = QName.create(IETF_NETCONF_MONITORING, "identifier");
-    public static final QName IETF_NETCONF_MONITORING_SCHEMA_VERSION = QName.create(IETF_NETCONF_MONITORING, "version");
-    public static final QName IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE = QName.create(IETF_NETCONF_MONITORING, "namespace");
+    public static final QName IETF_NETCONF_MONITORING = QName.create(NetconfState.QNAME, "ietf-netconf-monitoring").intern();
+    public static final QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data").intern();
+    public static final QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema").intern();
+    public static final QName IETF_NETCONF_MONITORING_SCHEMA_FORMAT = QName.create(IETF_NETCONF_MONITORING, "format").intern();
+    public static final QName IETF_NETCONF_MONITORING_SCHEMA_LOCATION = QName.create(IETF_NETCONF_MONITORING, "location").intern();
+    public static final QName IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER = QName.create(IETF_NETCONF_MONITORING, "identifier").intern();
+    public static final QName IETF_NETCONF_MONITORING_SCHEMA_VERSION = QName.create(IETF_NETCONF_MONITORING, "version").intern();
+    public static final QName IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE = QName.create(IETF_NETCONF_MONITORING, "namespace").intern();
 
-    public static final QName IETF_NETCONF_NOTIFICATIONS = QName.create(NetconfCapabilityChange.QNAME, "ietf-netconf-notifications");
+    public static final QName IETF_NETCONF_NOTIFICATIONS = QName.create(NetconfCapabilityChange.QNAME, "ietf-netconf-notifications").intern();
 
-    public static final QName NETCONF_QNAME = QName.cachedReference(QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "netconf"));
+    public static final QName NETCONF_QNAME = QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "netconf").intern();
     public static final URI NETCONF_URI = NETCONF_QNAME.getNamespace();
 
-    public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data");
-    public static final QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply");
-    public static final QName NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok");
-    public static final QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option");
-    public static final QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running");
-    public static final QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source");
-    public static final QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate");
-    public static final QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target");
-    public static final QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config");
-    public static final QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit");
-    public static final QName NETCONF_VALIDATE_QNAME = QName.create(NETCONF_QNAME, "validate");
-    public static final QName NETCONF_COPY_CONFIG_QNAME = QName.create(NETCONF_QNAME, "copy-config");
-    public static final QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation");
-    public static final QName NETCONF_DEFAULT_OPERATION_QNAME = QName.create(NETCONF_OPERATION_QNAME, "default-operation");
-    public static final QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config");
+    public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data").intern();
+    public static final QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply").intern();
+    public static final QName NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok").intern();
+    public static final QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option").intern();
+    public static final QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running").intern();
+    public static final QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source").intern();
+    public static final QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate").intern();
+    public static final QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target").intern();
+    public static final QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config").intern();
+    public static final QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit").intern();
+    public static final QName NETCONF_VALIDATE_QNAME = QName.create(NETCONF_QNAME, "validate").intern();
+    public static final QName NETCONF_COPY_CONFIG_QNAME = QName.create(NETCONF_QNAME, "copy-config").intern();
+    public static final QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation").intern();
+    public static final QName NETCONF_DEFAULT_OPERATION_QNAME = QName.create(NETCONF_OPERATION_QNAME, "default-operation").intern();
+    public static final QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config").intern();
     public static final QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
     public static final QName NETCONF_DISCARD_CHANGES_QNAME = QName.create(NETCONF_QNAME, "discard-changes");
-    public static final QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type");
-    public static final QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter");
-    public static final QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get");
-    public static final QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc");
+    public static final QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type").intern();
+    public static final QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter").intern();
+    public static final QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get").intern();
+    public static final QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc").intern();
 
     public static final URI NETCONF_ROLLBACK_ON_ERROR_URI = URI
             .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0");
@@ -122,29 +131,29 @@ public class NetconfMessageTransformUtil {
     public static final URI NETCONF_RUNNING_WRITABLE_URI = URI
             .create("urn:ietf:params:netconf:capability:writable-running:1.0");
 
-    public static final QName NETCONF_LOCK_QNAME = QName.create(NETCONF_QNAME, "lock");
-    public static final QName NETCONF_UNLOCK_QNAME = QName.create(NETCONF_QNAME, "unlock");
+    public static final QName NETCONF_LOCK_QNAME = QName.create(NETCONF_QNAME, "lock").intern();
+    public static final QName NETCONF_UNLOCK_QNAME = QName.create(NETCONF_QNAME, "unlock").intern();
 
     // Discard changes message
     public static final ContainerNode DISCARD_CHANGES_RPC_CONTENT =
-            Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_DISCARD_CHANGES_QNAME)).build();
+            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(NETCONF_DISCARD_CHANGES_QNAME)).build();
 
     // Commit changes message
     public static final ContainerNode COMMIT_RPC_CONTENT =
-            Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_COMMIT_QNAME)).build();
+            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(NETCONF_COMMIT_QNAME)).build();
 
     // Get message
     public static final ContainerNode GET_RPC_CONTENT =
-            Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_GET_QNAME)).build();
+            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(NETCONF_GET_QNAME)).build();
 
     // Create-subscription changes message
     public static final ContainerNode CREATE_SUBSCRIPTION_RPC_CONTENT =
-            Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CREATE_SUBSCRIPTION_RPC_QNAME)).build();
+            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(CREATE_SUBSCRIPTION_RPC_QNAME)).build();
 
     public static final DataContainerChild<?, ?> EMPTY_FILTER;
 
     static {
-        final NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME));
+        final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME));
         anyXmlBuilder.withAttributes(Collections.singletonMap(NETCONF_TYPE_QNAME, SUBTREE));
 
         final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_FILTER_QNAME.getLocalName(), Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString()));
@@ -156,7 +165,7 @@ public class NetconfMessageTransformUtil {
     }
 
     public static DataContainerChild<?, ?> toFilterStructure(final YangInstanceIdentifier identifier, final SchemaContext ctx) {
-        final NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME));
+        final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME));
         anyXmlBuilder.withAttributes(Collections.singletonMap(NETCONF_TYPE_QNAME, SUBTREE));
 
         final NormalizedNode<?, ?> filterContent = ImmutableNodes.fromInstanceId(ctx, identifier);
@@ -241,12 +250,12 @@ public class NetconfMessageTransformUtil {
         }
     }
 
-    public static YangInstanceIdentifier.NodeIdentifier toId(final YangInstanceIdentifier.PathArgument qname) {
+    public static NodeIdentifier toId(final PathArgument qname) {
         return toId(qname.getNodeType());
     }
 
-    public static YangInstanceIdentifier.NodeIdentifier toId(final QName nodeType) {
-        return new YangInstanceIdentifier.NodeIdentifier(nodeType);
+    public static NodeIdentifier toId(final QName nodeType) {
+        return new NodeIdentifier(nodeType);
     }
 
     public static Element getDataSubtree(final Document doc) {
@@ -271,7 +280,7 @@ public class NetconfMessageTransformUtil {
         return Builders.containerBuilder().withNodeIdentifier(toId(name)).withValue(ImmutableList.copyOf(node)).build();
     }
 
-    public static DataContainerChild<?, ?> createEditConfigStructure(final SchemaContext ctx, final YangInstanceIdentifier dataPath,
+    public static AnyXmlNode createEditConfigAnyxml(final SchemaContext ctx, final YangInstanceIdentifier dataPath,
                                                                      final Optional<ModifyAction> operation, final Optional<NormalizedNode<?, ?>> lastChildOverride) {
         final NormalizedNode<?, ?> configContent;
 
@@ -294,8 +303,13 @@ public class NetconfMessageTransformUtil {
         }
         final DOMSource value = new DOMSource(element);
 
-        return Builders.choiceBuilder().withNodeIdentifier(toId(EditContent.QNAME)).withChild(
-                Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_CONFIG_QNAME)).withValue(value).build()).build();
+        return Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_CONFIG_QNAME)).withValue(value).build();
+    }
+
+    public static DataContainerChild<?, ?> createEditConfigStructure(final SchemaContext ctx, final YangInstanceIdentifier dataPath,
+                                                                     final Optional<ModifyAction> operation, final Optional<NormalizedNode<?, ?>> lastChildOverride) {
+        return Builders.choiceBuilder().withNodeIdentifier(toId(EditContent.QNAME))
+                .withChild(createEditConfigAnyxml(ctx, dataPath, operation, lastChildOverride)).build();
     }
 
     public static SchemaPath toPath(final QName rpc) {
@@ -358,4 +372,38 @@ public class NetconfMessageTransformUtil {
             return new AbstractMap.SimpleEntry<>(NetconfNotification.UNKNOWN_EVENT_TIME, notificationElement);
         }
     }
+
+    public static DOMResult prepareDomResultForRpcRequest(final QName rpcQName, final MessageCounter counter) {
+        final Document document = XmlUtil.newDocument();
+        final Element rpcNS =
+                document.createElementNS(NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName());
+        // set msg id
+        rpcNS.setAttribute(MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX));
+        final Element elementNS = document.createElementNS(rpcQName.getNamespace().toString(), rpcQName.getLocalName());
+        rpcNS.appendChild(elementNS);
+        document.appendChild(rpcNS);
+        return new DOMResult(elementNS);
+    }
+
+    public static void writeNormalizedRpc(final ContainerNode normalized, final DOMResult result,
+                                          final SchemaPath schemaPath, final SchemaContext baseNetconfCtx) throws IOException, XMLStreamException {
+        final XMLStreamWriter writer = NetconfUtil.XML_FACTORY.createXMLStreamWriter(result);
+        try {
+            try (final NormalizedNodeStreamWriter normalizedNodeStreamWriter =
+                    XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath)) {
+                try (final SchemaOrderedNormalizedNodeWriter normalizedNodeWriter =
+                        new SchemaOrderedNormalizedNodeWriter(normalizedNodeStreamWriter, baseNetconfCtx, schemaPath)) {
+                    Collection<DataContainerChild<?, ?>> value = normalized.getValue();
+                    normalizedNodeWriter.write(value);
+                    normalizedNodeWriter.flush();
+                }
+            }
+        } finally {
+            try {
+                writer.close();
+            } catch (final Exception e) {
+               LOG.warn("Unable to close resource properly", e);
+            }
+        }
+    }
 }
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfRpcStructureTransformer.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfRpcStructureTransformer.java
new file mode 100644 (file)
index 0000000..1572e20
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf.util;
+
+import com.google.common.base.Optional;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * Transforms rpc structures to normalized nodes and vice versa.
+ */
+class NetconfRpcStructureTransformer implements RpcStructureTransformer {
+
+    private final SchemaContext schemaContext;
+
+    NetconfRpcStructureTransformer(final SchemaContext schemaContext) {
+        this.schemaContext = schemaContext;
+    }
+
+    @Override
+    public Optional<NormalizedNode<?, ?>> selectFromDataStructure(final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> data,
+                                                                  final YangInstanceIdentifier path) {
+        return NormalizedNodes.findNode(data, path.getPathArguments());
+    }
+
+    @Override
+    public AnyXmlNode createEditConfigStructure(final Optional<NormalizedNode<?, ?>> data,
+                                                final YangInstanceIdentifier dataPath, Optional<ModifyAction> operation) {
+        return NetconfMessageTransformUtil.createEditConfigAnyxml(schemaContext, dataPath, operation, data);
+    }
+
+    @Override
+    public DataContainerChild<?, ?> toFilterStructure(final YangInstanceIdentifier path) {
+        return NetconfMessageTransformUtil.toFilterStructure(path, schemaContext);
+    }
+}
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/RpcStructureTransformer.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/RpcStructureTransformer.java
new file mode 100644 (file)
index 0000000..ef5009b
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf.util;
+
+import com.google.common.base.Optional;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+/**
+ * Transforms rpc structures to normalized nodes and vice versa.
+ */
+interface RpcStructureTransformer {
+
+    /**
+     * Transforms data and path to the config element structure. It means creating of parent xml structure
+     * specified by path and appending data to the structure. Operation is set as attribute on data element.
+     * @param data data
+     * @param dataPath path, where data will be written
+     * @param operation operation
+     * @return config structure
+     */
+    AnyXmlNode createEditConfigStructure(Optional<NormalizedNode<?, ?>> data,
+                                         YangInstanceIdentifier dataPath, Optional<ModifyAction> operation);
+
+    /**
+     * Transforms path to filter structure.
+     * @param path path
+     * @return filter structure
+     */
+    DataContainerChild<?,?> toFilterStructure(YangInstanceIdentifier path);
+
+    /**
+     * Selects data specified by path from data node. Data must be product of get-config rpc with filter created by
+     * {@link #toFilterStructure(YangInstanceIdentifier)} with same path.
+     * @param data data
+     * @param path path to select
+     * @return selected data
+     */
+    Optional<NormalizedNode<?, ?>> selectFromDataStructure(DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> data,
+                                                           YangInstanceIdentifier path);
+}
diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/SchemalessRpcStructureTransformer.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/SchemalessRpcStructureTransformer.java
new file mode 100644 (file)
index 0000000..eed36db
--- /dev/null
@@ -0,0 +1,235 @@
+/*
+ * Copyright (c) 2016 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.netconf.sal.connect.netconf.util;
+
+import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CONFIG_QNAME;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import javax.xml.transform.dom.DOMSource;
+import org.opendaylight.controller.config.util.xml.DocumentedException;
+import org.opendaylight.controller.config.util.xml.XmlElement;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * Transforms rpc structures to anyxml and vice versa. Provides support for devices, which don't expose their schema.
+ */
+class SchemalessRpcStructureTransformer implements RpcStructureTransformer {
+
+    /**
+     * Selects elements in anyxml data node, which matches path arguments QNames. Since class in not context aware,
+     * method searches for all elements as they are named in path.
+     * @param data data, must be of type {@link AnyXmlNode}
+     * @param path path to select
+     * @return selected data
+     */
+    @Override
+    public Optional<NormalizedNode<?, ?>> selectFromDataStructure(final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> data,
+                                                                  final YangInstanceIdentifier path) {
+        Preconditions.checkArgument(data instanceof AnyXmlNode);
+        final List<XmlElement> xmlElements = selectMatchingNodes(getSourceElement(((AnyXmlNode)data).getValue()), path);
+        final Document result = XmlUtil.newDocument();
+        final QName dataQName = NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
+        final Element dataElement = result.createElementNS(dataQName.getNamespace().toString(), dataQName.getLocalName());
+        result.appendChild(dataElement);
+        for (XmlElement xmlElement : xmlElements) {
+            dataElement.appendChild(result.importNode(xmlElement.getDomElement(), true));
+        }
+        final AnyXmlNode resultAnyxml = Builders.anyXmlBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(dataQName))
+                .withValue(new DOMSource(result))
+                .build();
+        return Optional.of(resultAnyxml);
+    }
+
+    /**
+     * This class in not context aware. All elements are present in resulting structure, which are present in data path.
+     * @see RpcStructureTransformer#createEditConfigStructure(Optional, YangInstanceIdentifier, Optional)
+     * @param data data
+     * @param dataPath path, where data will be written
+     * @param operation operation
+     * @return config structure
+     */
+    @Override
+    public AnyXmlNode createEditConfigStructure(final Optional<NormalizedNode<?, ?>> data,
+                                                final YangInstanceIdentifier dataPath, Optional<ModifyAction> operation) {
+        Preconditions.checkArgument(data.isPresent());
+        Preconditions.checkArgument(data.get() instanceof AnyXmlNode);
+
+        final AnyXmlNode anxmlData = (AnyXmlNode) data.get();
+        final Document document = XmlUtil.newDocument();
+        final Element dataNode = (Element) document.importNode(getSourceElement(anxmlData.getValue()), true);
+        checkDataValidForPath(dataPath, dataNode);
+
+        final Element configElement = document.createElementNS(NETCONF_CONFIG_QNAME.getNamespace().toString(),
+                NETCONF_CONFIG_QNAME.getLocalName());
+        document.appendChild(configElement);
+
+        final Element parentXmlStructure;
+        if (dataPath.equals(YangInstanceIdentifier.EMPTY)) {
+            parentXmlStructure = dataNode;
+            configElement.appendChild(parentXmlStructure);
+        } else {
+            final List<YangInstanceIdentifier.PathArgument> pathArguments = dataPath.getPathArguments();
+            //last will be appended later
+            final List<YangInstanceIdentifier.PathArgument> pathWithoutLast = pathArguments.subList(0, pathArguments.size() - 1);
+            parentXmlStructure = instanceIdToXmlStructure(pathWithoutLast, configElement);
+        }
+        if (operation.isPresent()) {
+            setOperationAttribute(operation, document, dataNode);
+        }
+        //append data
+        parentXmlStructure.appendChild(document.importNode(dataNode, true));
+        return Builders.anyXmlBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_CONFIG_QNAME))
+                .withValue(new DOMSource(document.getDocumentElement()))
+                .build();
+    }
+
+    /**
+     * This class in not context aware. All elements are present in resulting structure, which are present in data path.
+     * @see RpcStructureTransformer#toFilterStructure(YangInstanceIdentifier)
+     * @param path path
+     * @return filter structure
+     */
+    @Override
+    public DataContainerChild<?, ?> toFilterStructure(final YangInstanceIdentifier path) {
+        final Document document = XmlUtil.newDocument();
+        final QName filterQname = NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
+        final Element filter = document.createElementNS(filterQname.getNamespace().toString(), filterQname.getLocalName());
+        final Attr a = document.createAttributeNS(filterQname.getNamespace().toString(), "type");
+        a.setTextContent("subtree");
+        filter.setAttributeNode(a);
+        document.appendChild(filter);
+        instanceIdToXmlStructure(path.getPathArguments(), filter);
+        return Builders.anyXmlBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(filterQname))
+                .withValue(new DOMSource(document.getDocumentElement()))
+                .build();
+    }
+
+    private void checkDataValidForPath(final YangInstanceIdentifier dataPath, final Element dataNode) {
+        //if datapath is empty, consider dataNode to be a root node
+        if (dataPath.equals(YangInstanceIdentifier.EMPTY)) {
+            return;
+        }
+        final XmlElement dataElement = XmlElement.fromDomElement(dataNode);
+        final YangInstanceIdentifier.PathArgument lastPathArgument = dataPath.getLastPathArgument();
+        final QName nodeType = lastPathArgument.getNodeType();
+        if (!nodeType.getNamespace().toString().equals(dataNode.getNamespaceURI()) ||
+               !nodeType.getLocalName().equals(dataElement.getName())) {
+            throw new IllegalStateException(
+                    String.format("Can't write data '%s' to path %s", dataNode.getTagName(), dataPath));
+        }
+        if (lastPathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
+            checkKeyValuesValidForPath(dataElement, lastPathArgument);
+        }
+
+    }
+
+    private void checkKeyValuesValidForPath(final XmlElement dataElement,
+                                            final YangInstanceIdentifier.PathArgument lastPathArgument) {
+        final YangInstanceIdentifier.NodeIdentifierWithPredicates keyedId =
+                (YangInstanceIdentifier.NodeIdentifierWithPredicates) lastPathArgument;
+        final Map<QName, Object> keyValues = keyedId.getKeyValues();
+        for (QName qName : keyValues.keySet()) {
+            final List<XmlElement> key =
+                    dataElement.getChildElementsWithinNamespace(qName.getLocalName(), qName.getNamespace().toString());
+            if (key.isEmpty()) {
+                throw new IllegalStateException("No key present in xml");
+            }
+            if (key.size() > 1) {
+                throw new IllegalStateException("Multiple values for same key present");
+            }
+            final String textContent;
+            try {
+                textContent = key.get(0).getTextContent();
+            } catch (DocumentedException e) {
+                throw new IllegalStateException("Key value not present in key element");
+            }
+            if (!keyValues.get(qName).equals(textContent)) {
+                throw new IllegalStateException("Key value in path not equal to key value in xml");
+            }
+        }
+    }
+
+    private void setOperationAttribute(final Optional<ModifyAction> operation, final Document document,
+                                       final Element dataNode) {
+        final QName operationQname = NetconfMessageTransformUtil.NETCONF_OPERATION_QNAME;
+        final Attr operationAttribute =
+                document.createAttributeNS(operationQname.getNamespace().toString(), operationQname.getLocalName());
+        operationAttribute.setTextContent(toOperationString(operation.get()));
+        dataNode.setAttributeNode(operationAttribute);
+    }
+
+    private static Element instanceIdToXmlStructure(final List<YangInstanceIdentifier.PathArgument> pathArguments,
+                                                    final Element data) {
+        final Document doc = data.getOwnerDocument();
+        Element parent = data;
+        for (YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
+            final QName nodeType = pathArgument.getNodeType();
+            final Element element = doc.createElementNS(nodeType.getNamespace().toString(), nodeType.getLocalName());
+            parent.appendChild(element);
+            //if path argument is list id, add also keys to resulting xml
+            if (pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
+                YangInstanceIdentifier.NodeIdentifierWithPredicates listNode = (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
+                for (Map.Entry<QName, Object> key : listNode.getKeyValues().entrySet()) {
+                    final Element keyElement = doc.createElementNS(key.getKey().getNamespace().toString(), key.getKey().getLocalName());
+                    keyElement.setTextContent(key.getValue().toString());
+                    element.appendChild(keyElement);
+                }
+            }
+            parent = element;
+        }
+        return parent;
+    }
+
+    private List<XmlElement> selectMatchingNodes(final Element domElement, final YangInstanceIdentifier path) {
+        XmlElement element = XmlElement.fromDomElement(domElement);
+        for (YangInstanceIdentifier.PathArgument pathArgument : path.getPathArguments()) {
+            List<XmlElement> childElements = element.getChildElements(pathArgument.getNodeType().getLocalName());
+            if (childElements.size() == 1) {
+                element = childElements.get(0);
+            } else {
+                return childElements;
+            }
+        }
+        return Collections.singletonList(element);
+    }
+
+    private static String toOperationString(final ModifyAction operation) {
+        return operation.name().toLowerCase();
+    }
+
+    private static Element getSourceElement(final DOMSource source) {
+        final Node node = source.getNode();
+        switch (node.getNodeType()) {
+            case Node.DOCUMENT_NODE:
+                return ((Document)node).getDocumentElement();
+            case Node.ELEMENT_NODE:
+                return (Element) node;
+            default:
+                throw new IllegalStateException("DOMSource node must be document or element.");
+        }
+    }
+
+}
index 6dab3d6e1ae9afe0845ae44ae36230d7b5f10234..2c327d495e7781e7dfa91df84b48d0297be2fd36 100644 (file)
@@ -46,6 +46,11 @@ module netconf-node-topology {
             type boolean;
         }
 
+        leaf schemaless {
+            type boolean;
+            default false;
+        }
+
         container yang-module-capabilities {
             config true;
             leaf override {
index ac2fd3279d298f3aa81dceb2ed3fa48414db560b..8ecd5691a082bc34ccada5291af16d959a9390e2 100644 (file)
@@ -17,7 +17,7 @@ import java.util.Collections;
 import java.util.Set;
 import org.junit.Test;
 import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
+import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -34,7 +34,7 @@ public class NetconfStateSchemasTest {
 
     @Test
     public void testCreate() throws Exception {
-        final SchemaContext schemaContext = NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext();
+        final SchemaContext schemaContext = BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext();
         final DataSchemaNode schemasNode = ((ContainerSchemaNode) schemaContext.getDataChildByName("netconf-state")).getDataChildByName("schemas");
 
         final Document schemasXml = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/netconf-state.schemas.payload.xml"));
diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/SchemalessNetconfDeviceRpcTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/SchemalessNetconfDeviceRpcTest.java
new file mode 100644 (file)
index 0000000..575b7b9
--- /dev/null
@@ -0,0 +1,70 @@
+package org.opendaylight.netconf.sal.connect.netconf.sal;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import java.net.InetSocketAddress;
+import javax.xml.transform.dom.DOMSource;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
+import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+public class SchemalessNetconfDeviceRpcTest {
+
+    @Mock
+    private RemoteDeviceCommunicator<NetconfMessage> listener;
+
+    private SchemalessNetconfDeviceRpc deviceRpc;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        RpcResult<NetconfMessage> msg = null;
+        ListenableFuture<RpcResult<NetconfMessage>> future = Futures.immediateFuture(msg);
+        doReturn(future).when(listener).sendRequest(any(), any());
+        deviceRpc = new SchemalessNetconfDeviceRpc(new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830)), listener);
+
+    }
+
+    @Test
+    public void testInvokeRpc() throws Exception {
+        final QName qName = QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "get-config");
+        SchemaPath type = SchemaPath.create(true, qName);
+        DOMSource src = new DOMSource(XmlUtil.readXmlToDocument("<get-config xmlns=\"dd\">\n" +
+                "    <source>\n" +
+                "      <running/>\n" +
+                "    </source>\n" +
+                "    <filter type=\"subtree\">\n" +
+                "      <mainroot xmlns=\"urn:dummy:mod-0\">\n" +
+                "        <maincontent/>\n" +
+                "<choiceList></choiceList>\n" +
+                "      </mainroot>\n" +
+                "    </filter>\n" +
+                "  </get-config>"));
+        NormalizedNode<?, ?> input = Builders.anyXmlBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(qName))
+                .withValue(src)
+                .build();
+
+        deviceRpc.invokeRpc(type, input);
+        ArgumentCaptor<NetconfMessage> msgCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
+        ArgumentCaptor<QName> qNameCaptor = ArgumentCaptor.forClass(QName.class);
+        verify(listener).sendRequest(msgCaptor.capture(), qNameCaptor.capture());
+        System.out.println(XmlUtil.toString(msgCaptor.getValue().getDocument()));
+    }
+}
\ No newline at end of file
index ecb0eeb7796e4582c35690f263dc9a566db0dcfb..33cddf22d303b6c6ad7bf304a6c9616adf1b1f53 100644 (file)
@@ -33,7 +33,7 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
-import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
+import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
@@ -128,7 +128,7 @@ public class NetconfDeviceWriteOnlyTxTest {
                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
 
-        final WriteRunningTx tx = new WriteRunningTx(id, new NetconfBaseOps(rpc, NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext()),
+        final WriteRunningTx tx = new WriteRunningTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext()),
                 false);
         try {
             tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
index 91279cc1dfca469261b4cc4ddc2da378f9f3f204..6a81733f3e40540d06350f5af266ec5229ffb0dd 100644 (file)
@@ -104,7 +104,7 @@ public class NetconfMessageTransformerTest {
         final NetconfMessageTransformer transformer = new NetconfMessageTransformer(
                 partialSchema,
                 true,
-                NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
+                BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
         );
         NetconfMessage netconfMessage = transformer.toRpcRequest(
                 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
@@ -246,7 +246,7 @@ public class NetconfMessageTransformerTest {
         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
 
         final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build();
-        final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id, Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
+        final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id, Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
 
         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
 
diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/SchemalessRpcStructureTransformerTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/SchemalessRpcStructureTransformerTest.java
new file mode 100644 (file)
index 0000000..ea8ad18
--- /dev/null
@@ -0,0 +1,140 @@
+package org.opendaylight.netconf.sal.connect.netconf.util;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Map;
+import javax.xml.transform.dom.DOMSource;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.opendaylight.controller.config.util.xml.XmlElement;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.ModifyAction;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+@RunWith(Parameterized.class)
+public class SchemalessRpcStructureTransformerTest {
+
+    private static final String NAMESPACE = "http://example.com/schema/1.2/config";
+
+    @Rule
+    public final ExpectedException thrown = ExpectedException.none();
+    private final Class<? extends Exception> expectedException;
+
+    private final String expectedConfig;
+    private final String expectedFilter;
+    private final String getConfigData;
+    private final YangInstanceIdentifier path;
+    private final DOMSource source;
+
+    private final SchemalessRpcStructureTransformer adapter = new SchemalessRpcStructureTransformer();
+    private final String testDataset;
+
+    public SchemalessRpcStructureTransformerTest(YangInstanceIdentifier path, String testDataset, Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
+        this.path = path;
+        this.testDataset = testDataset;
+        this.expectedException = expectedException;
+        this.source = new DOMSource(XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/schemaless/data/" + testDataset)).getDocumentElement());
+        this.expectedConfig = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/edit-config/" + testDataset).toURI())));
+        this.expectedFilter = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/filter/" + testDataset).toURI())));
+        this.getConfigData = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/get-config/" + testDataset).toURI())));
+    }
+
+    @Parameterized.Parameters
+    public static Collection parameters() {
+        Object[][] params = {
+                {YangInstanceIdentifier.builder()
+                        .node(createNodeId("top"))
+                        .node(createNodeId("users"))
+                        .build(), "container.xml", null},
+                {YangInstanceIdentifier.builder()
+                        .node(createNodeId("top"))
+                        .node(createNodeId("users"))
+                        .node(createListNodeId("user", "key", "k1"))
+                        .build(), "keyed-list.xml", null},
+                {YangInstanceIdentifier.builder()
+                        .node(createNodeId("top"))
+                        .node(createNodeId("users"))
+                        .node(createListNodeId("user", ImmutableMap.of(QName.create(NAMESPACE, "key1"), "k1", QName.create(NAMESPACE, "key2"), "k2")))
+                        .build(), "keyed-list-compound-key.xml", null},
+                {YangInstanceIdentifier.builder()
+                        .node(createNodeId("top"))
+                        .node(createNodeId("users"))
+                        .node(createListNodeId("user", "key", "k2"))
+                        .build(), "keyed-list-bad-key.xml", IllegalStateException.class}
+        };
+        return Arrays.asList(params);
+    }
+
+    @BeforeClass
+    public static void suiteSetup() {
+        XMLUnit.setIgnoreWhitespace(true);
+    }
+
+    @Test
+    public void testCreateEditConfigStructure() throws Exception {
+        if(expectedException != null) {
+            thrown.expect(expectedException);
+        }
+        AnyXmlNode data = Builders.anyXmlBuilder()
+                .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
+                .withValue(source)
+                .build();
+        final AnyXmlNode anyXmlNode = adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
+        final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
+        Diff diff = new Diff(expectedConfig, s);
+        Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
+    }
+
+    @Test
+    public void testToFilterStructure() throws Exception {
+        final AnyXmlNode anyXmlNode = (AnyXmlNode) adapter.toFilterStructure(path);
+        final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
+        Diff diff = new Diff(expectedFilter, s);
+        Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
+    }
+
+    @Test
+    public void testSelectFromDataStructure() throws Exception {
+        AnyXmlNode data = Builders.anyXmlBuilder()
+                .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
+                .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
+                .build();
+        final AnyXmlNode dataStructure = (AnyXmlNode) adapter.selectFromDataStructure(data, path).get();
+        final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.getValue().getNode());
+        final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
+        final String expectedData = XmlUtil.toString((Element) source.getNode());
+        Diff diff = new Diff(expectedData, dataFromReply);
+        Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
+    }
+
+    private static YangInstanceIdentifier.NodeIdentifier createNodeId(String name) {
+        return new YangInstanceIdentifier.NodeIdentifier(QName.create(NAMESPACE, name));
+    }
+
+    private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, String keyName, String id) {
+        return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), QName.create(NAMESPACE, keyName), id);
+    }
+
+    private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, Map<QName, Object> keys) {
+        return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), keys);
+    }
+}
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/data/container.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/data/container.xml
new file mode 100644 (file)
index 0000000..001bd58
--- /dev/null
@@ -0,0 +1,5 @@
+<users xmlns="http://example.com/schema/1.2/config">
+    <user>
+        <name>fred</name>
+    </user>
+</users>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list-bad-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list-bad-key.xml
new file mode 100644 (file)
index 0000000..63762cf
--- /dev/null
@@ -0,0 +1,4 @@
+<user xmlns="http://example.com/schema/1.2/config">
+    <key>k1</key>
+    <name>fred</name>
+</user>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list-compound-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list-compound-key.xml
new file mode 100644 (file)
index 0000000..097325c
--- /dev/null
@@ -0,0 +1,5 @@
+<user xmlns="http://example.com/schema/1.2/config">
+    <key1>k1</key1>
+    <key2>k2</key2>
+    <name>fred</name>
+</user>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/data/keyed-list.xml
new file mode 100644 (file)
index 0000000..63762cf
--- /dev/null
@@ -0,0 +1,4 @@
+<user xmlns="http://example.com/schema/1.2/config">
+    <key>k1</key>
+    <name>fred</name>
+</user>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/container.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/container.xml
new file mode 100644 (file)
index 0000000..b7500ac
--- /dev/null
@@ -0,0 +1,9 @@
+<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users xmlns:ns0="urn:ietf:params:xml:ns:netconf:base:1.0" ns0:operation="replace">
+            <user>
+                <name>fred</name>
+            </user>
+        </users>
+    </top>
+</config>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list-bad-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list-bad-key.xml
new file mode 100644 (file)
index 0000000..7b4d68d
--- /dev/null
@@ -0,0 +1 @@
+empty
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list-compound-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list-compound-key.xml
new file mode 100644 (file)
index 0000000..84f669a
--- /dev/null
@@ -0,0 +1,11 @@
+<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user xmlns:ns0="urn:ietf:params:xml:ns:netconf:base:1.0" ns0:operation="replace">
+                <key1>k1</key1>
+                <key2>k2</key2>
+                <name>fred</name>
+            </user>
+        </users>
+    </top>
+</config>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/edit-config/keyed-list.xml
new file mode 100644 (file)
index 0000000..72e319c
--- /dev/null
@@ -0,0 +1,10 @@
+<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user xmlns:ns0="urn:ietf:params:xml:ns:netconf:base:1.0" ns0:operation="replace">
+                <key>k1</key>
+                <name>fred</name>
+            </user>
+        </users>
+    </top>
+</config>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/container.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/container.xml
new file mode 100644 (file)
index 0000000..d889c8d
--- /dev/null
@@ -0,0 +1,6 @@
+<filter xmlns:ns0="urn:ietf:params:xml:ns:netconf:base:1.0" ns0:type="subtree" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+        </users>
+    </top>
+</filter>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list-bad-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list-bad-key.xml
new file mode 100644 (file)
index 0000000..2906847
--- /dev/null
@@ -0,0 +1,9 @@
+<filter xmlns:ns0="urn:ietf:params:xml:ns:netconf:base:1.0" ns0:type="subtree" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user>
+                <key>k2</key>
+            </user>
+        </users>
+    </top>
+</filter>
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list-compound-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list-compound-key.xml
new file mode 100644 (file)
index 0000000..ce6c522
--- /dev/null
@@ -0,0 +1,10 @@
+<filter xmlns:ns0="urn:ietf:params:xml:ns:netconf:base:1.0" ns0:type="subtree" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user>
+                <key1>k1</key1>
+                <key2>k2</key2>
+            </user>
+        </users>
+    </top>
+</filter>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/filter/keyed-list.xml
new file mode 100644 (file)
index 0000000..ae2e01e
--- /dev/null
@@ -0,0 +1,9 @@
+<filter xmlns:ns0="urn:ietf:params:xml:ns:netconf:base:1.0" ns0:type="subtree" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user>
+                <key>k1</key>
+            </user>
+        </users>
+    </top>
+</filter>
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/container.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/container.xml
new file mode 100644 (file)
index 0000000..e0a7d5e
--- /dev/null
@@ -0,0 +1,9 @@
+<data operation="replace" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user>
+                <name>fred</name>
+            </user>
+        </users>
+    </top>
+</data>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list-bad-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list-bad-key.xml
new file mode 100644 (file)
index 0000000..cd36b4e
--- /dev/null
@@ -0,0 +1,10 @@
+<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user>
+                <key>k1</key>
+                <name>fred</name>
+            </user>
+        </users>
+    </top>
+</data>
\ No newline at end of file
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list-compound-key.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list-compound-key.xml
new file mode 100644 (file)
index 0000000..75b7865
--- /dev/null
@@ -0,0 +1,11 @@
+<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user>
+                <key1>k1</key1>
+                <key2>k2</key2>
+                <name>fred</name>
+            </user>
+        </users>
+    </top>
+</data>
diff --git a/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list.xml b/netconf/sal-netconf-connector/src/test/resources/schemaless/get-config/keyed-list.xml
new file mode 100644 (file)
index 0000000..cd36b4e
--- /dev/null
@@ -0,0 +1,10 @@
+<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+    <top xmlns="http://example.com/schema/1.2/config">
+        <users>
+            <user>
+                <key>k1</key>
+                <name>fred</name>
+            </user>
+        </users>
+    </top>
+</data>
\ No newline at end of file
index e7c772521cabfe69b8e149bca7397b82cd93c361..324ea36880f5b3aa39ba206a6b2d1af7e1758f66 100644 (file)
@@ -47,7 +47,7 @@ import org.slf4j.LoggerFactory;
 public final class Main {
 
     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
-
+    
     public static void main(final String[] args) {
         final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
         params.validate();
@@ -56,6 +56,7 @@ public final class Main {
 
         final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(params.threadPoolSize);
         try {
+            LOG.debug("Trying to start netconf test-tool with parameters {}", params);
             final List<Integer> openDevices = netconfDeviceSimulator.start(params);
             if (openDevices.size() == 0) {
                 LOG.error("Failed to start any simulated devices, exiting...");
index 605d7ba35fb6aa7c5de08e48397aa7b1c24b7974..767a8ef83f7ff03c011f3379ef6f5ea6bf192980 100644 (file)
@@ -117,7 +117,7 @@ class MdsalOperationProvider implements NetconfOperationServiceFactory {
             this.sourceProvider = sourceProvider;
             this.schemaService = createSchemaService();
 
-            this.dataBroker = createDataStore(schemaService);
+            this.dataBroker = createDataStore(schemaService, currentSessionId);
 
         }
 
@@ -206,7 +206,8 @@ class MdsalOperationProvider implements NetconfOperationServiceFactory {
                     new YangInstanceIdentifier.NodeIdentifier(NetconfState.QNAME)).withChild(schemasContainer).build();
         }
 
-        private DOMDataBroker createDataStore(SchemaService schemaService) {
+        private DOMDataBroker createDataStore(SchemaService schemaService, long sessionId) {
+            LOG.debug("Session {}: Creating data stores for simulated device", sessionId);
             final DOMStore operStore = InMemoryDOMDataStoreFactory
                     .create("DOM-OPER", schemaService);
             final DOMStore configStore = InMemoryDOMDataStoreFactory
index cb5710749d9de261f54cea5746c715a91c44fbd3..171b0b5af971b8e5716fdb83e908743d0b7d608c 100644 (file)
@@ -19,8 +19,11 @@ import org.opendaylight.netconf.impl.NetconfServerSessionNegotiatorFactoryBuilde
 import org.opendaylight.netconf.impl.SessionIdProvider;
 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class TesttoolNegotiationFactory extends NetconfServerSessionNegotiatorFactory {
+    private static final Logger LOG = LoggerFactory.getLogger(TesttoolNegotiationFactory.class);
 
     private final Map<SocketAddress, NetconfOperationService> cachedOperationServices = new HashMap<>();
 
@@ -39,10 +42,14 @@ public class TesttoolNegotiationFactory extends NetconfServerSessionNegotiatorFa
     @Override
     protected NetconfOperationService getOperationServiceForAddress(final String netconfSessionIdForReporting, final SocketAddress socketAddress) {
         if (cachedOperationServices.containsKey(socketAddress)) {
+            LOG.debug("Session {}: Getting cached operation service factory for test tool device on address {}",
+                    netconfSessionIdForReporting, socketAddress);
             return cachedOperationServices.get(socketAddress);
         } else {
             final NetconfOperationService service = getOperationServiceFactory().createService(netconfSessionIdForReporting);
             cachedOperationServices.put(socketAddress, service);
+            LOG.debug("Session {}: Creating new operation service factory for test tool device on address {}",
+                    netconfSessionIdForReporting, socketAddress);
             return service;
         }
     }
index f2f3e09173361561722adfff9a89153bfea78183..a5a075b30551e435a119d593d169e7bf3f523b16 100644 (file)
@@ -404,4 +404,34 @@ public class TesttoolParameters {
         }
         return payloads;
     }
+
+    //TODO This may be more scalable enumerating parameters via reflection
+    @Override
+    public String toString() {
+        StringBuffer params = new StringBuffer("TesttoolParameters{");
+        params.append("edit-content='").append(editContent).append('\'');
+        params.append(", async='").append(async).append('\'');
+        params.append(", thread-amount='").append(threadAmount).append('\'');
+        params.append(", throttle='").append(throttle).append('\'');
+        params.append(", auth='").append(auth).append('\'');
+        params.append(", controller-destination='").append(controllerDestination).append('\'');
+        params.append(", schemas-dir='").append(schemasDir).append('\'');
+        params.append(", devices-count='").append(deviceCount).append('\'');
+        params.append(", devices-per-port='").append(devicesPerPort).append('\'');
+        params.append(", starting-port='").append(startingPort).append('\'');
+        params.append(", generate-config-connection-timeout='").append(generateConfigsTimeout).append('\'');
+        params.append(", generate-config-address='").append(generateConfigsAddress).append('\'');
+        params.append(", distro-folder='").append(distroFolder).append('\'');
+        params.append(", generate-configs-batch-size='").append(generateConfigBatchSize).append('\'');
+        params.append(", ssh='").append(ssh).append('\'');
+        params.append(", exi='").append(exi).append('\'');
+        params.append(", debug='").append(debug).append('\'');
+        params.append(", notification-file='").append(notificationFile).append('\'');
+        params.append(", md-sal='").append(mdSal).append('\'');
+        params.append(", initial-config-xml-file='").append(initialConfigXMLFile).append('\'');
+        params.append(", time-out='").append(timeOut).append('\'');
+        params.append('}');
+
+        return params.toString();
+    }
 }
index 47710708f6236f6b284313b3dcab009982b93a71..da3672a2b4334297cabd07d9b8b362cb20f2f739 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.netconf.sal.restconf.impl;
 
 import com.google.common.base.Preconditions;
+import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import java.util.Collection;
@@ -58,7 +59,7 @@ public class RestconfDocumentedException extends WebApplicationException {
      *            The underlying exception cause.
      */
     public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag, Throwable cause) {
-        this(cause, new RestconfError(errorType, errorTag, message, null, RestconfError.toErrorInfo(cause)));
+        this(cause, new RestconfError(errorType, errorTag, message, null, Throwables.getStackTraceAsString(cause)));
     }
 
     /**
@@ -86,7 +87,7 @@ public class RestconfDocumentedException extends WebApplicationException {
      */
     public RestconfDocumentedException(String message, Throwable cause) {
         this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
-                message, null, RestconfError.toErrorInfo(cause)));
+                message, null, Throwables.getStackTraceAsString(cause)));
     }
 
     /**
index c86706cedd705163060ffcbebaa5a29e03199812..316e766bd34bfa3d18dfc80ad041342a8cbcf59a 100644 (file)
@@ -8,8 +8,7 @@
 package org.opendaylight.netconf.sal.restconf.impl;
 
 import com.google.common.base.Preconditions;
-import java.io.PrintWriter;
-import java.io.StringWriter;
+import com.google.common.base.Throwables;
 import org.opendaylight.yangtools.yang.common.RpcError;
 
 /**
@@ -99,15 +98,8 @@ public class RestconfError {
     private final String errorInfo;
     private final String errorAppTag;
     private final String errorMessage;
-
     // TODO: Add in the error-path concept as defined in the ietf draft.
 
-    static String toErrorInfo(Throwable cause) {
-        StringWriter writer = new StringWriter();
-        cause.printStackTrace(new PrintWriter(writer));
-        return writer.toString();
-    }
-
     /**
      * Constructs a RestConfError
      *
@@ -180,7 +172,7 @@ public class RestconfError {
         String errorInfo = null;
         if (rpcError.getInfo() == null) {
             if (rpcError.getCause() != null) {
-                errorInfo = toErrorInfo(rpcError.getCause());
+                errorInfo = Throwables.getStackTraceAsString(rpcError.getCause());
             } else if (rpcError.getSeverity() != null) {
                 errorInfo = "<severity>" + rpcError.getSeverity().toString().toLowerCase() + "</severity>";
             }
index 30b35486158bb11d18ec8faea4d5018d47c6ba20..6fc16fa232a78a18b0b2e4d0a2c87addb79e3fe5 100644 (file)
@@ -11,8 +11,11 @@ import com.google.common.base.CharMatcher;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import org.opendaylight.netconf.md.sal.rest.common.RestconfValidationUtils;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
 import org.opendaylight.restconf.utils.RestconfConstants;
 import org.opendaylight.restconf.utils.parser.builder.ParserBuilderConstants;
 import org.opendaylight.restconf.utils.schema.context.RestconfSchemaUtil;
@@ -50,31 +53,34 @@ public final class YangInstanceIdentifierDeserializer {
      */
     public static Iterable<PathArgument> create(final SchemaContext schemaContext, final String data) {
         final List<PathArgument> path = new LinkedList<>();
-        DataSchemaContextNode<?> current = DataSchemaContextTree.from(schemaContext).getRoot();
-        final int offset = 0;
-        final MainVarsWrapper variables = new YangInstanceIdentifierDeserializer.MainVarsWrapper(data,
-                current, offset, schemaContext);
-
-        while (!allCharsConsumed(variables)) {
-            validArg(variables);
-            final QName qname = prepareQName(variables);
-
-            // this is the last identifier (input is consumed) or end of identifier (slash)
-            if (allCharsConsumed(variables)
-                    || currentChar(variables.getOffset(), variables.getData()) == RestconfConstants.SLASH) {
-                prepareIdentifier(qname, path, variables);
-                path.add(variables.getCurrent().getIdentifier());
-            } else if (currentChar(variables.getOffset(),
-                    variables.getData()) == ParserBuilderConstants.Deserializer.EQUAL) {
-                current = nextContextNode(qname, path, variables);
-                if (!current.isKeyedEntry()) {
-                    prepareNodeWithValue(qname, path, variables);
+        final MainVarsWrapper variables = new YangInstanceIdentifierDeserializer.MainVarsWrapper(
+                data, DataSchemaContextTree.from(schemaContext).getRoot(),
+                YangInstanceIdentifierDeserializer.MainVarsWrapper.STARTING_OFFSET, schemaContext);
+
+        checkValid(!data.isEmpty(), "Empty path is not valid", variables.getData(), variables.getOffset());
+
+        if (!data.equals(String.valueOf(RestconfConstants.SLASH))) {
+            while (!allCharsConsumed(variables)) {
+                validArg(variables);
+                final QName qname = prepareQName(variables);
+
+                // this is the last identifier (input is consumed) or end of identifier (slash)
+                if (allCharsConsumed(variables)
+                        || currentChar(variables.getOffset(), variables.getData()) == RestconfConstants.SLASH) {
+                    prepareIdentifier(qname, path, variables);
+                    path.add(variables.getCurrent().getIdentifier());
+                } else if (currentChar(variables.getOffset(),
+                        variables.getData()) == ParserBuilderConstants.Deserializer.EQUAL) {
+                    if (nextContextNode(qname, path, variables).getDataSchemaNode() instanceof ListSchemaNode) {
+                        prepareNodeWithPredicates(qname, path, variables);
+                    } else {
+                        prepareNodeWithValue(qname, path, variables);
+                    }
                 } else {
-                    prepareNodeWithPredicates(qname, path, variables);
+                    throw new IllegalArgumentException(
+                            "Bad char " + currentChar(variables.getOffset(), variables.getData()) + " on position "
+                                    + variables.getOffset() + ".");
                 }
-            } else {
-                throw new IllegalArgumentException(
-                        "Bad char " + currentChar(offset, data) + " on position " + offset + ".");
             }
         }
 
@@ -82,36 +88,85 @@ public final class YangInstanceIdentifierDeserializer {
     }
 
     private static void prepareNodeWithPredicates(final QName qname, final List<PathArgument> path,
-            final MainVarsWrapper variables) {
-        final List<QName> keyDefinitions = ((ListSchemaNode) variables.getCurrent().getDataSchemaNode())
-                .getKeyDefinition();
-        final ImmutableMap.Builder<QName, Object> keyValues = ImmutableMap.builder();
-
-        for (final QName keyQName : keyDefinitions) {
-            skipCurrentChar(variables);
-            String value = null;
-            if ((currentChar(variables.getOffset(), variables.getData()) == ParserBuilderConstants.Deserializer.COMMA)
-                    || (currentChar(variables.getOffset(), variables.getData()) == RestconfConstants.SLASH)) {
-                value = ParserBuilderConstants.Deserializer.EMPTY_STRING;
-            } else {
-                value = nextIdentifierFromNextSequence(ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE,
-                        variables);
+                                                  final MainVarsWrapper variables) {
+
+        final DataSchemaNode dataSchemaNode = variables.getCurrent().getDataSchemaNode();
+        checkValid((dataSchemaNode != null), "Data schema node is null", variables.getData(), variables.getOffset());
+
+        final Iterator<QName> keys = ((ListSchemaNode) dataSchemaNode).getKeyDefinition().iterator();
+        final ImmutableMap.Builder<QName, Object> values = ImmutableMap.builder();
+
+        // skip already expected equal sign
+        skipCurrentChar(variables);
+
+        // read key value separated by comma
+        while (keys.hasNext() && !allCharsConsumed(variables) && currentChar(variables.getOffset(),
+                variables.getData()) != RestconfConstants.SLASH) {
+
+            // empty key value
+            if (currentChar(variables.getOffset(), variables.getData()) == ParserBuilderConstants.Deserializer.COMMA) {
+                values.put(keys.next(), ParserBuilderConstants.Deserializer.EMPTY_STRING);
+                skipCurrentChar(variables);
+                continue;
+            }
+
+            // check if next value is parsable
+            RestconfValidationUtils.checkDocumentedError(
+                    ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE
+                            .matches(currentChar(variables.getOffset(), variables.getData())),
+                    RestconfError.ErrorType.PROTOCOL,
+                    RestconfError.ErrorTag.MALFORMED_MESSAGE,
+                    ""
+            );
+
+            // parse value
+            values.put(keys.next(), findAndParsePercentEncoded(nextIdentifierFromNextSequence(
+                    ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE, variables)));
+
+            // skip comma
+            if (keys.hasNext() && !allCharsConsumed(variables) && currentChar(
+                    variables.getOffset(), variables.getData()) == ParserBuilderConstants.Deserializer.COMMA) {
+                skipCurrentChar(variables);
+            }
+        }
+
+        // the last key is considered to be empty
+        if (keys.hasNext()) {
+            if (allCharsConsumed(variables)
+                    || currentChar(variables.getOffset(), variables.getData()) == RestconfConstants.SLASH) {
+                values.put(keys.next(), ParserBuilderConstants.Deserializer.EMPTY_STRING);
             }
-            value = findAndParsePercentEncoded(value);
-            keyValues.put(keyQName, value);
+
+            // there should be no more missing keys
+            RestconfValidationUtils.checkDocumentedError(
+                    !keys.hasNext(),
+                    RestconfError.ErrorType.PROTOCOL,
+                    RestconfError.ErrorTag.MISSING_ATTRIBUTE,
+                    "Key value missing for: " + qname
+            );
         }
-        path.add(new YangInstanceIdentifier.NodeIdentifierWithPredicates(qname, keyValues.build()));
+
+        path.add(new YangInstanceIdentifier.NodeIdentifierWithPredicates(qname, values.build()));
     }
 
+
     private static QName prepareQName(final MainVarsWrapper variables) {
         checkValid(
                 ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR
                         .matches(currentChar(variables.getOffset(), variables.getData())),
                 "Identifier must start with character from set 'a-zA-Z_'", variables.getData(), variables.getOffset());
-        final String preparedPrefix = nextIdentifierFromNextSequence(ParserBuilderConstants.Deserializer.IDENTIFIER, variables);
+        final String preparedPrefix = nextIdentifierFromNextSequence(
+                ParserBuilderConstants.Deserializer.IDENTIFIER, variables);
         final String prefix, localName;
 
+        if (allCharsConsumed(variables)) {
+            return getQNameOfDataSchemaNode(preparedPrefix, variables);
+        }
+
         switch (currentChar(variables.getOffset(), variables.getData())) {
+            case RestconfConstants.SLASH:
+                prefix = preparedPrefix;
+                return getQNameOfDataSchemaNode(prefix, variables);
             case ParserBuilderConstants.Deserializer.COLON:
                 prefix = preparedPrefix;
                 skipCurrentChar(variables);
@@ -122,10 +177,14 @@ public final class YangInstanceIdentifierDeserializer {
                         variables.getOffset());
                 localName = nextIdentifierFromNextSequence(ParserBuilderConstants.Deserializer.IDENTIFIER, variables);
 
-                final Module module = moduleForPrefix(prefix, variables.getSchemaContext());
-                Preconditions.checkArgument(module != null, "Failed to lookup prefix %s", prefix);
-
-                return QName.create(module.getQNameModule(), localName);
+                if (!allCharsConsumed(variables) && currentChar
+                        (variables.getOffset(), variables.getData()) == ParserBuilderConstants.Deserializer.EQUAL) {
+                    return getQNameOfDataSchemaNode(localName, variables);
+                } else {
+                    final Module module = moduleForPrefix(prefix, variables.getSchemaContext());
+                    Preconditions.checkArgument(module != null, "Failed to lookup prefix %s", prefix);
+                    return QName.create(module.getQNameModule(), localName);
+                }
             case ParserBuilderConstants.Deserializer.EQUAL:
                 prefix = preparedPrefix;
                 return getQNameOfDataSchemaNode(prefix, variables);
@@ -150,9 +209,18 @@ public final class YangInstanceIdentifierDeserializer {
     private static void prepareNodeWithValue(final QName qname, final List<PathArgument> path,
             final MainVarsWrapper variables) {
         skipCurrentChar(variables);
-        String value = nextIdentifierFromNextSequence(ParserBuilderConstants.Deserializer.IDENTIFIER, variables);
-        value = findAndParsePercentEncoded(value);
-        path.add(new YangInstanceIdentifier.NodeWithValue<>(qname, value));
+        final String value = nextIdentifierFromNextSequence(
+                ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE, variables);
+
+        // exception if value attribute is missing
+        RestconfValidationUtils.checkDocumentedError(
+                !value.isEmpty(),
+                RestconfError.ErrorType.PROTOCOL,
+                RestconfError.ErrorTag.MISSING_ATTRIBUTE,
+                "Value missing for: " + qname
+        );
+
+        path.add(new YangInstanceIdentifier.NodeWithValue<>(qname, findAndParsePercentEncoded(value)));
     }
 
     private static void prepareIdentifier(final QName qname, final List<PathArgument> path,
@@ -176,26 +244,26 @@ public final class YangInstanceIdentifierDeserializer {
         return current;
     }
 
-    private static String findAndParsePercentEncoded(String preparedPrefix) {
+    private static String findAndParsePercentEncoded(final String preparedPrefix) {
         if (!preparedPrefix.contains(String.valueOf(ParserBuilderConstants.Deserializer.PERCENT_ENCODING))) {
             return preparedPrefix;
         }
-        final StringBuilder newPrefix = new StringBuilder();
-        int i = 0;
-        int startPoint = 0;
-        int endPoint = preparedPrefix.length();
-        while ((i = preparedPrefix.indexOf(ParserBuilderConstants.Deserializer.PERCENT_ENCODING)) != -1) {
-            newPrefix.append(preparedPrefix.substring(startPoint, i));
-            startPoint = i;
-            startPoint++;
-            final String hex = preparedPrefix.substring(startPoint, startPoint + 2);
-            startPoint += 2;
-            newPrefix.append((char) Integer.parseInt(hex, 16));
-            preparedPrefix = preparedPrefix.substring(startPoint, endPoint);
-            startPoint = 0;
-            endPoint = preparedPrefix.length();
+
+        final StringBuilder parsedPrefix = new StringBuilder(preparedPrefix);
+        final CharMatcher matcher = CharMatcher.is(ParserBuilderConstants.Deserializer.PERCENT_ENCODING);
+
+        while (matcher.matchesAnyOf(parsedPrefix)) {
+            final int percentCharPosition = matcher.indexIn(parsedPrefix);
+            parsedPrefix.replace(
+                    percentCharPosition,
+                    percentCharPosition + ParserBuilderConstants.Deserializer.LAST_ENCODED_CHAR,
+                    String.valueOf((char) Integer.parseInt(parsedPrefix.substring(
+                            percentCharPosition + ParserBuilderConstants.Deserializer.FIRST_ENCODED_CHAR,
+                            percentCharPosition + ParserBuilderConstants.Deserializer.LAST_ENCODED_CHAR),
+                            ParserBuilderConstants.Deserializer.PERCENT_ENCODED_RADIX)));
         }
-        return newPrefix.toString();
+
+        return parsedPrefix.toString();
     }
 
     private static QName getQNameOfDataSchemaNode(final String nodeName, final MainVarsWrapper variables) {
@@ -244,7 +312,8 @@ public final class YangInstanceIdentifierDeserializer {
         return variables.getOffset() == variables.getData().length();
     }
 
-    private static class MainVarsWrapper {
+    private final static class MainVarsWrapper {
+        private static final int STARTING_OFFSET = 0;
 
         private final SchemaContext schemaContext;
         private final String data;
@@ -254,9 +323,9 @@ public final class YangInstanceIdentifierDeserializer {
         public MainVarsWrapper(final String data, final DataSchemaContextNode<?> current, final int offset,
                 final SchemaContext schemaContext) {
             this.data = data;
-            this.schemaContext = schemaContext;
             this.setCurrent(current);
             this.setOffset(offset);
+            this.schemaContext = schemaContext;
         }
 
         public String getData() {
@@ -282,6 +351,5 @@ public final class YangInstanceIdentifierDeserializer {
         public SchemaContext getSchemaContext() {
             return this.schemaContext;
         }
-
     }
 }
index b8ac697b73a6568852c78032370065e8af7406f3..4e60d4e25ea23b13a1d7d670630938e654df7f76 100644 (file)
@@ -7,13 +7,16 @@
  */
 package org.opendaylight.restconf.parser.builder;
 
+import static org.opendaylight.restconf.utils.parser.builder.ParserBuilderConstants.Serializer;
+
 import com.google.common.base.Preconditions;
 import java.net.URI;
-import java.util.Map;
+import java.util.Iterator;
 import java.util.Map.Entry;
-import java.util.Set;
+import org.opendaylight.restconf.utils.RestconfConstants;
 import org.opendaylight.restconf.utils.parser.builder.ParserBuilderConstants;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
@@ -44,36 +47,57 @@ public final class YangInstanceIdentifierSerializer {
      */
     public static String create(final SchemaContext schemaContext, final YangInstanceIdentifier data) {
         final DataSchemaContextNode<?> current = DataSchemaContextTree.from(schemaContext).getRoot();
-        final MainVarsWrappar variables = new YangInstanceIdentifierSerializer.MainVarsWrappar(current);
+        final MainVarsWrapper variables = new MainVarsWrapper(current);
+
+        // for empty data return slash
+        final StringBuilder path = (data.getPathArguments().size() == 0) ?
+                new StringBuilder(String.valueOf(RestconfConstants.SLASH)) : new StringBuilder();
 
-        final StringBuilder path = prepareFirstArgForPath(variables, data);
+        QNameModule parentModule = null;
+        for (int i = 0; i < data.getPathArguments().size(); i++) {
+            // get module of the parent
+            if (!variables.getCurrent().isMixin()) {
+                parentModule = variables.getCurrent().getDataSchemaNode().getQName().getModule();
+            }
 
-        for (int i = 1; i < data.getPathArguments().size(); i++) {
             final PathArgument arg = data.getPathArguments().get(i);
             variables.setCurrent(variables.getCurrent().getChild(arg));
-            Preconditions.checkArgument(current != null,
+
+            Preconditions.checkArgument(variables.getCurrent() != null,
                     "Invalid input %s: schema for argument %s (after %s) not found", data, arg, path);
 
             if (variables.getCurrent().isMixin()) {
                 continue;
             }
-            path.append('/');
+
+            // append namespace before every node which is defined in other module than its parent
+            // condition is satisfied also for the first path argument
+            if (!arg.getNodeType().getModule().equals(parentModule)) {
+                path.append(RestconfConstants.SLASH
+                        + prefixForNamespace(arg.getNodeType())
+                        + ParserBuilderConstants.Deserializer.COLON);
+            } else {
+                path.append(RestconfConstants.SLASH);
+            }
 
             if (arg instanceof NodeIdentifierWithPredicates) {
                 prepareNodeWithPredicates(path, arg);
             } else if (arg instanceof NodeWithValue) {
                 prepareNodeWithValue(path, arg);
+            } else {
+                appendQName(path, arg.getNodeType());
             }
         }
+
         return path.toString();
     }
 
     private static void prepareNodeWithValue(final StringBuilder path, final PathArgument arg) {
         path.append(arg.getNodeType().getLocalName());
-        path.append("=");
+        path.append(ParserBuilderConstants.Deserializer.EQUAL);
 
         String value = ((NodeWithValue<String>) arg).getValue();
-        if (ParserBuilderConstants.Serializer.PERCENT_ENCODE_CHARS.matchesAnyOf(value)) {
+        if (Serializer.PERCENT_ENCODE_CHARS.matchesAnyOf(value)) {
             value = parsePercentEncodeChars(value);
         }
         path.append(value);
@@ -81,37 +105,28 @@ public final class YangInstanceIdentifierSerializer {
 
     private static void prepareNodeWithPredicates(final StringBuilder path, final PathArgument arg) {
         path.append(arg.getNodeType().getLocalName());
-        path.append("=");
-
-        final Set<Entry<QName, Object>> entrySet = ((NodeIdentifierWithPredicates) arg).getKeyValues().entrySet();
-        final int endOfSet = entrySet.size();
-        int s = 1;
-        for (final Map.Entry<QName, Object> entry : entrySet) {
-            String valueOf = String.valueOf(entry.getValue());
-            if (ParserBuilderConstants.Serializer.PERCENT_ENCODE_CHARS.matchesAnyOf(valueOf)) {
+
+        final Iterator<Entry<QName, Object>> iterator = ((NodeIdentifierWithPredicates) arg).getKeyValues()
+                .entrySet().iterator();
+
+        if (iterator.hasNext()) {
+            path.append(ParserBuilderConstants.Deserializer.EQUAL);
+        }
+
+        while (iterator.hasNext()) {
+            String valueOf = String.valueOf(iterator.next().getValue());
+            if (Serializer.PERCENT_ENCODE_CHARS.matchesAnyOf(valueOf)) {
                 valueOf = parsePercentEncodeChars(valueOf);
             }
             path.append(valueOf);
-            if (s != endOfSet) {
-                path.append(",");
-                s++;
+            if (iterator.hasNext()) {
+                path.append(ParserBuilderConstants.Deserializer.COMMA);
             }
         }
     }
 
-    private static StringBuilder prepareFirstArgForPath(final MainVarsWrappar variables,
-            final YangInstanceIdentifier data) {
-        final PathArgument firstArg = data.getPathArguments().get(0);
-        variables.setCurrent(variables.getCurrent().getChild(firstArg));
-
-        final StringBuilder path = new StringBuilder("/");
-        appendQName(path, firstArg.getNodeType());
-        return path;
-    }
-
     /**
-     * Encode {@link YangInstanceIdentifierSerializer#DISABLED_CHARS}
-     * chars to percent encoded chars
+     * Encode {@link Serializer#DISABLED_CHARS} chars to percent encoded chars
      *
      * @param valueOf
      *            - string to encode
@@ -121,10 +136,10 @@ public final class YangInstanceIdentifierSerializer {
         final StringBuilder sb = new StringBuilder();
         int start = 0;
         while (start < valueOf.length()) {
-            if (ParserBuilderConstants.Serializer.PERCENT_ENCODE_CHARS.matches(valueOf.charAt(start))) {
+            if (Serializer.PERCENT_ENCODE_CHARS.matches(valueOf.charAt(start))) {
                 final String format = String.format("%x", (int) valueOf.charAt(start));
                 final String upperCase = format.toUpperCase();
-                sb.append("%" + upperCase);
+                sb.append(ParserBuilderConstants.Deserializer.PERCENT_ENCODING + upperCase);
             } else {
                 sb.append(valueOf.charAt(start));
             }
@@ -143,31 +158,29 @@ public final class YangInstanceIdentifierSerializer {
      * @return {@link StringBuilder}
      */
     private final static StringBuilder appendQName(final StringBuilder path, final QName qname) {
-        final String prefix = prefixForNamespace(qname.getNamespace());
-        Preconditions.checkArgument(prefix != null, "Failed to map QName {}", qname);
-        path.append(prefix);
-        path.append(':');
         path.append(qname.getLocalName());
         return path;
     }
 
     /**
-     * Create prefix of namespace from {@link URI}
+     * Create prefix of namespace from {@link QName}
      *
-     * @param namespace
-     *            - {@link URI}
+     * @param qname
+     *            - {@link QName}
      * @return {@link String}
      */
-    private static String prefixForNamespace(final URI namespace) {
+    private static String prefixForNamespace(final QName qname) {
+        final URI namespace = qname.getNamespace();
+        Preconditions.checkArgument(namespace != null, "Failed to map QName {}", qname);
         final String prefix = namespace.toString();
-        return prefix.replace(':', '-');
+        return prefix.replace(ParserBuilderConstants.Deserializer.COLON, ParserBuilderConstants.Deserializer.HYPHEN);
     }
 
-    private static class MainVarsWrappar {
+    private static final class MainVarsWrapper {
 
         private DataSchemaContextNode<?> current;
 
-        public MainVarsWrappar(final DataSchemaContextNode<?> current) {
+        public MainVarsWrapper(final DataSchemaContextNode<?> current) {
             this.setCurrent(current);
         }
 
@@ -180,5 +193,4 @@ public final class YangInstanceIdentifierSerializer {
         }
 
     }
-
 }
index c4bc7ae959ff78f94453989506c74b293aeb76f6..006f59f50466ca739c298ac09e1713be181cd983 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.restconf.utils.parser.builder;
 
 import com.google.common.base.CharMatcher;
+import java.util.Arrays;
 import org.opendaylight.restconf.parser.builder.YangInstanceIdentifierDeserializer;
 import org.opendaylight.restconf.parser.builder.YangInstanceIdentifierSerializer;
 
@@ -32,7 +33,9 @@ public final class ParserBuilderConstants {
             throw new UnsupportedOperationException("Util class");
         }
 
-        public static final String DISABLED_CHARS = ",': /";
+        public static final String DISABLED_CHARS = Arrays.toString(new char[] { ':', '/', '?', '#', '[', ']', '@' })
+                .concat(Arrays.toString(new char[] { '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=' }));
+
         public static final CharMatcher PERCENT_ENCODE_CHARS = CharMatcher.anyOf(DISABLED_CHARS).precomputed();
     }
 
@@ -48,22 +51,31 @@ public final class ParserBuilderConstants {
 
         public static final CharMatcher BASE = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'))
                 .precomputed();
+
         public static final CharMatcher IDENTIFIER_FIRST_CHAR = BASE.or(CharMatcher.is('_')).precomputed();
+
         public static final CharMatcher IDENTIFIER = IDENTIFIER_FIRST_CHAR.or(CharMatcher.inRange('0', '9'))
                 .or(CharMatcher.anyOf(".-")).precomputed();
+
         public static final CharMatcher IDENTIFIER_HEXA = CharMatcher.inRange('a', 'f')
                 .or(CharMatcher.inRange('A', 'F')).or(CharMatcher.inRange('0', '9')).precomputed();
 
         public static final char COLON = ':';
         public static final char EQUAL = '=';
         public static final char COMMA = ',';
+        public static final char HYPHEN = '-';
         public static final char PERCENT_ENCODING = '%';
-        public static final char QUOTE = '"';
 
-        public static final CharMatcher IDENTIFIER_FIRST_CHAR_PREDICATE = BASE.or(CharMatcher.inRange('0', '9'))
-                .or(CharMatcher.is(QUOTE)).or(CharMatcher.is(PERCENT_ENCODING)).precomputed();
+        public static final CharMatcher IDENTIFIER_PREDICATE = CharMatcher.noneOf(
+                Serializer.DISABLED_CHARS).precomputed();
 
-        public static final CharMatcher IDENTIFIER_PREDICATE = IDENTIFIER_FIRST_CHAR_PREDICATE;
         public static final String EMPTY_STRING = "";
+
+        // position of the first encoded char after percent sign in percent encoded string
+        public static final int FIRST_ENCODED_CHAR = 1;
+        // position of the last encoded char after percent sign in percent encoded string
+        public static final int LAST_ENCODED_CHAR = 3;
+        // percent encoded radix for parsing integers
+        public static final int PERCENT_ENCODED_RADIX = 16;
     }
 }
\ No newline at end of file