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.collect.ImmutableList.Builder;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Objects;
16 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
29 // FIXME: Move this to the AugmentStatementDefinition#ApplyAction
30 public final class AugmentUtils {
31 private AugmentUtils() {
34 public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
35 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
36 copyDeclaredStmts(sourceCtx, targetCtx);
37 copyEffectiveStmts(sourceCtx, targetCtx);
40 // FIXME: Declared statements should not be copied.
41 private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
42 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
44 final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
45 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
46 boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
47 Utils.getRootModuleQName(targetCtx));
49 TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
50 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
52 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
53 if (needToCopyByAugment(originalStmtCtx)) {
54 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
56 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
57 targetCtx.addEffectiveSubstatement(copy);
58 } else if (isReusedByAugment(originalStmtCtx)) {
59 targetCtx.addEffectiveSubstatement(originalStmtCtx);
64 private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
65 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
67 final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
68 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
69 boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
70 Utils.getRootModuleQName(targetCtx));
72 TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
73 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
75 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
76 if (needToCopyByAugment(originalStmtCtx)) {
77 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
79 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
80 targetCtx.addEffectiveSubstatement(copy);
81 } else if (isReusedByAugment(originalStmtCtx)) {
82 targetCtx.addEffectiveSubstatement(originalStmtCtx);
87 private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
88 final List<StatementContextBase<?, ?, ?>> targetSubStatements, final boolean sourceAndTargetInSameModule) {
90 if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
94 if (!sourceAndTargetInSameModule) {
95 final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
96 .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
98 for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
99 if (sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()
100 .equals(MandatoryStatement.class)) {
101 throw new InferenceException(
103 "An augment cannot add node '%s' because it is mandatory and in module different from target",
104 sourceCtx.rawStatementArgument()), sourceCtx.getStatementSourceReference());
109 for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
111 final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
112 .getPublicDefinition().getDeclaredRepresentationClass());
113 final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
114 .getPublicDefinition().getDeclaredRepresentationClass());
115 boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
116 && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
119 throw new InferenceException(String.format(
120 "An augment cannot add node named '%s' because this name is already used in target",
121 sourceCtx.rawStatementArgument()), sourceCtx.getStatementSourceReference());
126 private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
128 public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
129 return !NOCOPY_DEV_SET.contains(stmtContext.getPublicDefinition());
132 private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
134 public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
135 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
138 static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
141 * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must be not for example
142 * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
143 * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
144 * Grouping must be ignored as disallowed augment target.
147 Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
148 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
150 // if no allowed target is returned we consider all targets allowed
151 return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
152 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());