2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.netconf.client.mdsal.spi;
10 import static java.util.Objects.requireNonNull;
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;
34 record NetconfDeviceDOMRpcService(
35 EffectiveModelContext modelContext,
36 RemoteDeviceCommunicator communicator,
37 RpcTransformer<ContainerNode, DOMRpcResult> transformer) implements DOMRpcService {
38 NetconfDeviceDOMRpcService {
39 requireNonNull(modelContext);
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);
47 final var ret = SettableFuture.<DOMRpcResult>create();
48 Futures.addCallback(delegateFuture, new FutureCallback<>() {
50 public void onSuccess(final RpcResult<NetconfMessage> result) {
51 final DOMRpcResult rpcResult;
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));
64 public void onFailure(final Throwable cause) {
65 ret.setException(new DOMRpcImplementationNotAvailableException(cause, "Unable to invoke rpc %s",
69 }, MoreExecutors.directExecutor());
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);