Add utility wrappers for instantiating builders/nodes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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.impl.schema.tree;
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.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.data.impl.schema.tree.NormalizedNodeContainerSupport.Single;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22
23 /**
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.
27  */
28 class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
29     private static final class EnforcingMandatory extends ContainerModificationStrategy {
30         private final MandatoryLeafEnforcer enforcer;
31
32         EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
33                 final MandatoryLeafEnforcer enforcer) {
34             super(schemaNode, treeConfig);
35             this.enforcer = requireNonNull(enforcer);
36         }
37
38         @Override
39         void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
40             super.verifyStructure(writtenValue, verifyChildren);
41             if (verifyChildren) {
42                 enforcer.enforceOnData(writtenValue);
43             }
44         }
45
46         @Override
47         protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
48                 final Version version) {
49             final TreeNode ret = super.applyMerge(modification, currentMeta, version);
50             enforcer.enforceOnTreeNode(ret);
51             return ret;
52         }
53
54         @Override
55         protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
56                 final Optional<TreeNode> currentMeta, final Version version) {
57             final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
58             enforcer.enforceOnTreeNode(ret);
59             return ret;
60         }
61
62         @Override
63         protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
64                 final Version version) {
65             final TreeNode ret = super.applyTouch(modification, currentMeta, version);
66             enforcer.enforceOnTreeNode(ret);
67             return ret;
68         }
69     }
70
71     private static final Single<NodeIdentifier, ContainerNode> SUPPORT = new Single<>(ContainerNode.class,
72             ImmutableContainerNodeBuilder::create, ImmutableContainerNodeBuilder::create);
73
74     ContainerModificationStrategy(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig) {
75         super(SUPPORT, schemaNode, treeConfig);
76     }
77
78     static ModificationApplyOperation of(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) {
79         if (schema.isPresenceContainer()) {
80             final Optional<MandatoryLeafEnforcer> enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
81             return enforcer.isPresent() ? new EnforcingMandatory(schema, treeConfig, enforcer.get())
82                     : new ContainerModificationStrategy(schema, treeConfig);
83         }
84
85         return new StructuralContainerModificationStrategy(schema, treeConfig);
86     }
87 }