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.Collection;
14 import java.util.List;
15 import java.util.Objects;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
19 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
32 // FIXME: Move this to the AugmentStatementDefinition#ApplyAction
33 public final class AugmentUtils {
34 private AugmentUtils() {
37 public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
38 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
39 copyDeclaredStmts(sourceCtx, targetCtx);
40 copyEffectiveStmts(sourceCtx, targetCtx);
43 // FIXME: Declared statements should not be copied.
44 private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
45 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
47 TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
48 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
50 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
51 if (needToCopyByAugment(originalStmtCtx)) {
52 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
54 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
55 targetCtx.addEffectiveSubstatement(copy);
56 } else if (isReusedByAugment(originalStmtCtx)) {
57 targetCtx.addEffectiveSubstatement(originalStmtCtx);
62 private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
63 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
64 TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
65 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
67 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
68 if (needToCopyByAugment(originalStmtCtx)) {
69 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
71 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
72 targetCtx.addEffectiveSubstatement(copy);
73 } else if (isReusedByAugment(originalStmtCtx)) {
74 targetCtx.addEffectiveSubstatement(originalStmtCtx);
79 private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
80 final StatementContextBase<?, ?, ?> targetCtx) {
82 if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
86 if (reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
87 final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
88 .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
90 for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
91 InferenceException.throwIf(MandatoryStatement.class.equals(
92 sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()),
93 sourceCtx.getStatementSourceReference(),
94 "An augment cannot add node '%s' because it is mandatory and in module different from target",
95 sourceCtx.rawStatementArgument());
99 final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
100 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
102 for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
104 final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
105 .getPublicDefinition().getDeclaredRepresentationClass());
106 final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
107 .getPublicDefinition().getDeclaredRepresentationClass());
108 boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
109 && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
111 InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
112 "An augment cannot add node named '%s' because this name is already used in target",
113 sourceCtx.rawStatementArgument());
117 private static boolean reguiredCheckOfMandatoryNodes(StatementContextBase<?, ?, ?> sourceCtx,
118 StatementContextBase<?, ?, ?> targetCtx) {
120 * If the statement argument is not QName, it cannot be mandatory statement,
121 * therefore return false and skip mandatory nodes validation
123 if(!(sourceCtx.getStatementArgument() instanceof QName)) {
126 QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
128 RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
130 Verify.verify(targetCtx.getStatementArgument() instanceof QName,
131 "Argument of augment target statement must be QName.");
132 QName targetStmtQName = (QName) targetCtx.getStatementArgument();
134 * If target is from another module, return true and perform
135 * mandatory nodes validation
137 if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
141 * If target or one of its parent is a presence container from
142 * the same module, return false and skip mandatory nodes
145 if (Utils.isPresenceContainer(targetCtx)) {
149 } while ((targetCtx = targetCtx.getParentContext()) != root);
152 * All target node's parents belong to the same module as source node,
153 * therefore return false and skip mandatory nodes validation.
158 private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES, Rfc6020Mapping.WHEN,
159 Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
161 public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
162 return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
165 private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
167 public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
168 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
171 static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
174 * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must not be for example
175 * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
176 * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
177 * Grouping must be ignored as disallowed augment target.
180 Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
181 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
183 // if no allowed target is returned we consider all targets allowed
184 return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
185 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());