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.ImmutableNodes;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
21 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
22 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 * General container modification strategy. This is used by {@link EnforcingMandatory} in case of presence containers
27 * with mandatory nodes, as it needs to tap into {@link SchemaAwareApplyOperation}'s operations, or subclassed
28 * for the purposes of {@link StructuralContainerModificationStrategy}'s automatic lifecycle.
30 sealed class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerLike> {
31 static final class EnforcingMandatory extends ContainerModificationStrategy {
32 private final MandatoryLeafEnforcer enforcer;
34 EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
35 final MandatoryLeafEnforcer enforcer) {
36 super(schemaNode, treeConfig);
37 this.enforcer = requireNonNull(enforcer);
41 void mandatoryVerifyValueChildren(final DistinctNodeContainer<?, ?> writtenValue) {
42 enforcer.enforceOnData(writtenValue);
46 protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
47 final Version version) {
48 final var ret = super.applyMerge(modification, currentMeta, version);
49 enforcer.enforceOnTreeNode(ret);
54 protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
55 final TreeNode currentMeta, final Version version) {
56 final var ret = super.applyWrite(modification, newValue, currentMeta, version);
57 enforcer.enforceOnTreeNode(ret);
62 protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
63 final Version version) {
64 final var ret = super.applyTouch(modification, currentMeta, version);
65 enforcer.enforceOnTreeNode(ret);
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 Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
88 final Version version) {
89 return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
94 TreeNode defaultTreeNode() {
95 return defaultTreeNode(emptyNode);
99 private static final NormalizedNodeContainerSupport<NodeIdentifier, ContainerNode> SUPPORT =
100 new NormalizedNodeContainerSupport<>(ContainerNode.class, ImmutableContainerNodeBuilder::create,
101 ImmutableContainerNodeBuilder::create);
103 ContainerModificationStrategy(final ContainerLike schemaNode, final DataTreeConfiguration treeConfig) {
104 super(SUPPORT, schemaNode, treeConfig);
107 static ContainerModificationStrategy of(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) {
108 if (schema.isPresenceContainer()) {
109 final var enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
110 return enforcer != null ? new EnforcingMandatory(schema, treeConfig, enforcer)
111 : new ContainerModificationStrategy(schema, treeConfig);
113 return new Structural(schema, treeConfig);