5cccb68fa596272142327a7944bd6ee8df7de03f
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / 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.rfc7950.stmt;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.HashSet;
15 import java.util.LinkedHashMap;
16 import java.util.LinkedHashSet;
17 import java.util.Map;
18 import java.util.Optional;
19 import java.util.Set;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
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 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.choice.ImplicitChoiceCaseNode;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
36
37 public abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
38         extends AbstractEffectiveDocumentedNode<A, D> implements DataNodeContainer {
39
40     private final Map<QName, DataSchemaNode> childNodes;
41     private final Set<GroupingDefinition> groupings;
42     private final Set<UsesNode> uses;
43     private final Set<TypeDefinition<?>> typeDefinitions;
44     private final Set<DataSchemaNode> publicChildNodes;
45
46     protected AbstractEffectiveDocumentedDataNodeContainer(final StmtContext<A, D, ?> ctx) {
47         super(ctx);
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<?, ?> stmt : effectiveSubstatements()) {
56             if (stmt instanceof DataSchemaNode) {
57                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) stmt;
58                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
59                     /**
60                      * Add case short hand when augmenting choice with short hand
61                      **/
62                     if (this instanceof AugmentationSchemaNode
63                             && !(stmt instanceof ChoiceCaseNode || stmt instanceof ChoiceSchemaNode)
64                             && YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmt.statementDefinition())
65                             && Boolean.TRUE.equals(ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
66                         final ImplicitChoiceCaseNode caseShorthand = new ImplicitChoiceCaseNode(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, stmt);
75                 }
76             }
77             if (stmt instanceof UsesNode) {
78                 UsesNode usesNode = (UsesNode) stmt;
79                 if (!mutableUses.contains(usesNode)) {
80                     mutableUses.add(usesNode);
81                 } else {
82                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
83                 }
84             }
85             if (stmt instanceof TypedefEffectiveStatement) {
86                 TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) stmt;
87                 TypeDefinition<?> type = typeDef.getTypeDefinition();
88                 if (!mutableTypeDefinitions.contains(type)) {
89                     mutableTypeDefinitions.add(type);
90                 } else {
91                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
92                 }
93             }
94             if (stmt instanceof GroupingDefinition) {
95                 GroupingDefinition grp = (GroupingDefinition) stmt;
96                 if (!mutableGroupings.contains(grp)) {
97                     mutableGroupings.add(grp);
98                 } else {
99                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
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 Optional<DataSchemaNode> findDataChildByName(final QName name) {
128         // Child nodes are keyed by their container name, so we can do a direct lookup
129         return Optional.ofNullable(childNodes.get(requireNonNull(name)));
130     }
131
132     @Override
133     public Set<UsesNode> getUses() {
134         return uses;
135     }
136 }