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