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 com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.LinkedHashMap;
15 import java.util.LinkedHashSet;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
33 abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
34 extends AbstractEffectiveDocumentedNode<A, D> implements
37 private final Map<QName, DataSchemaNode> childNodes;
38 private final Set<GroupingDefinition> groupings;
39 private final Set<UsesNode> uses;
40 private final Set<TypeDefinition<?>> typeDefinitions;
41 private final Set<DataSchemaNode> publicChildNodes;
43 protected AbstractEffectiveDocumentedDataNodeContainer(
44 final StmtContext<A, D, ?> ctx) {
47 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
49 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
50 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
51 Set<UsesNode> mutableUses = new HashSet<>();
52 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
53 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
55 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
56 if (effectiveStatement instanceof DataSchemaNode) {
57 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
58 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
60 * Add case short hand when augmenting choice with short hand
62 if (this instanceof AugmentationSchema && !(effectiveStatement instanceof ChoiceCaseNode ||
63 effectiveStatement instanceof ChoiceSchemaNode) &&
64 (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) &&
65 Boolean.TRUE.equals(ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
66 final CaseShorthandImpl caseShorthand = new CaseShorthandImpl(dataSchemaNode);
67 mutableChildNodes.put(caseShorthand.getQName(), caseShorthand);
68 mutablePublicChildNodes.add(caseShorthand);
70 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
71 mutablePublicChildNodes.add(dataSchemaNode);
74 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
77 if (effectiveStatement instanceof UsesNode) {
78 UsesNode usesNode = (UsesNode) effectiveStatement;
79 if (!mutableUses.contains(usesNode)) {
80 mutableUses.add(usesNode);
82 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
85 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
86 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
87 TypeDefinition<?> type = typeDef.getTypeDefinition();
88 if (!mutableTypeDefinitions.contains(type)) {
89 mutableTypeDefinitions.add(type);
91 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
94 if (effectiveStatement instanceof GroupingDefinition) {
95 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
96 if (!mutableGroupings.contains(grp)) {
97 mutableGroupings.add(grp);
99 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
104 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
105 this.groupings = ImmutableSet.copyOf(mutableGroupings);
106 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
107 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
108 this.uses = ImmutableSet.copyOf(mutableUses);
112 public final Set<TypeDefinition<?>> getTypeDefinitions() {
113 return typeDefinitions;
117 public final Set<DataSchemaNode> getChildNodes() {
118 return publicChildNodes;
122 public final Set<GroupingDefinition> getGroupings() {
127 public final DataSchemaNode getDataChildByName(final QName name) {
128 // Child nodes are keyed by their container name, so we can do a direct
130 return childNodes.get(name);
134 public Set<UsesNode> getUses() {