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