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 java.util.Map.Entry;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
21 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 class CaseEnforcer implements Immutable {
25 private static final class EnforcingMandatory extends CaseEnforcer {
26 private final MandatoryLeafEnforcer enforcer;
28 EnforcingMandatory(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
29 final MandatoryLeafEnforcer enforcer) {
31 this.enforcer = requireNonNull(enforcer);
35 void enforceOnChoice(final ChoiceNode choice) {
36 enforcer.enforceOnData(choice);
40 private final ImmutableMap<NodeIdentifier, DataSchemaNode> children;
42 CaseEnforcer(final ImmutableMap<NodeIdentifier, DataSchemaNode> children) {
43 this.children = requireNonNull(children);
46 static CaseEnforcer forTree(final CaseSchemaNode schema, final DataTreeConfiguration treeConfig) {
47 final TreeType type = treeConfig.getTreeType();
48 final Builder<NodeIdentifier, DataSchemaNode> childrenBuilder = ImmutableMap.builder();
49 if (SchemaAwareApplyOperation.belongsToTree(type, schema)) {
50 for (final DataSchemaNode child : schema.getChildNodes()) {
51 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
52 childrenBuilder.put(NodeIdentifier.create(child.getQName()), child);
57 final ImmutableMap<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
58 if (children.isEmpty()) {
61 final var enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
62 return enforcer != null ? new EnforcingMandatory(children, enforcer) : new CaseEnforcer(children);
65 final Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
66 return children.entrySet();
69 final Set<NodeIdentifier> getChildIdentifiers() {
70 return children.keySet();
73 void enforceOnChoice(final ChoiceNode choice) {