2 * Copyright (c) 2023 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.mdsal.dom.broker;
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.mdsal.dom.api.DOMActionNotAvailableException;
13 import org.opendaylight.mdsal.dom.api.DOMActionResult;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
15 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
17 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 final class OperationInvocation {
26 private static final Logger LOG = LoggerFactory.getLogger(OperationInvocation.class);
28 private OperationInvocation() {
32 static ListenableFuture<? extends DOMActionResult> invoke(final DOMActionRoutingTableEntry entry,
33 final Absolute type, final DOMDataTreeIdentifier path, final ContainerNode input) {
34 var impls = entry.getImplementations(path);
36 impls = entry.getImplementations(DOMDataTreeIdentifier.of(path.datastore(), YangInstanceIdentifier.of()));
38 return Futures.immediateFailedFuture(new DOMActionNotAvailableException(
39 "No implementation of Action %s available for %s", type, path));
43 return impls.get(0).invokeAction(type, path, input);
46 static ListenableFuture<? extends DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
47 final ContainerNode input) {
48 if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
49 return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
50 "%s is not resolved to an RPC", entry.getType()));
51 } else if (entry instanceof RoutedDOMRpcRoutingTableEntry routed) {
52 return invokeRoutedRpc(routed, input);
53 } else if (entry instanceof GlobalDOMRpcRoutingTableEntry global) {
54 return invokeGlobalRpc(global, input);
57 return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
60 private static ListenableFuture<? extends DOMRpcResult> invokeRoutedRpc(
61 final RoutedDOMRpcRoutingTableEntry entry, final ContainerNode input) {
62 final var maybeKey = NormalizedNodes.findNode(input, entry.getRpcId().getContextReference());
64 // Routing key is present, attempt to deliver as a routed RPC
65 if (maybeKey.isPresent()) {
66 final var key = maybeKey.orElseThrow();
67 final var value = key.body();
68 if (value instanceof YangInstanceIdentifier iid) {
69 // Find a DOMRpcImplementation for a specific iid
70 final var specificImpls = entry.getImplementations(iid);
71 if (specificImpls != null) {
72 return specificImpls.get(0)
73 .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
76 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
78 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
79 // implementation this way
80 final var mayBeRemoteImpls = entry.getImplementations(YangInstanceIdentifier.of());
81 if (mayBeRemoteImpls != null) {
82 return mayBeRemoteImpls.get(0)
83 .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
87 LOG.warn("Ignoring wrong context value {}", value);
91 final var impls = entry.getImplementations(null);
93 return impls.get(0).invokeRpc(entry.getRpcId(), input);
96 return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
97 "No implementation of RPC %s available", entry.getType()));
100 private static ListenableFuture<? extends DOMRpcResult> invokeGlobalRpc(
101 final GlobalDOMRpcRoutingTableEntry entry, final ContainerNode input) {
102 return entry.getImplementations(YangInstanceIdentifier.of()).get(0).invokeRpc(entry.getRpcId(), input);