*/
package org.opendaylight.netconf.sal.connect.netconf.sal;
-import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
*/
public final class NetconfDeviceRpc implements DOMRpcService {
- private static final Function<RpcDefinition, DOMRpcIdentifier> RPC_TO_RPC_IDENTIFIER = new Function<RpcDefinition, DOMRpcIdentifier>() {
- @Override
- public DOMRpcIdentifier apply(final RpcDefinition input) {
- return DOMRpcIdentifier.create(input.getPath());
- }
- };
-
- private final RemoteDeviceCommunicator<NetconfMessage> listener;
+ private final RemoteDeviceCommunicator<NetconfMessage> communicator;
private final MessageTransformer<NetconfMessage> transformer;
private final Collection<DOMRpcIdentifier> availableRpcs;
- public NetconfDeviceRpc(final SchemaContext schemaContext, final RemoteDeviceCommunicator<NetconfMessage> listener, final MessageTransformer<NetconfMessage> transformer) {
- this.listener = listener;
+ public NetconfDeviceRpc(final SchemaContext schemaContext,
+ final RemoteDeviceCommunicator<NetconfMessage> communicator,
+ final MessageTransformer<NetconfMessage> transformer) {
+ this.communicator = communicator;
this.transformer = transformer;
- availableRpcs = Collections2.transform(schemaContext.getOperations(), RPC_TO_RPC_IDENTIFIER);
+ availableRpcs = Collections2.transform(schemaContext.getOperations(),
+ input -> DOMRpcIdentifier.create(input.getPath()));
}
@Nonnull
@Override
- public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
+ public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
+ @Nullable final NormalizedNode<?, ?> input) {
final NetconfMessage message = transformer.toRpcRequest(type, input);
- final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult = listener.sendRequest(message, type.getLastComponent());
+ final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult =
+ communicator.sendRequest(message, type.getLastComponent());
- final ListenableFuture<DOMRpcResult> transformed = Futures.transform(delegateFutureWithPureResult, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
- @Override
- public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
- if (input.isSuccessful()) {
- return transformer.toRpcResult(input.getResult(), type);
+ final ListenableFuture<DOMRpcResult> transformed =
+ Futures.transform(delegateFutureWithPureResult, input1 -> {
+ if (input1.isSuccessful()) {
+ return transformer.toRpcResult(input1.getResult(), type);
} else {
- // TODO check whether the listener sets errors properly
- return new DefaultDOMRpcResult(input.getErrors());
+ return new DefaultDOMRpcResult(input1.getErrors());
}
- }
- });
+ }, MoreExecutors.directExecutor());
- return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
- @Nullable
- @Override
- public DOMRpcException apply(@Nullable final Exception e) {
- // FIXME what other possible exceptions are there ?
- return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s", type);
- }
- });
+ return Futures.makeChecked(transformed, exception ->
+ new DOMRpcImplementationNotAvailableException(exception, "Unable to invoke rpc %s", type));
}
@Nonnull
@Override
- public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
+ public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
+ @Nonnull final T listener) {
listener.onRpcAvailable(availableRpcs);