2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.LinkedHashMap;
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;
25 * An {@link AbstractEffectiveDocumentedNode} which can optionally support {@link SchemaTreeAwareEffectiveStatement}.
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
32 public abstract class AbstractSchemaEffectiveDocumentedNode<A, D extends DeclaredStatement<A>>
33 extends AbstractEffectiveDocumentedNode<A, D> {
35 private final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace;
37 protected AbstractSchemaEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
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");
46 schemaTreeNamespace = ImmutableMap.copyOf(schemaChildren);
48 schemaTreeNamespace = ImmutableMap.of();
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);
60 return super.getNamespaceContents(namespace);
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);