/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; 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; abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>> extends AbstractEffectiveDocumentedNode<A, D> implements DataNodeContainer { 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(); 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<?, ?> effectiveStatement : effectiveSubstatements) { if (effectiveStatement instanceof DataSchemaNode) { final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement; if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) { /** * Add case short hand when augmenting choice with short hand **/ if (this instanceof AugmentationSchema && !(effectiveStatement instanceof ChoiceCaseNode || effectiveStatement instanceof ChoiceSchemaNode) && (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.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, effectiveStatement); } } if (effectiveStatement instanceof UsesNode) { UsesNode usesNode = (UsesNode) effectiveStatement; if (!mutableUses.contains(usesNode)) { mutableUses.add(usesNode); } else { throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); } } if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) { TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement; TypeDefinition<?> type = typeDef.getTypeDefinition(); if (!mutableTypeDefinitions.contains(type)) { mutableTypeDefinitions.add(type); } else { throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); } } if (effectiveStatement instanceof GroupingDefinition) { GroupingDefinition grp = (GroupingDefinition) effectiveStatement; if (!mutableGroupings.contains(grp)) { mutableGroupings.add(grp); } else { throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); } } } 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 public final Set<TypeDefinition<?>> getTypeDefinitions() { return typeDefinitions; } @Override public final Set<DataSchemaNode> getChildNodes() { return publicChildNodes; } @Override public final Set<GroupingDefinition> getGroupings() { return groupings; } @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 Set<UsesNode> getUses() { return uses; } }