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