Merge "Bug 2363, Bug 2205. Beta version of LeafRefContext tree computation"
[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.ImmutableSet;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20
21 import java.util.Map;
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 HashMap<QName, DataSchemaNode>();
47         Set<GroupingDefinition> mutableGroupings = new HashSet<GroupingDefinition>();
48         Set<UsesNode> mutableUses = new HashSet<UsesNode>();
49         Set<TypeDefinition<?>> mutableTypeDefinitions = new HashSet<TypeDefinition<?>>();
50         Set<DataSchemaNode> mutablePublicChildNodes = new HashSet<DataSchemaNode>();
51
52         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
53             if (effectiveStatement instanceof DataSchemaNode) {
54                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
55
56                 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
57                 mutablePublicChildNodes.add(dataSchemaNode);
58             }
59             if (effectiveStatement instanceof UsesNode) {
60                 UsesNode usesNode = (UsesNode) effectiveStatement;
61                 mutableUses.add(usesNode);
62             }
63             if (effectiveStatement instanceof TypeDefinition) {
64                 TypeDefinition<?> typeDef = (TypeDefinition<?>) effectiveStatement;
65                 mutableTypeDefinitions.add(typeDef);
66             }
67             if (effectiveStatement instanceof GroupingDefinition) {
68                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
69                 mutableGroupings.add(grp);
70             }
71         }
72
73         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
74         this.groupings = ImmutableSet.copyOf(mutableGroupings);
75         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
76         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
77         this.uses = ImmutableSet.copyOf(mutableUses);
78     }
79
80     @Override
81     public final Set<TypeDefinition<?>> getTypeDefinitions() {
82         return typeDefinitions;
83     }
84
85     @Override
86     public final Set<DataSchemaNode> getChildNodes() {
87         return publicChildNodes;
88     }
89
90     @Override
91     public final Set<GroupingDefinition> getGroupings() {
92         return groupings;
93     }
94
95     @Override
96     public final DataSchemaNode getDataChildByName(final QName name) {
97         // Child nodes are keyed by their container name, so we can do a direct
98         // lookup
99         return childNodes.get(name);
100     }
101
102     @Override
103     public final DataSchemaNode getDataChildByName(final String name) {
104         for (DataSchemaNode node : childNodes.values()) {
105             if (node.getQName().getLocalName().equals(name)) {
106                 return node;
107             }
108         }
109         return null;
110     }
111
112     @Override
113     public Set<UsesNode> getUses() {
114         return uses;
115     }
116
117 }