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