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.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Objects;
16 import java.util.regex.Pattern;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
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.CaseStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
46 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
47 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
48 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
49 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
50 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
51 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
57 private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
58 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
59 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
60 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
61 .builder(YangStmtMapping.AUGMENT)
62 .addAny(YangStmtMapping.ANYXML)
63 .addAny(YangStmtMapping.CASE)
64 .addAny(YangStmtMapping.CHOICE)
65 .addAny(YangStmtMapping.CONTAINER)
66 .addOptional(YangStmtMapping.DESCRIPTION)
67 .addAny(YangStmtMapping.IF_FEATURE)
68 .addAny(YangStmtMapping.LEAF)
69 .addAny(YangStmtMapping.LEAF_LIST)
70 .addAny(YangStmtMapping.LIST)
71 .addOptional(YangStmtMapping.REFERENCE)
72 .addOptional(YangStmtMapping.STATUS)
73 .addAny(YangStmtMapping.USES)
74 .addOptional(YangStmtMapping.WHEN)
77 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
81 public static class Definition extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement,
82 EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
85 super(YangStmtMapping.AUGMENT);
89 public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
90 SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
91 || PATH_REL_PATTERN2.matcher(value).matches(), ctx.getStatementSourceReference(),
92 "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses",
95 return Utils.nodeIdentifierFromPath(ctx, value);
99 public AugmentStatement createDeclared(
100 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
101 return new AugmentStatementImpl(ctx);
105 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
106 final StmtContext<SchemaNodeIdentifier, AugmentStatement,
107 EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
108 return new AugmentEffectiveStatementImpl(ctx);
112 public void onFullDefinitionDeclared(final Mutable<SchemaNodeIdentifier, AugmentStatement,
113 EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
114 if (!augmentNode.isSupportedByFeatures()) {
118 super.onFullDefinitionDeclared(augmentNode);
120 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
124 final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
125 ModelProcessingPhase.EFFECTIVE_MODEL);
126 final Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement,
127 EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
128 augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
129 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
130 augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode),
131 SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
133 augmentAction.apply(new ModelActionBuilder.InferenceAction() {
135 public void apply(final ModelActionBuilder.InferenceContext ctx) {
136 final StatementContextBase<?, ?, ?> augmentTargetCtx =
137 (StatementContextBase<?, ?, ?>) target.resolve(ctx);
138 if (!isSupportedAugmentTarget(augmentTargetCtx)
139 || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
140 augmentNode.setIsSupportedToBuildEffective(false);
144 * Marks case short hand in augment
146 if (augmentTargetCtx.getPublicDefinition() == YangStmtMapping.CHOICE) {
147 augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, Boolean.TRUE);
150 // FIXME: this is a workaround for models which augment a node which is added via an extension
151 // which we do not handle. This needs to be reworked in terms of unknown schema nodes.
152 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
154 copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
155 augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
156 updateAugmentOrder(augmentSourceCtx);
157 } catch (final SourceException e) {
158 LOG.warn("Failed to add augmentation {} defined at {}",
159 augmentTargetCtx.getStatementSourceReference(),
160 augmentSourceCtx.getStatementSourceReference(), e);
164 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
165 Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
166 YangStmtMapping.AUGMENT);
167 if (currentOrder == null) {
173 augmentSourceCtx.addToNs(StmtOrderingNamespace.class, YangStmtMapping.AUGMENT, currentOrder);
177 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
179 * Do not fail, if it is an uses-augment to an unknown node.
181 if (YangStmtMapping.USES == augmentNode.getParentContext().getPublicDefinition()) {
182 final StatementContextBase<?, ?, ?> targetNode = Utils.findNode(getSearchRoot(augmentNode),
183 augmentNode.getStatementArgument());
184 if (targetNode != null && StmtContextUtils.isUnknownStatement(targetNode)) {
185 augmentNode.setIsSupportedToBuildEffective(false);
187 "Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
188 augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference());
193 throw new InferenceException(augmentNode.getStatementSourceReference(),
194 "Augment target '%s' not found", augmentNode.getStatementArgument());
199 private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
200 final Mutable<?, ?, ?> parent = augmentContext.getParentContext();
201 // Augment is in uses - we need to augment instantiated nodes in parent.
202 if (YangStmtMapping.USES == parent.getPublicDefinition()) {
203 return parent.getParentContext();
208 public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
209 final StatementContextBase<?, ?, ?> targetCtx) {
210 final CopyType typeOfCopy = UsesStatement.class.equals(sourceCtx.getParentContext().getPublicDefinition()
211 .getDeclaredRepresentationClass()) ? CopyType.ADDED_BY_USES_AUGMENTATION
212 : CopyType.ADDED_BY_AUGMENTATION;
214 * Since Yang 1.1, if an augmentation is made conditional with a
215 * "when" statement, it is allowed to add mandatory nodes.
217 final boolean skipCheckOfMandatoryNodes = YangVersion.VERSION_1_1.equals(sourceCtx.getRootVersion())
218 && isConditionalAugmentStmt(sourceCtx);
220 final Collection<? extends Mutable<?, ?, ?>> declared = sourceCtx.mutableDeclaredSubstatements();
221 final Collection<? extends Mutable<?, ?, ?>> effective = sourceCtx.mutableEffectiveSubstatements();
222 final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
224 for (final Mutable<?, ?, ?> originalStmtCtx : declared) {
225 if (originalStmtCtx.isSupportedByFeatures()) {
226 copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
229 for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
230 copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
233 targetCtx.addEffectiveSubstatements(buffer);
237 * Checks whether supplied statement context is conditional augment
241 * statement context to be checked
243 * @return true if supplied statement context is conditional augment
244 * statement, otherwise false
246 private static boolean isConditionalAugmentStmt(final StmtContext<?, ?, ?> ctx) {
247 return ctx.getPublicDefinition() == YangStmtMapping.AUGMENT
248 && StmtContextUtils.findFirstSubstatement(ctx, WhenStatement.class) != null;
251 private static void copyStatement(final Mutable<?, ?, ?> original, final StatementContextBase<?, ?, ?> target,
252 final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer,
253 final boolean skipCheckOfMandatoryNodes) {
254 if (needToCopyByAugment(original)) {
255 validateNodeCanBeCopiedByAugment(original, target, typeOfCopy, skipCheckOfMandatoryNodes);
257 buffer.add(target.childCopyOf(original, typeOfCopy));
258 } else if (isReusedByAugment(original)) {
259 buffer.add(original);
263 private static void validateNodeCanBeCopiedByAugment(final StmtContext<?, ?, ?> sourceCtx,
264 final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy,
265 final boolean skipCheckOfMandatoryNodes) {
267 if (WhenStatement.class.equals(sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
271 if (!skipCheckOfMandatoryNodes && typeOfCopy == CopyType.ADDED_BY_AUGMENTATION
272 && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
273 checkForMandatoryNodes(sourceCtx);
276 for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
277 final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
278 .getPublicDefinition().getDeclaredRepresentationClass());
279 final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
280 .getPublicDefinition().getDeclaredRepresentationClass());
281 final boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
282 && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
284 InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
285 "An augment cannot add node named '%s' because this name is already used in target",
286 sourceCtx.rawStatementArgument());
290 private static void checkForMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx) {
291 if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
293 * We need to iterate over both declared and effective sub-statements,
294 * because a mandatory node can be:
295 * a) declared in augment body
296 * b) added to augment body also via uses of a grouping and
297 * such sub-statements are stored in effective sub-statements collection.
299 sourceCtx.allSubstatementsStream().forEach(Definition::checkForMandatoryNodes);
302 InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx),
303 sourceCtx.getStatementSourceReference(),
304 "An augment cannot add node '%s' because it is mandatory and in module different than target",
305 sourceCtx.rawStatementArgument());
308 private static boolean reguiredCheckOfMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx,
309 Mutable<?, ?, ?> targetCtx) {
311 * If the statement argument is not QName, it cannot be mandatory
312 * statement, therefore return false and skip mandatory nodes validation
314 if (!(sourceCtx.getStatementArgument() instanceof QName)) {
317 final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
319 // RootStatementContext, for example
320 final Mutable<?, ?, ?> root = targetCtx.getRoot();
322 Verify.verify(targetCtx.getStatementArgument() instanceof QName,
323 "Argument of augment target statement must be QName.");
324 final QName targetStmtQName = (QName) targetCtx.getStatementArgument();
326 * If target is from another module, return true and perform
327 * mandatory nodes validation
329 if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
334 * If target or one of the target's ancestors from the same namespace
335 * is a presence container
336 * or is non-mandatory choice
337 * or is non-mandatory list
338 * return false and skip mandatory nodes validation, because these nodes
339 * are not mandatory node containers according to RFC 6020 section 3.1.
341 if (StmtContextUtils.isPresenceContainer(targetCtx)
342 || StmtContextUtils.isNotMandatoryNodeOfType(targetCtx, YangStmtMapping.CHOICE)
343 || StmtContextUtils.isNotMandatoryNodeOfType(targetCtx, YangStmtMapping.LIST)) {
346 } while ((targetCtx = targetCtx.getParentContext()) != root);
349 * All target node's parents belong to the same module as source node,
350 * therefore return false and skip mandatory nodes validation.
355 private static final Set<YangStmtMapping> NOCOPY_DEF_SET = ImmutableSet.of(YangStmtMapping.USES,
356 YangStmtMapping.WHEN, YangStmtMapping.DESCRIPTION, YangStmtMapping.REFERENCE, YangStmtMapping.STATUS);
358 public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
359 return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
362 private static final Set<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(YangStmtMapping.TYPEDEF);
364 public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
365 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
368 static boolean isSupportedAugmentTarget(final StmtContext<?, ?, ?> substatementCtx) {
370 * :TODO Substatement must be allowed augment target type e.g.
371 * Container, etc... and must not be for example grouping, identity etc.
372 * It is problem in case when more than one substatements have the same
373 * QName, for example Grouping and Container are siblings and they have
374 * the same QName. We must find the Container and the Grouping must be
375 * ignored as disallowed augment target.
377 final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(
378 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
380 // if no allowed target is returned we consider all targets allowed
381 return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
382 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
386 protected SubstatementValidator getSubstatementValidator() {
387 return SUBSTATEMENT_VALIDATOR;
393 public SchemaNodeIdentifier getTargetNode() {
399 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
400 return allDeclared(DataDefinitionStatement.class);
405 public Collection<? extends ActionStatement> getActions() {
406 return allDeclared(ActionStatement.class);
411 public final Collection<? extends NotificationStatement> getNotifications() {
412 return allDeclared(NotificationStatement.class);
417 public ReferenceStatement getReference() {
418 return firstDeclared(ReferenceStatement.class);
423 public DescriptionStatement getDescription() {
424 return firstDeclared(DescriptionStatement.class);
429 public StatusStatement getStatus() {
430 return firstDeclared(StatusStatement.class);
435 public WhenStatement getWhenStatement() {
436 return firstDeclared(WhenStatement.class);
441 public Collection<? extends CaseStatement> getCases() {
442 return allDeclared(CaseStatement.class);
447 public Collection<? extends IfFeatureStatement> getIfFeatures() {
448 return allDeclared(IfFeatureStatement.class);