9802db52b3b520c2d745005b46c46a1679d46f27
[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 org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
11
12 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Set;
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;
30
31 public final class GroupingUtils {
32
33     private GroupingUtils() {
34     }
35
36     /**
37      * @param sourceGrpStmtCtx
38      * @param targetCtx
39      * @throws SourceException
40      */
41     public static void copyFromSourceToTarget(StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
42             StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
43
44         QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
45         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
46         copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
47
48     }
49
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);
60             }
61         }
62     }
63
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);
74             }
75         }
76     }
77
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());
83             }
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();
89
90                 QName sourceQName = (QName) sourceStmtArgument;
91                 QNameModule sourceQNameModule = sourceQName.getModule();
92
93                 if (targetQNameModule.equals(sourceQNameModule)) {
94                     return null;
95                 } else {
96                     return targetQNameModule;
97                 }
98             } else {
99                 return null;
100             }
101         } else {
102             return null;
103         }
104     }
105
106     public static boolean needToCreateNewQName(StatementDefinition publicDefinition) {
107         return true;
108     }
109
110     public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
111
112         Set<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
113         noCopyDefSet.add(Rfc6020Mapping.USES);
114
115         StatementDefinition def = stmtContext.getPublicDefinition();
116         return !noCopyDefSet.contains(def);
117     }
118
119     public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
120
121         Set<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
122         reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
123
124         StatementDefinition def = stmtContext.getPublicDefinition();
125         return reusedDefSet.contains(def);
126     }
127
128     public static void resolveUsesNode(
129             Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
130             StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
131
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);
137             }
138             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
139                 // :TODO resolve and perform refine statement
140             }
141             if (StmtContextUtils.producesDeclared(subStmtCtx, AugmentStatement.class)) {
142                 // :TODO find target node and perform augmentation
143             }
144             // :TODO resolve other uses substatements
145         }
146     }
147
148 }