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