BUG-6972: Remove GroupingUtils.needToCreateNewQName()
[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.CopyType;
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.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
44      *            source grouping statement context
45      * @param targetCtx
46      *            target context
47      * @param usesNode
48      *            uses node
49      * @throws SourceException
50      *             instance of SourceException
51      */
52     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
53             final StatementContextBase<?, ?, ?> targetCtx,
54             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
55
56         final QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
57         for (final StatementContextBase<?, ?, ?> original : sourceGrpStmtCtx.declaredSubstatements()) {
58             if (StmtContextUtils.areFeaturesSupported(original)) {
59                 copyStatement(original, targetCtx, usesNode, newQNameModule);
60             }
61         }
62
63         for (final StatementContextBase<?, ?, ?> original : sourceGrpStmtCtx.effectiveSubstatements()) {
64             copyStatement(original, targetCtx, usesNode, newQNameModule);
65         }
66     }
67
68     private static void copyStatement(final StatementContextBase<?, ?, ?> original,
69             final StatementContextBase<?, ?, ?> targetCtx,
70             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> targetUses,
71             final QNameModule targetModule) {
72         if (needToCopyByUses(original)) {
73             final StatementContextBase<?, ?, ?> copy = original.createCopy(targetModule, targetCtx,
74                     CopyType.ADDED_BY_USES);
75             targetCtx.addEffectiveSubstatement(copy);
76             targetUses.addAsEffectOfStatement(copy);
77         } else if (isReusedByUsesOnTop(original)) {
78             targetCtx.addEffectiveSubstatement(original);
79             targetUses.addAsEffectOfStatement(original);
80         }
81     }
82
83     public static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
84             final StmtContext<?, ?, ?> stmtContext) {
85         if (targetCtx.isRootContext()) {
86             return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
87         }
88         if (targetCtx.getPublicDefinition() == Rfc6020Mapping.AUGMENT) {
89             return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
90         }
91
92         final Object targetStmtArgument = targetCtx.getStatementArgument();
93         final Object sourceStmtArgument = stmtContext.getStatementArgument();
94         if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
95             return ((QName) targetStmtArgument).getModule();
96         }
97
98         return null;
99     }
100
101     private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES,
102             Rfc6020Mapping.TYPEDEF, Rfc6020Mapping.TYPE);
103     private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(Rfc6020Mapping.DESCRIPTION,
104             Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
105     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF,
106             Rfc6020Mapping.TYPE, Rfc6020Mapping.USES);
107     private static final Set<Rfc6020Mapping> TOP_REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF,
108             Rfc6020Mapping.TYPE);
109
110     public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
111         final StatementDefinition def = stmtContext.getPublicDefinition();
112
113         return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def) && Rfc6020Mapping.GROUPING
114                 .equals(stmtContext.getParentContext().getPublicDefinition())));
115     }
116
117     public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
118         return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
119     }
120
121     public static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
122         return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
123     }
124
125     public static void resolveUsesNode(
126             final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
127             final StatementContextBase<?, ?, ?> targetNodeStmtCtx) {
128         for (final StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
129             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
130                 performRefine(subStmtCtx, targetNodeStmtCtx);
131             }
132         }
133     }
134
135     private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
136             final StatementContextBase<?, ?, ?> usesParentCtx) {
137
138         final Object refineArgument = refineCtx.getStatementArgument();
139         Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
140                 "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
141                 refineCtx.getStatementSourceReference());
142
143         final SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
144         final StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx,
145                 refineTargetNodeIdentifier);
146         Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
147                 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
148         if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
149             LOG.debug(
150                     "Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
151                     refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(),
152                     refineTargetNodeCtx.getStatementArgument(), refineCtx.getStatementSourceReference());
153             refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
154             return;
155         }
156
157         addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
158         refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
159     }
160
161     private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
162             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
163         for (final StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
164             if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
165                 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
166             }
167         }
168     }
169
170     private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
171             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
172
173         final StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
174
175         SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
176                 refineSubstatementCtx.getStatementSourceReference(),
177                 "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
178                 refineSubstatementCtx.getRoot().getStatementArgument(), refineSubstatementCtx.getParentContext()
179                         .getStatementArgument(), refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx
180                         .getPublicDefinition());
181
182         if (isAllowedToAddByRefine(refineSubstatementDef)) {
183             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
184         } else {
185             refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
186             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
187         }
188     }
189
190     private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
191
192     private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
193         return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
194     }
195
196     private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
197         final Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
198                 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
199
200         return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
201                 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
202                 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
203     }
204
205     private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
206             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
207
208         final Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS
209                 .get(refineSubstatementCtx.getPublicDefinition());
210
211         return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
212                 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
213     }
214 }