2 * Copyright (c) 2021 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.yangtools.rfc8040.parser;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Collection;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.rfc8040.model.api.YangDataConstants;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
27 * An {@link InferenceAction} tasked with identifying when we are dealing with {@link YangDataConstants#RFC8040_SOURCE}.
29 final class OperationsValidateModuleAction implements InferenceAction {
30 private static final String IETF_RESTCONF = YangDataConstants.RFC8040_SOURCE.getName();
32 private final Prerequisite<? extends Mutable<?, ?, ?>> prereq;
34 private OperationsValidateModuleAction(final Prerequisite<? extends Mutable<?, ?, ?>> prereq) {
35 this.prereq = requireNonNull(prereq);
38 static void applyTo(@NonNull final Mutable<?, ?, ?> module) {
39 // Quick checks we can
40 if (module.producesDeclared(ModuleStatement.class) && IETF_RESTCONF.equals(module.rawArgument())) {
41 // This is 'yang-api' definition within a 'ietf-restconf' module, but we are not certain about revisions
42 // and its structure. Next up we require the module to be fully declared, hence an inference action is
43 // needed to continue this process.
44 final var action = module.newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
45 final var prereq = action.mutatesEffectiveCtx(module);
47 action.apply(new OperationsValidateModuleAction(prereq));
52 public void apply(final InferenceContext ctx) {
53 final Mutable<?, ?, ?> moduleCtx = prereq.resolve(ctx);
55 // Check namespace and revision first
56 final QNameModule moduleQName = moduleCtx.getFromNamespace(ModuleCtxToModuleQName.class, moduleCtx);
57 if (!YangDataConstants.RFC8040_MODULE.equals(moduleQName)) {
61 // Now carefully locate the operations container:
63 // grouping restconf {
64 // container restconf {
65 // container operations;
69 for (var moduleSub : moduleCtx.mutableDeclaredSubstatements()) {
70 if (moduleSub.producesDeclared(GroupingStatement.class) && "restconf".equals(moduleSub.rawArgument())) {
71 for (var grpSub : moduleSub.mutableDeclaredSubstatements()) {
72 if (grpSub.producesDeclared(ContainerStatement.class) && "restconf".equals(grpSub.rawArgument())) {
73 for (var contSub : grpSub.mutableDeclaredSubstatements()) {
74 if (contSub.producesDeclared(ContainerStatement.class)
75 && "operations".equals(contSub.rawArgument())) {
76 // Alright, we have a match. Hook the second stage of processing.
77 OperationsCreateLeafStatements.applyTo(moduleCtx, contSub);
88 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
89 // We do not really need to fail, as this means reactor will fail anyway