Enforce augment statement argument well-formedness
[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.base.Verify;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Lists;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Objects;
20 import java.util.Optional;
21 import java.util.regex.Pattern;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Status;
25 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
29 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
34 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
35 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
40 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
41 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException;
42 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
54 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
55 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
56 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
57 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 abstract class AbstractAugmentStatementSupport
62         extends BaseStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
63     private static final Logger LOG = LoggerFactory.getLogger(AbstractAugmentStatementSupport.class);
64     private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
65     private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
66
67     AbstractAugmentStatementSupport() {
68         super(YangStmtMapping.AUGMENT);
69     }
70
71     @Override
72     public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
73         SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
74             || PATH_REL_PATTERN2.matcher(value).matches(), ctx.sourceReference(),
75             "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses",
76             value);
77
78         // As per:
79         //   https://tools.ietf.org/html/rfc6020#section-7.15
80         //   https://tools.ietf.org/html/rfc7950#section-7.17
81         //
82         // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
83         // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
84         // is in the correct domain.
85         final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
86         final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
87         if (parent == YangStmtMapping.USES) {
88             SourceException.throwIf(result instanceof Absolute, ctx.sourceReference(),
89                 "Absolute schema node identifier is not allowed when used within a uses statement");
90         } else {
91             SourceException.throwIf(result instanceof Descendant, ctx.sourceReference(),
92                 "Descendant schema node identifier is not allowed when used outside of a uses statement");
93         }
94         return result;
95     }
96
97     @Override
98     public final void onFullDefinitionDeclared(
99             final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode) {
100         if (!augmentNode.isSupportedByFeatures()) {
101             // We need this augment node to be present, but it should not escape to effective world
102             augmentNode.setIsSupportedToBuildEffective(false);
103         }
104
105         super.onFullDefinitionDeclared(augmentNode);
106
107         if (StmtContextUtils.isInExtensionBody(augmentNode)) {
108             return;
109         }
110
111         final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
112         augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
113         final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtxPath(
114             getSearchRoot(augmentNode), SchemaTreeNamespace.class, augmentNode.getArgument().getNodeIdentifiers());
115
116         augmentAction.apply(new InferenceAction() {
117             @Override
118             public void apply(final InferenceContext ctx) {
119                 final StatementContextBase<?, ?, ?> augmentTargetCtx =
120                         (StatementContextBase<?, ?, ?>) target.resolve(ctx);
121                 if (!isSupportedAugmentTarget(augmentTargetCtx)
122                         || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
123                     augmentNode.setIsSupportedToBuildEffective(false);
124                     return;
125                 }
126
127                 // We are targeting a context which is creating implicit nodes. In order to keep things consistent,
128                 // we will need to circle back when creating effective statements.
129                 if (augmentTargetCtx.hasImplicitParentSupport()) {
130                     augmentNode.addToNs(AugmentImplicitHandlingNamespace.class, augmentNode, augmentTargetCtx);
131                 }
132
133                 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
134                 // FIXME: this is a workaround for models which augment a node which is added via an extension
135                 //        which we do not handle. This needs to be reworked in terms of unknown schema nodes.
136                 try {
137                     copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
138                     augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
139                 } catch (final SourceException e) {
140                     LOG.warn("Failed to add augmentation {} defined at {}", augmentTargetCtx.sourceReference(),
141                             augmentSourceCtx.sourceReference(), e);
142                 }
143             }
144
145             @Override
146             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
147                 /*
148                  * Do not fail, if it is an uses-augment to an unknown node.
149                  */
150                 if (YangStmtMapping.USES == augmentNode.coerceParentContext().publicDefinition()) {
151                     final SchemaNodeIdentifier augmentArg = augmentNode.getArgument();
152                     final Optional<StmtContext<?, ?, ?>> targetNode = SchemaTreeNamespace.findNode(
153                         getSearchRoot(augmentNode), augmentArg);
154                     if (targetNode.isPresent() && StmtContextUtils.isUnknownStatement(targetNode.get())) {
155                         augmentNode.setIsSupportedToBuildEffective(false);
156                         LOG.warn("Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
157                             augmentArg, augmentNode.sourceReference());
158                         return;
159                     }
160                 }
161
162                 throw new InferenceException(augmentNode.sourceReference(), "Augment target '%s' not found",
163                     augmentNode.argument());
164             }
165         });
166     }
167
168     @Override
169     protected final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx,
170             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
171         return new RegularAugmentStatement(ctx.getRawArgument(), ctx.getArgument(), substatements);
172     }
173
174     @Override
175     protected final AugmentStatement createEmptyDeclared(
176             final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
177         return new EmptyAugmentStatement(ctx.getRawArgument(), ctx.getArgument());
178     }
179
180     @Override
181     protected final List<? extends StmtContext<?, ?, ?>> statementsToBuild(
182             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
183             final List<? extends StmtContext<?, ?, ?>> substatements) {
184         // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
185         // children through target's implicit wrapping.
186         final StatementContextBase<?, ?, ?> implicitDef = stmt.getFromNamespace(AugmentImplicitHandlingNamespace.class,
187             stmt.caerbannog());
188         return implicitDef == null ? substatements : Lists.transform(substatements, subCtx -> {
189             verify(subCtx instanceof StatementContextBase);
190             return implicitDef.wrapWithImplicit((StatementContextBase<?, ?, ?>) subCtx);
191         });
192     }
193
194     @Override
195     protected final AugmentEffectiveStatement createEffective(
196             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
197             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
198         final int flags = new FlagsBuilder()
199                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
200                 .toFlags();
201
202         try {
203             return new AugmentEffectiveStatementImpl(stmt.declared(), stmt.getArgument(), flags,
204                 StmtContextUtils.getRootModuleQName(stmt.caerbannog()), substatements,
205                 (AugmentationSchemaNode) stmt.original());
206         } catch (SubstatementIndexingException e) {
207             throw new SourceException(e.getMessage(), stmt.sourceReference(), e);
208         }
209     }
210
211     private static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
212         // Augment is in uses - we need to augment instantiated nodes in parent.
213         final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
214         if (YangStmtMapping.USES == parent.publicDefinition()) {
215             return parent.getParentContext();
216         }
217         return parent;
218     }
219
220     final void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
221             final StatementContextBase<?, ?, ?> targetCtx) {
222         final CopyType typeOfCopy = sourceCtx.coerceParentContext().producesDeclared(UsesStatement.class)
223                 ? CopyType.ADDED_BY_USES_AUGMENTATION : CopyType.ADDED_BY_AUGMENTATION;
224         /*
225          * Since Yang 1.1, if an augmentation is made conditional with a
226          * "when" statement, it is allowed to add mandatory nodes.
227          */
228         final boolean skipCheckOfMandatoryNodes = allowsMandatory(sourceCtx);
229         final boolean unsupported = !sourceCtx.isSupportedByFeatures();
230
231         final Collection<? extends Mutable<?, ?, ?>> declared = sourceCtx.mutableDeclaredSubstatements();
232         final Collection<? extends Mutable<?, ?, ?>> effective = sourceCtx.mutableEffectiveSubstatements();
233         final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
234
235         for (final Mutable<?, ?, ?> originalStmtCtx : declared) {
236             copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes,
237                 unsupported || !originalStmtCtx.isSupportedByFeatures());
238         }
239         for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
240             copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes, unsupported);
241         }
242
243         targetCtx.addEffectiveSubstatements(buffer);
244     }
245
246     abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
247
248     static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
249         return ctx.hasSubstatement(WhenEffectiveStatement.class);
250     }
251
252     private static void copyStatement(final Mutable<?, ?, ?> original, final StatementContextBase<?, ?, ?> target,
253             final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer,
254             final boolean skipCheckOfMandatoryNodes, final boolean unsupported) {
255         // We always copy statements, but if either the source statement or the augmentation which causes it are not
256         // supported to build we also mark the target as such.
257         if (needToCopyByAugment(original)) {
258             validateNodeCanBeCopiedByAugment(original, target, typeOfCopy, skipCheckOfMandatoryNodes);
259
260             final Mutable<?, ?, ?> copy = target.childCopyOf(original, typeOfCopy);
261             if (unsupported) {
262                 copy.setIsSupportedToBuildEffective(false);
263             }
264             buffer.add(copy);
265         } else if (isReusedByAugment(original) && !unsupported) {
266             buffer.add(original);
267         }
268     }
269
270     private static void validateNodeCanBeCopiedByAugment(final StmtContext<?, ?, ?> sourceCtx,
271             final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy,
272             final boolean skipCheckOfMandatoryNodes) {
273         if (!skipCheckOfMandatoryNodes && typeOfCopy == CopyType.ADDED_BY_AUGMENTATION
274                 && requireCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
275             checkForMandatoryNodes(sourceCtx);
276         }
277
278         // Data definition statements must not collide on their namespace
279         if (sourceCtx.producesDeclared(DataDefinitionStatement.class)) {
280             for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
281                 if (subStatement.producesDeclared(DataDefinitionStatement.class)) {
282                     InferenceException.throwIf(Objects.equals(sourceCtx.argument(), subStatement.argument()),
283                         sourceCtx.sourceReference(),
284                         "An augment cannot add node named '%s' because this name is already used in target",
285                         sourceCtx.rawArgument());
286                 }
287             }
288         }
289     }
290
291     private static void checkForMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx) {
292         if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
293             /*
294              * We need to iterate over both declared and effective sub-statements,
295              * because a mandatory node can be:
296              * a) declared in augment body
297              * b) added to augment body also via uses of a grouping and
298              * such sub-statements are stored in effective sub-statements collection.
299              */
300             sourceCtx.allSubstatementsStream().forEach(AbstractAugmentStatementSupport::checkForMandatoryNodes);
301         }
302
303         InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx), sourceCtx.sourceReference(),
304             "An augment cannot add node '%s' because it is mandatory and in module different than target",
305             sourceCtx.rawArgument());
306     }
307
308     private static boolean requireCheckOfMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx,
309             Mutable<?, ?, ?> targetCtx) {
310         /*
311          * If the statement argument is not QName, it cannot be mandatory
312          * statement, therefore return false and skip mandatory nodes validation
313          */
314         final Object arg = sourceCtx.argument();
315         if (!(arg instanceof QName)) {
316             return false;
317         }
318         final QName sourceStmtQName = (QName) arg;
319
320         // RootStatementContext, for example
321         final Mutable<?, ?, ?> root = targetCtx.getRoot();
322         do {
323             final Object targetArg = targetCtx.argument();
324             verify(targetArg instanceof QName, "Argument of augment target statement must be QName, not %s", targetArg);
325             final QName targetStmtQName = (QName) targetArg;
326             /*
327              * If target is from another module, return true and perform mandatory nodes validation
328              */
329             if (!targetStmtQName.getModule().equals(sourceStmtQName.getModule())) {
330                 return true;
331             }
332
333             /*
334              * If target or one of the target's ancestors from the same namespace
335              * is a presence container
336              * or is non-mandatory choice
337              * or is non-mandatory list
338              * return false and skip mandatory nodes validation, because these nodes
339              * are not mandatory node containers according to RFC 6020 section 3.1.
340              */
341             if (StmtContextUtils.isPresenceContainer(targetCtx)
342                     || StmtContextUtils.isNotMandatoryNodeOfType(targetCtx, YangStmtMapping.CHOICE)
343                     || StmtContextUtils.isNotMandatoryNodeOfType(targetCtx, YangStmtMapping.LIST)) {
344                 return false;
345             }
346
347             // This could be an augmentation stacked on top of a previous augmentation from the same module, which is
348             // conditional -- in which case we do not run further checks
349             if (targetCtx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
350                 final Optional<? extends StmtContext<?, ?, ?>> optPrevCopy = targetCtx.getPreviousCopyCtx();
351                 if (optPrevCopy.isPresent()) {
352                     final StmtContext<?, ?, ?> original = optPrevCopy.get();
353                     final Object origArg = original.getArgument();
354                     Verify.verify(origArg instanceof QName, "Unexpected statement argument %s", origArg);
355
356                     if (sourceStmtQName.getModule().equals(((QName) origArg).getModule())
357                             && hasWhenSubstatement(getParentAugmentation(original))) {
358                         return false;
359                     }
360                 }
361             }
362         } while ((targetCtx = targetCtx.getParentContext()) != root);
363
364         /*
365          * All target node's parents belong to the same module as source node,
366          * therefore return false and skip mandatory nodes validation.
367          */
368         return false;
369     }
370
371     private static StmtContext<?, ?, ?> getParentAugmentation(final StmtContext<?, ?, ?> child) {
372         StmtContext<?, ?, ?> parent = Verify.verifyNotNull(child.getParentContext(), "Child %s has not parent", child);
373         while (parent.publicDefinition() != YangStmtMapping.AUGMENT) {
374             parent = Verify.verifyNotNull(parent.getParentContext(), "Failed to find augmentation parent of %s", child);
375         }
376         return parent;
377     }
378
379     private static final ImmutableSet<YangStmtMapping> NOCOPY_DEF_SET = ImmutableSet.of(YangStmtMapping.USES,
380         YangStmtMapping.WHEN, YangStmtMapping.DESCRIPTION, YangStmtMapping.REFERENCE, YangStmtMapping.STATUS);
381
382     private static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
383         return !NOCOPY_DEF_SET.contains(stmtContext.publicDefinition());
384     }
385
386     private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(YangStmtMapping.TYPEDEF);
387
388     private static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
389         return REUSED_DEF_SET.contains(stmtContext.publicDefinition());
390     }
391
392     static boolean isSupportedAugmentTarget(final StmtContext<?, ?, ?> substatementCtx) {
393         /*
394          * :TODO Substatement must be allowed augment target type e.g.
395          * Container, etc... and must not be for example grouping, identity etc.
396          * It is problem in case when more than one substatements have the same
397          * QName, for example Grouping and Container are siblings and they have
398          * the same QName. We must find the Container and the Grouping must be
399          * ignored as disallowed augment target.
400          */
401         final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(
402             ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
403
404         // if no allowed target is returned we consider all targets allowed
405         return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
406                 || allowedAugmentTargets.contains(substatementCtx.publicDefinition());
407     }
408 }