Make declared statement inheritance a StatementContextBase property
[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     @Override
40     public final D createDeclared(final StmtContext<A, D, ?> ctx) {
41         final ImmutableList<? extends DeclaredStatement<?>> substatements = ctx.declaredSubstatements().stream()
42                 .map(StmtContext::buildDeclared)
43                 .collect(ImmutableList.toImmutableList());
44         return substatements.isEmpty() ? createEmptyDeclared(ctx) : createDeclared(ctx, substatements);
45     }
46
47     protected abstract @NonNull D createDeclared(@NonNull StmtContext<A, D, ?> ctx,
48             @NonNull ImmutableList<? extends DeclaredStatement<?>> substatements);
49
50     protected abstract @NonNull D createEmptyDeclared(@NonNull StmtContext<A, D, ?> ctx);
51
52     @Override
53     public final E createEffective(final StmtContext<A, D, E> ctx) {
54         final D declared = ctx.buildDeclared();
55         final ImmutableList<? extends EffectiveStatement<?, ?>> substatements = buildEffectiveSubstatements(ctx);
56         return substatements.isEmpty() ? createEmptyEffective(ctx, declared)
57                 : createEffective(ctx, declared, substatements);
58     }
59
60     protected abstract @NonNull E createEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared,
61             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
62
63     protected abstract @NonNull E createEmptyEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared);
64
65     protected static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
66             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
67         for (EffectiveStatement<?, ?> stmt : statements) {
68             if (type.isInstance(stmt)) {
69                 return type.cast(stmt);
70             }
71         }
72         return null;
73     }
74
75     protected static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
76             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type, final A defValue) {
77         final @Nullable E stmt = findFirstStatement(statements, type);
78         return stmt != null ? stmt.argument() : defValue;
79     }
80
81     /**
82      * Create a set of substatements. This method is split out so it can be overridden in
83      * ExtensionEffectiveStatementImpl to leak a not-fully-initialized instance.
84      *
85      * @param substatements proposed substatements
86      * @return Filtered substatements
87      */
88     private static ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
89             final List<? extends StmtContext<?, ?, ?>> substatements) {
90         return substatements.stream()
91                 .filter(StmtContext::isSupportedToBuildEffective)
92                 .map(StmtContext::buildEffective)
93                 .collect(ImmutableList.toImmutableList());
94     }
95
96     static final ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
97             final StmtContext<?, ?, ?> ctx) {
98         return buildEffectiveSubstatements(declaredSubstatements(ctx));
99     }
100
101     static final @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(final StmtContext<?, ?, ?> ctx) {
102         /*
103          * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
104          * the statements were defined -- i.e. if we have something like this:
105          *
106          * container foo {
107          *   uses bar;
108          *   uses baz;
109          * }
110          *
111          * grouping baz {
112          *   leaf baz {
113          *     type string;
114          *   }
115          * }
116          *
117          * grouping bar {
118          *   leaf bar {
119          *     type string;
120          *   }
121          * }
122          *
123          * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
124          * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
125          *
126          * FIXME: 5.0.0: this really should be handled by UsesStatementSupport such that 'uses baz' would have a
127          *               prerequisite of a resolved 'uses bar'.
128          */
129         final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
130         Set<StmtContext<?, ?, ?>> filteredStatements = null;
131         for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
132             if (declaredSubstatement.isSupportedByFeatures()) {
133                 substatementsInit.add(declaredSubstatement);
134
135                 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
136                 if (!effect.isEmpty()) {
137                     if (filteredStatements == null) {
138                         filteredStatements = new HashSet<>();
139                     }
140                     filteredStatements.addAll(effect);
141                     substatementsInit.addAll(effect);
142                 }
143             }
144         }
145
146         if (filteredStatements != null) {
147             for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
148                 if (!filteredStatements.contains(stmt)) {
149                     substatementsInit.add(stmt);
150                 }
151             }
152         } else {
153             substatementsInit.addAll(ctx.effectiveSubstatements());
154         }
155
156         return substatementsInit;
157     }
158 }