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.DeclarationReference;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
25 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
29 * Baseline implementation class for common {@link StatementSupport} implementations. This class performs many of the
30 * its duties in the canonical way -- taking away some amount of freedom for common functionality.
32 * @param <A> Argument type
33 * @param <D> Declared Statement representation
34 * @param <E> Effective Statement representation
37 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>,
38 E extends EffectiveStatement<A, D>> extends StatementSupport<A, D, E> {
39 private final boolean retainDeclarationReference;
41 protected AbstractStatementSupport(final StatementDefinition publicDefinition, final StatementPolicy<A, D> policy,
42 final YangParserConfiguration config) {
43 super(publicDefinition, policy);
44 this.retainDeclarationReference = config.retainDeclarationReferences();
48 public final D createDeclared(final StmtContext<A, D, ?> ctx) {
49 final D stmt = createDeclared(ctx, ctx.declaredSubstatements().stream()
50 .map(StmtContext::declared)
51 .collect(ImmutableList.toImmutableList()));
52 return retainDeclarationReference ? attachDeclarationReference(stmt, ctx) : stmt;
55 protected abstract @NonNull D createDeclared(@NonNull StmtContext<A, D, ?> ctx,
56 @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
58 private @NonNull D attachDeclarationReference(final @NonNull D stmt, final @NonNull StmtContext<A, D, ?> ctx) {
59 final DeclarationReference ref = ctx.sourceReference().declarationReference();
60 return ref == null ? stmt : attachDeclarationReference(stmt, ref);
64 * Attach specified {@link DeclarationReference} to a baseline declared statement. If an implementation does not
65 * support attaching DeclarationReferences, it should return the statement unchanged.
67 * @param stmt Declared statement
68 * @param reference {@link DeclarationReference} to attach
69 * @return Equivalent of stmt, potentially with specified reference attached.
71 // FIXME: make this method abstract
72 protected @NonNull D attachDeclarationReference(final @NonNull D stmt,
73 final @NonNull DeclarationReference reference) {
78 public final E createEffective(final Current<A, D> stmt,
79 final Stream<? extends StmtContext<?, ?, ?>> declaredSubstatements,
80 final Stream<? extends StmtContext<?, ?, ?>> inferredSubstatements) {
81 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements =
82 buildEffectiveSubstatements(stmt, statementsToBuild(stmt,
83 declaredSubstatements(declaredSubstatements, inferredSubstatements)));
84 return createEffective(stmt, substatements);
87 protected abstract @NonNull E createEffective(@NonNull Current<A, D> stmt,
88 @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
91 public E copyEffective(final Current<A, D> stmt, final E original) {
92 // Most implementations are only interested in substatements. copyOf() here should be a no-op
93 return createEffective(stmt, ImmutableList.copyOf(original.effectiveSubstatements()));
97 * Give statement support a hook to transform statement contexts before they are built. Default implementation
98 * does nothing, but note {@code augment} statement performs a real transformation.
100 * @param ctx Effective capture of this statement's significant state
101 * @param substatements Substatement contexts which have been determined to be built
102 * @return Substatement context which are to be actually built
104 protected List<? extends StmtContext<?, ?, ?>> statementsToBuild(final Current<A, D> ctx,
105 final List<? extends StmtContext<?, ?, ?>> substatements) {
106 return substatements;
109 // FIXME: add documentation
110 public static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
111 final Collection<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
112 for (EffectiveStatement<?, ?> stmt : statements) {
113 if (type.isInstance(stmt)) {
114 return type.cast(stmt);
120 // FIXME: add documentation
121 public static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
122 final Collection<? extends EffectiveStatement<?, ?>> statements, final Class<@NonNull E> type,
124 final @Nullable E stmt = findFirstStatement(statements, type);
125 return stmt != null ? stmt.argument() : defValue;
129 * Create a set of substatements. This method is split out so it can be overridden in subclasses adjust the
130 * resulting statements.
132 * @param stmt Current statement context
133 * @param substatements proposed substatements
134 * @return Built effective substatements
136 protected @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
137 final Current<A, D> stmt, final List<? extends StmtContext<?, ?, ?>> substatements) {
138 return substatements.stream().map(StmtContext::buildEffective).collect(ImmutableList.toImmutableList());
141 private static @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(
142 final Stream<? extends StmtContext<?, ?, ?>> declaredSubstatements,
143 final Stream<? extends StmtContext<?, ?, ?>> effectiveSubstatements) {
145 * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
146 * the statements were defined -- i.e. if we have something like this:
165 * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
166 * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
168 * FIXME: 7.0.0: this really should be handled by UsesStatementSupport such that 'uses baz' would have a
169 * prerequisite of a resolved 'uses bar'.
171 final List<StmtContext<?, ?, ?>> declaredInit = declaredSubstatements
172 .filter(StmtContext::isSupportedByFeatures)
173 .collect(Collectors.toList());
175 final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
176 Set<StmtContext<?, ?, ?>> filteredStatements = null;
177 for (final StmtContext<?, ?, ?> declaredSubstatement : declaredInit) {
178 substatementsInit.add(declaredSubstatement);
180 // FIXME: YANGTOOLS-1161: we need to integrate this functionality into the reactor, so that this
181 // transformation is something reactor's declared statements already take into
183 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
184 if (!effect.isEmpty()) {
185 if (filteredStatements == null) {
186 filteredStatements = new HashSet<>();
188 filteredStatements.addAll(effect);
189 // Note: we need to filter here to exclude unsupported statements
190 effect.stream().filter(StmtContext::isSupportedToBuildEffective).forEach(substatementsInit::add);
194 final Stream<? extends StmtContext<?, ?, ?>> effective;
195 if (filteredStatements != null) {
196 final Set<StmtContext<?, ?, ?>> filtered = filteredStatements;
197 effective = effectiveSubstatements.filter(stmt -> !filtered.contains(stmt));
199 effective = effectiveSubstatements;
202 substatementsInit.addAll(effective.collect(Collectors.toList()));
203 return substatementsInit;