2 * Copyright (c) 2020 PANTHEON.tech, 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;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 * Implementation-internal base class for {@link AbstractStatementSupport} implementations.
30 * @param <A> Argument type
31 * @param <D> Declared Statement representation
32 * @param <E> Effective Statement representation
35 public abstract class BaseStatementSupport<A, D extends DeclaredStatement<A>,
36 E extends EffectiveStatement<A, D>> extends AbstractStatementSupport<A, D, E> {
37 protected BaseStatementSupport(final StatementDefinition publicDefinition) {
38 super(publicDefinition);
42 public final E createEffective(final StmtContext<A, D, E> ctx) {
43 final D declared = buildDeclared(ctx);
44 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements = buildEffectiveSubstatements(ctx);
45 return substatements.isEmpty() ? createEmptyEffective(ctx, declared)
46 : createEffective(ctx, declared, substatements);
49 protected abstract @NonNull E createEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared,
50 @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
52 protected abstract @NonNull E createEmptyEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared);
54 protected static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
55 final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
56 for (EffectiveStatement<?, ?> stmt : statements) {
57 if (type.isInstance(stmt)) {
58 return type.cast(stmt);
64 protected static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
65 final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type, final A defValue) {
66 final @Nullable E stmt = findFirstStatement(statements, type);
67 return stmt != null ? stmt.argument() : defValue;
70 static final <A, D extends DeclaredStatement<A>> @NonNull D buildDeclared(final StmtContext<A, D, ?> ctx) {
72 * Share original instance of declared statement between all effective
73 * statements which have been copied or derived from this original
76 @SuppressWarnings("unchecked")
77 final StmtContext<?, D, ?> lookupCtx = (StmtContext<?, D, ?>) ctx.getOriginalCtx().orElse(ctx);
78 return verifyNotNull(lookupCtx.buildDeclared(), "Statement %s failed to build declared statement", lookupCtx);
82 * Create a set of substatements. This method is split out so it can be overridden in
83 * ExtensionEffectiveStatementImpl to leak a not-fully-initialized instance.
85 * @param substatements proposed substatements
86 * @return Filtered substatements
88 private static ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
89 final List<? extends StmtContext<?, ?, ?>> substatements) {
90 return substatements.stream()
91 .filter(StmtContext::isSupportedToBuildEffective)
92 .map(StmtContext::buildEffective)
93 .collect(ImmutableList.toImmutableList());
96 static final ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
97 final StmtContext<?, ?, ?> ctx) {
98 return buildEffectiveSubstatements(declaredSubstatements(ctx));
101 static final @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(final StmtContext<?, ?, ?> ctx) {
103 * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
104 * the statements were defined -- i.e. if we have something like this:
123 * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
124 * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
126 * FIXME: 5.0.0: this really should be handled by UsesStatementSupport such that 'uses baz' would have a
127 * prerequisite of a resolved 'uses bar'.
129 final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
130 Set<StmtContext<?, ?, ?>> filteredStatements = null;
131 for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
132 if (declaredSubstatement.isSupportedByFeatures()) {
133 substatementsInit.add(declaredSubstatement);
135 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
136 if (!effect.isEmpty()) {
137 if (filteredStatements == null) {
138 filteredStatements = new HashSet<>();
140 filteredStatements.addAll(effect);
141 substatementsInit.addAll(effect);
146 if (filteredStatements != null) {
147 for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
148 if (!filteredStatements.contains(stmt)) {
149 substatementsInit.add(stmt);
153 substatementsInit.addAll(ctx.effectiveSubstatements());
156 return substatementsInit;