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 java.util.Optional;
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.builder.impl.ImmutableContainerNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
19 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
20 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
21 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 * General container modification strategy. This is used by {@link EnforcingMandatory} in case of presence containers
26 * with mandatory nodes, as it needs to tap into {@link SchemaAwareApplyOperation}'s operations, or subclassed
27 * for the purposes of {@link StructuralContainerModificationStrategy}'s automatic lifecycle.
29 class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerLike> {
30 private static final class EnforcingMandatory extends ContainerModificationStrategy {
31 private final MandatoryLeafEnforcer enforcer;
33 EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
34 final MandatoryLeafEnforcer enforcer) {
35 super(schemaNode, treeConfig);
36 this.enforcer = requireNonNull(enforcer);
40 void mandatoryVerifyValueChildren(final DistinctNodeContainer<?, ?> writtenValue) {
41 enforcer.enforceOnData(writtenValue);
45 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
46 final Version version) {
47 final TreeNode ret = super.applyMerge(modification, currentMeta, version);
48 enforcer.enforceOnTreeNode(ret);
53 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
54 final Optional<? extends TreeNode> currentMeta, final Version version) {
55 final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
56 enforcer.enforceOnTreeNode(ret);
61 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
62 final Version version) {
63 final TreeNode ret = super.applyTouch(modification, currentMeta, version);
64 enforcer.enforceOnTreeNode(ret);
69 private static final NormalizedNodeContainerSupport<NodeIdentifier, ContainerNode> SUPPORT =
70 new NormalizedNodeContainerSupport<>(ContainerNode.class, ImmutableContainerNodeBuilder::create,
71 ImmutableContainerNodeBuilder::create);
73 ContainerModificationStrategy(final ContainerLike schemaNode, final DataTreeConfiguration treeConfig) {
74 super(SUPPORT, schemaNode, treeConfig);
77 static ModificationApplyOperation of(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) {
78 if (schema.isPresenceContainer()) {
79 final var enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
80 return enforcer != null ? new EnforcingMandatory(schema, treeConfig, enforcer)
81 : new ContainerModificationStrategy(schema, treeConfig);
84 return new StructuralContainerModificationStrategy(schema, treeConfig);