Do not duplicate findModuleByNamespaceAndRevision()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveDocumentedDataNodeContainer.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020.effective;
9
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;
16 import java.util.Map;
17 import java.util.Set;
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;
32
33 abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
34         extends AbstractEffectiveDocumentedNode<A, D> implements
35         DataNodeContainer {
36
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;
42
43     protected AbstractEffectiveDocumentedDataNodeContainer(
44             final StmtContext<A, D, ?> ctx) {
45         super(ctx);
46
47         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
48
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<>();
54
55         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
56             if (effectiveStatement instanceof DataSchemaNode) {
57                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
58                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
59                     /**
60                      * Add case short hand when augmenting choice with short hand
61                      **/
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);
69                     } else {
70                         mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
71                         mutablePublicChildNodes.add(dataSchemaNode);
72                     }
73                 } else {
74                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
75                 }
76             }
77             if (effectiveStatement instanceof UsesNode) {
78                 UsesNode usesNode = (UsesNode) effectiveStatement;
79                 if (!mutableUses.contains(usesNode)) {
80                     mutableUses.add(usesNode);
81                 } else {
82                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
83                 }
84             }
85             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
86                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
87                 TypeDefinition<?> type = typeDef.getTypeDefinition();
88                 if (!mutableTypeDefinitions.contains(type)) {
89                     mutableTypeDefinitions.add(type);
90                 } else {
91                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
92                 }
93             }
94             if (effectiveStatement instanceof GroupingDefinition) {
95                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
96                 if (!mutableGroupings.contains(grp)) {
97                     mutableGroupings.add(grp);
98                 } else {
99                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
100                 }
101             }
102         }
103
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);
109     }
110
111     @Override
112     public final Set<TypeDefinition<?>> getTypeDefinitions() {
113         return typeDefinitions;
114     }
115
116     @Override
117     public final Set<DataSchemaNode> getChildNodes() {
118         return publicChildNodes;
119     }
120
121     @Override
122     public final Set<GroupingDefinition> getGroupings() {
123         return groupings;
124     }
125
126     @Override
127     public final DataSchemaNode getDataChildByName(final QName name) {
128         // Child nodes are keyed by their container name, so we can do a direct
129         // lookup
130         return childNodes.get(name);
131     }
132
133     @Override
134     public Set<UsesNode> getUses() {
135         return uses;
136     }
137 }