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;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
27 public class GroupingUtils {
30 * @param sourceGrpStmtCtx
32 * @throws SourceException
34 public static void copyFromSourceToTarget(
35 StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
36 StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
38 QNameModule newQNameModule = getNewQNameModule(targetCtx,
40 copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
41 copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
45 public static void copyDeclaredStmts(
46 StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
47 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
48 throws SourceException {
49 Collection<? extends StatementContextBase<?, ?, ?>> declaredSubstatements = sourceGrpStmtCtx
50 .declaredSubstatements();
51 for (StatementContextBase<?, ?, ?> originalStmtCtx : declaredSubstatements) {
52 if (needToCopyByUses(originalStmtCtx)) {
53 StatementContextBase<?, ?, ?> copy = originalStmtCtx
54 .createCopy(newQNameModule, targetCtx);
55 targetCtx.addEffectiveSubstatement(copy);
56 } else if (isReusedByUses(originalStmtCtx)) {
57 targetCtx.addEffectiveSubstatement(originalStmtCtx);
62 public static void copyEffectiveStmts(
63 StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
64 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
65 throws SourceException {
66 Collection<? extends StatementContextBase<?, ?, ?>> effectiveSubstatements = sourceGrpStmtCtx
67 .effectiveSubstatements();
68 for (StatementContextBase<?, ?, ?> originalStmtCtx : effectiveSubstatements) {
69 if (needToCopyByUses(originalStmtCtx)) {
70 StatementContextBase<?, ?, ?> copy = originalStmtCtx
71 .createCopy(newQNameModule, targetCtx);
72 targetCtx.addEffectiveSubstatement(copy);
73 } else if (isReusedByUses(originalStmtCtx)) {
74 targetCtx.addEffectiveSubstatement(originalStmtCtx);
79 public static QNameModule getNewQNameModule(
80 StatementContextBase<?, ?, ?> targetCtx,
81 StmtContext<?, ?, ?> stmtContext) {
82 if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
83 Object targetStmtArgument = targetCtx.getStatementArgument();
84 Object sourceStmtArgument = stmtContext.getStatementArgument();
85 if (targetStmtArgument instanceof QName
86 && sourceStmtArgument instanceof QName) {
87 QName targetQName = (QName) targetStmtArgument;
88 QNameModule targetQNameModule = targetQName.getModule();
90 QName sourceQName = (QName) sourceStmtArgument;
91 QNameModule sourceQNameModule = sourceQName.getModule();
93 if (targetQNameModule.equals(sourceQNameModule))
96 return targetQNameModule;
103 public static boolean needToCreateNewQName(
104 StatementDefinition publicDefinition) {
108 public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
110 HashSet<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
111 noCopyDefSet.add(Rfc6020Mapping.USES);
113 StatementDefinition def = stmtContext.getPublicDefinition();
114 if (noCopyDefSet.contains(def))
120 public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
122 HashSet<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
123 reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
125 StatementDefinition def = stmtContext.getPublicDefinition();
126 if (reusedDefSet.contains(def))
132 public static void resolveUsesNode(
133 Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
134 StatementContextBase<?, ?, ?> targetNodeStmtCtx)
135 throws SourceException {
137 Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode
138 .declaredSubstatements();
139 for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
140 if (StmtContextUtils.producesDeclared(subStmtCtx,
141 WhenStatement.class)) {
142 StatementContextBase<?, ?, ?> copy = subStmtCtx.createCopy(
143 null, targetNodeStmtCtx);
144 targetNodeStmtCtx.addEffectiveSubstatement(copy);
146 if (StmtContextUtils.producesDeclared(subStmtCtx,
147 RefineStatement.class)) {
148 // :TODO resolve and perform refine statement
150 if (StmtContextUtils.producesDeclared(subStmtCtx,
151 AugmentStatement.class)) {
152 // :TODO find target node and perform augmentation
154 // :TODO resolve other uses substatements