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.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;
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;
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)
73 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
77 public static class Definition extends
78 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
81 super(YangStmtMapping.AUGMENT);
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",
91 return Utils.nodeIdentifierFromPath(ctx, value);
95 public AugmentStatement createDeclared(
96 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
97 return new AugmentStatementImpl(ctx);
101 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
102 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
103 return new AugmentEffectiveStatementImpl(ctx);
107 public void onFullDefinitionDeclared(
108 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
109 if (!StmtContextUtils.areFeaturesSupported(augmentNode)) {
113 super.onFullDefinitionDeclared(augmentNode);
115 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
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() {
128 public void apply() {
129 final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
130 if (!isSupportedAugmentTarget(augmentTargetCtx)
131 || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
132 augmentNode.setIsSupportedToBuildEffective(false);
136 * Marks case short hand in augment
138 if (augmentTargetCtx.getPublicDefinition() == YangStmtMapping.CHOICE) {
139 augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, Boolean.TRUE);
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;
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);
156 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
157 Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
158 YangStmtMapping.AUGMENT);
159 if (currentOrder == null) {
165 augmentSourceCtx.setOrder(currentOrder);
166 augmentSourceCtx.addToNs(StmtOrderingNamespace.class, YangStmtMapping.AUGMENT, currentOrder);
170 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
172 * Do not fail, if it is an uses-augment to an unknown node.
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);
180 "Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
181 augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference());
186 throw new InferenceException(augmentNode.getStatementSourceReference(),
187 "Augment target '%s' not found", augmentNode.getStatementArgument());
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();
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;
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());
211 for (final StatementContextBase<?, ?, ?> originalStmtCtx : declared) {
212 if (StmtContextUtils.areFeaturesSupported(originalStmtCtx)) {
213 copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer);
216 for (final StatementContextBase<?, ?, ?> originalStmtCtx : effective) {
217 copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer);
220 targetCtx.addEffectiveSubstatements(buffer);
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);
229 final StatementContextBase<?, ?, ?> copy = original.createCopy(target, typeOfCopy);
231 } else if (isReusedByAugment(original)) {
232 buffer.add(original);
236 private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
237 final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy) {
239 if (WhenStatement.class.equals(sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
243 if (typeOfCopy == CopyType.ADDED_BY_AUGMENTATION && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
244 checkForMandatoryNodes(sourceCtx);
247 final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
248 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
250 for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
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());
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());
265 private static void checkForMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx) {
266 if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
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.
274 sourceCtx.declaredSubstatements().forEach(Definition::checkForMandatoryNodes);
275 sourceCtx.effectiveSubstatements().forEach(Definition::checkForMandatoryNodes);
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());
284 private static boolean reguiredCheckOfMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx,
285 StatementContextBase<?, ?, ?> targetCtx) {
287 * If the statement argument is not QName, it cannot be mandatory
288 * statement, therefore return false and skip mandatory nodes validation
290 if (!(sourceCtx.getStatementArgument() instanceof QName)) {
293 final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
295 final RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
297 Verify.verify(targetCtx.getStatementArgument() instanceof QName,
298 "Argument of augment target statement must be QName.");
299 final QName targetStmtQName = (QName) targetCtx.getStatementArgument();
301 * If target is from another module, return true and perform
302 * mandatory nodes validation
304 if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
309 * If target or one of its parent is a presence container from
310 * the same module, return false and skip mandatory nodes
313 if (StmtContextUtils.isPresenceContainer(targetCtx)) {
316 } while ((targetCtx = targetCtx.getParentContext()) != root);
319 * All target node's parents belong to the same module as source node,
320 * therefore return false and skip mandatory nodes validation.
325 private static final Set<YangStmtMapping> NOCOPY_DEF_SET = ImmutableSet.of(YangStmtMapping.USES, YangStmtMapping.WHEN,
326 YangStmtMapping.DESCRIPTION, YangStmtMapping.REFERENCE, YangStmtMapping.STATUS);
328 public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
329 return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
332 private static final Set<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(YangStmtMapping.TYPEDEF);
334 public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
335 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
338 static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
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.
349 final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
350 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
352 // if no allowed target is returned we consider all targets allowed
353 return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
354 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
358 protected SubstatementValidator getSubstatementValidator() {
359 return SUBSTATEMENT_VALIDATOR;
366 public SchemaNodeIdentifier getTargetNode() {
372 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
373 return allDeclared(DataDefinitionStatement.class);
378 public Collection<? extends ActionStatement> getActions() {
379 return allDeclared(ActionStatement.class);
383 public final Collection<? extends NotificationStatement> getNotifications() {
384 return allDeclared(NotificationStatement.class);