c7650293fca189f78560376cdb8dd6f669a237fd
[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 static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
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.AbstractStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26
27 /**
28  * Implementation-internal base class for {@link AbstractStatementSupport} implementations.
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 BaseStatementSupport<A, D extends DeclaredStatement<A>,
36         E extends EffectiveStatement<A, D>> extends AbstractStatementSupport<A, D, E> {
37     protected BaseStatementSupport(final StatementDefinition publicDefinition) {
38         super(publicDefinition);
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::buildDeclared)
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 StmtContext<A, D, E> ctx) {
56         final D declared = buildDeclared(ctx);
57         final ImmutableList<? extends EffectiveStatement<?, ?>> substatements = buildEffectiveSubstatements(ctx);
58         return substatements.isEmpty() ? createEmptyEffective(ctx, declared)
59                 : createEffective(ctx, declared, substatements);
60     }
61
62     protected abstract @NonNull E createEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared,
63             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
64
65     protected abstract @NonNull E createEmptyEffective(@NonNull StmtContext<A, D, E> ctx, @NonNull D declared);
66
67     protected static final <E extends EffectiveStatement<?, ?>> @Nullable E findFirstStatement(
68             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type) {
69         for (EffectiveStatement<?, ?> stmt : statements) {
70             if (type.isInstance(stmt)) {
71                 return type.cast(stmt);
72             }
73         }
74         return null;
75     }
76
77     protected static final <A, E extends EffectiveStatement<A, ?>> A findFirstArgument(
78             final ImmutableList<? extends EffectiveStatement<?, ?>> statements, final Class<E> type, final A defValue) {
79         final @Nullable E stmt = findFirstStatement(statements, type);
80         return stmt != null ? stmt.argument() : defValue;
81     }
82
83     static final <A, D extends DeclaredStatement<A>> @NonNull D buildDeclared(final StmtContext<A, D, ?> ctx) {
84         /*
85          * Share original instance of declared statement between all effective
86          * statements which have been copied or derived from this original
87          * declared statement.
88          */
89         @SuppressWarnings("unchecked")
90         final StmtContext<?, D, ?> lookupCtx = (StmtContext<?, D, ?>) ctx.getOriginalCtx().orElse(ctx);
91         return verifyNotNull(lookupCtx.buildDeclared(), "Statement %s failed to build declared statement", lookupCtx);
92     }
93
94     /**
95      * Create a set of substatements. This method is split out so it can be overridden in
96      * ExtensionEffectiveStatementImpl to leak a not-fully-initialized instance.
97      *
98      * @param substatements proposed substatements
99      * @return Filtered substatements
100      */
101     private static ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
102             final List<? extends StmtContext<?, ?, ?>> substatements) {
103         return substatements.stream()
104                 .filter(StmtContext::isSupportedToBuildEffective)
105                 .map(StmtContext::buildEffective)
106                 .collect(ImmutableList.toImmutableList());
107     }
108
109     static final ImmutableList<? extends EffectiveStatement<?, ?>> buildEffectiveSubstatements(
110             final StmtContext<?, ?, ?> ctx) {
111         return buildEffectiveSubstatements(declaredSubstatements(ctx));
112     }
113
114     static final @NonNull List<StmtContext<?, ?, ?>> declaredSubstatements(final StmtContext<?, ?, ?> ctx) {
115         /*
116          * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
117          * the statements were defined -- i.e. if we have something like this:
118          *
119          * container foo {
120          *   uses bar;
121          *   uses baz;
122          * }
123          *
124          * grouping baz {
125          *   leaf baz {
126          *     type string;
127          *   }
128          * }
129          *
130          * grouping bar {
131          *   leaf bar {
132          *     type string;
133          *   }
134          * }
135          *
136          * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
137          * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
138          *
139          * FIXME: 5.0.0: this really should be handled by UsesStatementSupport such that 'uses baz' would have a
140          *               prerequisite of a resolved 'uses bar'.
141          */
142         final List<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
143         Set<StmtContext<?, ?, ?>> filteredStatements = null;
144         for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
145             if (declaredSubstatement.isSupportedByFeatures()) {
146                 substatementsInit.add(declaredSubstatement);
147
148                 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
149                 if (!effect.isEmpty()) {
150                     if (filteredStatements == null) {
151                         filteredStatements = new HashSet<>();
152                     }
153                     filteredStatements.addAll(effect);
154                     substatementsInit.addAll(effect);
155                 }
156             }
157         }
158
159         if (filteredStatements != null) {
160             for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
161                 if (!filteredStatements.contains(stmt)) {
162                     substatementsInit.add(stmt);
163                 }
164             }
165         } else {
166             substatementsInit.addAll(ctx.effectiveSubstatements());
167         }
168
169         return substatementsInit;
170     }
171 }