3cc6ae7d64470da1548dbb3c1c4375eb2ccf71a4
[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.DataValidationFailedException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
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
27  * nodes and disappear whenever they are empty. We could implement this as a
28  * subclass of {@link SchemaAwareApplyOperation}, but the automatic semantic
29  * is quite different from all the other strategies. We create a
30  * {@link PresenceContainerModificationStrategy} to tap into that logic, but
31  * wrap it so we only call out into it
32  */
33 final class StructuralContainerModificationStrategy extends ModificationApplyOperation {
34     /**
35      * Fake TreeNode version used in {@link #checkApplicable(YangInstanceIdentifier, NodeModification, Optional)}.
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 PresenceContainerModificationStrategy delegate;
41
42     StructuralContainerModificationStrategy(final ContainerSchemaNode schemaNode, final TreeType treeType) {
43         this.delegate = new PresenceContainerModificationStrategy(schemaNode, treeType);
44     }
45
46     private Optional<TreeNode> fakeMeta(final Version version) {
47         final ContainerNode container = ImmutableNodes.containerNode(delegate.getSchema().getQName());
48         return Optional.of(TreeNodeFactory.createTreeNode(container, version));
49     }
50
51     @Override
52     Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> storeMeta, final Version version) {
53         final Optional<TreeNode> ret;
54         if (modification.getOperation() == LogicalOperation.TOUCH && !storeMeta.isPresent()) {
55             // Container is not present, let's take care of the 'magically appear' part of our job
56             ret = delegate.apply(modification, fakeMeta(version), version);
57
58             // Fake container got removed: that is a no-op
59             if (!ret.isPresent()) {
60                 modification.resolveModificationType(ModificationType.UNMODIFIED);
61                 return ret;
62             }
63
64             // If the delegate indicated SUBTREE_MODIFIED, account for the fake and report APPEARED
65             if (modification.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
66                 modification.resolveModificationType(ModificationType.APPEARED);
67             }
68         } else {
69             // Container is present, run normal apply operation
70             ret = delegate.apply(modification, storeMeta, version);
71
72             // Container was explicitly deleted, no magic required
73             if (!ret.isPresent()) {
74                 return ret;
75             }
76         }
77
78         /*
79          * At this point ret is guaranteed to be present. We need to take care of the 'magically disappear' part of
80          * our job. Check if there are any child nodes left. If there are none, remove this container and turn the
81          * modification into a DISAPPEARED.
82          */
83         if (((NormalizedNodeContainer<?, ?, ?>) ret.get().getData()).getValue().isEmpty()) {
84             modification.resolveModificationType(ModificationType.DISAPPEARED);
85             return Optional.absent();
86         }
87
88         return ret;
89     }
90
91     @Override
92     void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current) throws DataValidationFailedException {
93         if (modification.getOperation() == LogicalOperation.TOUCH && !current.isPresent()) {
94             // Structural containers are created as needed, so we pretend this container is here
95             delegate.checkApplicable(path, modification, fakeMeta(FAKE_VERSION));
96         } else {
97             delegate.checkApplicable(path, modification, current);
98         }
99     }
100
101     @Override
102     void verifyStructure(final NormalizedNode<?, ?> modification, final boolean verifyChildren) throws IllegalArgumentException {
103         delegate.verifyStructure(modification, verifyChildren);
104     }
105
106     @Override
107     ChildTrackingPolicy getChildPolicy() {
108         return delegate.getChildPolicy();
109     }
110
111     @Override
112     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
113         return delegate.getChild(child);
114     }
115 }