2 * Copyright (c) 2018 Pantheon Technologies, 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 com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import java.lang.invoke.VarHandle;
14 import java.util.LinkedHashMap;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
21 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeAwareEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
32 * An {@link AbstractEffectiveDocumentedNode} which can optionally support {@link SchemaTreeAwareEffectiveStatement}.
34 * @param <A> Argument type ({@link Void} if statement does not have argument.)
35 * @param <D> Class representing declared version of this statement.
36 * @author Robert Varga
39 public abstract class AbstractSchemaEffectiveDocumentedNode<A, D extends DeclaredStatement<A>>
40 extends AbstractEffectiveDocumentedNode<A, D> {
41 private final ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace;
42 private final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace;
44 protected AbstractSchemaEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
47 if (this instanceof SchemaTreeAwareEffectiveStatement) {
48 final StatementSourceReference ref = ctx.getStatementSourceReference();
49 final Map<QName, SchemaTreeEffectiveStatement<?>> schemaChildren = new LinkedHashMap<>();
50 streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(child -> {
51 putChild(schemaChildren, child, ref, "schema");
53 schemaTreeNamespace = ImmutableMap.copyOf(schemaChildren);
55 if (this instanceof DataTreeAwareEffectiveStatement && !schemaTreeNamespace.isEmpty()) {
56 final Map<QName, DataTreeEffectiveStatement<?>> dataChildren = new LinkedHashMap<>();
57 boolean sameAsSchema = true;
59 for (SchemaTreeEffectiveStatement<?> child : schemaTreeNamespace.values()) {
60 if (child instanceof DataTreeEffectiveStatement) {
61 putChild(dataChildren, (DataTreeEffectiveStatement<?>) child, ref, "data");
64 putChoiceDataChildren(dataChildren, ref, child);
68 // This is a mighty hack to lower memory usage: if we consumed all schema tree children as data nodes,
69 // the two maps are equal and hence we can share the instance.
70 dataTreeNamespace = sameAsSchema ? (ImmutableMap) schemaTreeNamespace
71 : ImmutableMap.copyOf(dataChildren);
73 dataTreeNamespace = ImmutableMap.of();
76 dataTreeNamespace = ImmutableMap.of();
77 schemaTreeNamespace = ImmutableMap.of();
82 @SuppressWarnings("unchecked")
83 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
84 final Class<N> namespace) {
85 if (this instanceof SchemaTreeAwareEffectiveStatement
86 && SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
87 return Optional.of((Map<K, V>) schemaTreeNamespace);
89 if (this instanceof DataTreeAwareEffectiveStatement
90 && DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
91 return Optional.of((Map<K, V>) dataTreeNamespace);
93 return super.getNamespaceContents(namespace);
96 protected final <T> @NonNull ImmutableSet<T> derivedSet(final VarHandle vh, final @NonNull Class<T> clazz) {
97 final ImmutableSet<T> existing = (ImmutableSet<T>) vh.getAcquire(this);
98 return existing != null ? existing : loadSet(vh, clazz);
101 @SuppressWarnings("unchecked")
102 private <T> @NonNull ImmutableSet<T> loadSet(final VarHandle vh, final @NonNull Class<T> clazz) {
103 final ImmutableSet<T> computed = ImmutableSet.copyOf(allSubstatementsOfType(clazz));
104 final Object witness = vh.compareAndExchangeRelease(this, null, computed);
105 return witness == null ? computed : (ImmutableSet<T>) witness;
108 private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map,
109 final T child, final StatementSourceReference ref, final String tree) {
110 final QName id = child.getIdentifier();
111 final T prev = map.putIfAbsent(id, child);
112 SourceException.throwIf(prev != null, ref,
113 "Cannot add %s tree child with name %s, a conflicting child already exists", tree, id);
116 private static void putChoiceDataChildren(final Map<QName, DataTreeEffectiveStatement<?>> map,
117 final StatementSourceReference ref, final SchemaTreeEffectiveStatement<?> child) {
118 // For choice statements go through all their cases and fetch their data children
119 if (child instanceof ChoiceEffectiveStatement) {
120 child.streamEffectiveSubstatements(CaseEffectiveStatement.class).forEach(
121 caseStmt -> caseStmt.streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(stmt -> {
122 if (stmt instanceof DataTreeEffectiveStatement) {
123 putChild(map, (DataTreeEffectiveStatement<?>) stmt, ref, "data");
125 putChoiceDataChildren(map, ref, stmt);