Refactor ModificationApplyOperation
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / ContainerModificationStrategy.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.tree.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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;
24
25 /**
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.
29  */
30 sealed class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerLike> {
31     static final class EnforcingMandatory extends ContainerModificationStrategy {
32         private final MandatoryLeafEnforcer enforcer;
33
34         EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
35                 final MandatoryLeafEnforcer enforcer) {
36             super(schemaNode, treeConfig);
37             this.enforcer = requireNonNull(enforcer);
38         }
39
40         @Override
41         void mandatoryVerifyValueChildren(final DistinctNodeContainer<?, ?> writtenValue) {
42             enforcer.enforceOnData(writtenValue);
43         }
44
45         @Override
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);
50             return ret;
51         }
52
53         @Override
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);
58             return ret;
59         }
60
61         @Override
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);
66             return ret;
67         }
68     }
69
70     /**
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.
77      */
78     static final class Structural extends ContainerModificationStrategy {
79         private final ContainerNode emptyNode;
80
81         Structural(final ContainerLike schema, final DataTreeConfiguration treeConfig) {
82             super(schema, treeConfig);
83             emptyNode = ImmutableNodes.containerNode(schema.getQName());
84         }
85
86         @Override
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,
90                 version);
91         }
92
93         @Override
94         TreeNode defaultTreeNode() {
95             return defaultTreeNode(emptyNode);
96         }
97     }
98
99     private static final NormalizedNodeContainerSupport<NodeIdentifier, ContainerNode> SUPPORT =
100             new NormalizedNodeContainerSupport<>(ContainerNode.class, ImmutableContainerNodeBuilder::create,
101                     ImmutableContainerNodeBuilder::create);
102
103     ContainerModificationStrategy(final ContainerLike schemaNode, final DataTreeConfiguration treeConfig) {
104         super(SUPPORT, schemaNode, treeConfig);
105     }
106
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);
112         }
113         return new Structural(schema, treeConfig);
114     }
115 }