Add DataPatchPath
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / OperationsPostPath.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.restconf.server.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNullByDefault;
13 import org.opendaylight.restconf.api.ApiPath;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
19 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
20
21 /**
22  * An {@link ApiPath} subpath of {@code /operations} {@code POST} HTTP operation, as defined in
23  * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-3.6">RFC8040 Operation Resource</a> and
24  * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-4.4.2">RFC8040 Invoke Operation Mode</a>.
25  *
26  * @param databind Associated {@link DatabindContext}
27  * @param operation Associated {@link Inference} pointing to the {@link EffectiveStatement} of an {@code rpc} or an
28  *                  {@code action} invocation, inference pointing to the statement
29  * @see OperationsPostResult
30  */
31 @NonNullByDefault
32 public record OperationsPostPath(DatabindContext databind, Inference operation) implements DatabindAware {
33     public OperationsPostPath {
34         requireNonNull(databind);
35         requireNonNull(operation);
36     }
37
38     public Inference input() {
39         final var stack = operation.toSchemaInferenceStack();
40         stack.enterDataTree(inputQName(stack));
41         return stack.toInference();
42     }
43
44     public QName inputQName() {
45         return inputQName(operation.toSchemaInferenceStack());
46     }
47
48     private static QName inputQName(final SchemaInferenceStack stack) {
49         final var stmt = stack.currentStatement();
50         if (stmt instanceof RpcEffectiveStatement rpc) {
51             return rpc.input().argument();
52         } else if (stmt instanceof ActionEffectiveStatement action) {
53             return action.input().argument();
54         } else {
55             throw new IllegalStateException(stack + " does not identify an 'rpc' nor an 'action' statement");
56         }
57     }
58 }