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