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 org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
12 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
13 import java.util.Collection;
14 import java.util.HashSet;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
21 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31 public final class GroupingUtils {
33 private GroupingUtils() {
37 * @param sourceGrpStmtCtx
39 * @throws SourceException
41 public static void copyFromSourceToTarget(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
42 StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
44 QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
45 copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
46 copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
50 public static void copyDeclaredStmts(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
51 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule) throws SourceException {
52 Collection<? extends StatementContextBase<?, ?, ?>> declaredSubstatements = sourceGrpStmtCtx
53 .declaredSubstatements();
54 for (StatementContextBase<?, ?, ?> originalStmtCtx : declaredSubstatements) {
55 if (needToCopyByUses(originalStmtCtx)) {
56 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx, TypeOfCopy.ADDED_BY_USES);
57 targetCtx.addEffectiveSubstatement(copy);
58 } else if (isReusedByUses(originalStmtCtx)) {
59 targetCtx.addEffectiveSubstatement(originalStmtCtx);
64 public static void copyEffectiveStmts(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
65 StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule) throws SourceException {
66 Collection<? extends StatementContextBase<?, ?, ?>> effectiveSubstatements = sourceGrpStmtCtx
67 .effectiveSubstatements();
68 for (StatementContextBase<?, ?, ?> originalStmtCtx : effectiveSubstatements) {
69 if (needToCopyByUses(originalStmtCtx)) {
70 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx, TypeOfCopy.ADDED_BY_USES);
71 targetCtx.addEffectiveSubstatement(copy);
72 } else if (isReusedByUses(originalStmtCtx)) {
73 targetCtx.addEffectiveSubstatement(originalStmtCtx);
78 public static QNameModule getNewQNameModule(StatementContextBase<?, ?, ?> targetCtx,
79 StmtContext<?, ?, ?> stmtContext) {
80 if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
81 if(targetCtx.isRootContext()) {
82 return targetCtx.getFromNamespace(ModuleNameToModuleQName.class, targetCtx.rawStatementArgument());
84 Object targetStmtArgument = targetCtx.getStatementArgument();
85 Object sourceStmtArgument = stmtContext.getStatementArgument();
86 if (targetStmtArgument instanceof QName && 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;
106 public static boolean needToCreateNewQName(StatementDefinition publicDefinition) {
110 public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
112 Set<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
113 noCopyDefSet.add(Rfc6020Mapping.USES);
115 StatementDefinition def = stmtContext.getPublicDefinition();
116 return !noCopyDefSet.contains(def);
119 public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
121 Set<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
122 reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
124 StatementDefinition def = stmtContext.getPublicDefinition();
125 return reusedDefSet.contains(def);
128 public static void resolveUsesNode(
129 Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
130 StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
132 Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode.declaredSubstatements();
133 for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
134 if (StmtContextUtils.producesDeclared(subStmtCtx, WhenStatement.class)) {
135 StatementContextBase<?, ?, ?> copy = subStmtCtx.createCopy(null, targetNodeStmtCtx, TypeOfCopy.ADDED_BY_USES);
136 targetNodeStmtCtx.addEffectiveSubstatement(copy);
138 if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
139 // :TODO resolve and perform refine statement
141 if (StmtContextUtils.producesDeclared(subStmtCtx, AugmentStatement.class)) {
142 // :TODO find target node and perform augmentation
144 // :TODO resolve other uses substatements