Do not clutter logs with a thrown exception
[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 Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace;
39     private final Map<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 ? (Map) schemaTreeNamespace : ImmutableMap.copyOf(dataChildren);
68             } else {
69                 dataTreeNamespace = ImmutableMap.of();
70             }
71         } else {
72             dataTreeNamespace = ImmutableMap.of();
73             schemaTreeNamespace = ImmutableMap.of();
74         }
75     }
76
77     @Override
78     @SuppressWarnings("unchecked")
79     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
80             final Class<N> namespace) {
81         if (this instanceof SchemaTreeAwareEffectiveStatement
82                 && SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
83             return Optional.of((Map<K, V>) schemaTreeNamespace);
84         }
85         if (this instanceof DataTreeAwareEffectiveStatement
86                 && DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
87             return Optional.of((Map<K, V>) dataTreeNamespace);
88         }
89         return super.getNamespaceContents(namespace);
90     }
91
92     private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map,
93             final T child, final StatementSourceReference ref, final String tree) {
94         final QName id = child.getIdentifier();
95         final T prev = map.putIfAbsent(id, child);
96         SourceException.throwIf(prev != null, ref,
97                 "Cannot add %s tree child with name %s, a conflicting child already exists", tree, id);
98     }
99
100     private static void putChoiceDataChildren(final Map<QName, DataTreeEffectiveStatement<?>> map,
101             final StatementSourceReference ref, final SchemaTreeEffectiveStatement<?> child) {
102         // For choice statements go through all their cases and fetch their data children
103         if (child instanceof ChoiceEffectiveStatement) {
104             child.streamEffectiveSubstatements(CaseEffectiveStatement.class).forEach(
105                 caseStmt -> caseStmt.streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(stmt -> {
106                     if (stmt instanceof DataTreeEffectiveStatement) {
107                         putChild(map, (DataTreeEffectiveStatement<?>) stmt, ref, "data");
108                     } else {
109                         putChoiceDataChildren(map, ref, stmt);
110                     }
111                 }));
112         }
113     }
114 }