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)
51 throws SourceException {
53 QNameModule newQNameModule = getNewQNameModule(targetCtx,
55 copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
56 copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
60 public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
61 final StatementContextBase<?, ?, ?> targetCtx,
62 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
63 final QNameModule newQNameModule) throws SourceException {
64 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
65 if (needToCopyByUses(originalStmtCtx)) {
66 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
67 TypeOfCopy.ADDED_BY_USES);
68 targetCtx.addEffectiveSubstatement(copy);
69 usesNode.addAsEffectOfStatement(copy);
70 } else if (isReusedByUsesOnTop(originalStmtCtx)) {
71 targetCtx.addEffectiveSubstatement(originalStmtCtx);
72 usesNode.addAsEffectOfStatement(originalStmtCtx);
77 public static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
78 final StatementContextBase<?, ?, ?> targetCtx,
79 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
80 final QNameModule newQNameModule) throws SourceException {
81 for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
82 if (needToCopyByUses(originalStmtCtx)) {
83 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
84 TypeOfCopy.ADDED_BY_USES);
85 targetCtx.addEffectiveSubstatement(copy);
86 usesNode.addAsEffectOfStatement(copy);
87 } else if (isReusedByUsesOnTop(originalStmtCtx)) {
88 targetCtx.addEffectiveSubstatement(originalStmtCtx);
89 usesNode.addAsEffectOfStatement(originalStmtCtx);
94 public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
95 final StmtContext<?, ?, ?> stmtContext) {
96 if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
97 if (targetCtx.isRootContext()) {
98 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
100 if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
101 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
104 Object targetStmtArgument = targetCtx.getStatementArgument();
105 Object sourceStmtArgument = stmtContext.getStatementArgument();
106 if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
107 return ((QName) targetStmtArgument).getModule();
116 public static boolean needToCreateNewQName(final StatementDefinition publicDefinition) {
120 private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES,
121 Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
122 private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(Rfc6020Mapping.DESCRIPTION,
123 Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
124 private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF,
125 Rfc6020Mapping.TYPE, Rfc6020Mapping.USES);
126 private static final Set<Rfc6020Mapping> TOP_REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF,
127 Rfc6020Mapping.TYPE);
129 public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
130 final StatementDefinition def = stmtContext.getPublicDefinition();
132 return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def)
133 && Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition())));
136 public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
137 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
140 public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
141 return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
144 public static void resolveUsesNode(
145 final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
146 final StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
147 for (StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
148 if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
149 performRefine(subStmtCtx, targetNodeStmtCtx);
154 private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
155 final StatementContextBase<?, ?, ?> usesParentCtx) {
157 Object refineArgument = refineCtx.getStatementArgument();
158 Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
159 "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
160 refineCtx.getStatementSourceReference());
162 SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
163 StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
164 Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
165 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
166 if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
167 LOG.debug("Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
168 refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(), refineTargetNodeCtx.getStatementArgument(),
169 refineCtx.getStatementSourceReference());
170 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
174 addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
175 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
178 private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
179 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
180 for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
181 if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
182 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
187 private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
188 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
190 StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
192 SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
193 refineSubstatementCtx.getStatementSourceReference(),
194 "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
195 refineSubstatementCtx.getRoot().getStatementArgument(),
196 refineSubstatementCtx.getParentContext().getStatementArgument(),
197 refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
199 if (isAllowedToAddByRefine(refineSubstatementDef)) {
200 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
202 refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
203 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
207 private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
209 private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
210 return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
213 private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
214 Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
215 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
217 return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
218 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
219 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
222 private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
223 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
225 Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
226 refineSubstatementCtx.getPublicDefinition());
228 return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
229 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());