Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / GroupingUtils.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.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.Set;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
29 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31
32 public final class GroupingUtils {
33
34     private GroupingUtils() {
35         throw new UnsupportedOperationException();
36     }
37
38     /**
39      * @param sourceGrpStmtCtx source grouping statement context
40      * @param targetCtx target context
41      * @param usesNode uses node
42      * @throws SourceException instance of SourceException
43      */
44     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
45             final StatementContextBase<?, ?, ?> targetCtx,
46             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode)
47             throws SourceException {
48
49         QNameModule newQNameModule = getNewQNameModule(targetCtx,
50                 sourceGrpStmtCtx);
51         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
52         copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
53                 newQNameModule);
54     }
55
56     public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
57             final StatementContextBase<?, ?, ?> targetCtx,
58             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
59             final QNameModule newQNameModule) throws SourceException {
60         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
61             if (needToCopyByUses(originalStmtCtx)) {
62                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
63                     TypeOfCopy.ADDED_BY_USES);
64                 targetCtx.addEffectiveSubstatement(copy);
65                 usesNode.addAsEffectOfStatement(copy);
66             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
67                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
68                 usesNode.addAsEffectOfStatement(originalStmtCtx);
69             }
70         }
71     }
72
73     public static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
74             final StatementContextBase<?, ?, ?> targetCtx,
75             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
76             final QNameModule newQNameModule) throws SourceException {
77         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
78             if (needToCopyByUses(originalStmtCtx)) {
79                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
80                     TypeOfCopy.ADDED_BY_USES);
81                 targetCtx.addEffectiveSubstatement(copy);
82                 usesNode.addAsEffectOfStatement(copy);
83             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
84                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
85                 usesNode.addAsEffectOfStatement(originalStmtCtx);
86             }
87         }
88     }
89
90     public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
91             final StmtContext<?, ?, ?> stmtContext) {
92         if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
93             if (targetCtx.isRootContext()) {
94                 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
95             }
96             if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
97                 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
98             }
99
100             Object targetStmtArgument = targetCtx.getStatementArgument();
101             Object sourceStmtArgument = stmtContext.getStatementArgument();
102             if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
103                 return ((QName) targetStmtArgument).getModule();
104             } else {
105                 return null;
106             }
107         } else {
108             return null;
109         }
110     }
111
112     public static boolean needToCreateNewQName(final StatementDefinition publicDefinition) {
113         return true;
114     }
115
116     private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(
117         Rfc6020Mapping.USES, Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
118     private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
119         Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE);
120     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(
121         Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE, Rfc6020Mapping.USES);
122     private static final Set<Rfc6020Mapping> TOP_REUSED_DEF_SET = ImmutableSet.of(
123         Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
124
125     public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
126         final StatementDefinition def = stmtContext.getPublicDefinition();
127
128         return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def)
129                 && Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition())));
130     }
131
132     public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
133         return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
134     }
135
136     public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
137         return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
138     }
139
140     public static void resolveUsesNode(
141             final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
142             final StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
143         for (StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
144             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
145                 performRefine(subStmtCtx, targetNodeStmtCtx);
146             }
147         }
148     }
149
150     private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
151             final StatementContextBase<?, ?, ?> usesParentCtx) {
152
153         Object refineArgument = refineCtx.getStatementArgument();
154         Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
155             "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
156                 refineCtx.getStatementSourceReference());
157
158         SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
159         StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
160         Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
161                 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
162
163         addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
164         refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
165     }
166
167     private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
168             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
169         for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
170             if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
171                 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
172             }
173         }
174     }
175
176     private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
177             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
178
179         StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
180         StatementDefinition refineTargetNodeDef = refineTargetNodeCtx.getPublicDefinition();
181
182         SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
183             refineSubstatementCtx.getStatementSourceReference(),
184             "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
185                     refineSubstatementCtx.getRoot().getStatementArgument(),
186                     refineSubstatementCtx.getParentContext().getStatementArgument(),
187                     refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
188
189         if (isAllowedToAddByRefine(refineSubstatementDef)) {
190             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
191         } else {
192             refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
193             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
194         }
195     }
196
197     private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
198
199     private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
200         return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
201     }
202
203     private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
204         Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
205             ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
206
207         return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
208                 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
209                 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
210     }
211
212     private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
213             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
214
215         Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
216             refineSubstatementCtx.getPublicDefinition());
217
218         return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
219                 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
220     }
221 }