Merge "Bug 1073: Added Transaction Chain support to InMemoryDataTreeModification."
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.sal.connect.netconf.sal;
9
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.Futures;
12 import java.util.Collections;
13 import java.util.Set;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Executor;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import javax.annotation.Nullable;
20 import org.opendaylight.controller.netconf.api.NetconfMessage;
21 import org.opendaylight.controller.sal.common.util.Rpcs;
22 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
23 import org.opendaylight.controller.sal.connect.api.RemoteDeviceCommunicator;
24 import org.opendaylight.controller.sal.core.api.RpcImplementation;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28
29 import com.google.common.util.concurrent.ListenableFuture;
30
31 /**
32  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
33  */
34 public final class NetconfDeviceRpc implements RpcImplementation {
35     private final RemoteDeviceCommunicator<NetconfMessage> listener;
36     private final MessageTransformer<NetconfMessage> transformer;
37
38     public NetconfDeviceRpc(final RemoteDeviceCommunicator<NetconfMessage> listener, final MessageTransformer<NetconfMessage> transformer) {
39         this.listener = listener;
40         this.transformer = transformer;
41     }
42
43     @Override
44     public Set<QName> getSupportedRpcs() {
45         // TODO is this correct ?
46         return Collections.emptySet();
47     }
48
49     @Override
50     public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(final QName rpc, final CompositeNode input) {
51         final NetconfMessage message = transformRequest(rpc, input);
52         final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult = listener.sendRequest(
53                 message, rpc);
54
55
56         return Futures.transform(delegateFutureWithPureResult, new Function<RpcResult<NetconfMessage>, RpcResult<CompositeNode>>() {
57             @Override
58             public RpcResult<CompositeNode> apply(@Nullable final RpcResult<NetconfMessage> input) {
59                 return transformResult(input, rpc);
60             }
61         });
62     }
63
64     private NetconfMessage transformRequest(final QName rpc, final CompositeNode input) {
65         return transformer.toRpcRequest(rpc, input);
66     }
67
68     private RpcResult<CompositeNode> transformResult(final RpcResult<NetconfMessage> netconfMessageRpcResult,
69                                                                   final QName rpc) {
70         if (netconfMessageRpcResult.isSuccessful()) {
71             return transformer.toRpcResult(netconfMessageRpcResult.getResult(), rpc);
72         } else {
73             return Rpcs.getRpcResult(false, netconfMessageRpcResult.getErrors());
74         }
75     }
76
77 }