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.FluentFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
18 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
19 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
23 import org.opendaylight.netconf.api.NetconfMessage;
24 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
36 public final class NetconfDeviceRpc implements DOMRpcService {
38 private final RemoteDeviceCommunicator<NetconfMessage> communicator;
39 private final MessageTransformer<NetconfMessage> transformer;
40 private final SchemaContext schemaContext;
42 public NetconfDeviceRpc(final SchemaContext schemaContext,
43 final RemoteDeviceCommunicator<NetconfMessage> communicator,
44 final MessageTransformer<NetconfMessage> transformer) {
45 this.communicator = communicator;
46 this.transformer = transformer;
47 this.schemaContext = requireNonNull(schemaContext);
51 public FluentFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
52 final FluentFuture<RpcResult<NetconfMessage>> delegateFuture = communicator.sendRequest(
53 transformer.toRpcRequest(type, input), type.getLastComponent());
55 final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
56 delegateFuture.addCallback(new FutureCallback<RpcResult<NetconfMessage>>() {
58 public void onSuccess(RpcResult<NetconfMessage> result) {
59 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
60 : new DefaultDOMRpcResult(result.getErrors()));
64 public void onFailure(Throwable cause) {
65 ret.setException(new DOMRpcImplementationNotAvailableException(cause, "Unable to invoke rpc %s", type));
68 }, MoreExecutors.directExecutor());
73 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
74 listener.onRpcAvailable(Collections2.transform(schemaContext.getOperations(),
75 input -> DOMRpcIdentifier.create(input.getPath())));
77 // NOOP, no rpcs appear and disappear in this implementation
78 return NoOpListenerRegistration.of(listener);