80ae818550fd6ea241544706c64b2c5f3032eebe
[yangtools.git] / parser / rfc8040-parser-support / src / main / java / org / opendaylight / yangtools / rfc8040 / parser / OperationsValidateModuleAction.java
1 /*
2  * Copyright (c) 2021 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.yangtools.rfc8040.parser;
9
10 import static java.util.Objects.requireNonNull;
11
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;
25
26 /**
27  * An {@link InferenceAction} tasked with identifying when we are dealing with {@link YangDataConstants#RFC8040_SOURCE}.
28  */
29 final class OperationsValidateModuleAction implements InferenceAction {
30     private static final String IETF_RESTCONF = YangDataConstants.RFC8040_SOURCE.getName();
31
32     private final Prerequisite<? extends Mutable<?, ?, ?>> prereq;
33
34     private OperationsValidateModuleAction(final Prerequisite<? extends Mutable<?, ?, ?>> prereq) {
35         this.prereq = requireNonNull(prereq);
36     }
37
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);
46
47             action.apply(new OperationsValidateModuleAction(prereq));
48         }
49     }
50
51     @Override
52     public void apply(final InferenceContext ctx) {
53         final Mutable<?, ?, ?> moduleCtx = prereq.resolve(ctx);
54
55         // Check namespace and revision first
56         final QNameModule moduleQName = moduleCtx.getFromNamespace(ModuleCtxToModuleQName.class, moduleCtx);
57         if (!YangDataConstants.RFC8040_MODULE.equals(moduleQName)) {
58             return;
59         }
60
61         // Now carefully locate the operations container:
62         //
63         //   grouping restconf {
64         //     container restconf {
65         //       container operations;
66         //     }
67         //   }
68         //
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);
78                                 return;
79                             }
80                         }
81                     }
82                 }
83             }
84         }
85     }
86
87     @Override
88     public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
89         // We do not really need to fail, as this means reactor will fail anyway
90     }
91 }