Do not use StatementSourceReference in AbstractDeclaredEffectiveStatement
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveStatement.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 java.util.Objects.requireNonNull;
11
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;
17 import java.util.Map;
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;
28
29 /**
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.
33  *
34  * <p>
35  * This finds immense value in catering the common case, for example effective statements which can, but typically
36  * do not, contain substatements.
37  *
38  * @param <A> Argument type ({@link Void} if statement does not have argument.)
39  * @param <D> Class representing declared version of this statement.
40  */
41 @Beta
42 public abstract class AbstractEffectiveStatement<A, D extends DeclaredStatement<A>>
43         extends AbstractModelStatement<A> implements EffectiveStatement<A, D> {
44     @Override
45     public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
46             final K identifier) {
47         return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
48     }
49
50     @Override
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();
54     }
55
56     @Override
57     public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
58         return ImmutableList.of();
59     }
60
61     /**
62      * Return the statement-specific contents of specified namespace, if available.
63      *
64      * @param namespace Requested namespace
65      * @return Namespace contents, if available.
66      */
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();
70     }
71
72     /**
73      * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
74      *
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
79      */
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);
84     }
85
86     // TODO: below methods need to find a better place, this is just a temporary hideout as their public class is on
87     //       its way out
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;
94     }
95
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;
102
103         for (SchemaTreeEffectiveStatement<?> child : schemaTreeStatements) {
104             if (child instanceof DataTreeEffectiveStatement) {
105                 putChild(dataChildren, (DataTreeEffectiveStatement<?>) child, "data");
106             } else {
107                 sameAsSchema = false;
108                 putChoiceDataChildren(dataChildren, child);
109             }
110         }
111
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);
115     }
116
117     private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map, final T child,
118             final String tree) {
119         final QName id = child.getIdentifier();
120         final T prev = map.putIfAbsent(id, child);
121         if (prev != null) {
122             throw new SubstatementIndexingException(
123                 "Cannot add " + tree + " tree child with name " + id + ", a conflicting child already exists");
124         }
125     }
126
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");
135                     } else {
136                         putChoiceDataChildren(map, stmt);
137                     }
138                 }));
139         }
140     }
141 }