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 org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.spi.node.MandatoryLeafEnforcer;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
21 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
22 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
23 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 * General container modification strategy. This is used by {@link EnforcingMandatory} in case of presence containers
28 * with mandatory nodes, as it needs to tap into {@link SchemaAwareApplyOperation}'s operations, or subclassed
29 * for the purposes of {@link StructuralContainerModificationStrategy}'s automatic lifecycle.
31 sealed class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerLike> {
32 static final class EnforcingMandatory extends ContainerModificationStrategy {
33 private final MandatoryLeafEnforcer enforcer;
35 EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
36 final MandatoryLeafEnforcer enforcer) {
37 super(schemaNode, treeConfig);
38 this.enforcer = requireNonNull(enforcer);
42 void mandatoryVerifyValueChildren(final DistinctNodeContainer<?, ?> writtenValue) {
43 enforcer.enforceOnData(writtenValue);
47 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
48 final Version version) {
49 return enforce(super.applyMerge(modification, currentMeta, version));
53 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
54 final TreeNode currentMeta, final Version version) {
55 return enforce(super.applyWrite(modification, newValue, currentMeta, version));
59 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
60 final Version version) {
61 return enforce(super.applyTouch(modification, currentMeta, version));
64 private @NonNull TreeNode enforce(final TreeNode treeNode) {
65 enforcer.enforceOnData(treeNode.getData());
71 * Structural containers are special in that they appear when implied by child nodes and disappear whenever they are
72 * empty. We could implement this as a subclass of {@link SchemaAwareApplyOperation}, but the automatic semantic
73 * is quite different from all the other strategies. We create a {@link ContainerModificationStrategy} to tap into
74 * that logic, but wrap it so we only call out into it. We do not use {@link PresenceContainerModificationStrategy}
75 * because it enforces presence of mandatory leaves, which is not something we want here, as structural containers
76 * are not root anchors for that validation.
78 static final class Structural extends ContainerModificationStrategy {
79 private final ContainerNode emptyNode;
81 Structural(final ContainerLike schema, final DataTreeConfiguration treeConfig) {
82 super(schema, treeConfig);
83 emptyNode = ImmutableNodes.containerNode(schema.getQName());
87 TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
88 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
93 TreeNode defaultTreeNode() {
94 return defaultTreeNode(emptyNode);
98 private static final NormalizedNodeContainerSupport<NodeIdentifier, ContainerNode> SUPPORT =
99 new NormalizedNodeContainerSupport<>(ContainerNode.class, ImmutableContainerNodeBuilder::create,
100 ImmutableContainerNodeBuilder::new);
102 ContainerModificationStrategy(final ContainerLike schemaNode, final DataTreeConfiguration treeConfig) {
103 super(SUPPORT, schemaNode, treeConfig);
106 static ContainerModificationStrategy of(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) {
107 if (schema.isPresenceContainer()) {
108 final var enforcer = enforcerFor(schema, treeConfig);
109 return enforcer != null ? new EnforcingMandatory(schema, treeConfig, enforcer)
110 : new ContainerModificationStrategy(schema, treeConfig);
112 return new Structural(schema, treeConfig);