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