Merge branch 'master' of ../controller
[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.LinkedHashMap;
13 import java.util.Map;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
18 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeAwareEffectiveStatement;
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     private final ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace;
39     private final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace;
40
41     protected AbstractSchemaEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
42         super(ctx);
43
44         if (this instanceof SchemaTreeAwareEffectiveStatement) {
45             final StatementSourceReference ref = ctx.getStatementSourceReference();
46             final Map<QName, SchemaTreeEffectiveStatement<?>> schemaChildren = new LinkedHashMap<>();
47             streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(child -> {
48                 putChild(schemaChildren, child, ref, "schema");
49             });
50             schemaTreeNamespace = ImmutableMap.copyOf(schemaChildren);
51
52             if (this instanceof DataTreeAwareEffectiveStatement && !schemaTreeNamespace.isEmpty()) {
53                 final Map<QName, DataTreeEffectiveStatement<?>> dataChildren = new LinkedHashMap<>();
54                 boolean sameAsSchema = true;
55
56                 for (SchemaTreeEffectiveStatement<?> child : schemaTreeNamespace.values()) {
57                     if (child instanceof DataTreeEffectiveStatement) {
58                         putChild(dataChildren, (DataTreeEffectiveStatement<?>) child, ref, "data");
59                     } else {
60                         sameAsSchema = false;
61                         putChoiceDataChildren(dataChildren, ref, child);
62                     }
63                 }
64
65                 // This is a mighty hack to lower memory usage: if we consumed all schema tree children as data nodes,
66                 // the two maps are equal and hence we can share the instance.
67                 dataTreeNamespace = sameAsSchema ? (ImmutableMap) schemaTreeNamespace
68                         : ImmutableMap.copyOf(dataChildren);
69             } else {
70                 dataTreeNamespace = ImmutableMap.of();
71             }
72         } else {
73             dataTreeNamespace = ImmutableMap.of();
74             schemaTreeNamespace = ImmutableMap.of();
75         }
76     }
77
78     @Override
79     @SuppressWarnings("unchecked")
80     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
81             final Class<N> namespace) {
82         if (this instanceof SchemaTreeAwareEffectiveStatement
83                 && SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
84             return Optional.of((Map<K, V>) schemaTreeNamespace);
85         }
86         if (this instanceof DataTreeAwareEffectiveStatement
87                 && DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
88             return Optional.of((Map<K, V>) dataTreeNamespace);
89         }
90         return super.getNamespaceContents(namespace);
91     }
92
93     private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map,
94             final T child, final StatementSourceReference ref, final String tree) {
95         final QName id = child.getIdentifier();
96         final T prev = map.putIfAbsent(id, child);
97         SourceException.throwIf(prev != null, ref,
98                 "Cannot add %s tree child with name %s, a conflicting child already exists", tree, id);
99     }
100
101     private static void putChoiceDataChildren(final Map<QName, DataTreeEffectiveStatement<?>> map,
102             final StatementSourceReference ref, final SchemaTreeEffectiveStatement<?> child) {
103         // For choice statements go through all their cases and fetch their data children
104         if (child instanceof ChoiceEffectiveStatement) {
105             child.streamEffectiveSubstatements(CaseEffectiveStatement.class).forEach(
106                 caseStmt -> caseStmt.streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(stmt -> {
107                     if (stmt instanceof DataTreeEffectiveStatement) {
108                         putChild(map, (DataTreeEffectiveStatement<?>) stmt, ref, "data");
109                     } else {
110                         putChoiceDataChildren(map, ref, stmt);
111                     }
112                 }));
113         }
114     }
115 }