2a70a9929b54ce5c0b2b74fc219999f3dca66fd9
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / augment / AbstractAugmentStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, 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.yang.parser.rfc7950.stmt.augment;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.regex.Pattern;
12 import java.util.stream.Stream;
13 import org.opendaylight.yangtools.yang.common.Empty;
14 import org.opendaylight.yangtools.yang.model.api.Status;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
25 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
28 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
29 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
30 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
31 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
32 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46
47 abstract class AbstractAugmentStatementSupport
48         extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
49     private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
50     private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
51
52     AbstractAugmentStatementSupport(final YangParserConfiguration config, final SubstatementValidator validator) {
53         super(YangStmtMapping.AUGMENT, StatementPolicy.copyDeclared(
54             (copy, current, substatements) ->
55                 copy.getArgument().equals(current.getArgument())
56                 && copy.moduleName().getModule().equals(current.moduleName().getModule())
57             ), config, validator);
58     }
59
60     @Override
61     public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
62         SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
63             || PATH_REL_PATTERN2.matcher(value).matches(), ctx,
64             "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses", value);
65
66         // As per:
67         //   https://www.rfc-editor.org/rfc/rfc6020#section-7.15
68         //   https://www.rfc-editor.org/rfc/rfc7950#section-7.17
69         //
70         // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
71         // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
72         // is in the correct domain.
73         final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
74         final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
75         if (parent == YangStmtMapping.USES) {
76             SourceException.throwIf(result instanceof Absolute, ctx,
77                 "Absolute schema node identifier is not allowed when used within a uses statement");
78         } else {
79             SourceException.throwIf(result instanceof Descendant, ctx,
80                 "Descendant schema node identifier is not allowed when used outside of a uses statement");
81         }
82         return result;
83     }
84
85     @Override
86     public final void onFullDefinitionDeclared(
87             final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode) {
88         if (!augmentNode.isSupportedByFeatures()) {
89             // We need this augment node to be present, but it should not escape to effective world
90             augmentNode.setUnsupported();
91         }
92
93         super.onFullDefinitionDeclared(augmentNode);
94
95         if (StmtContextUtils.isInExtensionBody(augmentNode)) {
96             return;
97         }
98
99         final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
100         augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
101         final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtxPath(
102             getSearchRoot(augmentNode), ParserNamespaces.schemaTree(), augmentNode.getArgument().getNodeIdentifiers());
103
104         augmentAction.apply(new AugmentInferenceAction(this, augmentNode, target));
105     }
106
107     @Override
108     protected final AugmentStatement createDeclared(final BoundStmtCtx<SchemaNodeIdentifier> ctx,
109             final ImmutableList<DeclaredStatement<?>> substatements) {
110         return DeclaredStatements.createAugment(ctx.getRawArgument(), ctx.getArgument(), substatements);
111     }
112
113     @Override
114     protected final AugmentStatement attachDeclarationReference(final AugmentStatement stmt,
115             final DeclarationReference reference) {
116         return DeclaredStatementDecorators.decorateAugment(stmt, reference);
117     }
118
119     @Override
120     protected final Stream<? extends StmtContext<?, ?, ?>> statementsToBuild(
121             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
122             final Stream<? extends StmtContext<?, ?, ?>> substatements) {
123         // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
124         // children through target's implicit wrapping.
125         final var implicitDef = stmt.namespaceItem(AugmentImplicitHandlingNamespace.INSTANCE, Empty.value());
126         return implicitDef == null ? substatements
127             : substatements.map(subCtx -> implicitDef.wrapWithImplicit(subCtx));
128     }
129
130     @Override
131     protected final AugmentEffectiveStatement createEffective(
132             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
133             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
134         final int flags = new FlagsBuilder()
135                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
136                 .toFlags();
137
138         try {
139             return EffectiveStatements.createAugment(stmt.declared(), stmt.getArgument(), flags,
140                 stmt.moduleName().getModule(), substatements);
141         } catch (SubstatementIndexingException e) {
142             throw new SourceException(e.getMessage(), stmt, e);
143         }
144     }
145
146     abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
147
148     static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
149         // Augment is in uses - we need to augment instantiated nodes in parent.
150         final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
151         if (YangStmtMapping.USES == parent.publicDefinition()) {
152             return parent.getParentContext();
153         }
154         return parent;
155     }
156
157     static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
158         return ctx.hasSubstatement(WhenEffectiveStatement.class);
159     }
160 }