Rework EffectiveStatementBase statement order restoration
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / EffectiveStatementBase.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.Set;
21 import java.util.function.Predicate;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29
30 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
31     private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
32
33     /**
34      * Constructor.
35      *
36      * @param ctx context of statement.
37      */
38     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
39         final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
40
41         /*
42          * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
43          * the statements were defined -- i.e. if we have something like this:
44          *
45          * container foo {
46          *   uses bar;
47          *   uses baz;
48          * }
49          *
50          * grouping baz {
51          *   leaf baz {
52          *     type string;
53          *   }
54          * }
55          *
56          * grouping bar {
57          *   leaf bar {
58          *     type string;
59          *   }
60          * }
61          *
62          * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
63          * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
64          *
65          * TODO: this really should be handled by UsesStatementSupport such that 'uses baz' would have a prerequisite
66          *       of a resolved 'uses bar'.
67          */
68         Set<StmtContext<?, ?, ?>> filteredStatements = null;
69         for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
70             if (declaredSubstatement.isSupportedByFeatures()) {
71                 substatementsInit.add(declaredSubstatement);
72
73                 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
74                 if (!effect.isEmpty()) {
75                     if (filteredStatements == null) {
76                         filteredStatements = new HashSet<>();
77                     }
78                     filteredStatements.addAll(effect);
79                     substatementsInit.addAll(effect);
80                 }
81             }
82         }
83
84         if (filteredStatements != null) {
85             for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
86                 if (!filteredStatements.contains(stmt)) {
87                     substatementsInit.add(stmt);
88                 }
89             }
90         } else {
91             substatementsInit.addAll(ctx.effectiveSubstatements());
92         }
93
94         this.substatements = ImmutableList.copyOf(initSubstatements(ctx, substatementsInit));
95     }
96
97     @Beta
98     protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(final StmtContext<A, D, ?> ctx,
99             final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
100         return initSubstatements(substatementsInit);
101     }
102
103     /**
104      * Create a set of substatements. This method is split out so it can be overridden in
105      * ExtensionEffectiveStatementImpl to leak a not-fully-initialized instance.
106      *
107      * @param substatementsInit proposed substatements
108      * @return Filtered substatements
109      */
110     protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(
111             final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
112         return Collections2.transform(Collections2.filter(substatementsInit,
113             StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
114     }
115
116     @Override
117     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
118         return findAll(namespace).get(requireNonNull(identifier));
119     }
120
121     @Override
122     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
123         return getNamespaceContents(requireNonNull(namespace)).orElse(null);
124     }
125
126     /**
127      * Return the statement-specific contents of specified namespace, if available.
128      *
129      * @param namespace Requested namespace
130      * @return Namespace contents, if available.
131      */
132     @Beta
133     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
134             final @NonNull Class<N> namespace) {
135         return Optional.empty();
136     }
137
138     @Override
139     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
140         return substatements;
141     }
142
143     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
144         return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
145     }
146
147     @SuppressWarnings("unchecked")
148     public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
149         return Collection.class.cast(Collections2.filter(substatements, type::isInstance));
150     }
151
152     protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
153         return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
154     }
155
156     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
157         return substatements.stream()
158                 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
159                 .findFirst().map(returnType::cast).orElse(null);
160     }
161
162     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
163         return substatements.stream().filter(type::isInstance).findFirst().orElse(null);
164     }
165 }