Do not use Optional in returns
[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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
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;
23
24 /**
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.
28  */
29 sealed class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerLike> {
30     static final class EnforcingMandatory extends ContainerModificationStrategy {
31         private final MandatoryLeafEnforcer enforcer;
32
33         EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
34                 final MandatoryLeafEnforcer enforcer) {
35             super(schemaNode, treeConfig);
36             this.enforcer = requireNonNull(enforcer);
37         }
38
39         @Override
40         void mandatoryVerifyValueChildren(final DistinctNodeContainer<?, ?> writtenValue) {
41             enforcer.enforceOnData(writtenValue);
42         }
43
44         @Override
45         protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
46                 final Version version) {
47             final var ret = super.applyMerge(modification, currentMeta, version);
48             enforcer.enforceOnTreeNode(ret);
49             return ret;
50         }
51
52         @Override
53         protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode newValue,
54                 final TreeNode currentMeta, final Version version) {
55             final var ret = super.applyWrite(modification, newValue, currentMeta, version);
56             enforcer.enforceOnTreeNode(ret);
57             return ret;
58         }
59
60         @Override
61         protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
62                 final Version version) {
63             final var ret = super.applyTouch(modification, currentMeta, version);
64             enforcer.enforceOnTreeNode(ret);
65             return ret;
66         }
67     }
68
69     /**
70      * Structural containers are special in that they appear when implied by child nodes and disappear whenever they are
71      * empty. We could implement this as a subclass of {@link SchemaAwareApplyOperation}, but the automatic semantic
72      * is quite different from all the other strategies. We create a {@link ContainerModificationStrategy} to tap into
73      * that logic, but wrap it so we only call out into it. We do not use {@link PresenceContainerModificationStrategy}
74      * because it enforces presence of mandatory leaves, which is not something we want here, as structural containers
75      * are not root anchors for that validation.
76      */
77     static final class Structural extends ContainerModificationStrategy {
78         private final ContainerNode emptyNode;
79
80         Structural(final ContainerLike schema, final DataTreeConfiguration treeConfig) {
81             super(schema, treeConfig);
82             emptyNode = ImmutableNodes.containerNode(schema.getQName());
83         }
84
85         @Override
86         TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
87             return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
88                 version);
89         }
90
91         @Override
92         TreeNode defaultTreeNode() {
93             return defaultTreeNode(emptyNode);
94         }
95     }
96
97     private static final NormalizedNodeContainerSupport<NodeIdentifier, ContainerNode> SUPPORT =
98             new NormalizedNodeContainerSupport<>(ContainerNode.class, ImmutableContainerNodeBuilder::create,
99                     ImmutableContainerNodeBuilder::create);
100
101     ContainerModificationStrategy(final ContainerLike schemaNode, final DataTreeConfiguration treeConfig) {
102         super(SUPPORT, schemaNode, treeConfig);
103     }
104
105     static ContainerModificationStrategy of(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) {
106         if (schema.isPresenceContainer()) {
107             final var enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
108             return enforcer != null ? new EnforcingMandatory(schema, treeConfig, enforcer)
109                 : new ContainerModificationStrategy(schema, treeConfig);
110         }
111         return new Structural(schema, treeConfig);
112     }
113 }