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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.