Expose EffectiveModuleStatement prefix mapping
[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.List;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.function.Predicate;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31
32 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
33     private final List<? extends EffectiveStatement<?, ?>> substatements;
34
35     /**
36      * Constructor.
37      *
38      * @param ctx
39      *            context of statement.
40      */
41     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
42         final Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
43         final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
44
45         final Collection<? extends StmtContext<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
46                 ctx.declaredSubstatements(), StmtContext::isSupportedByFeatures);
47         for (final StmtContext<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
48             if (YangStmtMapping.USES == declaredSubstatement.getPublicDefinition()) {
49                 substatementsInit.add(declaredSubstatement);
50                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
51                 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
52                         .getEffectOfStatement());
53             } else {
54                 substatementsInit.add(declaredSubstatement);
55             }
56         }
57         substatementsInit.addAll(effectiveSubstatements);
58
59         this.substatements = ImmutableList.copyOf(initSubstatements(substatementsInit));
60     }
61
62     /**
63      * Create a set of substatements. This method is split out so it can be overridden in
64      * ExtensionEffectiveStatementImpl to leak a not-fully-initialized instance.
65      *
66      * @param substatementsInit proposed substatements
67      * @return Filtered substatements
68      */
69     protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(
70             final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
71         return Collections2.transform(Collections2.filter(substatementsInit,
72             StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
73     }
74
75     @Override
76     public final <K, V, N extends IdentifierNamespace<K, V>> V get(@Nonnull final Class<N> namespace,
77             @Nonnull final K identifier) {
78         return findAll(namespace).get(requireNonNull(identifier));
79     }
80
81     @Override
82     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull final Class<N> namespace) {
83         return getNamespaceContents(requireNonNull(namespace)).orElse(null);
84     }
85
86     /**
87      * Return the statement-specific contents of specified namespace, if available.
88      *
89      * @param namespace Requested namespace
90      * @return Namespace contents, if available.
91      */
92     @Beta
93     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
94             final @NonNull Class<N> namespace) {
95         return Optional.empty();
96     }
97
98     @Nonnull
99     @Override
100     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
101         return substatements;
102     }
103
104     /**
105      * Find first substatement of specified type.
106      *
107      * @param type Requested type
108      * @return First matching substatement, or null if no match is found.
109      *
110      * @deprecated Use {@link #findFirstEffectiveSubstatement(Class)} instead.
111      */
112     @Deprecated
113     public final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
114         return findFirstEffectiveSubstatement(type).orElse(null);
115     }
116
117     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
118         return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
119     }
120
121     @SuppressWarnings("unchecked")
122     public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
123         return Collection.class.cast(Collections2.filter(substatements, type::isInstance));
124     }
125
126     @Nullable protected final <T> T firstSubstatementOfType(final Class<T> type) {
127         return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
128     }
129
130     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
131         return substatements.stream()
132                 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
133                 .findFirst().map(returnType::cast).orElse(null);
134     }
135
136     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
137         return substatements.stream().filter(type::isInstance).findFirst().orElse(null);
138     }
139 }