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