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. At %s", refineArgument,
156 refineCtx.getStatementSourceReference());
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());
163 addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
164 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
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);
176 private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
177 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
179 StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
180 StatementDefinition refineTargetNodeDef = refineTargetNodeCtx.getPublicDefinition();
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());
189 if (isAllowedToAddByRefine(refineSubstatementDef)) {
190 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
192 refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
193 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
197 private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
199 private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
200 return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
203 private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
204 Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
205 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
207 return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
208 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
209 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
212 private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
213 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
215 Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
216 refineSubstatementCtx.getPublicDefinition());
218 return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
219 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());