Merge "Bug 2906 - Added support of entering notification by #streamChild()"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / RootModificationApplyOperation.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 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.tree.DataValidationFailedException;
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
17 /**
18  * Represents a {@link ModificationApplyOperation} which is rooted at conceptual
19  * top of data tree.
20  *
21  * <p>
22  * This implementation differs from other implementations in this package that
23  * is not immutable, but may be upgraded to newer state if available by
24  * explicitly invoking {@link #upgradeIfPossible()} and also serves as factory
25  * for deriving snapshot {@link RootModificationApplyOperation} which will not
26  * be affected by upgrade of original one.
27  *
28  * <p>
29  * There are two variations of this {@link ModificationApplyOperation}:
30  * <ul>
31  * <li>
32  * <b>Upgradable</b> - operation may be upgraded to different backing
33  * implementation by invoking {@link #upgradeIfPossible()}.</li>
34  * <li><b>Not Upgradable</b> - operation is immutable, invocation of
35  * {@link #upgradeIfPossible()} is no-op and method {@link #snapshot()} returns
36  * pointer on same object.
37  *
38  * <h3>Upgradable Root Modification Operation</h3>
39  *
40  * Upgradable Root Modification Operation may be created using:
41  * <ul>
42  * <li> {@link #from(ModificationApplyOperation)} with other upgradable root
43  * modification as an argument
44  * <li>using factory {@link LatestOperationHolder} which instantiates Upgradable
45  * Root Modification Operations and provides an option to set latest
46  * implementation.
47  * </ul>
48  * <p>
49  * Upgradable root operation is never upgraded to latest operation
50  * automatically, but client code must explicitly invoke
51  * {@link #upgradeIfPossible()} to get latest implementation.
52  *
53  * Note: This is helpful for implementing
54  * {@link org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification}
55  * which may be derived from
56  * {@link org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree} before
57  * update of schema and user actually writes data after schema update. During
58  * update user did not invoked any operation.
59  *
60  */
61 abstract class RootModificationApplyOperation extends ModificationApplyOperation {
62
63     @Override
64     public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
65         return getDelegate().getChild(child);
66     }
67
68     @Override
69     final void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current)
70             throws DataValidationFailedException {
71         getDelegate().checkApplicable(path, modification, current);
72     }
73
74     @Override
75     final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta,
76             final Version version) {
77         return getDelegate().apply(modification, currentMeta, version);
78     }
79
80     @Override
81     public final boolean equals(final Object obj) {
82         return getDelegate().equals(obj);
83     }
84
85     @Override
86     public final int hashCode() {
87         return getDelegate().hashCode();
88     }
89
90     @Override
91     public final String toString() {
92         return getDelegate().toString();
93     }
94
95     @Override
96     final void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
97         getDelegate().verifyStructure(modification);
98     }
99
100     @Override
101     final ChildTrackingPolicy getChildPolicy() {
102         return getDelegate().getChildPolicy();
103     }
104
105     /**
106      * Return the underlying delegate.
107      *
108      * @return Underlying delegate.
109      */
110     abstract ModificationApplyOperation getDelegate();
111
112     /**
113      * Creates a snapshot from this modification, which may have separate
114      * upgrade lifecycle and is not affected by upgrades
115      * <p>
116      * Newly created snapshot uses backing implementation of this modi
117      *
118      * @return Derived {@link RootModificationApplyOperation} with separate
119      *         upgrade lifecycle.
120      */
121     abstract RootModificationApplyOperation snapshot();
122
123     /**
124      * Upgrades backing implementation to latest available, if possible.
125      * <p>
126      * Latest implementation of {@link RootModificationApplyOperation} is
127      * managed by {@link LatestOperationHolder} which was used to construct this
128      * operation and latest operation is updated by
129      * {@link LatestOperationHolder#setCurrent(ModificationApplyOperation)}.
130      */
131     abstract void upgradeIfPossible();
132
133     static RootModificationApplyOperation from(final ModificationApplyOperation resolver) {
134         if (resolver instanceof RootModificationApplyOperation) {
135             return ((RootModificationApplyOperation) resolver).snapshot();
136         }
137         return new NotUpgradableModificationApplyOperation(resolver);
138     }
139 }