2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020.effective;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.HashSet;
15 import java.util.LinkedHashMap;
16 import java.util.LinkedHashSet;
18 import java.util.Optional;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.UsesNode;
29 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
32 import org.opendaylight.yangtools.yang.parser.rfc6020.util.AbstractEffectiveDocumentedNode;
33 import org.opendaylight.yangtools.yang.parser.rfc6020.util.EffectiveStmtUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
38 abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
39 extends AbstractEffectiveDocumentedNode<A, D> implements
42 private final Map<QName, DataSchemaNode> childNodes;
43 private final Set<GroupingDefinition> groupings;
44 private final Set<UsesNode> uses;
45 private final Set<TypeDefinition<?>> typeDefinitions;
46 private final Set<DataSchemaNode> publicChildNodes;
48 protected AbstractEffectiveDocumentedDataNodeContainer(
49 final StmtContext<A, D, ?> ctx) {
52 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
53 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
54 Set<UsesNode> mutableUses = new HashSet<>();
55 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
56 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
58 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
59 if (stmt instanceof DataSchemaNode) {
60 final DataSchemaNode dataSchemaNode = (DataSchemaNode) stmt;
61 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
63 * Add case short hand when augmenting choice with short hand
65 if (this instanceof AugmentationSchemaNode
66 && !(stmt instanceof ChoiceCaseNode || stmt instanceof ChoiceSchemaNode)
67 && YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmt.statementDefinition())
68 && Boolean.TRUE.equals(ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
69 final CaseShorthandImpl caseShorthand = new CaseShorthandImpl(dataSchemaNode);
70 mutableChildNodes.put(caseShorthand.getQName(), caseShorthand);
71 mutablePublicChildNodes.add(caseShorthand);
73 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
74 mutablePublicChildNodes.add(dataSchemaNode);
77 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
80 if (stmt instanceof UsesNode) {
81 UsesNode usesNode = (UsesNode) stmt;
82 if (!mutableUses.contains(usesNode)) {
83 mutableUses.add(usesNode);
85 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
88 if (stmt instanceof TypedefEffectiveStatement) {
89 TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) stmt;
90 TypeDefinition<?> type = typeDef.getTypeDefinition();
91 if (!mutableTypeDefinitions.contains(type)) {
92 mutableTypeDefinitions.add(type);
94 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
97 if (stmt instanceof GroupingDefinition) {
98 GroupingDefinition grp = (GroupingDefinition) stmt;
99 if (!mutableGroupings.contains(grp)) {
100 mutableGroupings.add(grp);
102 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
107 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
108 this.groupings = ImmutableSet.copyOf(mutableGroupings);
109 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
110 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
111 this.uses = ImmutableSet.copyOf(mutableUses);
115 public final Set<TypeDefinition<?>> getTypeDefinitions() {
116 return typeDefinitions;
120 public final Set<DataSchemaNode> getChildNodes() {
121 return publicChildNodes;
125 public final Set<GroupingDefinition> getGroupings() {
130 public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
131 // Child nodes are keyed by their container name, so we can do a direct lookup
132 return Optional.ofNullable(childNodes.get(requireNonNull(name)));
136 public Set<UsesNode> getUses() {