X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Frfc6020%2FGroupingUtils.java;fp=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Frfc6020%2FGroupingUtils.java;h=44f91489ab9045b41b0a2d560e135e7d2a6ed99e;hb=481a692d463636bbcf75f023da71703913e1b605;hp=0000000000000000000000000000000000000000;hpb=c7c67c1ad9fbd77e2705345400f1db5b3d46e200;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/GroupingUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/GroupingUtils.java new file mode 100644 index 0000000000..44f91489ab --- /dev/null +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/GroupingUtils.java @@ -0,0 +1,158 @@ +/** + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; + +import java.util.Collection; +import java.util.HashSet; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition; +import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; +import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase; + +public class GroupingUtils { + + /** + * @param sourceGrpStmtCtx + * @param targetCtx + * @throws SourceException + */ + public static void copyFromSourceToTarget( + StatementContextBase sourceGrpStmtCtx, + StatementContextBase targetCtx) throws SourceException { + + QNameModule newQNameModule = getNewQNameModule(targetCtx, + sourceGrpStmtCtx); + copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule); + copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule); + + } + + public static void copyDeclaredStmts( + StatementContextBase sourceGrpStmtCtx, + StatementContextBase targetCtx, QNameModule newQNameModule) + throws SourceException { + Collection> declaredSubstatements = sourceGrpStmtCtx + .declaredSubstatements(); + for (StatementContextBase originalStmtCtx : declaredSubstatements) { + if (needToCopyByUses(originalStmtCtx)) { + StatementContextBase copy = originalStmtCtx + .createCopy(newQNameModule, targetCtx); + targetCtx.addEffectiveSubstatement(copy); + } else if (isReusedByUses(originalStmtCtx)) { + targetCtx.addEffectiveSubstatement(originalStmtCtx); + } + } + } + + public static void copyEffectiveStmts( + StatementContextBase sourceGrpStmtCtx, + StatementContextBase targetCtx, QNameModule newQNameModule) + throws SourceException { + Collection> effectiveSubstatements = sourceGrpStmtCtx + .effectiveSubstatements(); + for (StatementContextBase originalStmtCtx : effectiveSubstatements) { + if (needToCopyByUses(originalStmtCtx)) { + StatementContextBase copy = originalStmtCtx + .createCopy(newQNameModule, targetCtx); + targetCtx.addEffectiveSubstatement(copy); + } else if (isReusedByUses(originalStmtCtx)) { + targetCtx.addEffectiveSubstatement(originalStmtCtx); + } + } + } + + public static QNameModule getNewQNameModule( + StatementContextBase targetCtx, + StmtContext stmtContext) { + if (needToCreateNewQName(stmtContext.getPublicDefinition())) { + Object targetStmtArgument = targetCtx.getStatementArgument(); + Object sourceStmtArgument = stmtContext.getStatementArgument(); + if (targetStmtArgument instanceof QName + && sourceStmtArgument instanceof QName) { + QName targetQName = (QName) targetStmtArgument; + QNameModule targetQNameModule = targetQName.getModule(); + + QName sourceQName = (QName) sourceStmtArgument; + QNameModule sourceQNameModule = sourceQName.getModule(); + + if (targetQNameModule.equals(sourceQNameModule)) + return null; + else + return targetQNameModule; + } else + return null; + } else + return null; + } + + public static boolean needToCreateNewQName( + StatementDefinition publicDefinition) { + return true; + } + + public static boolean needToCopyByUses(StmtContext stmtContext) { + + HashSet noCopyDefSet = new HashSet(); + noCopyDefSet.add(Rfc6020Mapping.USES); + + StatementDefinition def = stmtContext.getPublicDefinition(); + if (noCopyDefSet.contains(def)) + return false; + else + return true; + } + + public static boolean isReusedByUses(StmtContext stmtContext) { + + HashSet reusedDefSet = new HashSet(); + reusedDefSet.add(Rfc6020Mapping.TYPEDEF); + + StatementDefinition def = stmtContext.getPublicDefinition(); + if (reusedDefSet.contains(def)) + return true; + else + return false; + } + + public static void resolveUsesNode( + Mutable> usesNode, + StatementContextBase targetNodeStmtCtx) + throws SourceException { + + Collection> declaredSubstatements = usesNode + .declaredSubstatements(); + for (StatementContextBase subStmtCtx : declaredSubstatements) { + if (StmtContextUtils.producesDeclared(subStmtCtx, + WhenStatement.class)) { + StatementContextBase copy = subStmtCtx.createCopy( + null, targetNodeStmtCtx); + targetNodeStmtCtx.addEffectiveSubstatement(copy); + } + if (StmtContextUtils.producesDeclared(subStmtCtx, + RefineStatement.class)) { + // :TODO resolve and perform refine statement + } + if (StmtContextUtils.producesDeclared(subStmtCtx, + AugmentStatement.class)) { + // :TODO find target node and perform augmentation + } + // :TODO resolve other uses substatements + } + } + +}