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.spi.meta;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
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.EffectiveStmtCtx.Current;
27 * Baseline implementation class for common {@link StatementSupport} implementations. This class performs many of the
28 * its duties in the canonical way -- taking away some amount of freedom for common functionality.
30 * @param <A> Argument type
31 * @param <D> Declared Statement representation
32 * @param <E> Effective Statement representation
35 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>,
36 E extends EffectiveStatement<A, D>> extends StatementSupport<A, D, E> {
38 protected AbstractStatementSupport(final StatementDefinition publicDefinition, final CopyPolicy copyPolicy) {
39 super(publicDefinition, copyPolicy);
42 protected AbstractStatementSupport(final StatementDefinition publicDefinition, final StatementPolicy<A, D> policy) {
43 super(publicDefinition, policy);
47 public final D createDeclared(final StmtContext<A, D, ?> ctx) {
48 final ImmutableList<? extends DeclaredStatement<?>> substatements = ctx.declaredSubstatements().stream()
49 .map(StmtContext::declared)
50 .collect(ImmutableList.toImmutableList());
51 return substatements.isEmpty() ? createEmptyDeclared(ctx) : createDeclared(ctx, substatements);
54 protected abstract @NonNull D createDeclared(@NonNull StmtContext<A, D, ?> ctx,
55 @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
57 protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<A, D, ?> ctx);
60 public E createEffective(final Current<A, D> stmt,
61 final Stream<? extends StmtContext<?, ?, ?>> declaredSubstatements,
62 final Stream<? extends StmtContext<?, ?, ?>> effectiveSubstatements) {
63 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements =
64 buildEffectiveSubstatements(stmt, statementsToBuild(stmt,
65 declaredSubstatements(declaredSubstatements, effectiveSubstatements)));
66 return createEffective(stmt, substatements);
69 protected abstract @NonNull E createEffective(@NonNull Current<A, D> stmt,
70 @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
73 * Give statement support a hook to transform statement contexts before they are built. Default implementation
74 * does nothing, but note {@code augment} statement performs a real transformation.
76 * @param ctx Effective capture of this statement's significant state
77 * @param substatements Substatement contexts which have been determined to be built
78 * @return Substatement context which are to be actually built
80 protected List<? extends StmtContext<?, ?, ?>> statementsToBuild(final Current<A, D> ctx,
81 final List<? extends StmtContext<?, ?, ?>> substatements) {
85 // FIXME: add documentation
86 public static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
87 final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
88 for (EffectiveStatement<?, ?> stmt : statements) {
89 if (type.isInstance(stmt)) {
90 return type.cast(stmt);
96 // FIXME: add documentation
97 public static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
98 final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<@NonNull E> type,
100 final @Nullable E stmt = findFirstStatement(statements, type);
101 return stmt != null ? stmt.argument() : defValue;
105 * Create a set of substatements. This method is split out so it can be overridden in subclasses adjust the
106 * resulting statements.
108 * @param stmt Current statement context
109 * @param substatements proposed substatements
110 * @return Built effective substatements
112 protected @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
113 final Current<A, D> stmt, final List<? extends StmtContext<?, ?, ?>> substatements) {
114 return defaultBuildEffectiveSubstatements(substatements);
117 private static @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> defaultBuildEffectiveSubstatements(
118 final List<? extends StmtContext<?, ?, ?>> substatements) {
119 return substatements.stream()
120 .filter(StmtContext::isSupportedToBuildEffective)
121 .map(StmtContext::buildEffective)
122 .collect(ImmutableList.toImmutableList());
125 private static @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(
126 final Stream<? extends StmtContext<?, ?, ?>> declaredSubstatements,
127 final Stream<? extends StmtContext<?, ?, ?>> effectiveSubstatements) {
129 * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
130 * the statements were defined -- i.e. if we have something like this:
149 * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
150 * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
152 * FIXME: 7.0.0: this really should be handled by UsesStatementSupport such that 'uses baz' would have a
153 * prerequisite of a resolved 'uses bar'.
155 final List<StmtContext<?, ?, ?>> declaredInit = declaredSubstatements
156 .filter(StmtContext::isSupportedByFeatures)
157 .collect(Collectors.toList());
159 final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
160 Set<StmtContext<?, ?, ?>> filteredStatements = null;
161 for (final StmtContext<?, ?, ?> declaredSubstatement : declaredInit) {
162 substatementsInit.add(declaredSubstatement);
164 // FIXME: YANGTOOLS-1161: we need to integrate this functionality into the reactor, so that this
165 // transformation is something reactor's declared statements already take into
167 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
168 if (!effect.isEmpty()) {
169 if (filteredStatements == null) {
170 filteredStatements = new HashSet<>();
172 filteredStatements.addAll(effect);
173 substatementsInit.addAll(effect);
177 final Stream<? extends StmtContext<?, ?, ?>> effective;
178 if (filteredStatements != null) {
179 final Set<StmtContext<?, ?, ?>> filtered = filteredStatements;
180 effective = effectiveSubstatements.filter(stmt -> !filtered.contains(stmt));
182 effective = effectiveSubstatements;
185 substatementsInit.addAll(effective.collect(Collectors.toList()));
186 return substatementsInit;