2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import com.google.common.collect.Sets;
15 import java.util.Map.Entry;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
24 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 final class CaseEnforcer implements Immutable {
30 private final Map<NodeIdentifier, DataSchemaNode> children;
31 private final Map<AugmentationIdentifier, AugmentationSchemaNode> augmentations;
32 private final MandatoryLeafEnforcer enforcer;
34 private CaseEnforcer(final Map<NodeIdentifier, DataSchemaNode> children,
35 final Map<AugmentationIdentifier, AugmentationSchemaNode> augmentations,
36 final MandatoryLeafEnforcer enforcer) {
37 this.children = Preconditions.checkNotNull(children);
38 this.augmentations = Preconditions.checkNotNull(augmentations);
39 this.enforcer = Preconditions.checkNotNull(enforcer);
42 static CaseEnforcer forTree(final CaseSchemaNode schema, final DataTreeConfiguration treeConfig) {
43 final TreeType type = treeConfig.getTreeType();
44 final Builder<NodeIdentifier, DataSchemaNode> childrenBuilder = ImmutableMap.builder();
45 final Builder<AugmentationIdentifier, AugmentationSchemaNode> augmentationsBuilder = ImmutableMap.builder();
46 if (SchemaAwareApplyOperation.belongsToTree(type, schema)) {
47 for (final DataSchemaNode child : schema.getChildNodes()) {
48 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
49 childrenBuilder.put(NodeIdentifier.create(child.getQName()), child);
52 for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) {
53 if (augment.getChildNodes().stream()
54 .anyMatch(child -> SchemaAwareApplyOperation.belongsToTree(type, child))) {
55 augmentationsBuilder.put(DataSchemaContextNode.augmentationIdentifierFrom(augment), augment);
60 final Map<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
61 final Map<AugmentationIdentifier, AugmentationSchemaNode> augmentations = augmentationsBuilder.build();
62 return children.isEmpty() ? null
63 : new CaseEnforcer(children, augmentations, MandatoryLeafEnforcer.forContainer(schema, treeConfig));
66 Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
67 return children.entrySet();
70 Set<NodeIdentifier> getChildIdentifiers() {
71 return children.keySet();
74 Set<Entry<AugmentationIdentifier, AugmentationSchemaNode>> getAugmentationEntries() {
75 return augmentations.entrySet();
78 Set<AugmentationIdentifier> getAugmentationIdentifiers() {
79 return augmentations.keySet();
82 Set<PathArgument> getAllChildIdentifiers() {
83 return Sets.union(children.keySet(), augmentations.keySet());
86 void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {
87 enforcer.enforceOnData(normalizedNode);