2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.Collections2;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import com.google.common.util.concurrent.SettableFuture;
18 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
19 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
20 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
21 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
22 import org.opendaylight.mdsal.dom.api.DefaultDOMRpcException;
23 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
26 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to
37 * {@link NormalizedNode}.
39 public final class NetconfDeviceRpc implements Rpcs.Normalized {
40 private final RemoteDeviceCommunicator communicator;
41 private final MessageTransformer transformer;
42 private final EffectiveModelContext modelContext;
44 public NetconfDeviceRpc(final EffectiveModelContext modelContext, final RemoteDeviceCommunicator communicator,
45 final MessageTransformer transformer) {
46 this.modelContext = requireNonNull(modelContext);
47 this.communicator = communicator;
48 this.transformer = transformer;
52 @SuppressWarnings("checkstyle:IllegalCatch")
53 public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
54 final ListenableFuture<RpcResult<NetconfMessage>> delegateFuture = communicator.sendRequest(
55 transformer.toRpcRequest(type, input), type);
57 final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
58 Futures.addCallback(delegateFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
60 public void onSuccess(final RpcResult<NetconfMessage> result) {
62 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
63 : new DefaultDOMRpcResult(result.getErrors()));
64 } catch (Exception cause) {
65 ret.setException(new DefaultDOMRpcException(
66 "Unable to parse rpc reply. type: " + type + " input: " + input, cause));
71 public void onFailure(final Throwable cause) {
72 ret.setException(new DOMRpcImplementationNotAvailableException(cause, "Unable to invoke rpc %s", type));
75 }, MoreExecutors.directExecutor());
80 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
81 listener.onRpcAvailable(Collections2.transform(modelContext.getOperations(),
82 input -> DOMRpcIdentifier.create(input.getQName())));
84 // NOOP, no rpcs appear and disappear in this implementation
85 return NoOpListenerRegistration.of(listener);