3ef69252f71a60bf719c80a96a34f5c6603d1c07
[netconf.git] /
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.client.mdsal.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.Collections2;
13 import com.google.common.util.concurrent.FutureCallback;
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 com.google.common.util.concurrent.SettableFuture;
18 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
19 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
20 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
21 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
22 import org.opendaylight.mdsal.dom.api.DOMRpcService;
23 import org.opendaylight.mdsal.dom.api.DefaultDOMRpcException;
24 import org.opendaylight.netconf.api.messages.NetconfMessage;
25 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceCommunicator;
26 import org.opendaylight.netconf.client.mdsal.api.RpcTransformer;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33
34 record NetconfDeviceDOMRpcService(
35         EffectiveModelContext modelContext,
36         RemoteDeviceCommunicator communicator,
37         RpcTransformer<ContainerNode, DOMRpcResult> transformer) implements DOMRpcService {
38     NetconfDeviceDOMRpcService {
39         requireNonNull(modelContext);
40     }
41
42     @Override
43     @SuppressWarnings("checkstyle:IllegalCatch")
44     public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
45         final var delegateFuture = communicator.sendRequest(transformer.toRpcRequest(type, input), type);
46
47         final var ret = SettableFuture.<DOMRpcResult>create();
48         Futures.addCallback(delegateFuture, new FutureCallback<>() {
49             @Override
50             public void onSuccess(final RpcResult<NetconfMessage> result) {
51                 final DOMRpcResult rpcResult;
52                 try {
53                     rpcResult = transformer.toRpcResult(result, type);
54                 } catch (Exception cause) {
55                     ret.setException(new DefaultDOMRpcException(
56                         "Unable to parse rpc reply. type: " + type + " input: " + input, cause));
57                     return;
58                 }
59
60                 ret.set(rpcResult);
61             }
62
63             @Override
64             public void onFailure(final Throwable cause) {
65                 ret.setException(new DOMRpcImplementationNotAvailableException(cause, "Unable to invoke rpc %s",
66                     type));
67             }
68
69         }, MoreExecutors.directExecutor());
70         return ret;
71     }
72
73     @Override
74     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
75         listener.onRpcAvailable(Collections2.transform(modelContext.getOperations(),
76             input -> DOMRpcIdentifier.create(input.getQName())));
77         return NoOpListenerRegistration.of(listener);
78     }
79 }