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