Track current path in a stack
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / StructuralContainerModificationStrategy.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 java.util.Optional;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
12 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23
24 /**
25  * Structural containers are special in that they appear when implied by child nodes and disappear whenever they are
26  * empty. We could implement this as a subclass of {@link SchemaAwareApplyOperation}, but the automatic semantic
27  * is quite different from all the other strategies. We create a {@link ContainerModificationStrategy} to tap into that
28  * logic, but wrap it so we only call out into it. We do not use {@link PresenceContainerModificationStrategy} because
29  * it enforces presence of mandatory leaves, which is not something we want here, as structural containers are not
30  * root anchors for that validation.
31  */
32 final class StructuralContainerModificationStrategy extends ModificationApplyOperation {
33     /**
34      * Fake TreeNode version used in
35      * {@link #checkApplicable(ModificationPath, NodeModification, Optional, Version)}.
36      * It is okay to use a global constant, as the delegate will ignore it anyway. For
37      * {@link #apply(ModifiedNode, Optional, Version)} we will use the appropriate version as provided to us.
38      */
39     private static final Version FAKE_VERSION = Version.initial();
40     private final ContainerModificationStrategy delegate;
41
42     StructuralContainerModificationStrategy(final ContainerSchemaNode schemaNode,
43         final DataTreeConfiguration treeConfig) {
44         this.delegate = new ContainerModificationStrategy(schemaNode, treeConfig);
45     }
46
47     private Optional<TreeNode> fakeMeta(final Version version) {
48         final ContainerNode container = ImmutableNodes.containerNode(delegate.getSchema().getQName());
49         return Optional.of(TreeNodeFactory.createTreeNode(container, version));
50     }
51
52     @Override
53     Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
54             final Version version) {
55         final Optional<TreeNode> ret;
56         if (modification.getOperation() == LogicalOperation.TOUCH && !storeMeta.isPresent()) {
57             // Container is not present, let's take care of the 'magically appear' part of our job
58             ret = delegate.apply(modification, fakeMeta(version), version);
59
60             // Fake container got removed: that is a no-op
61             if (!ret.isPresent()) {
62                 modification.resolveModificationType(ModificationType.UNMODIFIED);
63                 return ret;
64             }
65
66             // If the delegate indicated SUBTREE_MODIFIED, account for the fake and report APPEARED
67             if (modification.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
68                 modification.resolveModificationType(ModificationType.APPEARED);
69             }
70         } else {
71             // Container is present, run normal apply operation
72             ret = delegate.apply(modification, storeMeta, version);
73
74             // Container was explicitly deleted, no magic required
75             if (!ret.isPresent()) {
76                 return ret;
77             }
78         }
79
80         /*
81          * At this point ret is guaranteed to be present. We need to take care of the 'magically disappear' part of
82          * our job. Check if there are any child nodes left. If there are none, remove this container and turn the
83          * modification into a DISAPPEARED.
84          */
85         if (((NormalizedNodeContainer<?, ?, ?>) ret.get().getData()).getValue().isEmpty()) {
86             modification.resolveModificationType(ModificationType.DISAPPEARED);
87             return Optional.empty();
88         }
89
90         return ret;
91     }
92
93     @Override
94     void checkApplicable(final ModificationPath path, final NodeModification modification,
95             final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
96         if (modification.getOperation() == LogicalOperation.TOUCH && !current.isPresent()) {
97             // Structural containers are created as needed, so we pretend this container is here
98             delegate.checkApplicable(path, modification, fakeMeta(FAKE_VERSION), version);
99         } else {
100             delegate.checkApplicable(path, modification, current, version);
101         }
102     }
103
104     @Override
105     void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) {
106         delegate.verifyStructure(modification, verifyChildren);
107     }
108
109     @Override
110     void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
111         delegate.recursivelyVerifyStructure(value);
112     }
113
114     @Override
115     ChildTrackingPolicy getChildPolicy() {
116         return delegate.getChildPolicy();
117     }
118
119     @Override
120     void mergeIntoModifiedNode(final ModifiedNode modification, final NormalizedNode<?, ?> value,
121             final Version version) {
122         delegate.mergeIntoModifiedNode(modification, value, version);
123     }
124
125     @Override
126     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
127         return delegate.getChild(child);
128     }
129 }