2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.spi.meta;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.Collection;
15 import java.util.LinkedHashMap;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.Empty;
20 import org.opendaylight.yangtools.yang.common.QName;
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.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
30 * Baseline stateless implementation of an EffectiveStatement. This class adds a few default implementations and
31 * namespace dispatch, but does not actually force any state on its subclasses. This approach adds requirements for an
32 * implementation, but it leaves it up to the final class to provide object layout.
35 * This finds immense value in catering the common case, for example effective statements which can, but typically
36 * do not, contain substatements.
38 * @param <A> Argument type ({@link Empty} if statement does not have argument.)
39 * @param <D> Class representing declared version of this statement.
41 abstract class AbstractEffectiveStatement<A, D extends DeclaredStatement<A>>
42 extends AbstractModelStatement<A> implements EffectiveStatement<A, D> {
44 public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
46 return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
50 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
51 final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
52 return ret.isPresent() ? ret.get() : ImmutableMap.of();
56 public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
57 return ImmutableList.of();
61 * Return the statement-specific contents of specified namespace, if available.
63 * @param namespace Requested namespace
64 * @return Namespace contents, if available.
66 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
67 final @NonNull Class<N> namespace) {
68 return Optional.empty();
72 * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
74 * @param masked list to unmask
75 * @return Unmasked list
76 * @throws NullPointerException if masked is null
77 * @throws ClassCastException if masked object does not match EffectiveStatement
79 @SuppressWarnings({ "rawtypes", "unchecked" })
80 protected static final @NonNull ImmutableList<? extends @NonNull EffectiveStatement<?, ?>> unmaskList(
81 final @NonNull Object masked) {
82 return (ImmutableList) unmaskList(masked, EffectiveStatement.class);
85 // TODO: below methods need to find a better place, this is just a temporary hideout as their public class is on
87 protected static @NonNull Map<QName, SchemaTreeEffectiveStatement<?>> createSchemaTreeNamespace(
88 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
89 final Map<QName, SchemaTreeEffectiveStatement<?>> schemaChildren = new LinkedHashMap<>();
90 substatements.stream().filter(SchemaTreeEffectiveStatement.class::isInstance)
91 .forEach(child -> putChild(schemaChildren, (SchemaTreeEffectiveStatement<?>) child, "schema"));
92 return schemaChildren;
95 protected static @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> createDataTreeNamespace(
96 final Collection<SchemaTreeEffectiveStatement<?>> schemaTreeStatements,
97 // Note: this dance is needed to not retain ImmutableMap$Values
98 final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace) {
99 final Map<QName, DataTreeEffectiveStatement<?>> dataChildren = new LinkedHashMap<>();
100 boolean sameAsSchema = true;
102 for (SchemaTreeEffectiveStatement<?> child : schemaTreeStatements) {
103 if (!indexDataTree(dataChildren, child)) {
104 sameAsSchema = false;
108 // This is a mighty hack to lower memory usage: if we consumed all schema tree children as data nodes,
109 // the two maps are equal and hence we can share the instance.
110 return sameAsSchema ? (ImmutableMap) schemaTreeNamespace : ImmutableMap.copyOf(dataChildren);
113 private static boolean indexDataTree(final Map<QName, DataTreeEffectiveStatement<?>> map,
114 final EffectiveStatement<?, ?> stmt) {
115 if (stmt instanceof DataTreeEffectiveStatement) {
116 putChild(map, (DataTreeEffectiveStatement<?>) stmt, "data");
118 } else if (stmt instanceof ChoiceEffectiveStatement) {
119 // For choice statements go through all their cases and fetch their data children
120 for (EffectiveStatement<?, ?> choiceChild : stmt.effectiveSubstatements()) {
121 if (choiceChild instanceof CaseEffectiveStatement) {
122 for (EffectiveStatement<?, ?> caseChild : choiceChild.effectiveSubstatements()) {
123 indexDataTree(map, caseChild);
127 } else if (stmt instanceof CaseEffectiveStatement) {
128 // For case statements go through all their statements
129 for (EffectiveStatement<?, ?> child : stmt.effectiveSubstatements()) {
130 indexDataTree(map, child);
136 private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map, final T child,
138 final QName id = child.getIdentifier();
139 final T prev = map.putIfAbsent(id, child);
141 throw new SubstatementIndexingException(
142 "Cannot add " + tree + " tree child with name " + id + ", a conflicting child already exists");