Un-deprecate CopyableNode, AddedByUsesAware
[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     protected AbstractStatementSupport(final StatementDefinition publicDefinition, final StatementPolicy<A, D> policy) {
38         super(publicDefinition, policy);
39     }
40
41     @Override
42     public final D createDeclared(final StmtContext<A, D, ?> ctx) {
43         final ImmutableList<? extends DeclaredStatement<?>> substatements = ctx.declaredSubstatements().stream()
44                 .map(StmtContext::declared)
45                 .collect(ImmutableList.toImmutableList());
46         return substatements.isEmpty() ? createEmptyDeclared(ctx) : createDeclared(ctx, substatements);
47     }
48
49     protected abstract @NonNull D createDeclared(@NonNull StmtContext<A, D, ?> ctx,
50             @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
51
52     protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<A, D, ?> ctx);
53
54     @Override
55     public final E createEffective(final Current<A, D> stmt,
56             final Stream<? extends StmtContext<?, ?, ?>> declaredSubstatements,
57             final Stream<? extends StmtContext<?, ?, ?>> inferredSubstatements) {
58         final ImmutableList<? extends EffectiveStatement<?, ?>> substatements =
59                 buildEffectiveSubstatements(stmt, statementsToBuild(stmt,
60                     declaredSubstatements(declaredSubstatements, inferredSubstatements)));
61         return createEffective(stmt, substatements);
62     }
63
64     protected abstract @NonNull E createEffective(@NonNull Current<A, D> stmt,
65             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
66
67     @Override
68     public E copyEffective(final Current<A, D> stmt, final E original) {
69         // Most implementations are only interested in substatements. copyOf() here should be a no-op
70         return createEffective(stmt, ImmutableList.copyOf(original.effectiveSubstatements()));
71     }
72
73     /**
74      * Give statement support a hook to transform statement contexts before they are built. Default implementation
75      * does nothing, but note {@code augment} statement performs a real transformation.
76      *
77      * @param ctx Effective capture of this statement's significant state
78      * @param substatements Substatement contexts which have been determined to be built
79      * @return Substatement context which are to be actually built
80      */
81     protected List<? extends StmtContext<?, ?, ?>> statementsToBuild(final Current<A, D> ctx,
82             final List<? extends StmtContext<?, ?, ?>> substatements) {
83         return substatements;
84     }
85
86     // FIXME: add documentation
87     public static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
88             final Collection<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
89         for (EffectiveStatement<?, ?> stmt : statements) {
90             if (type.isInstance(stmt)) {
91                 return type.cast(stmt);
92             }
93         }
94         return null;
95     }
96
97     // FIXME: add documentation
98     public static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
99             final Collection<? extends EffectiveStatement<?, ?>> statements, final Class<@NonNull E> type,
100                     final A defValue) {
101         final @Nullable E stmt = findFirstStatement(statements, type);
102         return stmt != null ? stmt.argument() : defValue;
103     }
104
105     /**
106      * Create a set of substatements. This method is split out so it can be overridden in subclasses adjust the
107      * resulting statements.
108      *
109      * @param stmt Current statement context
110      * @param substatements proposed substatements
111      * @return Built effective substatements
112      */
113     protected @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
114             final Current<A, D> stmt, final List<? extends StmtContext<?, ?, ?>> substatements) {
115         return substatements.stream().map(StmtContext::buildEffective).collect(ImmutableList.toImmutableList());
116     }
117
118     private static @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(
119             final Stream<? extends StmtContext<?, ?, ?>> declaredSubstatements,
120             final Stream<? extends StmtContext<?, ?, ?>> effectiveSubstatements) {
121         /*
122          * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
123          * the statements were defined -- i.e. if we have something like this:
124          *
125          * container foo {
126          *   uses bar;
127          *   uses baz;
128          * }
129          *
130          * grouping baz {
131          *   leaf baz {
132          *     type string;
133          *   }
134          * }
135          *
136          * grouping bar {
137          *   leaf bar {
138          *     type string;
139          *   }
140          * }
141          *
142          * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
143          * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
144          *
145          * FIXME: 7.0.0: this really should be handled by UsesStatementSupport such that 'uses baz' would have a
146          *               prerequisite of a resolved 'uses bar'.
147          */
148         final List<StmtContext<?, ?, ?>> declaredInit = declaredSubstatements
149             .filter(StmtContext::isSupportedByFeatures)
150             .collect(Collectors.toList());
151
152         final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
153         Set<StmtContext<?, ?, ?>> filteredStatements = null;
154         for (final StmtContext<?, ?, ?> declaredSubstatement : declaredInit) {
155             substatementsInit.add(declaredSubstatement);
156
157             // FIXME: YANGTOOLS-1161: we need to integrate this functionality into the reactor, so that this
158             //                        transformation is something reactor's declared statements already take into
159             //                        account.
160             final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
161             if (!effect.isEmpty()) {
162                 if (filteredStatements == null) {
163                     filteredStatements = new HashSet<>();
164                 }
165                 filteredStatements.addAll(effect);
166                 // Note: we need to filter here to exclude unsupported statements
167                 effect.stream().filter(StmtContext::isSupportedToBuildEffective).forEach(substatementsInit::add);
168             }
169         }
170
171         final Stream<? extends StmtContext<?, ?, ?>> effective;
172         if (filteredStatements != null) {
173             final Set<StmtContext<?, ?, ?>> filtered = filteredStatements;
174             effective = effectiveSubstatements.filter(stmt -> !filtered.contains(stmt));
175         } else {
176             effective = effectiveSubstatements;
177         }
178
179         substatementsInit.addAll(effective.collect(Collectors.toList()));
180         return substatementsInit;
181     }
182 }