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.tree.impl;
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;
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.tree.api.DataTreeConfiguration;
23 import org.opendaylight.yangtools.yang.data.tree.api.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 class CaseEnforcer implements Immutable {
30 private static final class EnforcingMandatory extends CaseEnforcer {
31 private final MandatoryLeafEnforcer enforcer;
33 EnforcingMandatory(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
34 final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations,
35 final MandatoryLeafEnforcer enforcer) {
36 super(children, augmentations);
37 this.enforcer = requireNonNull(enforcer);
41 void enforceOnTreeNode(final NormalizedNode normalizedNode) {
42 enforcer.enforceOnData(normalizedNode);
46 private final ImmutableMap<NodeIdentifier, DataSchemaNode> children;
47 private final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations;
49 CaseEnforcer(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
50 final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations) {
51 this.children = requireNonNull(children);
52 this.augmentations = requireNonNull(augmentations);
55 static CaseEnforcer forTree(final CaseSchemaNode schema, final DataTreeConfiguration treeConfig) {
56 final TreeType type = treeConfig.getTreeType();
57 final Builder<NodeIdentifier, DataSchemaNode> childrenBuilder = ImmutableMap.builder();
58 final Builder<AugmentationIdentifier, AugmentationSchemaNode> augmentationsBuilder = ImmutableMap.builder();
59 if (SchemaAwareApplyOperation.belongsToTree(type, schema)) {
60 for (final DataSchemaNode child : schema.getChildNodes()) {
61 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
62 childrenBuilder.put(NodeIdentifier.create(child.getQName()), child);
65 for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) {
66 if (augment.getChildNodes().stream()
67 .anyMatch(child -> SchemaAwareApplyOperation.belongsToTree(type, child))) {
68 augmentationsBuilder.put(DataSchemaContextNode.augmentationIdentifierFrom(augment), augment);
73 final ImmutableMap<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
74 if (children.isEmpty()) {
77 final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations = augmentationsBuilder.build();
78 final var enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
79 return enforcer != null ? new EnforcingMandatory(children, augmentations, enforcer)
80 : new CaseEnforcer(children, augmentations);
83 final Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
84 return children.entrySet();
87 final Set<NodeIdentifier> getChildIdentifiers() {
88 return children.keySet();
91 final Set<Entry<AugmentationIdentifier, AugmentationSchemaNode>> getAugmentationEntries() {
92 return augmentations.entrySet();
95 final Set<AugmentationIdentifier> getAugmentationIdentifiers() {
96 return augmentations.keySet();
99 final Set<PathArgument> getAllChildIdentifiers() {
100 return Sets.union(children.keySet(), augmentations.keySet());
103 void enforceOnTreeNode(final NormalizedNode normalizedNode) {