Move dataTree/schemaTree to AbstractEffectiveModule
[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 java.util.Collection;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
27
28 /**
29  * An {@link AbstractEffectiveDocumentedNode} which can optionally support {@link SchemaTreeAwareEffectiveStatement}.
30  *
31  * @param <A> Argument type ({@link Void} if statement does not have argument.)
32  * @param <D> Class representing declared version of this statement.
33  * @author Robert Varga
34  */
35 @Beta
36 public abstract class AbstractSchemaEffectiveDocumentedNode<A, D extends DeclaredStatement<A>>
37         extends AbstractEffectiveDocumentedNode<A, D> {
38     protected AbstractSchemaEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
39         super(ctx);
40     }
41
42     static @NonNull Map<QName, SchemaTreeEffectiveStatement<?>> createSchemaTreeNamespace(
43             final StatementSourceReference ref, final Collection<? extends EffectiveStatement<?, ?>> substatements) {
44         final Map<QName, SchemaTreeEffectiveStatement<?>> schemaChildren = new LinkedHashMap<>();
45         substatements.stream().filter(SchemaTreeEffectiveStatement.class::isInstance)
46             .forEach(child -> putChild(schemaChildren, (SchemaTreeEffectiveStatement) child, ref, "schema"));
47         return schemaChildren;
48     }
49
50     static @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> createDataTreeNamespace(
51             final StatementSourceReference ref,
52             final Collection<SchemaTreeEffectiveStatement<?>> schemaTreeStatements,
53             // Note: this dance is needed to not retain ImmutableMap$Values
54             final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace) {
55         final Map<QName, DataTreeEffectiveStatement<?>> dataChildren = new LinkedHashMap<>();
56         boolean sameAsSchema = true;
57
58         for (SchemaTreeEffectiveStatement<?> child : schemaTreeStatements) {
59             if (child instanceof DataTreeEffectiveStatement) {
60                 putChild(dataChildren, (DataTreeEffectiveStatement<?>) child, ref, "data");
61             } else {
62                 sameAsSchema = false;
63                 putChoiceDataChildren(dataChildren, ref, child);
64             }
65         }
66
67         // This is a mighty hack to lower memory usage: if we consumed all schema tree children as data nodes,
68         // the two maps are equal and hence we can share the instance.
69         return sameAsSchema ? (ImmutableMap) schemaTreeNamespace : ImmutableMap.copyOf(dataChildren);
70     }
71
72     private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map,
73             final T child, final StatementSourceReference ref, final String tree) {
74         final QName id = child.getIdentifier();
75         final T prev = map.putIfAbsent(id, child);
76         SourceException.throwIf(prev != null, ref,
77                 "Cannot add %s tree child with name %s, a conflicting child already exists", tree, id);
78     }
79
80     private static void putChoiceDataChildren(final Map<QName, DataTreeEffectiveStatement<?>> map,
81             final StatementSourceReference ref, final SchemaTreeEffectiveStatement<?> child) {
82         // For choice statements go through all their cases and fetch their data children
83         if (child instanceof ChoiceEffectiveStatement) {
84             child.streamEffectiveSubstatements(CaseEffectiveStatement.class).forEach(
85                 caseStmt -> caseStmt.streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(stmt -> {
86                     if (stmt instanceof DataTreeEffectiveStatement) {
87                         putChild(map, (DataTreeEffectiveStatement<?>) stmt, ref, "data");
88                     } else {
89                         putChoiceDataChildren(map, ref, stmt);
90                     }
91                 }));
92         }
93     }
94 }