Promote BaseStatementSupport to parser.spi.meta
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractStatementSupport.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.spi.meta;
9
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;
16 import java.util.Set;
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;
25
26 /**
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.
29  *
30  * @param <A> Argument type
31  * @param <D> Declared Statement representation
32  * @param <E> Effective Statement representation
33  */
34 @Beta
35 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>,
36         E extends EffectiveStatement<A, D>> extends StatementSupport<A, D, E> {
37     @Deprecated
38     protected AbstractStatementSupport(final StatementDefinition publicDefinition, final CopyPolicy copyPolicy) {
39         super(publicDefinition, copyPolicy);
40     }
41
42     protected AbstractStatementSupport(final StatementDefinition publicDefinition, final StatementPolicy<A, D> policy) {
43         super(publicDefinition, policy);
44     }
45
46     @Override
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);
52     }
53
54     protected abstract @NonNull D createDeclared(@NonNull StmtContext<A, D, ?> ctx,
55             @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
56
57     protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<A, D, ?> ctx);
58
59     @Override
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);
67     }
68
69     protected abstract @NonNull E createEffective(@NonNull Current<A, D> stmt,
70             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
71
72     /**
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.
75      *
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
79      */
80     protected List<? extends StmtContext<?, ?, ?>> statementsToBuild(final Current<A, D> ctx,
81             final List<? extends StmtContext<?, ?, ?>> substatements) {
82         return substatements;
83     }
84
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);
91             }
92         }
93         return null;
94     }
95
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,
99                     final A defValue) {
100         final @Nullable E stmt = findFirstStatement(statements, type);
101         return stmt != null ? stmt.argument() : defValue;
102     }
103
104     /**
105      * Create a set of substatements. This method is split out so it can be overridden in subclasses adjust the
106      * resulting statements.
107      *
108      * @param stmt Current statement context
109      * @param substatements proposed substatements
110      * @return Built effective substatements
111      */
112     protected @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
113             final Current<A, D> stmt, final List<? extends StmtContext<?, ?, ?>> substatements) {
114         return defaultBuildEffectiveSubstatements(substatements);
115     }
116
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());
123     }
124
125     private static @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(
126             final Stream<? extends StmtContext<?, ?, ?>> declaredSubstatements,
127             final Stream<? extends StmtContext<?, ?, ?>> effectiveSubstatements) {
128         /*
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:
131          *
132          * container foo {
133          *   uses bar;
134          *   uses baz;
135          * }
136          *
137          * grouping baz {
138          *   leaf baz {
139          *     type string;
140          *   }
141          * }
142          *
143          * grouping bar {
144          *   leaf bar {
145          *     type string;
146          *   }
147          * }
148          *
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.
151          *
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'.
154          */
155         final List<StmtContext<?, ?, ?>> declaredInit = declaredSubstatements
156             .filter(StmtContext::isSupportedByFeatures)
157             .collect(Collectors.toList());
158
159         final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
160         Set<StmtContext<?, ?, ?>> filteredStatements = null;
161         for (final StmtContext<?, ?, ?> declaredSubstatement : declaredInit) {
162             substatementsInit.add(declaredSubstatement);
163
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
166             //                        account.
167             final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
168             if (!effect.isEmpty()) {
169                 if (filteredStatements == null) {
170                     filteredStatements = new HashSet<>();
171                 }
172                 filteredStatements.addAll(effect);
173                 substatementsInit.addAll(effect);
174             }
175         }
176
177         final Stream<? extends StmtContext<?, ?, ?>> effective;
178         if (filteredStatements != null) {
179             final Set<StmtContext<?, ?, ?>> filtered = filteredStatements;
180             effective = effectiveSubstatements.filter(stmt -> !filtered.contains(stmt));
181         } else {
182             effective = effectiveSubstatements;
183         }
184
185         substatementsInit.addAll(effective.collect(Collectors.toList()));
186         return substatementsInit;
187     }
188 }