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.ImmutableSet;
12 import java.util.Collection;
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;
32 public final class GroupingUtils {
34 private GroupingUtils() {
35 throw new UnsupportedOperationException();
39 * @param sourceGrpStmtCtx source grouping statement context
40 * @param targetCtx target context
41 * @param usesNode uses node
42 * @throws SourceException instance of SourceException
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 {
49 QNameModule newQNameModule = getNewQNameModule(targetCtx,
51 copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
52 copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
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);
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);
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);
96 if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
97 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
100 Object targetStmtArgument = targetCtx.getStatementArgument();
101 Object sourceStmtArgument = stmtContext.getStatementArgument();
102 if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
103 return ((QName) targetStmtArgument).getModule();
112 public static boolean needToCreateNewQName(final StatementDefinition publicDefinition) {
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);
125 public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
126 final StatementDefinition def = stmtContext.getPublicDefinition();
128 return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def)
129 && Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition())));
132 public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
133 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
136 public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
137 return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
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);
150 private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
151 final StatementContextBase<?, ?, ?> usesParentCtx) {
153 Object refineArgument = refineCtx.getStatementArgument();
154 Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
155 "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier", refineArgument);
157 SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
158 StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
159 Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found.",
160 refineTargetNodeIdentifier);
162 addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
163 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
166 private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
167 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
168 for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
169 if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
170 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
175 private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
176 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
178 StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
179 StatementDefinition refineTargetNodeDef = refineTargetNodeCtx.getPublicDefinition();
181 if (!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx)) {
182 throw new SourceException("Error in module '"
183 + refineSubstatementCtx.getRoot().getStatementArgument()
184 + "' in the refine of uses '"
185 + refineSubstatementCtx.getParentContext().getStatementArgument()
186 + "': can not perform refine of '" + refineSubstatementCtx.getPublicDefinition()
187 + "' for the target '" + refineTargetNodeCtx.getPublicDefinition() + "'.",
188 refineSubstatementCtx.getStatementSourceReference());
191 if (isAllowedToAddByRefine(refineSubstatementDef)) {
192 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
194 refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
195 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
199 private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
201 private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
202 return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
205 private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
206 Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
207 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
209 return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
210 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
211 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
214 private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
215 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
217 Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
218 refineSubstatementCtx.getPublicDefinition());
220 return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
221 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());