Compute mustConstraints lazily
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractSchemaEffectiveDocumentedNode.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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 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;
15 import java.util.Map;
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;
30
31 /**
32  * An {@link AbstractEffectiveDocumentedNode} which can optionally support {@link SchemaTreeAwareEffectiveStatement}.
33  *
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
37  */
38 @Beta
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;
43
44     protected AbstractSchemaEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
45         super(ctx);
46
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");
52             });
53             schemaTreeNamespace = ImmutableMap.copyOf(schemaChildren);
54
55             if (this instanceof DataTreeAwareEffectiveStatement && !schemaTreeNamespace.isEmpty()) {
56                 final Map<QName, DataTreeEffectiveStatement<?>> dataChildren = new LinkedHashMap<>();
57                 boolean sameAsSchema = true;
58
59                 for (SchemaTreeEffectiveStatement<?> child : schemaTreeNamespace.values()) {
60                     if (child instanceof DataTreeEffectiveStatement) {
61                         putChild(dataChildren, (DataTreeEffectiveStatement<?>) child, ref, "data");
62                     } else {
63                         sameAsSchema = false;
64                         putChoiceDataChildren(dataChildren, ref, child);
65                     }
66                 }
67
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);
72             } else {
73                 dataTreeNamespace = ImmutableMap.of();
74             }
75         } else {
76             dataTreeNamespace = ImmutableMap.of();
77             schemaTreeNamespace = ImmutableMap.of();
78         }
79     }
80
81     @Override
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);
88         }
89         if (this instanceof DataTreeAwareEffectiveStatement
90                 && DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
91             return Optional.of((Map<K, V>) dataTreeNamespace);
92         }
93         return super.getNamespaceContents(namespace);
94     }
95
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);
99     }
100
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;
106     }
107
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);
114     }
115
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");
124                     } else {
125                         putChoiceDataChildren(map, ref, stmt);
126                     }
127                 }));
128         }
129     }
130 }