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.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 java.util.regex.Pattern;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
20 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
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.StatementContextBase;
31 // FIXME: Move this to the AugmentStatementDefinition#ApplyAction
32 public final class AugmentUtils {
34 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
35 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
37 private AugmentUtils() {
40 public static Iterable<QName> parseAugmentPath(final StmtContext<?, ?, ?> ctx, final String path) {
41 Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(path).matches()
42 && !PATH_REL_PATTERN2.matcher(path).matches(),
43 "An argument for augment can be only absolute path; or descendant if used in uses");
44 return Utils.parseXPath(ctx, path);
47 public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
48 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
49 copyDeclaredStmts(sourceCtx, targetCtx);
50 copyEffectiveStmts(sourceCtx, targetCtx);
53 // FIXME: Declared statements should not be copied.
54 private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
55 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
57 final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
58 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
59 boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
60 Utils.getRootModuleQName(targetCtx));
62 TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
63 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
65 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
66 if (needToCopyByAugment(originalStmtCtx)) {
67 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
69 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
70 targetCtx.addEffectiveSubstatement(copy);
71 } else if (isReusedByAugment(originalStmtCtx)) {
72 targetCtx.addEffectiveSubstatement(originalStmtCtx);
77 private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
78 final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
80 final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
81 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
82 boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
83 Utils.getRootModuleQName(targetCtx));
85 TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
86 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
88 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
89 if (needToCopyByAugment(originalStmtCtx)) {
90 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
92 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
93 targetCtx.addEffectiveSubstatement(copy);
94 } else if (isReusedByAugment(originalStmtCtx)) {
95 targetCtx.addEffectiveSubstatement(originalStmtCtx);
100 private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
101 final List<StatementContextBase<?, ?, ?>> targetSubStatements, final boolean sourceAndTargetInSameModule) {
103 if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
107 if (!sourceAndTargetInSameModule) {
108 final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
109 .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
111 for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
112 if (sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()
113 .equals(MandatoryStatement.class)) {
114 throw new IllegalArgumentException(
116 "An augment cannot add node '%s' because it is mandatory and in module different from target",
117 sourceCtx.rawStatementArgument()));
122 for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
124 final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
125 .getPublicDefinition().getDeclaredRepresentationClass());
126 final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
127 .getPublicDefinition().getDeclaredRepresentationClass());
128 boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
129 && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
132 throw new IllegalStateException(String.format(
133 "An augment cannot add node named '%s' because this name is already used in target",
134 sourceCtx.rawStatementArgument()));
139 private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
141 public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
142 return !NOCOPY_DEV_SET.contains(stmtContext.getPublicDefinition());
145 private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
147 public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
148 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
151 static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
154 * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must be not for example
155 * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
156 * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
157 * Grouping must be ignored as disallowed augment target.
160 Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
161 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
163 // if no allowed target is returned we consider all targets allowed
164 return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
165 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());