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