X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Fconnect%2Fnetconf%2Fsal%2FSchemalessNetconfDeviceRpc.java;h=1d66547a4d72408022278d88d21d99527c58eb9f;hb=refs%2Fchanges%2F23%2F103723%2F3;hp=d610904e6b76f2e34144d6c1a1f78a0c640e192b;hpb=79951c770e34cea96fca471c9c2bd465190991f6;p=netconf.git 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 index d610904e6b..1d66547a4d 100644 --- 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 @@ -7,103 +7,91 @@ */ 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.FutureCallback; 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 com.google.common.util.concurrent.MoreExecutors; +import com.google.common.util.concurrent.SettableFuture; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException; +import org.opendaylight.mdsal.dom.api.DOMRpcResult; +import org.opendaylight.mdsal.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.api.RemoteDeviceServices.Rpcs; +import org.opendaylight.netconf.sal.connect.api.RpcTransformer; 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.QName; import org.opendaylight.yangtools.yang.common.RpcResult; -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.DOMSourceAnyxmlNode; 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 listener; +public final class SchemalessNetconfDeviceRpc implements Rpcs.Schemaless { + private final RemoteDeviceCommunicator listener; private final BaseRpcSchemalessTransformer baseRpcTransformer; private final SchemalessMessageTransformer schemalessTransformer; private final RemoteDeviceId deviceId; - public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId, - final RemoteDeviceCommunicator listener) { + public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId, final RemoteDeviceCommunicator listener, + final BaseRpcSchemalessTransformer baseRpcTransformer, + final SchemalessMessageTransformer messageTransformer) { this.deviceId = deviceId; this.listener = listener; - final MessageCounter counter = new MessageCounter(); - baseRpcTransformer = new BaseRpcSchemalessTransformer(counter); - schemalessTransformer = new SchemalessMessageTransformer(counter); + this.baseRpcTransformer = baseRpcTransformer; + schemalessTransformer = messageTransformer; + } + + @Override + public ListenableFuture invokeNetconf(final QName type, final ContainerNode input) { + if (!isBaseRpc(type)) { + throw new IllegalArgumentException("Cannot handle " + type); + } + return handleRpc(type, input, baseRpcTransformer); } - @Nonnull @Override - public CheckedFuture invokeRpc(@Nonnull final SchemaPath type, - @Nullable final NormalizedNode input) { - final MessageTransformer transformer; - if (input instanceof AnyXmlNode) { + public ListenableFuture invokeRpc(final QName type, final NormalizedNode input) { + final RpcTransformer transformer; + if (input instanceof DOMSourceAnyxmlNode) { transformer = schemalessTransformer; } else if (isBaseRpc(type)) { transformer = baseRpcTransformer; } else { - return Futures.immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("Unable to invoke rpc %s", type)); + return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException( + "Unable to invoke rpc %s", type)); } return handleRpc(type, input, transformer); } - private CheckedFuture handleRpc(@Nonnull final SchemaPath type, - @Nullable final NormalizedNode input, - final MessageTransformer transformer) { - final NetconfMessage netconfMessage = transformer.toRpcRequest(type, input); - final ListenableFuture> rpcResultListenableFuture = listener.sendRequest(netconfMessage, type.getLastComponent()); - - final ListenableFuture transformed = Futures.transform(rpcResultListenableFuture, new Function, DOMRpcResult>() { + private @NonNull ListenableFuture handleRpc(final @NonNull QName type, + final @NonNull NormalizedNode input, final RpcTransformer transformer) { + final var delegateFuture = listener.sendRequest(transformer.toRpcRequest(type, input), type); + final var ret = SettableFuture.create(); + Futures.addCallback(delegateFuture, new FutureCallback<>() { @Override - public DOMRpcResult apply(final RpcResult input) { - if (input.isSuccessful()) { - return transformer.toRpcResult(input.getResult(), type); - } else { - return new DefaultDOMRpcResult(input.getErrors()); - } + public void onSuccess(final RpcResult result) { + ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type) + : new DefaultDOMRpcResult(result.getErrors())); } - }); - return Futures.makeChecked(transformed, new Function() { - @Nullable @Override - public DOMRpcException apply(@Nullable final Exception e) { - return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s on device %s", type, deviceId); + public void onFailure(final Throwable cause) { + ret.setException(new DOMRpcImplementationNotAvailableException(cause, + "Unable to invoke rpc %s on device %s", type, deviceId)); } - }); - } - - private boolean isBaseRpc(final SchemaPath type) { - return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getLastComponent().getNamespace()); + }, MoreExecutors.directExecutor()); + return ret; } - @Nonnull - @Override - public ListenerRegistration registerRpcListener(@Nonnull final T listener) { - throw new UnsupportedOperationException("Not available for netconf 1.0"); + private static boolean isBaseRpc(final QName type) { + return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getNamespace()); } - }