Merge "change to interface"
[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.StmtContextUtils;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
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(
40             StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
41             StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
42
43         QNameModule newQNameModule = getNewQNameModule(targetCtx,
44                 sourceGrpStmtCtx);
45         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
46         copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
47
48     }
49
50     public static void copyDeclaredStmts(
51             StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
52             StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
53             throws SourceException {
54         Collection<? extends StatementContextBase<?, ?, ?>> declaredSubstatements = sourceGrpStmtCtx
55                 .declaredSubstatements();
56         for (StatementContextBase<?, ?, ?> originalStmtCtx : declaredSubstatements) {
57             if (needToCopyByUses(originalStmtCtx)) {
58                 StatementContextBase<?, ?, ?> copy = originalStmtCtx
59                         .createCopy(newQNameModule, targetCtx);
60                 targetCtx.addEffectiveSubstatement(copy);
61             } else if (isReusedByUses(originalStmtCtx)) {
62                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
63             }
64         }
65     }
66
67     public static void copyEffectiveStmts(
68             StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
69             StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
70             throws SourceException {
71         Collection<? extends StatementContextBase<?, ?, ?>> effectiveSubstatements = sourceGrpStmtCtx
72                 .effectiveSubstatements();
73         for (StatementContextBase<?, ?, ?> originalStmtCtx : effectiveSubstatements) {
74             if (needToCopyByUses(originalStmtCtx)) {
75                 StatementContextBase<?, ?, ?> copy = originalStmtCtx
76                         .createCopy(newQNameModule, targetCtx);
77                 targetCtx.addEffectiveSubstatement(copy);
78             } else if (isReusedByUses(originalStmtCtx)) {
79                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
80             }
81         }
82     }
83
84     public static QNameModule getNewQNameModule(
85             StatementContextBase<?, ?, ?> targetCtx,
86             StmtContext<?, ?, ?> stmtContext) {
87         if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
88             Object targetStmtArgument = targetCtx.getStatementArgument();
89             Object sourceStmtArgument = stmtContext.getStatementArgument();
90             if (targetStmtArgument instanceof QName
91                     && sourceStmtArgument instanceof QName) {
92                 QName targetQName = (QName) targetStmtArgument;
93                 QNameModule targetQNameModule = targetQName.getModule();
94
95                 QName sourceQName = (QName) sourceStmtArgument;
96                 QNameModule sourceQNameModule = sourceQName.getModule();
97
98                 if (targetQNameModule.equals(sourceQNameModule)) {
99                     return null;
100                 }
101                 else {
102                     return targetQNameModule;
103                 }
104             } else {
105                 return null;
106             }
107         } else {
108             return null;
109         }
110     }
111
112     public static boolean needToCreateNewQName(
113             StatementDefinition publicDefinition) {
114         return true;
115     }
116
117     public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
118
119         Set<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
120         noCopyDefSet.add(Rfc6020Mapping.USES);
121
122         StatementDefinition def = stmtContext.getPublicDefinition();
123         return (!noCopyDefSet.contains(def));
124     }
125
126     public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
127
128         Set<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
129         reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
130
131         StatementDefinition def = stmtContext.getPublicDefinition();
132         return (reusedDefSet.contains(def));
133     }
134
135     public static void resolveUsesNode(
136             Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
137             StatementContextBase<?, ?, ?> targetNodeStmtCtx)
138             throws SourceException {
139
140         Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode
141                 .declaredSubstatements();
142         for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
143             if (StmtContextUtils.producesDeclared(subStmtCtx,
144                     WhenStatement.class)) {
145                 StatementContextBase<?, ?, ?> copy = subStmtCtx.createCopy(
146                         null, targetNodeStmtCtx);
147                 targetNodeStmtCtx.addEffectiveSubstatement(copy);
148             }
149             if (StmtContextUtils.producesDeclared(subStmtCtx,
150                     RefineStatement.class)) {
151                 // :TODO resolve and perform refine statement
152             }
153             if (StmtContextUtils.producesDeclared(subStmtCtx,
154                     AugmentStatement.class)) {
155                 // :TODO find target node and perform augmentation
156             }
157             // :TODO resolve other uses substatements
158         }
159     }
160
161 }