8106aa6350fd0d086d7d6ddaa2df73502e89e7b3
[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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.Set;
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.RefineStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
29 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public final class GroupingUtils {
35
36     private static final Logger LOG = LoggerFactory.getLogger(GroupingUtils.class);
37
38     private GroupingUtils() {
39         throw new UnsupportedOperationException();
40     }
41
42     /**
43      * @param sourceGrpStmtCtx source grouping statement context
44      * @param targetCtx target context
45      * @param usesNode uses node
46      * @throws SourceException instance of SourceException
47      */
48     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
49             final StatementContextBase<?, ?, ?> targetCtx,
50             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode)
51             throws SourceException {
52
53         QNameModule newQNameModule = getNewQNameModule(targetCtx,
54                 sourceGrpStmtCtx);
55         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
56         copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
57                 newQNameModule);
58     }
59
60     public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
61             final StatementContextBase<?, ?, ?> targetCtx,
62             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
63             final QNameModule newQNameModule) throws SourceException {
64         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
65             if (needToCopyByUses(originalStmtCtx)) {
66                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
67                     TypeOfCopy.ADDED_BY_USES);
68                 targetCtx.addEffectiveSubstatement(copy);
69                 usesNode.addAsEffectOfStatement(copy);
70             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
71                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
72                 usesNode.addAsEffectOfStatement(originalStmtCtx);
73             }
74         }
75     }
76
77     public static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
78             final StatementContextBase<?, ?, ?> targetCtx,
79             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
80             final QNameModule newQNameModule) throws SourceException {
81         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
82             if (needToCopyByUses(originalStmtCtx)) {
83                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
84                     TypeOfCopy.ADDED_BY_USES);
85                 targetCtx.addEffectiveSubstatement(copy);
86                 usesNode.addAsEffectOfStatement(copy);
87             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
88                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
89                 usesNode.addAsEffectOfStatement(originalStmtCtx);
90             }
91         }
92     }
93
94     public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
95             final StmtContext<?, ?, ?> stmtContext) {
96         if (needToCreateNewQName(stmtContext.getPublicDefinition())) {
97             if (targetCtx.isRootContext()) {
98                 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
99             }
100             if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
101                 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
102             }
103
104             Object targetStmtArgument = targetCtx.getStatementArgument();
105             Object sourceStmtArgument = stmtContext.getStatementArgument();
106             if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
107                 return ((QName) targetStmtArgument).getModule();
108             } else {
109                 return null;
110             }
111         } else {
112             return null;
113         }
114     }
115
116     public static boolean needToCreateNewQName(final StatementDefinition publicDefinition) {
117         return true;
118     }
119
120     private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(
121         Rfc6020Mapping.USES, Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
122     private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
123         Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE);
124     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(
125         Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE, Rfc6020Mapping.USES);
126     private static final Set<Rfc6020Mapping> TOP_REUSED_DEF_SET = ImmutableSet.of(
127         Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
128
129     public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
130         final StatementDefinition def = stmtContext.getPublicDefinition();
131
132         return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def)
133                 && Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition())));
134     }
135
136     public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
137         return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
138     }
139
140     public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
141         return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
142     }
143
144     public static void resolveUsesNode(
145             final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
146             final StatementContextBase<?, ?, ?> targetNodeStmtCtx) throws SourceException {
147         for (StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
148             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
149                 performRefine(subStmtCtx, targetNodeStmtCtx);
150             }
151         }
152     }
153
154     private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
155             final StatementContextBase<?, ?, ?> usesParentCtx) {
156
157         Object refineArgument = refineCtx.getStatementArgument();
158         Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
159             "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
160                 refineCtx.getStatementSourceReference());
161
162         SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
163         StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
164         Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
165                 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
166         if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
167             LOG.warn("Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
168                     refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(), refineTargetNodeCtx.getStatementArgument(),
169                     refineCtx.getStatementSourceReference());
170             refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
171             return;
172         }
173
174         addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
175         refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
176     }
177
178     private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
179             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
180         for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
181             if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
182                 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
183             }
184         }
185     }
186
187     private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
188             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
189
190         StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
191
192         SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
193             refineSubstatementCtx.getStatementSourceReference(),
194             "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
195                     refineSubstatementCtx.getRoot().getStatementArgument(),
196                     refineSubstatementCtx.getParentContext().getStatementArgument(),
197                     refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
198
199         if (isAllowedToAddByRefine(refineSubstatementDef)) {
200             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
201         } else {
202             refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
203             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
204         }
205     }
206
207     private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
208
209     private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
210         return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
211     }
212
213     private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
214         Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
215             ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
216
217         return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
218                 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
219                 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
220     }
221
222     private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
223             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
224
225         Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
226             refineSubstatementCtx.getPublicDefinition());
227
228         return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
229                 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
230     }
231 }