5056929b80d9be52ac22ef0ccadca86a593a434a
[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 org.opendaylight.yangtools.yang.model.util.ExtendedType;
11
12 import java.util.LinkedHashSet;
13 import java.util.LinkedHashMap;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.Map;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import java.util.Set;
23 import org.opendaylight.yangtools.yang.common.QName;
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
30 public abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
31         extends AbstractEffectiveDocumentedNode<A, D> implements
32         DataNodeContainer {
33
34     private final ImmutableMap<QName, DataSchemaNode> childNodes;
35     private final ImmutableSet<GroupingDefinition> groupings;
36     private final ImmutableSet<UsesNode> uses;
37     private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
38     private final ImmutableSet<DataSchemaNode> publicChildNodes;
39
40     protected AbstractEffectiveDocumentedDataNodeContainer(
41             final StmtContext<A, D, ?> ctx) {
42         super(ctx);
43
44         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
45
46         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
47         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
48         Set<UsesNode> mutableUses = new HashSet<>();
49         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
50         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
51
52         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
53             if (effectiveStatement instanceof DataSchemaNode) {
54                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
55                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
56                     mutableChildNodes.put(dataSchemaNode.getQName(),
57                             dataSchemaNode);
58                     mutablePublicChildNodes.add(dataSchemaNode);
59                 } else {
60                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
61                 }
62             }
63             if (effectiveStatement instanceof UsesNode) {
64                 UsesNode usesNode = (UsesNode) effectiveStatement;
65                 if (!mutableUses.contains(usesNode)) {
66                     mutableUses.add(usesNode);
67                 } else {
68                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
69                 }
70             }
71             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
72                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
73                 ExtendedType extendedType = typeDef.buildType();
74                 if (!mutableTypeDefinitions.contains(extendedType)) {
75                     mutableTypeDefinitions.add(extendedType);
76                 } else {
77                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
78                 }
79             }
80             if (effectiveStatement instanceof GroupingDefinition) {
81                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
82                 if (!mutableGroupings.contains(grp)) {
83                     mutableGroupings.add(grp);
84                 } else {
85                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
86                 }
87             }
88         }
89
90         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
91         this.groupings = ImmutableSet.copyOf(mutableGroupings);
92         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
93         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
94         this.uses = ImmutableSet.copyOf(mutableUses);
95     }
96
97     @Override
98     public final Set<TypeDefinition<?>> getTypeDefinitions() {
99         return typeDefinitions;
100     }
101
102     @Override
103     public final Set<DataSchemaNode> getChildNodes() {
104         return publicChildNodes;
105     }
106
107     @Override
108     public final Set<GroupingDefinition> getGroupings() {
109         return groupings;
110     }
111
112     @Override
113     public final DataSchemaNode getDataChildByName(final QName name) {
114         // Child nodes are keyed by their container name, so we can do a direct
115         // lookup
116         return childNodes.get(name);
117     }
118
119     @Override
120     public final DataSchemaNode getDataChildByName(final String name) {
121         for (DataSchemaNode node : childNodes.values()) {
122             if (node.getQName().getLocalName().equals(name)) {
123                 return node;
124             }
125         }
126         return null;
127     }
128
129     @Override
130     public Set<UsesNode> getUses() {
131         return uses;
132     }
133
134 }