Merge "Remove blocking checkedGet call"
[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 com.google.common.base.Function;
11 import com.google.common.collect.Collections2;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Collection;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
24 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
25 import org.opendaylight.netconf.api.NetconfMessage;
26 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34
35 /**
36  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
37  */
38 public final class NetconfDeviceRpc implements DOMRpcService {
39
40     private static final Function<RpcDefinition, DOMRpcIdentifier> RPC_TO_RPC_IDENTIFIER = new Function<RpcDefinition, DOMRpcIdentifier>() {
41         @Override
42         public DOMRpcIdentifier apply(final RpcDefinition input) {
43             return DOMRpcIdentifier.create(input.getPath());
44         }
45     };
46
47     private final RemoteDeviceCommunicator<NetconfMessage> listener;
48     private final MessageTransformer<NetconfMessage> transformer;
49     private final Collection<DOMRpcIdentifier> availableRpcs;
50
51     public NetconfDeviceRpc(final SchemaContext schemaContext, final RemoteDeviceCommunicator<NetconfMessage> listener, final MessageTransformer<NetconfMessage> transformer) {
52         this.listener = listener;
53         this.transformer = transformer;
54
55         availableRpcs = Collections2.transform(schemaContext.getOperations(), RPC_TO_RPC_IDENTIFIER);
56     }
57
58     @Nonnull
59     @Override
60     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
61         final NetconfMessage message = transformer.toRpcRequest(type, input);
62         final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult = listener.sendRequest(message, type.getLastComponent());
63
64         final ListenableFuture<DOMRpcResult> transformed = Futures.transform(delegateFutureWithPureResult, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
65             @Override
66             public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
67                 if (input.isSuccessful()) {
68                     return transformer.toRpcResult(input.getResult(), type);
69                 } else {
70                     // TODO check whether the listener sets errors properly
71                     return new DefaultDOMRpcResult(input.getErrors());
72                 }
73             }
74         });
75
76         return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
77             @Nullable
78             @Override
79             public DOMRpcException apply(@Nullable final Exception e) {
80                 // FIXME what other possible exceptions are there ?
81                 return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s", type);
82             }
83         });
84     }
85
86     @Nonnull
87     @Override
88     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
89
90         listener.onRpcAvailable(availableRpcs);
91
92         return new ListenerRegistration<T>() {
93             @Override
94             public void close() {
95                 // NOOP, no rpcs appear and disappear in this implementation
96             }
97
98             @Override
99             public T getInstance() {
100                 return listener;
101             }
102         };
103     }
104 }