7faca35ea4da6fe5b0341641d5566bc56c88cfb1
[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.DataTreeConfiguration;
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,
26             final DataTreeConfiguration treeConfig) {
27         super(schemaNode, treeConfig);
28         enforcer = MandatoryLeafEnforcer.forContainer(schemaNode, treeConfig);
29     }
30
31     @Override
32     void verifyStructure(final NormalizedNode<?, ?> writtenValue, final boolean verifyChildren) {
33         super.verifyStructure(writtenValue, verifyChildren);
34         if (verifyChildren) {
35             enforcer.enforceOnData(writtenValue);
36         }
37     }
38
39     @Override
40     protected TreeNode applyMerge(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
41         final TreeNode ret = super.applyMerge(modification, currentMeta, version);
42         enforcer.enforceOnTreeNode(ret);
43         return ret;
44     }
45
46     @Override
47     protected TreeNode applyWrite(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
48             final Version version) {
49         final TreeNode ret = super.applyWrite(modification, currentMeta, version);
50         enforcer.enforceOnTreeNode(ret);
51         return ret;
52     }
53
54     @Override
55     protected TreeNode applyTouch(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
56         final TreeNode ret = super.applyTouch(modification, currentMeta, version);
57         enforcer.enforceOnTreeNode(ret);
58         return ret;
59     }
60 }