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