Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentUtils.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
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;
15 import java.util.Set;
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;
28
29 // FIXME: Move this to the AugmentStatementDefinition#ApplyAction
30 public final class AugmentUtils {
31     private AugmentUtils() {
32     }
33
34     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
35             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
36         copyDeclaredStmts(sourceCtx, targetCtx);
37         copyEffectiveStmts(sourceCtx, targetCtx);
38     }
39
40     // FIXME: Declared statements should not be copied.
41     private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
42             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
43
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));
48
49         TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
50                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
51
52         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
53             if (needToCopyByAugment(originalStmtCtx)) {
54                 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
55
56                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
57                 targetCtx.addEffectiveSubstatement(copy);
58             } else if (isReusedByAugment(originalStmtCtx)) {
59                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
60             }
61         }
62     }
63
64     private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
65             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
66
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));
71
72         TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
73                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
74
75         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
76             if (needToCopyByAugment(originalStmtCtx)) {
77                 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
78
79                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
80                 targetCtx.addEffectiveSubstatement(copy);
81             } else if (isReusedByAugment(originalStmtCtx)) {
82                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
83             }
84         }
85     }
86
87     private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
88             final List<StatementContextBase<?, ?, ?>> targetSubStatements, final boolean sourceAndTargetInSameModule) {
89
90         if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
91             return;
92         }
93
94         if (!sourceAndTargetInSameModule) {
95             final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
96                     .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
97
98             for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
99                 InferenceException.throwIf(MandatoryStatement.class.equals(
100                     sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()),
101                     sourceCtx.getStatementSourceReference(),
102                     "An augment cannot add node '%s' because it is mandatory and in module different from target",
103                     sourceCtx.rawStatementArgument());
104             }
105         }
106
107         for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
108
109             final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
110                     .getPublicDefinition().getDeclaredRepresentationClass());
111             final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
112                     .getPublicDefinition().getDeclaredRepresentationClass());
113             boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
114                     && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
115
116             InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
117                 "An augment cannot add node named '%s' because this name is already used in target",
118                 sourceCtx.rawStatementArgument());
119         }
120     }
121
122     private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
123
124     public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
125         return !NOCOPY_DEV_SET.contains(stmtContext.getPublicDefinition());
126     }
127
128     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
129
130     public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
131         return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
132     }
133
134     static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
135
136         /*
137          * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must be not for example
138          * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
139          * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
140          * Grouping must be ignored as disallowed augment target.
141          */
142
143         Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
144                 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
145
146         // if no allowed target is returned we consider all targets allowed
147         return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
148                 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
149     }
150 }