Revert "Add DataTreeAwareEffectiveStatement"
[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.SchemaTreeAwareEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
23
24 /**
25  * An {@link AbstractEffectiveDocumentedNode} which can optionally support {@link SchemaTreeAwareEffectiveStatement}.
26  *
27  * @param <A> Argument type ({@link Void} if statement does not have argument.)
28  * @param <D> Class representing declared version of this statement.
29  * @author Robert Varga
30  */
31 @Beta
32 public abstract class AbstractSchemaEffectiveDocumentedNode<A, D extends DeclaredStatement<A>>
33         extends AbstractEffectiveDocumentedNode<A, D> {
34
35     private final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace;
36
37     protected AbstractSchemaEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
38         super(ctx);
39
40         if (this instanceof SchemaTreeAwareEffectiveStatement) {
41             final StatementSourceReference ref = ctx.getStatementSourceReference();
42             final Map<QName, SchemaTreeEffectiveStatement<?>> schemaChildren = new LinkedHashMap<>();
43             streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class).forEach(child -> {
44                 putChild(schemaChildren, child, ref, "schema");
45             });
46             schemaTreeNamespace = ImmutableMap.copyOf(schemaChildren);
47         } else {
48             schemaTreeNamespace = ImmutableMap.of();
49         }
50     }
51
52     @Override
53     @SuppressWarnings("unchecked")
54     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
55             final Class<N> namespace) {
56         if (this instanceof SchemaTreeAwareEffectiveStatement
57                 && SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
58             return Optional.of((Map<K, V>) schemaTreeNamespace);
59         }
60         return super.getNamespaceContents(namespace);
61     }
62
63     private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map,
64             final T child, final StatementSourceReference ref, final String tree) {
65         final QName id = child.getIdentifier();
66         final T prev = map.putIfAbsent(id, child);
67         SourceException.throwIf(prev != null, ref,
68                 "Cannot add %s tree child with name %s, a conflicting child already exists", tree, id);
69     }
70 }