Fixup collections return implementations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / BaseStatementSupport.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.rfc7950.stmt;
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 org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24
25 /**
26  * Implementation-internal base class for {@link AbstractStatementSupport} implementations.
27  *
28  * @param <A> Argument type
29  * @param <D> Declared Statement representation
30  * @param <E> Effective Statement representation
31  */
32 @Beta
33 public abstract class BaseStatementSupport<A, D extends DeclaredStatement<A>,
34         E extends EffectiveStatement<A, D>> extends AbstractStatementSupport<A, D, E> {
35     protected BaseStatementSupport(final StatementDefinition publicDefinition) {
36         super(publicDefinition);
37     }
38
39     protected BaseStatementSupport(final StatementDefinition publicDefinition, final CopyPolicy copyPolicy) {
40         super(publicDefinition, copyPolicy);
41     }
42
43     @Override
44     public final D createDeclared(final StmtContext<A, D, ?> ctx) {
45         final ImmutableList<? extends DeclaredStatement<?>> substatements = ctx.declaredSubstatements().stream()
46                 .map(StmtContext::buildDeclared)
47                 .collect(ImmutableList.toImmutableList());
48         return substatements.isEmpty() ? createEmptyDeclared(ctx) : createDeclared(ctx, substatements);
49     }
50
51     protected abstract @NonNull D createDeclared(@NonNull StmtContext<A, D, ?> ctx,
52             @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
53
54     protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<A, D, ?> ctx);
55
56     @Override
57     public E createEffective(final StmtContext<A, D, E> ctx) {
58         final D declared = ctx.buildDeclared();
59         final ImmutableList<? extends EffectiveStatement<?, ?>> substatements =
60                 buildEffectiveSubstatements(ctx, statementsToBuild(ctx, declaredSubstatements(ctx)));
61         return substatements.isEmpty() ? createEmptyEffective(ctx, declared)
62                 : createEffective(ctx, declared, substatements);
63     }
64
65     protected abstract @NonNull E createEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared,
66             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
67
68     protected abstract @NonNull E createEmptyEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared);
69
70     /**
71      * Give statement support a hook to transform statement contexts before they are built. Default implementation
72      * does nothing, but note {@code augment} statement performs a real transformation.
73      *
74      * @param ctx Parent statement context
75      * @param substatements Substatement contexts which have been determined to be built
76      * @return Substatement context which are to be actually built
77      */
78     protected List<? extends StmtContext<?, ?, ?>> statementsToBuild(final StmtContext<A, D, E> ctx,
79             final List<? extends StmtContext<?, ?, ?>> substatements) {
80         return substatements;
81     }
82
83     protected static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
84             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
85         for (EffectiveStatement<?, ?> stmt : statements) {
86             if (type.isInstance(stmt)) {
87                 return type.cast(stmt);
88             }
89         }
90         return null;
91     }
92
93     protected static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
94             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<@NonNull E> type,
95                     final A defValue) {
96         final @Nullable E stmt = findFirstStatement(statements, type);
97         return stmt != null ? stmt.argument() : defValue;
98     }
99
100     /**
101      * Create a set of substatements. This method is split out so it can be overridden in subclasses adjust the
102      * resulting statements.
103      *
104      * @param ctx Parent statement context
105      * @param substatements proposed substatements
106      * @return Built effective substatements
107      */
108     protected ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
109             final StmtContext<A, D, E> ctx, final List<? extends StmtContext<?, ?, ?>> substatements) {
110         return defaultBuildEffectiveSubstatements(substatements);
111     }
112
113     private static ImmutableList<? extends EffectiveStatement<?, ?>> defaultBuildEffectiveSubstatements(
114             final List<? extends StmtContext<?, ?, ?>> substatements) {
115         return substatements.stream()
116                 .filter(StmtContext::isSupportedToBuildEffective)
117                 .map(StmtContext::buildEffective)
118                 .collect(ImmutableList.toImmutableList());
119     }
120
121     private static @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(final StmtContext<?, ?, ?> ctx) {
122         /*
123          * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
124          * the statements were defined -- i.e. if we have something like this:
125          *
126          * container foo {
127          *   uses bar;
128          *   uses baz;
129          * }
130          *
131          * grouping baz {
132          *   leaf baz {
133          *     type string;
134          *   }
135          * }
136          *
137          * grouping bar {
138          *   leaf bar {
139          *     type string;
140          *   }
141          * }
142          *
143          * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
144          * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
145          *
146          * FIXME: 7.0.0: this really should be handled by UsesStatementSupport such that 'uses baz' would have a
147          *               prerequisite of a resolved 'uses bar'.
148          */
149         final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
150         Set<StmtContext<?, ?, ?>> filteredStatements = null;
151         for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
152             if (declaredSubstatement.isSupportedByFeatures()) {
153                 substatementsInit.add(declaredSubstatement);
154
155                 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
156                 if (!effect.isEmpty()) {
157                     if (filteredStatements == null) {
158                         filteredStatements = new HashSet<>();
159                     }
160                     filteredStatements.addAll(effect);
161                     substatementsInit.addAll(effect);
162                 }
163             }
164         }
165
166         if (filteredStatements != null) {
167             for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
168                 if (!filteredStatements.contains(stmt)) {
169                     substatementsInit.add(stmt);
170                 }
171             }
172         } else {
173             substatementsInit.addAll(ctx.effectiveSubstatements());
174         }
175
176         return substatementsInit;
177     }
178 }