19de2565df65ff525b9ec31882d376f4a0bac3b6
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceRpc.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
10 import static java.util.Objects.requireNonNull;
11
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.DOMRpcService;
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.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33
34 /**
35  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
36  */
37 public final class NetconfDeviceRpc implements DOMRpcService {
38
39     private final RemoteDeviceCommunicator<NetconfMessage> communicator;
40     private final MessageTransformer<NetconfMessage> transformer;
41     private final SchemaContext schemaContext;
42
43     public NetconfDeviceRpc(final SchemaContext schemaContext,
44             final RemoteDeviceCommunicator<NetconfMessage> communicator,
45             final MessageTransformer<NetconfMessage> transformer) {
46         this.communicator = communicator;
47         this.transformer = transformer;
48         this.schemaContext = requireNonNull(schemaContext);
49     }
50
51     @Override
52     public ListenableFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
53         final ListenableFuture<RpcResult<NetconfMessage>> delegateFuture = communicator.sendRequest(
54             transformer.toRpcRequest(type, input), type.getLastComponent());
55
56         final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
57         Futures.addCallback(delegateFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
58             @Override
59             public void onSuccess(final RpcResult<NetconfMessage> result) {
60                 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
61                         : new DefaultDOMRpcResult(result.getErrors()));
62             }
63
64             @Override
65             public void onFailure(final Throwable cause) {
66                 ret.setException(new DOMRpcImplementationNotAvailableException(cause, "Unable to invoke rpc %s", type));
67             }
68
69         }, MoreExecutors.directExecutor());
70         return ret;
71     }
72
73     @Override
74     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
75         listener.onRpcAvailable(Collections2.transform(schemaContext.getOperations(),
76             input -> DOMRpcIdentifier.create(input.getPath())));
77
78         // NOOP, no rpcs appear and disappear in this implementation
79         return NoOpListenerRegistration.of(listener);
80     }
81 }