2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.uses;
10 import com.google.common.base.Verify;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.LinkedHashMap;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
29 import org.opendaylight.yangtools.yang.model.api.stmt.RefineEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.refine.RefineEffectiveStatementImpl;
39 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
52 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
53 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
54 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
55 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
56 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 public final class UsesStatementSupport
61 extends BaseQNameStatementSupport<UsesStatement, UsesEffectiveStatement> {
62 private static final Logger LOG = LoggerFactory.getLogger(UsesStatementSupport.class);
63 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
65 .addAny(YangStmtMapping.AUGMENT)
66 .addOptional(YangStmtMapping.DESCRIPTION)
67 .addAny(YangStmtMapping.IF_FEATURE)
68 .addAny(YangStmtMapping.REFINE)
69 .addOptional(YangStmtMapping.REFERENCE)
70 .addOptional(YangStmtMapping.STATUS)
71 .addOptional(YangStmtMapping.WHEN)
73 private static final UsesStatementSupport INSTANCE = new UsesStatementSupport();
75 private UsesStatementSupport() {
76 super(YangStmtMapping.USES);
79 public static UsesStatementSupport getInstance() {
84 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
85 return StmtContextUtils.parseNodeIdentifier(ctx, value);
89 public void onFullDefinitionDeclared(final Mutable<QName, UsesStatement, UsesEffectiveStatement> usesNode) {
90 if (!usesNode.isSupportedByFeatures()) {
93 super.onFullDefinitionDeclared(usesNode);
95 final ModelActionBuilder usesAction = usesNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
96 final QName groupingName = usesNode.getStatementArgument();
98 final Prerequisite<StmtContext<?, ?, ?>> sourceGroupingPre = usesAction.requiresCtx(usesNode,
99 GroupingNamespace.class, groupingName, ModelProcessingPhase.EFFECTIVE_MODEL);
100 final Prerequisite<? extends StmtContext.Mutable<?, ?, ?>> targetNodePre = usesAction.mutatesEffectiveCtx(
101 usesNode.getParentContext());
103 usesAction.apply(new InferenceAction() {
106 public void apply(final InferenceContext ctx) {
107 final StatementContextBase<?, ?, ?> targetNodeStmtCtx =
108 (StatementContextBase<?, ?, ?>) targetNodePre.resolve(ctx);
109 final StatementContextBase<?, ?, ?> sourceGrpStmtCtx =
110 (StatementContextBase<?, ?, ?>) sourceGroupingPre.resolve(ctx);
112 copyFromSourceToTarget(sourceGrpStmtCtx, targetNodeStmtCtx, usesNode);
113 resolveUsesNode(usesNode, targetNodeStmtCtx);
114 StmtContextUtils.validateIfFeatureAndWhenOnListKeys(usesNode);
118 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
119 InferenceException.throwIf(failed.contains(sourceGroupingPre),
120 usesNode.getStatementSourceReference(), "Grouping '%s' was not resolved.", groupingName);
121 throw new InferenceException("Unknown error occurred.", usesNode.getStatementSourceReference());
127 protected SubstatementValidator getSubstatementValidator() {
128 return SUBSTATEMENT_VALIDATOR;
132 protected UsesStatement createDeclared(final StmtContext<QName, UsesStatement, ?> ctx,
133 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
134 return new RegularUsesStatement(ctx, substatements);
138 protected UsesStatement createEmptyDeclared(final StmtContext<QName, UsesStatement, ?> ctx) {
139 return new EmptyUsesStatement(ctx);
143 protected UsesEffectiveStatement createEffective(
144 final StmtContext<QName, UsesStatement, UsesEffectiveStatement> ctx, final UsesStatement declared,
145 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
146 final GroupingDefinition sourceGrouping = getSourceGrouping(ctx);
147 final int flags = historyAndStatusFlags(ctx, substatements);
148 final QName argument = ctx.coerceStatementArgument();
149 if (declared.argument().equals(argument)) {
150 return new RegularLocalUsesEffectiveStatement(declared, sourceGrouping, flags, substatements);
152 if (findFirstStatement(substatements, RefineEffectiveStatement.class) == null) {
153 return new SimpleCopiedUsesEffectiveStatement(declared, argument, sourceGrouping, flags, substatements);
155 return new FullCopiedUsesEffectiveStatement(declared, argument, sourceGrouping, flags, substatements);
159 protected UsesEffectiveStatement createEmptyEffective(
160 final StmtContext<QName, UsesStatement, UsesEffectiveStatement> ctx, final UsesStatement declared) {
161 final GroupingDefinition sourceGrouping = getSourceGrouping(ctx);
162 final int flags = historyAndStatusFlags(ctx, ImmutableList.of());
163 final QName argument = ctx.coerceStatementArgument();
164 return argument.equals(declared.argument())
165 ? new EmptyLocalUsesEffectiveStatement(declared, sourceGrouping, flags)
166 : new SimpleCopiedUsesEffectiveStatement(declared, argument, sourceGrouping, flags);
169 static @NonNull ImmutableMap<Descendant, SchemaNode> indexRefines(
170 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
171 final Map<Descendant, SchemaNode> refines = new LinkedHashMap<>();
173 for (EffectiveStatement<?, ?> effectiveStatement : substatements) {
174 if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
175 final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
176 refines.put(refineStmt.argument(), refineStmt.getRefineTargetNode());
180 return ImmutableMap.copyOf(refines);
183 private static GroupingDefinition getSourceGrouping(final StmtContext<QName, ?, ?> ctx) {
184 return (GroupingDefinition) ctx.getFromNamespace(GroupingNamespace.class, ctx.coerceStatementArgument())
189 * Copy statements from a grouping to a target node.
191 * @param sourceGrpStmtCtx
192 * source grouping statement context
197 * @throws SourceException
198 * instance of SourceException
200 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
201 justification = "https://github.com/spotbugs/spotbugs/issues/811")
202 private static void copyFromSourceToTarget(final Mutable<?, ?, ?> sourceGrpStmtCtx,
203 final StatementContextBase<?, ?, ?> targetCtx,
204 final Mutable<QName, UsesStatement, UsesEffectiveStatement> usesNode) {
205 final Collection<? extends Mutable<?, ?, ?>> declared = sourceGrpStmtCtx.mutableDeclaredSubstatements();
206 final Collection<? extends Mutable<?, ?, ?>> effective = sourceGrpStmtCtx.mutableEffectiveSubstatements();
207 final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
208 final QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
210 for (final Mutable<?, ?, ?> original : declared) {
211 if (original.isSupportedByFeatures() && shouldCopy(original)) {
212 original.copyAsChildOf(targetCtx, CopyType.ADDED_BY_USES, newQNameModule).ifPresent(buffer::add);
216 for (final Mutable<?, ?, ?> original : effective) {
217 if (shouldCopy(original)) {
218 original.copyAsChildOf(targetCtx, CopyType.ADDED_BY_USES, newQNameModule).ifPresent(buffer::add);
222 targetCtx.addEffectiveSubstatements(buffer);
223 usesNode.addAsEffectOfStatement(buffer);
226 private static boolean shouldCopy(final StmtContext<?, ?, ?> stmt) {
227 // https://tools.ietf.org/html/rfc7950#section-7.13:
229 // The effect of a "uses" reference to a grouping is that the nodes
230 // defined by the grouping are copied into the current schema tree and
231 // are then updated according to the "refine" and "augment" statements.
233 // This means that the statement that is about to be copied (and can be subjected to buildEffective() I think)
234 // is actually a SchemaTreeEffectiveStatement
235 if (SchemaTreeEffectiveStatement.class.isAssignableFrom(
236 stmt.getPublicDefinition().getEffectiveRepresentationClass())) {
240 // As per https://tools.ietf.org/html/rfc7950#section-7.13.2:
242 // o Any node can get refined extensions, if the extension allows
243 // refinement. See Section 7.19 for details.
245 // and https://tools.ietf.org/html/rfc7950#section-7.19:
247 // An extension can allow refinement (see Section 7.13.2) and deviations
248 // (Section 7.20.3.2), but the mechanism for how this is defined is
249 // outside the scope of this specification.
251 // This is actively used out there (tailf-common.yang's tailf:action), which is incorrect, though. They do
252 // publish a bunch of metadata (through tailf-meta-extension.yang), but fail to publish a key aspect of the
253 // statement: it attaches to schema tree namespace (just as RFC7950 action does). Such an extension would
254 // automatically result in the extension being picked up by the above check and everybody would live happily
257 // We do not live in that world yet, hence we do the following and keep our fingers crossed.
258 // FIXME: YANGTOOLS-403: this should not be necessary once we implement the above (although tests will complain)
259 return StmtContextUtils.isUnknownStatement(stmt);
262 private static QNameModule getNewQNameModule(final StmtContext<?, ?, ?> targetCtx,
263 final StmtContext<?, ?, ?> stmtContext) {
264 if (targetCtx.getParentContext() == null) {
265 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
267 if (targetCtx.getPublicDefinition() == YangStmtMapping.AUGMENT) {
268 return StmtContextUtils.getRootModuleQName(targetCtx);
271 final Object targetStmtArgument = targetCtx.getStatementArgument();
272 final Object sourceStmtArgument = stmtContext.getStatementArgument();
273 if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
274 return ((QName) targetStmtArgument).getModule();
280 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
281 justification = "https://github.com/spotbugs/spotbugs/issues/811")
282 private static void resolveUsesNode(final Mutable<QName, UsesStatement, UsesEffectiveStatement> usesNode,
283 final StmtContext<?, ?, ?> targetNodeStmtCtx) {
284 for (final Mutable<?, ?, ?> subStmtCtx : usesNode.mutableDeclaredSubstatements()) {
285 if (subStmtCtx.producesDeclared(RefineStatement.class) && areFeaturesSupported(subStmtCtx)) {
286 performRefine(subStmtCtx, targetNodeStmtCtx);
291 private static boolean areFeaturesSupported(final StmtContext<?, ?, ?> subStmtCtx) {
293 * In case of Yang 1.1, checks whether features are supported.
295 return !YangVersion.VERSION_1_1.equals(subStmtCtx.getRootVersion()) || subStmtCtx.isSupportedByFeatures();
298 private static void performRefine(final Mutable<?, ?, ?> subStmtCtx, final StmtContext<?, ?, ?> usesParentCtx) {
299 final Object refineArgument = subStmtCtx.getStatementArgument();
300 InferenceException.throwIf(!(refineArgument instanceof SchemaNodeIdentifier),
301 subStmtCtx.getStatementSourceReference(),
302 "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier.", refineArgument);
304 final Optional<StmtContext<?, ?, ?>> optRefineTargetCtx = SchemaTreeNamespace.findNode(
305 usesParentCtx, (SchemaNodeIdentifier) refineArgument);
306 InferenceException.throwIf(!optRefineTargetCtx.isPresent(), subStmtCtx.getStatementSourceReference(),
307 "Refine target node %s not found.", refineArgument);
309 final StmtContext<?, ?, ?> refineTargetNodeCtx = optRefineTargetCtx.get();
310 if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
311 LOG.trace("Refine node '{}' in uses '{}' has target node unknown statement '{}'. "
312 + "Refine has been skipped. At line: {}", subStmtCtx.getStatementArgument(),
313 subStmtCtx.coerceParentContext().getStatementArgument(),
314 refineTargetNodeCtx.getStatementArgument(), subStmtCtx.getStatementSourceReference());
315 subStmtCtx.addAsEffectOfStatement(refineTargetNodeCtx);
319 Verify.verify(refineTargetNodeCtx instanceof StatementContextBase);
320 addOrReplaceNodes(subStmtCtx, (StatementContextBase<?, ?, ?>) refineTargetNodeCtx);
321 subStmtCtx.addAsEffectOfStatement(refineTargetNodeCtx);
324 private static void addOrReplaceNodes(final Mutable<?, ?, ?> subStmtCtx,
325 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
326 for (final Mutable<?, ?, ?> refineSubstatementCtx : subStmtCtx.mutableDeclaredSubstatements()) {
327 if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
328 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
333 private static void addOrReplaceNode(final Mutable<?, ?, ?> refineSubstatementCtx,
334 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
336 final StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
338 SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
339 refineSubstatementCtx.getStatementSourceReference(),
340 "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
341 refineSubstatementCtx.getRoot().rawStatementArgument(),
342 refineSubstatementCtx.coerceParentContext().getStatementArgument(),
343 refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
345 if (isAllowedToAddByRefine(refineSubstatementDef)) {
346 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
348 refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
349 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
353 private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
354 return YangStmtMapping.MUST.equals(publicDefinition);
357 private static boolean isSupportedRefineSubstatement(final StmtContext<?, ?, ?> refineSubstatementCtx) {
358 final Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
359 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
361 return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
362 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
363 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
366 private static boolean isSupportedRefineTarget(final StmtContext<?, ?, ?> refineSubstatementCtx,
367 final StmtContext<?, ?, ?> refineTargetNodeCtx) {
368 final Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
369 refineSubstatementCtx.getPublicDefinition());
371 return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
372 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());