Remove DOMRpcRouterServices
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / OperationInvocation.java
1 /*
2  * Copyright (c) 2023 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.mdsal.dom.broker;
9
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;
24
25 final class OperationInvocation {
26     private static final Logger LOG = LoggerFactory.getLogger(OperationInvocation.class);
27
28     private OperationInvocation() {
29         // hidden on purpose
30     }
31
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);
35         if (impls == null) {
36             impls = entry.getImplementations(
37                 new DOMDataTreeIdentifier(path.getDatastoreType(), YangInstanceIdentifier.of()));
38             if (impls == null) {
39                 return Futures.immediateFailedFuture(new DOMActionNotAvailableException(
40                     "No implementation of Action %s available for %s", type, path));
41             }
42         }
43
44         return impls.get(0).invokeAction(type, path, input);
45     }
46
47     static ListenableFuture<? extends DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
48             final ContainerNode input) {
49         if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
50             return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
51                 "%s is not resolved to an RPC", entry.getType()));
52         } else if (entry instanceof RoutedDOMRpcRoutingTableEntry routed) {
53             return invokeRoutedRpc(routed, input);
54         } else if (entry instanceof GlobalDOMRpcRoutingTableEntry global) {
55             return invokeGlobalRpc(global, input);
56         }
57
58         return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
59     }
60
61     private static ListenableFuture<? extends DOMRpcResult> invokeRoutedRpc(
62             final RoutedDOMRpcRoutingTableEntry entry, final ContainerNode input) {
63         final var maybeKey = NormalizedNodes.findNode(input, entry.getRpcId().getContextReference());
64
65         // Routing key is present, attempt to deliver as a routed RPC
66         if (maybeKey.isPresent()) {
67             final var key = maybeKey.orElseThrow();
68             final var value = key.body();
69             if (value instanceof YangInstanceIdentifier iid) {
70                 // Find a DOMRpcImplementation for a specific iid
71                 final var specificImpls = entry.getImplementations(iid);
72                 if (specificImpls != null) {
73                     return specificImpls.get(0)
74                         .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
75                 }
76
77                 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
78
79                 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
80                 // implementation this way
81                 final var mayBeRemoteImpls = entry.getImplementations(YangInstanceIdentifier.of());
82                 if (mayBeRemoteImpls != null) {
83                     return mayBeRemoteImpls.get(0)
84                         .invokeRpc(DOMRpcIdentifier.create(entry.getType(), iid), input);
85                 }
86
87             } else {
88                 LOG.warn("Ignoring wrong context value {}", value);
89             }
90         }
91
92         final var impls = entry.getImplementations(null);
93         if (impls != null) {
94             return impls.get(0).invokeRpc(entry.getRpcId(), input);
95         }
96
97         return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
98             "No implementation of RPC %s available", entry.getType()));
99     }
100
101     private static ListenableFuture<? extends DOMRpcResult> invokeGlobalRpc(
102             final GlobalDOMRpcRoutingTableEntry entry, final ContainerNode input) {
103         return entry.getImplementations(YangInstanceIdentifier.of()).get(0).invokeRpc(entry.getRpcId(), input);
104     }
105 }