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 java.util.Collection;
11 import java.util.HashSet;
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.AugmentStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
29 public final class GroupingUtils {
31 private GroupingUtils() {
35 * @param sourceGrpStmtCtx
37 * @throws SourceException
39 public static void copyFromSourceToTarget(
40 StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
41 StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
43 QNameModule newQNameModule = getNewQNameModule(targetCtx,
45 copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
46 copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
50 public static void copyDeclaredStmts(
51 StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
52 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
53 throws SourceException {
54 Collection<? extends StatementContextBase<?, ?, ?>> declaredSubstatements = sourceGrpStmtCtx
55 .declaredSubstatements();
56 for (StatementContextBase<?, ?, ?> originalStmtCtx : declaredSubstatements) {
57 if (needToCopyByUses(originalStmtCtx)) {
58 StatementContextBase<?, ?, ?> copy = originalStmtCtx
59 .createCopy(newQNameModule, targetCtx);
60 targetCtx.addEffectiveSubstatement(copy);
61 } else if (isReusedByUses(originalStmtCtx)) {
62 targetCtx.addEffectiveSubstatement(originalStmtCtx);
67 public static void copyEffectiveStmts(
68 StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
69 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
70 throws SourceException {
71 Collection<? extends StatementContextBase<?, ?, ?>> effectiveSubstatements = sourceGrpStmtCtx
72 .effectiveSubstatements();
73 for (StatementContextBase<?, ?, ?> originalStmtCtx : effectiveSubstatements) {
74 if (needToCopyByUses(originalStmtCtx)) {
75 StatementContextBase<?, ?, ?> copy = originalStmtCtx
76 .createCopy(newQNameModule, targetCtx);
77 targetCtx.addEffectiveSubstatement(copy);
78 } else if (isReusedByUses(originalStmtCtx)) {
79 targetCtx.addEffectiveSubstatement(originalStmtCtx);
84 public static QNameModule getNewQNameModule(
85 StatementContextBase<?, ?, ?> targetCtx,
86 StmtContext<?, ?, ?> stmtContext) {
87 if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
88 Object targetStmtArgument = targetCtx.getStatementArgument();
89 Object sourceStmtArgument = stmtContext.getStatementArgument();
90 if (targetStmtArgument instanceof QName
91 && sourceStmtArgument instanceof QName) {
92 QName targetQName = (QName) targetStmtArgument;
93 QNameModule targetQNameModule = targetQName.getModule();
95 QName sourceQName = (QName) sourceStmtArgument;
96 QNameModule sourceQNameModule = sourceQName.getModule();
98 if (targetQNameModule.equals(sourceQNameModule)) {
102 return targetQNameModule;
112 public static boolean needToCreateNewQName(
113 StatementDefinition publicDefinition) {
117 public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
119 Set<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
120 noCopyDefSet.add(Rfc6020Mapping.USES);
122 StatementDefinition def = stmtContext.getPublicDefinition();
123 return (!noCopyDefSet.contains(def));
126 public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
128 Set<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
129 reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
131 StatementDefinition def = stmtContext.getPublicDefinition();
132 return (reusedDefSet.contains(def));
135 public static void resolveUsesNode(
136 Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
137 StatementContextBase<?, ?, ?> targetNodeStmtCtx)
138 throws SourceException {
140 Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode
141 .declaredSubstatements();
142 for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
143 if (StmtContextUtils.producesDeclared(subStmtCtx,
144 WhenStatement.class)) {
145 StatementContextBase<?, ?, ?> copy = subStmtCtx.createCopy(
146 null, targetNodeStmtCtx);
147 targetNodeStmtCtx.addEffectiveSubstatement(copy);
149 if (StmtContextUtils.producesDeclared(subStmtCtx,
150 RefineStatement.class)) {
151 // :TODO resolve and perform refine statement
153 if (StmtContextUtils.producesDeclared(subStmtCtx,
154 AugmentStatement.class)) {
155 // :TODO find target node and perform augmentation
157 // :TODO resolve other uses substatements