Turn ModificationApplyOperation into an abstract class
[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 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 boolean equals(final Object obj) {
82         return getDelegate().equals(obj);
83     }
84
85     @Override
86     public int hashCode() {
87         return getDelegate().hashCode();
88     }
89
90     @Override
91     public 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     /**
101      * Return the underlying delegate.
102      *
103      * @return Underlying delegate.
104      */
105     abstract ModificationApplyOperation getDelegate();
106
107     /**
108      * Creates a snapshot from this modification, which may have separate
109      * upgrade lifecycle and is not affected by upgrades
110      * <p>
111      * Newly created snapshot uses backing implementation of this modi
112      *
113      * @return Derived {@link RootModificationApplyOperation} with separate
114      *         upgrade lifecycle.
115      */
116     abstract RootModificationApplyOperation snapshot();
117
118     /**
119      * Upgrades backing implementation to latest available, if possible.
120      * <p>
121      * Latest implementation of {@link RootModificationApplyOperation} is
122      * managed by {@link LatestOperationHolder} which was used to construct this
123      * operation and latest operation is updated by
124      * {@link LatestOperationHolder#setCurrent(ModificationApplyOperation)}.
125      */
126     abstract void upgradeIfPossible();
127
128     static RootModificationApplyOperation from(final ModificationApplyOperation resolver) {
129         if (resolver instanceof RootModificationApplyOperation) {
130             return ((RootModificationApplyOperation) resolver).snapshot();
131         }
132         return new NotUpgradableModificationApplyOperation(resolver);
133     }
134 }