2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
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;
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;
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)
72 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
76 public static class Definition extends
77 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
80 super(Rfc6020Mapping.AUGMENT);
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");
89 return Utils.nodeIdentifierFromPath(ctx, value);
93 public AugmentStatement createDeclared(
94 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
95 return new AugmentStatementImpl(ctx);
99 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
100 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
101 return new AugmentEffectiveStatementImpl(ctx);
105 public void onFullDefinitionDeclared(
106 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
107 if (!StmtContextUtils.areFeaturesSupported(augmentNode)) {
111 SUBSTATEMENT_VALIDATOR.validate(augmentNode);
113 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
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() {
126 public void apply() {
127 final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
128 if (!isSupportedAugmentTarget(augmentTargetCtx)
129 || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
130 augmentNode.setIsSupportedToBuildEffective(false);
134 * Marks case short hand in augment
136 if (augmentTargetCtx.getPublicDefinition() == Rfc6020Mapping.CHOICE) {
137 augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, true);
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;
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);
154 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
155 Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
156 Rfc6020Mapping.AUGMENT);
157 if (currentOrder == null) {
163 augmentSourceCtx.setOrder(currentOrder);
164 augmentSourceCtx.addToNs(StmtOrderingNamespace.class, Rfc6020Mapping.AUGMENT, currentOrder);
168 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
170 * Do not fail, if it is an uses-augment to an unknown node.
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);
178 "Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
179 augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference());
184 throw new InferenceException(augmentNode.getStatementSourceReference(),
185 "Augment target '%s' not found", augmentNode.getStatementArgument());
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();
199 public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
200 final StatementContextBase<?, ?, ?> targetCtx) {
201 copyDeclaredStmts(sourceCtx, targetCtx);
202 copyEffectiveStmts(sourceCtx, targetCtx);
205 // FIXME: Declared statements should not be copied.
206 private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
207 final StatementContextBase<?, ?, ?> targetCtx) {
209 final CopyType typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
210 .getDeclaredRepresentationClass().equals(UsesStatement.class) ? CopyType.ADDED_BY_USES_AUGMENTATION
211 : CopyType.ADDED_BY_AUGMENTATION;
213 for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
214 if (!StmtContextUtils.areFeaturesSupported(originalStmtCtx)) {
217 if (needToCopyByAugment(originalStmtCtx)) {
218 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
220 final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
221 targetCtx.addEffectiveSubstatement(copy);
222 } else if (isReusedByAugment(originalStmtCtx)) {
223 targetCtx.addEffectiveSubstatement(originalStmtCtx);
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;
234 for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
235 if (needToCopyByAugment(originalStmtCtx)) {
236 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
238 final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
239 targetCtx.addEffectiveSubstatement(copy);
240 } else if (isReusedByAugment(originalStmtCtx)) {
241 targetCtx.addEffectiveSubstatement(originalStmtCtx);
246 private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
247 final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy) {
249 if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
253 if (typeOfCopy == CopyType.ADDED_BY_AUGMENTATION && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
254 checkForMandatoryNodes(sourceCtx);
257 final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
258 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
260 for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
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());
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());
275 private static void checkForMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx) {
276 if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
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.
284 for (final StatementContextBase<?, ?, ?> sourceSubStatement : Iterables.concat(
285 sourceCtx.declaredSubstatements(), sourceCtx.declaredSubstatements())) {
286 checkForMandatoryNodes(sourceSubStatement);
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());
296 private static boolean reguiredCheckOfMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx,
297 StatementContextBase<?, ?, ?> targetCtx) {
299 * If the statement argument is not QName, it cannot be mandatory
300 * statement, therefore return false and skip mandatory nodes validation
302 if (!(sourceCtx.getStatementArgument() instanceof QName)) {
305 final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
307 final RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
309 Verify.verify(targetCtx.getStatementArgument() instanceof QName,
310 "Argument of augment target statement must be QName.");
311 final QName targetStmtQName = (QName) targetCtx.getStatementArgument();
313 * If target is from another module, return true and perform
314 * mandatory nodes validation
316 if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
320 * If target or one of its parent is a presence container from
321 * the same module, return false and skip mandatory nodes
324 if (StmtContextUtils.isPresenceContainer(targetCtx)) {
328 } while ((targetCtx = targetCtx.getParentContext()) != root);
331 * All target node's parents belong to the same module as source node,
332 * therefore return false and skip mandatory nodes validation.
337 private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES, Rfc6020Mapping.WHEN,
338 Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
340 public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
341 return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
344 private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
346 public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
347 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
350 static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
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.
361 final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
362 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
364 // if no allowed target is returned we consider all targets allowed
365 return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
366 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
373 public SchemaNodeIdentifier getTargetNode() {
378 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
379 return allDeclared(DataDefinitionStatement.class);