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