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