44f91489ab9045b41b0a2d560e135e7d2a6ed99e
[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 org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
26
27 public class GroupingUtils {
28
29     /**
30      * @param sourceGrpStmtCtx
31      * @param targetCtx
32      * @throws SourceException
33      */
34     public static void copyFromSourceToTarget(
35             StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
36             StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
37
38         QNameModule newQNameModule = getNewQNameModule(targetCtx,
39                 sourceGrpStmtCtx);
40         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
41         copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, newQNameModule);
42
43     }
44
45     public static void copyDeclaredStmts(
46             StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
47             StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
48             throws SourceException {
49         Collection<? extends StatementContextBase<?, ?, ?>> declaredSubstatements = sourceGrpStmtCtx
50                 .declaredSubstatements();
51         for (StatementContextBase<?, ?, ?> originalStmtCtx : declaredSubstatements) {
52             if (needToCopyByUses(originalStmtCtx)) {
53                 StatementContextBase<?, ?, ?> copy = originalStmtCtx
54                         .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(
63             StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
64             StatementContextBase<?, ?, ?> targetCtx, QNameModule newQNameModule)
65             throws SourceException {
66         Collection<? extends StatementContextBase<?, ?, ?>> effectiveSubstatements = sourceGrpStmtCtx
67                 .effectiveSubstatements();
68         for (StatementContextBase<?, ?, ?> originalStmtCtx : effectiveSubstatements) {
69             if (needToCopyByUses(originalStmtCtx)) {
70                 StatementContextBase<?, ?, ?> copy = originalStmtCtx
71                         .createCopy(newQNameModule, targetCtx);
72                 targetCtx.addEffectiveSubstatement(copy);
73             } else if (isReusedByUses(originalStmtCtx)) {
74                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
75             }
76         }
77     }
78
79     public static QNameModule getNewQNameModule(
80             StatementContextBase<?, ?, ?> targetCtx,
81             StmtContext<?, ?, ?> stmtContext) {
82         if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
83             Object targetStmtArgument = targetCtx.getStatementArgument();
84             Object sourceStmtArgument = stmtContext.getStatementArgument();
85             if (targetStmtArgument instanceof QName
86                     && 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             } else
98                 return null;
99         } else
100             return null;
101     }
102
103     public static boolean needToCreateNewQName(
104             StatementDefinition publicDefinition) {
105         return true;
106     }
107
108     public static boolean needToCopyByUses(StmtContext<?, ?, ?> stmtContext) {
109
110         HashSet<StatementDefinition> noCopyDefSet = new HashSet<StatementDefinition>();
111         noCopyDefSet.add(Rfc6020Mapping.USES);
112
113         StatementDefinition def = stmtContext.getPublicDefinition();
114         if (noCopyDefSet.contains(def))
115             return false;
116         else
117             return true;
118     }
119
120     public static boolean isReusedByUses(StmtContext<?, ?, ?> stmtContext) {
121
122         HashSet<StatementDefinition> reusedDefSet = new HashSet<StatementDefinition>();
123         reusedDefSet.add(Rfc6020Mapping.TYPEDEF);
124
125         StatementDefinition def = stmtContext.getPublicDefinition();
126         if (reusedDefSet.contains(def))
127             return true;
128         else
129             return false;
130     }
131
132     public static void resolveUsesNode(
133             Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
134             StatementContextBase<?, ?, ?> targetNodeStmtCtx)
135             throws SourceException {
136
137         Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = usesNode
138                 .declaredSubstatements();
139         for (StatementContextBase<?, ?, ?> subStmtCtx : declaredSubstatements) {
140             if (StmtContextUtils.producesDeclared(subStmtCtx,
141                     WhenStatement.class)) {
142                 StatementContextBase<?, ?, ?> copy = subStmtCtx.createCopy(
143                         null, targetNodeStmtCtx);
144                 targetNodeStmtCtx.addEffectiveSubstatement(copy);
145             }
146             if (StmtContextUtils.producesDeclared(subStmtCtx,
147                     RefineStatement.class)) {
148                 // :TODO resolve and perform refine statement
149             }
150             if (StmtContextUtils.producesDeclared(subStmtCtx,
151                     AugmentStatement.class)) {
152                 // :TODO find target node and perform augmentation
153             }
154             // :TODO resolve other uses substatements
155         }
156     }
157
158 }