4d3f22d58dc71a9510af5376a23e36d7c6044abc
[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.schema.ContainerNode;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
14 import org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer;
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 DelegatingModificationApplyOperation {
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
41     private final ContainerModificationStrategy delegate;
42     private final ContainerNode emptyNode;
43
44     StructuralContainerModificationStrategy(final ContainerSchemaNode schemaNode,
45         final DataTreeConfiguration treeConfig) {
46         this.delegate = new ContainerModificationStrategy(schemaNode, treeConfig);
47         this.emptyNode = ImmutableNodes.containerNode(schemaNode.getQName());
48     }
49
50     @Override
51     ModificationApplyOperation delegate() {
52         return delegate;
53     }
54
55     @Override
56     void fullVerifyStructure(final NormalizedNode<?, ?> modification) {
57         delegate.fullVerifyStructure(modification);
58     }
59
60     @Override
61     Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
62             final Version version) {
63         // The only way a tree node can disappear is through delete (which we handle here explicitly) or through
64         // actions of disappearResult(). It is therefore safe to perform Optional.get() on the results of
65         // delegate.apply()
66         final TreeNode ret;
67         if (modification.getOperation() == LogicalOperation.DELETE) {
68             if (modification.getChildren().isEmpty()) {
69                 return delegate.apply(modification, storeMeta, version);
70             }
71             // Delete with children, implies it really is an empty write
72             ret = delegate.applyWrite(modification, emptyNode, storeMeta, version);
73         } else if (modification.getOperation() == LogicalOperation.TOUCH && !storeMeta.isPresent()) {
74             ret = applyTouch(modification, storeMeta, version);
75         } else {
76             // No special handling required here, run normal apply operation
77             ret = delegate.apply(modification, storeMeta, version).get();
78         }
79
80         return disappearResult(modification, ret, storeMeta);
81     }
82
83     @Override
84     void checkApplicable(final ModificationPath path, final NodeModification modification,
85             final Optional<TreeNode> current, final Version version) throws DataValidationFailedException {
86         if (modification.getOperation() == LogicalOperation.TOUCH && !current.isPresent()) {
87             // Structural containers are created as needed, so we pretend this container is here
88             delegate.checkApplicable(path, modification, fakeMeta(FAKE_VERSION), version);
89         } else {
90             delegate.checkApplicable(path, modification, current, version);
91         }
92     }
93
94     private Optional<TreeNode> fakeMeta(final Version version) {
95         return Optional.of(TreeNodeFactory.createTreeNode(emptyNode, version));
96     }
97
98     private TreeNode applyTouch(final ModifiedNode modification, final Optional<TreeNode> storeMeta,
99             final Version version) {
100         // Container is not present, let's take care of the 'magically appear' part of our job
101         final Optional<TreeNode> ret = delegate.apply(modification, fakeMeta(version), version);
102
103         // If the delegate indicated SUBTREE_MODIFIED, account for the fake and report APPEARED
104         if (modification.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
105             modification.resolveModificationType(ModificationType.APPEARED);
106         }
107         return ret.get();
108     }
109
110     private static Optional<TreeNode> disappearResult(final ModifiedNode modification, final TreeNode result,
111             final Optional<TreeNode> storeMeta) {
112         // Check if the result is in fact empty before pulling any tricks
113         if (!isEmpty(result)) {
114             return Optional.of(result);
115         }
116
117         // We are pulling the 'disappear' trick, but what we report can be three different things
118         final ModificationType finalType;
119         if (!storeMeta.isPresent()) {
120             // ... there was nothing in the datastore, no change
121             finalType = ModificationType.UNMODIFIED;
122         } else if (modification.getModificationType() == ModificationType.WRITE) {
123             // ... this was an empty write, possibly originally a delete
124             finalType = ModificationType.DELETE;
125         } else {
126             // ... it really disappeared
127             finalType = ModificationType.DISAPPEARED;
128         }
129         modification.resolveModificationType(finalType);
130         return Optional.empty();
131     }
132
133     private static boolean isEmpty(final TreeNode treeNode) {
134         final NormalizedNode<?, ?> data = treeNode.getData();
135         if (data instanceof NormalizedNodeContainer) {
136             return ((NormalizedNodeContainer<?, ?, ?>) data).getValue().isEmpty();
137         }
138         if (data instanceof OrderedNodeContainer) {
139             return ((OrderedNodeContainer<?>) data).getSize() == 0;
140         }
141         throw new IllegalStateException("Unhandled data " + data);
142     }
143 }