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.parser.rfc7950.stmt;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collection;
16 import java.util.LinkedHashMap;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
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 Void} if statement does not have argument.)
39 * @param <D> Class representing declared version of this statement.
42 public abstract class AbstractEffectiveStatement<A, D extends DeclaredStatement<A>>
43 extends AbstractModelStatement<A> implements EffectiveStatement<A, D> {
45 public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
47 return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
51 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
52 final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
53 return ret.isPresent() ? ret.get() : ImmutableMap.of();
57 public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
58 return ImmutableList.of();
62 * Return the statement-specific contents of specified namespace, if available.
64 * @param namespace Requested namespace
65 * @return Namespace contents, if available.
67 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
68 final @NonNull Class<N> namespace) {
69 return Optional.empty();
73 * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
75 * @param masked list to unmask
76 * @return Unmasked list
77 * @throws NullPointerException if masked is null
78 * @throws ClassCastException if masked object does not match EffectiveStatement
80 @SuppressWarnings({ "rawtypes", "unchecked" })
81 protected static final @NonNull ImmutableList<? extends @NonNull EffectiveStatement<?, ?>> unmaskList(
82 final @NonNull Object masked) {
83 return (ImmutableList) unmaskList(masked, EffectiveStatement.class);
86 // TODO: below methods need to find a better place, this is just a temporary hideout as their public class is on
88 static @NonNull Map<QName, SchemaTreeEffectiveStatement<?>> createSchemaTreeNamespace(
89 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
90 final Map<QName, SchemaTreeEffectiveStatement<?>> schemaChildren = new LinkedHashMap<>();
91 substatements.stream().filter(SchemaTreeEffectiveStatement.class::isInstance)
92 .forEach(child -> putChild(schemaChildren, (SchemaTreeEffectiveStatement<?>) child, "schema"));
93 return schemaChildren;
96 static @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> createDataTreeNamespace(
97 final Collection<SchemaTreeEffectiveStatement<?>> schemaTreeStatements,
98 // Note: this dance is needed to not retain ImmutableMap$Values
99 final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace) {
100 final Map<QName, DataTreeEffectiveStatement<?>> dataChildren = new LinkedHashMap<>();
101 boolean sameAsSchema = true;
103 for (SchemaTreeEffectiveStatement<?> child : schemaTreeStatements) {
104 if (child instanceof DataTreeEffectiveStatement) {
105 putChild(dataChildren, (DataTreeEffectiveStatement<?>) child, "data");
107 sameAsSchema = false;
108 putChoiceDataChildren(dataChildren, child);
112 // This is a mighty hack to lower memory usage: if we consumed all schema tree children as data nodes,
113 // the two maps are equal and hence we can share the instance.
114 return sameAsSchema ? (ImmutableMap) schemaTreeNamespace : ImmutableMap.copyOf(dataChildren);
117 private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map, final T child,
119 final QName id = child.getIdentifier();
120 final T prev = map.putIfAbsent(id, child);
122 throw new SubstatementIndexingException(
123 "Cannot add " + tree + " tree child with name " + id + ", a conflicting child already exists");
127 private static void putChoiceDataChildren(final Map<QName, DataTreeEffectiveStatement<?>> map,
128 final SchemaTreeEffectiveStatement<?> child) {
129 // For choice statements go through all their cases and fetch their data children
130 if (child instanceof ChoiceEffectiveStatement) {
131 child.streamEffectiveSubstatements(CaseEffectiveStatement.class).forEach(
132 caseStmt -> caseStmt.streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(stmt -> {
133 if (stmt instanceof DataTreeEffectiveStatement) {
134 putChild(map, (DataTreeEffectiveStatement<?>) stmt, "data");
136 putChoiceDataChildren(map, stmt);