Introduce DataNodeContainer.findDataChildByName()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveDocumentedDataNodeContainer.java
index f0b521e21bc00e3df10866f5608572b60e821fbc..9cbb9c5766a24476882f25f8bc41a72e969a2c65 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import com.google.common.collect.ImmutableSet;
+import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableMap;
-import java.util.Collection;
-import java.util.HashMap;
+import com.google.common.collect.ImmutableSet;
 import java.util.HashSet;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
 
-public abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
+abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
         extends AbstractEffectiveDocumentedNode<A, D> implements
         DataNodeContainer {
 
-    private final ImmutableMap<QName, DataSchemaNode> childNodes;
-    private final ImmutableSet<GroupingDefinition> groupings;
-    private final ImmutableSet<UsesNode> uses;
-    private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
-    private final ImmutableSet<DataSchemaNode> publicChildNodes;
+    private final Map<QName, DataSchemaNode> childNodes;
+    private final Set<GroupingDefinition> groupings;
+    private final Set<UsesNode> uses;
+    private final Set<TypeDefinition<?>> typeDefinitions;
+    private final Set<DataSchemaNode> publicChildNodes;
 
     protected AbstractEffectiveDocumentedDataNodeContainer(
             final StmtContext<A, D, ?> ctx) {
         super(ctx);
 
-        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
-        HashMap<QName, DataSchemaNode> childNodes = new HashMap<QName, DataSchemaNode>();
-        HashSet<GroupingDefinition> groupings = new HashSet<GroupingDefinition>();
-        HashSet<UsesNode> uses = new HashSet<UsesNode>();
-        HashSet<TypeDefinition<?>> typeDefinitions = new HashSet<TypeDefinition<?>>();
-        HashSet<DataSchemaNode> publicChildNodes = new HashSet<DataSchemaNode>();
-
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof DataSchemaNode) {
-                DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
-
-                childNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
-                publicChildNodes.add(dataSchemaNode);
+        Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
+        Set<GroupingDefinition> mutableGroupings = new HashSet<>();
+        Set<UsesNode> mutableUses = new HashSet<>();
+        Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
+        Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
+
+        for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
+            if (stmt instanceof DataSchemaNode) {
+                final DataSchemaNode dataSchemaNode = (DataSchemaNode) stmt;
+                if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
+                    /**
+                     * Add case short hand when augmenting choice with short hand
+                     **/
+                    if (this instanceof AugmentationSchemaNode
+                            && !(stmt instanceof ChoiceCaseNode || stmt instanceof ChoiceSchemaNode)
+                            && YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(stmt.statementDefinition())
+                            && Boolean.TRUE.equals(ctx.getFromNamespace(AugmentToChoiceNamespace.class, ctx))) {
+                        final CaseShorthandImpl caseShorthand = new CaseShorthandImpl(dataSchemaNode);
+                        mutableChildNodes.put(caseShorthand.getQName(), caseShorthand);
+                        mutablePublicChildNodes.add(caseShorthand);
+                    } else {
+                        mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
+                        mutablePublicChildNodes.add(dataSchemaNode);
+                    }
+                } else {
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
+                }
             }
-            if (effectiveStatement instanceof UsesNode) {
-                UsesNode usesNode = (UsesNode) effectiveStatement;
-                uses.add(usesNode);
+            if (stmt instanceof UsesNode) {
+                UsesNode usesNode = (UsesNode) stmt;
+                if (!mutableUses.contains(usesNode)) {
+                    mutableUses.add(usesNode);
+                } else {
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
+                }
             }
-            if (effectiveStatement instanceof TypeDefinition) {
-                TypeDefinition<?> typeDef = (TypeDefinition<?>) effectiveStatement;
-                typeDefinitions.add(typeDef);
+            if (stmt instanceof TypeDefEffectiveStatementImpl) {
+                TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) stmt;
+                TypeDefinition<?> type = typeDef.getTypeDefinition();
+                if (!mutableTypeDefinitions.contains(type)) {
+                    mutableTypeDefinitions.add(type);
+                } else {
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
+                }
             }
-            if (effectiveStatement instanceof GroupingDefinition) {
-                GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
-                groupings.add(grp);
+            if (stmt instanceof GroupingDefinition) {
+                GroupingDefinition grp = (GroupingDefinition) stmt;
+                if (!mutableGroupings.contains(grp)) {
+                    mutableGroupings.add(grp);
+                } else {
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
+                }
             }
         }
 
-        this.childNodes = ImmutableMap.copyOf(childNodes);
-        this.groupings = ImmutableSet.copyOf(groupings);
-        this.publicChildNodes = ImmutableSet.copyOf(publicChildNodes);
-        this.typeDefinitions = ImmutableSet.copyOf(typeDefinitions);
-        this.uses = ImmutableSet.copyOf(uses);
+        this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
+        this.groupings = ImmutableSet.copyOf(mutableGroupings);
+        this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
+        this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
+        this.uses = ImmutableSet.copyOf(mutableUses);
     }
 
     @Override
@@ -90,25 +124,13 @@ public abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends
     }
 
     @Override
-    public final DataSchemaNode getDataChildByName(final QName name) {
-        // Child nodes are keyed by their container name, so we can do a direct
-        // lookup
-        return childNodes.get(name);
-    }
-
-    @Override
-    public final DataSchemaNode getDataChildByName(final String name) {
-        for (DataSchemaNode node : childNodes.values()) {
-            if (node.getQName().getLocalName().equals(name)) {
-                return node;
-            }
-        }
-        return null;
+    public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
+        // Child nodes are keyed by their container name, so we can do a direct lookup
+        return Optional.ofNullable(childNodes.get(requireNonNull(name)));
     }
 
     @Override
     public Set<UsesNode> getUses() {
         return uses;
     }
-
 }