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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.Sets;
15 import java.util.Map.Entry;
16 import java.util.Optional;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
25 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 class CaseEnforcer implements Immutable {
31 private static final class EnforcingMandatory extends CaseEnforcer {
32 private final MandatoryLeafEnforcer enforcer;
34 EnforcingMandatory(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
35 final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations,
36 final MandatoryLeafEnforcer enforcer) {
37 super(children, augmentations);
38 this.enforcer = requireNonNull(enforcer);
42 void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {
43 enforcer.enforceOnData(normalizedNode);
47 private final ImmutableMap<NodeIdentifier, DataSchemaNode> children;
48 private final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations;
50 CaseEnforcer(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
51 final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations) {
52 this.children = requireNonNull(children);
53 this.augmentations = requireNonNull(augmentations);
56 static CaseEnforcer forTree(final CaseSchemaNode schema, final DataTreeConfiguration treeConfig) {
57 final TreeType type = treeConfig.getTreeType();
58 final Builder<NodeIdentifier, DataSchemaNode> childrenBuilder = ImmutableMap.builder();
59 final Builder<AugmentationIdentifier, AugmentationSchemaNode> augmentationsBuilder = ImmutableMap.builder();
60 if (SchemaAwareApplyOperation.belongsToTree(type, schema)) {
61 for (final DataSchemaNode child : schema.getChildNodes()) {
62 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
63 childrenBuilder.put(NodeIdentifier.create(child.getQName()), child);
66 for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) {
67 if (augment.getChildNodes().stream()
68 .anyMatch(child -> SchemaAwareApplyOperation.belongsToTree(type, child))) {
69 augmentationsBuilder.put(DataSchemaContextNode.augmentationIdentifierFrom(augment), augment);
74 final ImmutableMap<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
75 if (children.isEmpty()) {
78 final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations = augmentationsBuilder.build();
79 final Optional<MandatoryLeafEnforcer> enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
80 return enforcer.isPresent() ? new EnforcingMandatory(children, augmentations, enforcer.get())
81 : new CaseEnforcer(children, augmentations);
84 final Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
85 return children.entrySet();
88 final Set<NodeIdentifier> getChildIdentifiers() {
89 return children.keySet();
92 final Set<Entry<AugmentationIdentifier, AugmentationSchemaNode>> getAugmentationEntries() {
93 return augmentations.entrySet();
96 final Set<AugmentationIdentifier> getAugmentationIdentifiers() {
97 return augmentations.keySet();
100 final Set<PathArgument> getAllChildIdentifiers() {
101 return Sets.union(children.keySet(), augmentations.keySet());
104 void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {