Bug 2444 - add missing API of some declared statements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import com.google.common.base.Verify;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Objects;
15 import java.util.Set;
16 import java.util.regex.Pattern;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
46 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
47 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
48 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
49 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
50 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
51 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
57     private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
58     private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
59     private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
60     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
61             .builder(YangStmtMapping.AUGMENT)
62             .addAny(YangStmtMapping.ANYXML)
63             .addAny(YangStmtMapping.CASE)
64             .addAny(YangStmtMapping.CHOICE)
65             .addAny(YangStmtMapping.CONTAINER)
66             .addOptional(YangStmtMapping.DESCRIPTION)
67             .addAny(YangStmtMapping.IF_FEATURE)
68             .addAny(YangStmtMapping.LEAF)
69             .addAny(YangStmtMapping.LEAF_LIST)
70             .addAny(YangStmtMapping.LIST)
71             .addOptional(YangStmtMapping.REFERENCE)
72             .addOptional(YangStmtMapping.STATUS)
73             .addAny(YangStmtMapping.USES)
74             .addOptional(YangStmtMapping.WHEN)
75             .build();
76
77     protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
78         super(context);
79     }
80
81     public static class Definition extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement,
82             EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
83
84         public Definition() {
85             super(YangStmtMapping.AUGMENT);
86         }
87
88         @Override
89         public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
90             SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
91                 || PATH_REL_PATTERN2.matcher(value).matches(), ctx.getStatementSourceReference(),
92                 "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses",
93                 value);
94
95             return Utils.nodeIdentifierFromPath(ctx, value);
96         }
97
98         @Override
99         public AugmentStatement createDeclared(
100                 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
101             return new AugmentStatementImpl(ctx);
102         }
103
104         @Override
105         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
106                 final StmtContext<SchemaNodeIdentifier, AugmentStatement,
107                 EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
108             return new AugmentEffectiveStatementImpl(ctx);
109         }
110
111         @Override
112         public void onFullDefinitionDeclared(final Mutable<SchemaNodeIdentifier, AugmentStatement,
113                 EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
114             if (!augmentNode.isSupportedByFeatures()) {
115                 return;
116             }
117
118             super.onFullDefinitionDeclared(augmentNode);
119
120             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
121                 return;
122             }
123
124             final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
125                 ModelProcessingPhase.EFFECTIVE_MODEL);
126             final Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement,
127                 EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
128                     augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
129             final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
130                     augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode),
131                         SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
132
133             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
134                 @Override
135                 public void apply(final ModelActionBuilder.InferenceContext ctx) {
136                     final StatementContextBase<?, ?, ?> augmentTargetCtx =
137                             (StatementContextBase<?, ?, ?>) target.resolve(ctx);
138                     if (!isSupportedAugmentTarget(augmentTargetCtx)
139                             || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
140                         augmentNode.setIsSupportedToBuildEffective(false);
141                         return;
142                     }
143                     /**
144                      * Marks case short hand in augment
145                      */
146                     if (augmentTargetCtx.getPublicDefinition() == YangStmtMapping.CHOICE) {
147                         augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, Boolean.TRUE);
148                     }
149
150                     // FIXME: this is a workaround for models which augment a node which is added via an extension
151                     //        which we do not handle. This needs to be reworked in terms of unknown schema nodes.
152                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
153                     try {
154                         copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
155                         augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
156                         updateAugmentOrder(augmentSourceCtx);
157                     } catch (final SourceException e) {
158                         LOG.warn("Failed to add augmentation {} defined at {}",
159                             augmentTargetCtx.getStatementSourceReference(),
160                                 augmentSourceCtx.getStatementSourceReference(), e);
161                     }
162                 }
163
164                 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
165                     Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
166                         YangStmtMapping.AUGMENT);
167                     if (currentOrder == null) {
168                         currentOrder = 1;
169                     } else {
170                         currentOrder++;
171                     }
172
173                     augmentSourceCtx.addToNs(StmtOrderingNamespace.class, YangStmtMapping.AUGMENT, currentOrder);
174                 }
175
176                 @Override
177                 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
178                     /*
179                      * Do not fail, if it is an uses-augment to an unknown node.
180                      */
181                     if (YangStmtMapping.USES == augmentNode.getParentContext().getPublicDefinition()) {
182                         final StatementContextBase<?, ?, ?> targetNode = Utils.findNode(getSearchRoot(augmentNode),
183                                 augmentNode.getStatementArgument());
184                         if (targetNode != null && StmtContextUtils.isUnknownStatement(targetNode)) {
185                             augmentNode.setIsSupportedToBuildEffective(false);
186                             LOG.warn(
187                                     "Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
188                                     augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference());
189                             return;
190                         }
191                     }
192
193                     throw new InferenceException(augmentNode.getStatementSourceReference(),
194                             "Augment target '%s' not found", augmentNode.getStatementArgument());
195                 }
196             });
197         }
198
199         private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
200             final Mutable<?, ?, ?> parent = augmentContext.getParentContext();
201             // Augment is in uses - we need to augment instantiated nodes in parent.
202             if (YangStmtMapping.USES == parent.getPublicDefinition()) {
203                 return parent.getParentContext();
204             }
205             return parent;
206         }
207
208         public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
209                 final StatementContextBase<?, ?, ?> targetCtx) {
210             final CopyType typeOfCopy = UsesStatement.class.equals(sourceCtx.getParentContext().getPublicDefinition()
211                     .getDeclaredRepresentationClass()) ? CopyType.ADDED_BY_USES_AUGMENTATION
212                     : CopyType.ADDED_BY_AUGMENTATION;
213             /*
214              * Since Yang 1.1, if an augmentation is made conditional with a
215              * "when" statement, it is allowed to add mandatory nodes.
216              */
217             final boolean skipCheckOfMandatoryNodes = YangVersion.VERSION_1_1.equals(sourceCtx.getRootVersion())
218                     && isConditionalAugmentStmt(sourceCtx);
219
220             final Collection<? extends Mutable<?, ?, ?>> declared = sourceCtx.mutableDeclaredSubstatements();
221             final Collection<? extends Mutable<?, ?, ?>> effective = sourceCtx.mutableEffectiveSubstatements();
222             final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
223
224             for (final Mutable<?, ?, ?> originalStmtCtx : declared) {
225                 if (originalStmtCtx.isSupportedByFeatures()) {
226                     copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
227                 }
228             }
229             for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
230                 copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
231             }
232
233             targetCtx.addEffectiveSubstatements(buffer);
234         }
235
236         /**
237          * Checks whether supplied statement context is conditional augment
238          * statement.
239          *
240          * @param ctx
241          *            statement context to be checked
242          *
243          * @return true if supplied statement context is conditional augment
244          *         statement, otherwise false
245          */
246         private static boolean isConditionalAugmentStmt(final StmtContext<?, ?, ?> ctx) {
247             return ctx.getPublicDefinition() == YangStmtMapping.AUGMENT
248                     && StmtContextUtils.findFirstSubstatement(ctx, WhenStatement.class) != null;
249         }
250
251         private static void copyStatement(final Mutable<?, ?, ?> original, final StatementContextBase<?, ?, ?> target,
252                 final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer,
253                 final boolean skipCheckOfMandatoryNodes) {
254             if (needToCopyByAugment(original)) {
255                 validateNodeCanBeCopiedByAugment(original, target, typeOfCopy, skipCheckOfMandatoryNodes);
256
257                 buffer.add(target.childCopyOf(original, typeOfCopy));
258             } else if (isReusedByAugment(original)) {
259                 buffer.add(original);
260             }
261         }
262
263         private static void validateNodeCanBeCopiedByAugment(final StmtContext<?, ?, ?> sourceCtx,
264                 final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy,
265                 final boolean skipCheckOfMandatoryNodes) {
266
267             if (WhenStatement.class.equals(sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
268                 return;
269             }
270
271             if (!skipCheckOfMandatoryNodes && typeOfCopy == CopyType.ADDED_BY_AUGMENTATION
272                     && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
273                 checkForMandatoryNodes(sourceCtx);
274             }
275
276             for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
277                 final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
278                         .getPublicDefinition().getDeclaredRepresentationClass());
279                 final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
280                         .getPublicDefinition().getDeclaredRepresentationClass());
281                 final boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
282                         && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
283
284                 InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
285                         "An augment cannot add node named '%s' because this name is already used in target",
286                         sourceCtx.rawStatementArgument());
287             }
288         }
289
290         private static void checkForMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx) {
291             if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
292                 /*
293                  * We need to iterate over both declared and effective sub-statements,
294                  * because a mandatory node can be:
295                  * a) declared in augment body
296                  * b) added to augment body also via uses of a grouping and
297                  * such sub-statements are stored in effective sub-statements collection.
298                  */
299                 sourceCtx.allSubstatementsStream().forEach(Definition::checkForMandatoryNodes);
300             }
301
302             InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx),
303                     sourceCtx.getStatementSourceReference(),
304                     "An augment cannot add node '%s' because it is mandatory and in module different than target",
305                     sourceCtx.rawStatementArgument());
306         }
307
308         private static boolean reguiredCheckOfMandatoryNodes(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             if (!(sourceCtx.getStatementArgument() instanceof QName)) {
315                 return false;
316             }
317             final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
318
319             // RootStatementContext, for example
320             final Mutable<?, ?, ?> root = targetCtx.getRoot();
321             do {
322                 Verify.verify(targetCtx.getStatementArgument() instanceof QName,
323                         "Argument of augment target statement must be QName.");
324                 final QName targetStmtQName = (QName) targetCtx.getStatementArgument();
325                 /*
326                  * If target is from another module, return true and perform
327                  * mandatory nodes validation
328                  */
329                 if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
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             } while ((targetCtx = targetCtx.getParentContext()) != root);
347
348             /*
349              * All target node's parents belong to the same module as source node,
350              * therefore return false and skip mandatory nodes validation.
351              */
352             return false;
353         }
354
355         private static final Set<YangStmtMapping> NOCOPY_DEF_SET = ImmutableSet.of(YangStmtMapping.USES,
356             YangStmtMapping.WHEN, YangStmtMapping.DESCRIPTION, YangStmtMapping.REFERENCE, YangStmtMapping.STATUS);
357
358         public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
359             return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
360         }
361
362         private static final Set<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(YangStmtMapping.TYPEDEF);
363
364         public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
365             return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
366         }
367
368         static boolean isSupportedAugmentTarget(final StmtContext<?, ?, ?> substatementCtx) {
369             /*
370              * :TODO Substatement must be allowed augment target type e.g.
371              * Container, etc... and must not be for example grouping, identity etc.
372              * It is problem in case when more than one substatements have the same
373              * QName, for example Grouping and Container are siblings and they have
374              * the same QName. We must find the Container and the Grouping must be
375              * ignored as disallowed augment target.
376              */
377             final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(
378                 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
379
380             // if no allowed target is returned we consider all targets allowed
381             return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
382                     || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
383         }
384
385         @Override
386         protected SubstatementValidator getSubstatementValidator() {
387             return SUBSTATEMENT_VALIDATOR;
388         }
389     }
390
391     @Nonnull
392     @Override
393     public SchemaNodeIdentifier getTargetNode() {
394         return argument();
395     }
396
397     @Nonnull
398     @Override
399     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
400         return allDeclared(DataDefinitionStatement.class);
401     }
402
403     @Nonnull
404     @Override
405     public Collection<? extends ActionStatement> getActions() {
406         return allDeclared(ActionStatement.class);
407     }
408
409     @Nonnull
410     @Override
411     public final Collection<? extends NotificationStatement> getNotifications() {
412         return allDeclared(NotificationStatement.class);
413     }
414
415     @Nullable
416     @Override
417     public ReferenceStatement getReference() {
418         return firstDeclared(ReferenceStatement.class);
419     }
420
421     @Nullable
422     @Override
423     public DescriptionStatement getDescription() {
424         return firstDeclared(DescriptionStatement.class);
425     }
426
427     @Nullable
428     @Override
429     public StatusStatement getStatus() {
430         return firstDeclared(StatusStatement.class);
431     }
432
433     @Nullable
434     @Override
435     public WhenStatement getWhenStatement() {
436         return firstDeclared(WhenStatement.class);
437     }
438
439     @Nonnull
440     @Override
441     public Collection<? extends CaseStatement> getCases() {
442         return allDeclared(CaseStatement.class);
443     }
444
445     @Nonnull
446     @Override
447     public Collection<? extends IfFeatureStatement> getIfFeatures() {
448         return allDeclared(IfFeatureStatement.class);
449     }
450 }