Bug 5830: Mandatory leaf enforcement is not correct with presence container
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / PresenceContainerModificationStrategy.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17
18 /**
19  * Presence container modification strategy. In addition to {@link ContainerModificationStrategy} it also enforces
20  * presence of mandatory leaves.
21  */
22 final class PresenceContainerModificationStrategy extends ContainerModificationStrategy {
23     private final MandatoryLeafEnforcer enforcer;
24
25     PresenceContainerModificationStrategy(final ContainerSchemaNode schemaNode, final TreeType treeType) {
26         super(schemaNode, treeType);
27         enforcer = MandatoryLeafEnforcer.forContainer(schemaNode, treeType);
28     }
29
30     @Override
31     void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
32         if(verifyChildrenStructure() && verifyChildren) {
33             enforcer.enforceOnTreeNode(writtenValue);
34         }
35         super.verifyStructure(writtenValue, verifyChildren);
36     }
37
38     @Override
39     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
40         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
41         enforcer.enforceOnTreeNode(ret);
42         return ret;
43     }
44
45     @Override
46     protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
47             final Version version) {
48         final TreeNode ret = super.applyWrite(modification, currentMeta, version);
49         enforcer.enforceOnTreeNode(ret);
50         return ret;
51     }
52
53     @Override
54     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
55         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
56         enforcer.enforceOnTreeNode(ret);
57         return ret;
58     }
59 }