Merge "Bug 1352: Fixed issue when ModificationSnapshot was not updated during schema...
[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 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
11 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
12 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
15
16 import com.google.common.base.Optional;
17
18 public abstract class RootModificationApplyOperation implements ModificationApplyOperation {
19
20     @Override
21     public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
22         return getDelegate().getChild(child);
23     }
24
25     @Override
26     public final void checkApplicable(final InstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current)
27             throws DataValidationFailedException {
28         getDelegate().checkApplicable(path, modification, current);
29     }
30
31     @Override
32     public final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta, final Version version) {
33         return getDelegate().apply(modification, currentMeta, version);
34     }
35
36     @Override
37     public boolean equals(final Object obj) {
38         return getDelegate().equals(obj);
39     }
40
41     @Override
42     public int hashCode() {
43         return getDelegate().hashCode();
44     }
45
46     @Override
47     public String toString() {
48         return getDelegate().toString();
49     }
50
51     @Override
52     public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
53         getDelegate().verifyStructure(modification);
54     }
55
56     abstract ModificationApplyOperation getDelegate();
57
58     public abstract RootModificationApplyOperation snapshot();
59
60     public abstract void upgradeIfPossible();
61
62
63
64     public static RootModificationApplyOperation from(final ModificationApplyOperation resolver) {
65         if(resolver instanceof RootModificationApplyOperation) {
66             return ((RootModificationApplyOperation) resolver).snapshot();
67         }
68         return new NotUpgradable(resolver);
69     }
70
71     private static final class Upgradable extends RootModificationApplyOperation {
72
73         private final LatestOperationHolder holder;
74         private ModificationApplyOperation delegate;
75
76
77         public Upgradable(final LatestOperationHolder holder, final ModificationApplyOperation delegate) {
78             this.holder = holder;
79             this.delegate = delegate;
80
81         }
82
83         @Override
84         public void upgradeIfPossible() {
85             ModificationApplyOperation holderCurrent = holder.getCurrent();
86             if(holderCurrent != delegate) {
87                 // FIXME: Allow update only if there is addition of models, not removals.
88                 delegate = holderCurrent;
89             }
90
91         }
92
93         @Override
94         ModificationApplyOperation getDelegate() {
95             return delegate;
96         }
97
98         @Override
99         public RootModificationApplyOperation snapshot() {
100             return new Upgradable(holder,getDelegate());
101         }
102
103     }
104
105     private static final class NotUpgradable extends RootModificationApplyOperation {
106
107         private final ModificationApplyOperation delegate;
108
109         public NotUpgradable(final ModificationApplyOperation delegate) {
110             this.delegate = delegate;
111         }
112
113         @Override
114         public ModificationApplyOperation getDelegate() {
115             return delegate;
116         }
117
118         @Override
119         public void upgradeIfPossible() {
120             // Intentional noop
121         }
122
123         @Override
124         public RootModificationApplyOperation snapshot() {
125             return this;
126         }
127     }
128
129     public static class LatestOperationHolder {
130
131         private ModificationApplyOperation current = new AlwaysFailOperation();
132
133         public ModificationApplyOperation getCurrent() {
134             return current;
135         }
136
137         public void setCurrent(final ModificationApplyOperation newApplyOper) {
138             current = newApplyOper;
139         }
140
141         public RootModificationApplyOperation newSnapshot() {
142             return new Upgradable(this,current);
143         }
144
145     }
146 }