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;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public final class GroupingUtils {
36 private static final Logger LOG = LoggerFactory.getLogger(GroupingUtils.class);
38 private GroupingUtils() {
39 throw new UnsupportedOperationException();
43 * @param sourceGrpStmtCtx source grouping statement context
44 * @param targetCtx target context
45 * @param usesNode uses node
46 * @throws SourceException instance of SourceException
48 public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
49 final StatementContextBase<?, ?, ?> targetCtx,
50 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
52 QNameModule newQNameModule = getNewQNameModule(targetCtx,
54 copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
55 copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
59 public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
60 final StatementContextBase<?, ?, ?> targetCtx,
61 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
62 final QNameModule newQNameModule) {
63 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
64 if (needToCopyByUses(originalStmtCtx)) {
65 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
66 TypeOfCopy.ADDED_BY_USES);
67 targetCtx.addEffectiveSubstatement(copy);
68 usesNode.addAsEffectOfStatement(copy);
69 } else if (isReusedByUsesOnTop(originalStmtCtx)) {
70 targetCtx.addEffectiveSubstatement(originalStmtCtx);
71 usesNode.addAsEffectOfStatement(originalStmtCtx);
76 public static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
77 final StatementContextBase<?, ?, ?> targetCtx,
78 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
79 final QNameModule newQNameModule) {
80 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
81 if (needToCopyByUses(originalStmtCtx)) {
82 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
83 TypeOfCopy.ADDED_BY_USES);
84 targetCtx.addEffectiveSubstatement(copy);
85 usesNode.addAsEffectOfStatement(copy);
86 } else if (isReusedByUsesOnTop(originalStmtCtx)) {
87 targetCtx.addEffectiveSubstatement(originalStmtCtx);
88 usesNode.addAsEffectOfStatement(originalStmtCtx);
93 public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
94 final StmtContext<?, ?, ?> stmtContext) {
95 if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
96 if (targetCtx.isRootContext()) {
97 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
99 if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
100 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
103 Object targetStmtArgument = targetCtx.getStatementArgument();
104 Object sourceStmtArgument = stmtContext.getStatementArgument();
105 if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
106 return ((QName) targetStmtArgument).getModule();
115 public static boolean needToCreateNewQName(final StatementDefinition publicDefinition) {
119 private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES,
120 Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
121 private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(Rfc6020Mapping.DESCRIPTION,
122 Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
123 private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF,
124 Rfc6020Mapping.TYPE, Rfc6020Mapping.USES);
125 private static final Set<Rfc6020Mapping> TOP_REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF,
126 Rfc6020Mapping.TYPE);
128 public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
129 final StatementDefinition def = stmtContext.getPublicDefinition();
131 return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def)
132 && Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition())));
135 public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
136 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
139 public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
140 return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
143 public static void resolveUsesNode(
144 final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
145 final StatementContextBase<?, ?, ?> targetNodeStmtCtx) {
146 for (StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
147 if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
148 performRefine(subStmtCtx, targetNodeStmtCtx);
153 private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
154 final StatementContextBase<?, ?, ?> usesParentCtx) {
156 Object refineArgument = refineCtx.getStatementArgument();
157 Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
158 "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
159 refineCtx.getStatementSourceReference());
161 SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
162 StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
163 Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
164 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
165 if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
166 LOG.debug("Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
167 refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(), refineTargetNodeCtx.getStatementArgument(),
168 refineCtx.getStatementSourceReference());
169 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
173 addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
174 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
177 private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
178 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
179 for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
180 if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
181 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
186 private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
187 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
189 StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
191 SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
192 refineSubstatementCtx.getStatementSourceReference(),
193 "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
194 refineSubstatementCtx.getRoot().getStatementArgument(),
195 refineSubstatementCtx.getParentContext().getStatementArgument(),
196 refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
198 if (isAllowedToAddByRefine(refineSubstatementDef)) {
199 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
201 refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
202 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
206 private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
208 private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
209 return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
212 private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
213 Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
214 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
216 return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
217 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
218 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
221 private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
222 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
224 Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
225 refineSubstatementCtx.getPublicDefinition());
227 return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
228 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());