131ac39f848f2fd3ff0c3f5256c481d93c7306d5
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / GroupingUtils.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import java.util.Collection;
11 import java.util.HashSet;
12 import java.util.Set;
13
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;
28
29 public final class GroupingUtils {
30
31     private GroupingUtils() {
32     }
33
34     /**
35      * @param sourceGrpStmtCtx
36      * @param targetCtx
37      * @throws SourceException
38      */
39     public static void copyFromSourceToTarget(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
40             StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
41
42         QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
43         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
44         copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
45
46     }
47
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);
58             }
59         }
60     }
61
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);
72             }
73         }
74     }
75
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();
84
85                 QName sourceQName = (QName) sourceStmtArgument;
86                 QNameModule sourceQNameModule = sourceQName.getModule();
87
88                 if (targetQNameModule.equals(sourceQNameModule)) {
89                     return null;
90                 }
91                 else {
92                     return targetQNameModule;
93                 }
94             } else {
95                 return null;
96             }
97         } else {
98             return null;
99         }
100     }
101
102     public static boolean needToCreateNewQName(StatementDefinition publicDefinition) {
103         return true;
104     }
105
106     public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
107
108         Set<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
109         noCopyDefSet.add(Rfc6020Mapping.USES);
110
111         StatementDefinition def = stmtContext.getPublicDefinition();
112         return (!noCopyDefSet.contains(def));
113     }
114
115     public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
116
117         Set<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
118         reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
119
120         StatementDefinition def = stmtContext.getPublicDefinition();
121         return (reusedDefSet.contains(def));
122     }
123
124     public static void resolveUsesNode(
125             Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
126             StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
127
128         Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode.declaredSubstatements();
129         for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
130             if (StmtContextUtils.producesDeclared(subStmtCtx, WhenStatement.class)) {
131                 StatementContextBase<?, ?, ?> copy = subStmtCtx.createCopy(null, targetNodeStmtCtx);
132                 targetNodeStmtCtx.addEffectiveSubstatement(copy);
133             }
134             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
135                 // :TODO resolve and perform refine statement
136             }
137
138             // :TODO resolve other uses substatements
139         }
140     }
141
142 }