YANGTOOLS-706: Split up base utility classes into rfc6020.util
[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 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.rfc6020.util.AbstractEffectiveDocumentedNode;
33 import org.opendaylight.yangtools.yang.parser.rfc6020.util.EffectiveStmtUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
37
38 abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
39         extends AbstractEffectiveDocumentedNode<A, D> implements
40         DataNodeContainer {
41
42     private final Map<QName, DataSchemaNode> childNodes;
43     private final Set<GroupingDefinition> groupings;
44     private final Set<UsesNode> uses;
45     private final Set<TypeDefinition<?>> typeDefinitions;
46     private final Set<DataSchemaNode> publicChildNodes;
47
48     protected AbstractEffectiveDocumentedDataNodeContainer(
49             final StmtContext<A, D, ?> ctx) {
50         super(ctx);
51
52         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
53         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
54         Set<UsesNode> mutableUses = new HashSet<>();
55         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
56         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
57
58         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
59             if (stmt instanceof DataSchemaNode) {
60                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) stmt;
61                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
62                     /**
63                      * Add case short hand when augmenting choice with short hand
64                      **/
65                     if (this instanceof AugmentationSchemaNode
66                             && !(stmt instanceof ChoiceCaseNode || stmt instanceof ChoiceSchemaNode)
67                             && YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmt.statementDefinition())
68                             && Boolean.TRUE.equals(ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
69                         final CaseShorthandImpl caseShorthand = new CaseShorthandImpl(dataSchemaNode);
70                         mutableChildNodes.put(caseShorthand.getQName(), caseShorthand);
71                         mutablePublicChildNodes.add(caseShorthand);
72                     } else {
73                         mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
74                         mutablePublicChildNodes.add(dataSchemaNode);
75                     }
76                 } else {
77                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
78                 }
79             }
80             if (stmt instanceof UsesNode) {
81                 UsesNode usesNode = (UsesNode) stmt;
82                 if (!mutableUses.contains(usesNode)) {
83                     mutableUses.add(usesNode);
84                 } else {
85                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
86                 }
87             }
88             if (stmt instanceof TypedefEffectiveStatement) {
89                 TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) stmt;
90                 TypeDefinition<?> type = typeDef.getTypeDefinition();
91                 if (!mutableTypeDefinitions.contains(type)) {
92                     mutableTypeDefinitions.add(type);
93                 } else {
94                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
95                 }
96             }
97             if (stmt instanceof GroupingDefinition) {
98                 GroupingDefinition grp = (GroupingDefinition) stmt;
99                 if (!mutableGroupings.contains(grp)) {
100                     mutableGroupings.add(grp);
101                 } else {
102                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
103                 }
104             }
105         }
106
107         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
108         this.groupings = ImmutableSet.copyOf(mutableGroupings);
109         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
110         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
111         this.uses = ImmutableSet.copyOf(mutableUses);
112     }
113
114     @Override
115     public final Set<TypeDefinition<?>> getTypeDefinitions() {
116         return typeDefinitions;
117     }
118
119     @Override
120     public final Set<DataSchemaNode> getChildNodes() {
121         return publicChildNodes;
122     }
123
124     @Override
125     public final Set<GroupingDefinition> getGroupings() {
126         return groupings;
127     }
128
129     @Override
130     public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
131         // Child nodes are keyed by their container name, so we can do a direct lookup
132         return Optional.ofNullable(childNodes.get(requireNonNull(name)));
133     }
134
135     @Override
136     public Set<UsesNode> getUses() {
137         return uses;
138     }
139 }