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.StmtContext.Mutable;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
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(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
40 StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
42 QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
43 copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
44 copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
48 public static void copyDeclaredStmts(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
49 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule) throws SourceException {
50 Collection<? extends StatementContextBase<?, ?, ?>> declaredSubstatements = sourceGrpStmtCtx
51 .declaredSubstatements();
52 for (StatementContextBase<?, ?, ?> originalStmtCtx : declaredSubstatements) {
53 if (needToCopyByUses(originalStmtCtx)) {
54 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx);
55 targetCtx.addEffectiveSubstatement(copy);
56 } else if (isReusedByUses(originalStmtCtx)) {
57 targetCtx.addEffectiveSubstatement(originalStmtCtx);
62 public static void copyEffectiveStmts(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
63 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule) throws SourceException {
64 Collection<? extends StatementContextBase<?, ?, ?>> effectiveSubstatements = sourceGrpStmtCtx
65 .effectiveSubstatements();
66 for (StatementContextBase<?, ?, ?> originalStmtCtx : effectiveSubstatements) {
67 if (needToCopyByUses(originalStmtCtx)) {
68 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx);
69 targetCtx.addEffectiveSubstatement(copy);
70 } else if (isReusedByUses(originalStmtCtx)) {
71 targetCtx.addEffectiveSubstatement(originalStmtCtx);
76 public static QNameModule getNewQNameModule(StatementContextBase<?, ?, ?> targetCtx,
77 StmtContext<?, ?, ?> stmtContext) {
78 if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
79 Object targetStmtArgument = targetCtx.getStatementArgument();
80 Object sourceStmtArgument = stmtContext.getStatementArgument();
81 if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
82 QName targetQName = (QName) targetStmtArgument;
83 QNameModule targetQNameModule = targetQName.getModule();
85 QName sourceQName = (QName) sourceStmtArgument;
86 QNameModule sourceQNameModule = sourceQName.getModule();
88 if (targetQNameModule.equals(sourceQNameModule)) {
91 return targetQNameModule;
101 public static boolean needToCreateNewQName(StatementDefinition publicDefinition) {
105 public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
107 Set<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
108 noCopyDefSet.add(Rfc6020Mapping.USES);
110 StatementDefinition def = stmtContext.getPublicDefinition();
111 return !noCopyDefSet.contains(def);
114 public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
116 Set<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
117 reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
119 StatementDefinition def = stmtContext.getPublicDefinition();
120 return reusedDefSet.contains(def);
123 public static void resolveUsesNode(
124 Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
125 StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
127 Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode.declaredSubstatements();
128 for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
129 if (StmtContextUtils.producesDeclared(subStmtCtx, WhenStatement.class)) {
130 StatementContextBase<?, ?, ?> copy = subStmtCtx.createCopy(null, targetNodeStmtCtx);
131 targetNodeStmtCtx.addEffectiveSubstatement(copy);
133 if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
134 // :TODO resolve and perform refine statement
136 if (StmtContextUtils.producesDeclared(subStmtCtx, AugmentStatement.class)) {
137 // :TODO find target node and perform augmentation
139 // :TODO resolve other uses substatements