Merge "Introduce InstanceIdToNodes into yang-data-impl"
[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                 } else {
91                     return targetQNameModule;
92                 }
93             } else {
94                 return null;
95             }
96         } else {
97             return null;
98         }
99     }
100
101     public static boolean needToCreateNewQName(StatementDefinition publicDefinition) {
102         return true;
103     }
104
105     public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
106
107         Set<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
108         noCopyDefSet.add(Rfc6020Mapping.USES);
109
110         StatementDefinition def = stmtContext.getPublicDefinition();
111         return !noCopyDefSet.contains(def);
112     }
113
114     public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
115
116         Set<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
117         reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
118
119         StatementDefinition def = stmtContext.getPublicDefinition();
120         return reusedDefSet.contains(def);
121     }
122
123     public static void resolveUsesNode(
124             Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
125             StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
126
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);
132             }
133             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
134                 // :TODO resolve and perform refine statement
135             }
136             if (StmtContextUtils.producesDeclared(subStmtCtx, AugmentStatement.class)) {
137                 // :TODO find target node and perform augmentation
138             }
139             // :TODO resolve other uses substatements
140         }
141     }
142
143 }