Merge branch 'master' of ../controller
[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.model.api.ContainerSchemaNode;
21
22 /**
23  * General container modification strategy. This is used by {@link EnforcingMandatory} in case of presence containers
24  * with mandatory nodes, as it needs to tap into {@link SchemaAwareApplyOperation}'s operations, or subclassed
25  * for the purposes of {@link StructuralContainerModificationStrategy}'s automatic lifecycle.
26  */
27 class ContainerModificationStrategy extends DataNodeContainerModificationStrategy<ContainerSchemaNode> {
28     private static final class EnforcingMandatory extends ContainerModificationStrategy {
29         private final MandatoryLeafEnforcer enforcer;
30
31         EnforcingMandatory(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig,
32                 final MandatoryLeafEnforcer enforcer) {
33             super(schemaNode, treeConfig);
34             this.enforcer = requireNonNull(enforcer);
35         }
36
37         @Override
38         void mandatoryVerifyValueChildren(final NormalizedNode<?, ?> writtenValue) {
39             enforcer.enforceOnData(writtenValue);
40         }
41
42         @Override
43         protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta,
44                 final Version version) {
45             final TreeNode ret = super.applyMerge(modification, currentMeta, version);
46             enforcer.enforceOnTreeNode(ret);
47             return ret;
48         }
49
50         @Override
51         protected TreeNode applyWrite(final ModifiedNode modification, final NormalizedNode<?, ?> newValue,
52                 final Optional<? extends TreeNode> currentMeta, final Version version) {
53             final TreeNode ret = super.applyWrite(modification, newValue, currentMeta, version);
54             enforcer.enforceOnTreeNode(ret);
55             return ret;
56         }
57
58         @Override
59         protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta,
60                 final Version version) {
61             final TreeNode ret = super.applyTouch(modification, currentMeta, version);
62             enforcer.enforceOnTreeNode(ret);
63             return ret;
64         }
65     }
66
67     private static final NormalizedNodeContainerSupport<NodeIdentifier, ContainerNode> SUPPORT =
68             new NormalizedNodeContainerSupport<>(ContainerNode.class, ImmutableContainerNodeBuilder::create,
69                     ImmutableContainerNodeBuilder::create);
70
71     ContainerModificationStrategy(final ContainerSchemaNode schemaNode, final DataTreeConfiguration treeConfig) {
72         super(SUPPORT, schemaNode, treeConfig);
73     }
74
75     static ModificationApplyOperation of(final ContainerSchemaNode schema, final DataTreeConfiguration treeConfig) {
76         if (schema.isPresenceContainer()) {
77             final Optional<MandatoryLeafEnforcer> enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
78             return enforcer.isPresent() ? new EnforcingMandatory(schema, treeConfig, enforcer.get())
79                     : new ContainerModificationStrategy(schema, treeConfig);
80         }
81
82         return new StructuralContainerModificationStrategy(schema, treeConfig);
83     }
84 }