3ceeb7e44db980855d4afad4b05d1489b0135f96
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / AbstractDataModification.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.controller.md.sal.common.impl;
9
10 import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.NEW;
11
12 import java.util.Collections;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17
18 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
19 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
20 import org.opendaylight.yangtools.concepts.Path;
21
22 public abstract class AbstractDataModification<P extends Path<P>, D> implements DataModification<P, D> {
23
24     private final ConcurrentMap<P, D> operationalOriginal;
25     private final ConcurrentMap<P, D> configurationOriginal;
26
27     private final ConcurrentMap<P, D> operationalCreated;
28     private final ConcurrentMap<P, D> configurationCreated;
29
30     private final ConcurrentMap<P, D> configurationUpdate;
31     private final ConcurrentMap<P, D> operationalUpdate;
32
33     private final ConcurrentMap<P, P> configurationRemove;
34     private final ConcurrentMap<P, P> operationalRemove;
35
36     private final Map<P, D> unmodifiable_configurationOriginal;
37     private final Map<P, D> unmodifiable_operationalOriginal;
38     private final Map<P, D> unmodifiable_configurationCreated;
39     private final Map<P, D> unmodifiable_operationalCreated;
40     private final Map<P, D> unmodifiable_configurationUpdate;
41     private final Map<P, D> unmodifiable_operationalUpdate;
42     private final Set<P> unmodifiable_configurationRemove;
43     private final Set<P> unmodifiable_OperationalRemove;
44     private final DataReader<P, D> reader;
45
46     public AbstractDataModification(DataReader<P, D> reader) {
47         this.reader = reader;
48         this.configurationUpdate = new ConcurrentHashMap<>();
49         this.operationalUpdate = new ConcurrentHashMap<>();
50         this.configurationRemove = new ConcurrentHashMap<>();
51         this.operationalRemove = new ConcurrentHashMap<>();
52
53         this.configurationOriginal = new ConcurrentHashMap<>();
54         this.operationalOriginal = new ConcurrentHashMap<>();
55
56         this.configurationCreated = new ConcurrentHashMap<>();
57         this.operationalCreated = new ConcurrentHashMap<>();
58
59         unmodifiable_configurationOriginal = Collections.unmodifiableMap(configurationOriginal);
60         unmodifiable_operationalOriginal = Collections.unmodifiableMap(operationalOriginal);
61         unmodifiable_configurationCreated = Collections.unmodifiableMap(configurationCreated);
62         unmodifiable_operationalCreated = Collections.unmodifiableMap(operationalCreated);
63         unmodifiable_configurationUpdate = Collections.unmodifiableMap(configurationUpdate);
64         unmodifiable_operationalUpdate = Collections.unmodifiableMap(operationalUpdate);
65         unmodifiable_configurationRemove = Collections.unmodifiableSet(configurationRemove.keySet());
66         unmodifiable_OperationalRemove = Collections.unmodifiableSet(operationalRemove.keySet());
67     }
68
69     @Override
70     public final void putConfigurationData(P path, D data) {
71         checkMutable();
72         D original = null;
73         if ((original = getConfigurationOriginal(path)) == null) {
74             configurationCreated.put(path, data);
75         }
76
77         configurationUpdate.put(path, mergeConfigurationData(path,original, data));
78     }
79
80     @Override
81     public final void putOperationalData(P path, D data) {
82         checkMutable();
83         D original = null;
84         if ((original = getOperationalOriginal(path)) == null) {
85             operationalCreated.put(path, data);
86         }
87         operationalUpdate.put(path, mergeOperationalData(path,original,data));
88     }
89
90     @Override
91     public final void removeOperationalData(P path) {
92         checkMutable();
93         getOperationalOriginal(path);
94         operationalUpdate.remove(path);
95         operationalRemove.put(path, path);
96     }
97
98     @Override
99     public final void removeConfigurationData(P path) {
100         checkMutable();
101         getConfigurationOriginal(path);
102         configurationUpdate.remove(path);
103         configurationRemove.put(path, path);
104     }
105
106     private final void checkMutable() {
107         if (!NEW.equals(this.getStatus()))
108             throw new IllegalStateException("Transaction was already submitted");
109     }
110
111     @Override
112     public final Map<P, D> getUpdatedConfigurationData() {
113
114         return unmodifiable_configurationUpdate;
115     }
116
117     @Override
118     public final Map<P, D> getUpdatedOperationalData() {
119         return unmodifiable_operationalUpdate;
120     }
121
122     @Override
123     public final Set<P> getRemovedConfigurationData() {
124         return unmodifiable_configurationRemove;
125     }
126
127     @Override
128     public final Set<P> getRemovedOperationalData() {
129         return unmodifiable_OperationalRemove;
130     }
131
132     @Override
133     public Map<P, D> getCreatedConfigurationData() {
134         return unmodifiable_configurationCreated;
135     }
136
137     @Override
138     public Map<P, D> getCreatedOperationalData() {
139         return unmodifiable_operationalCreated;
140     }
141
142     @Override
143     public Map<P, D> getOriginalConfigurationData() {
144         return unmodifiable_configurationOriginal;
145     }
146
147     @Override
148     public Map<P, D> getOriginalOperationalData() {
149         return unmodifiable_operationalOriginal;
150     }
151
152     @Override
153     public D readOperationalData(P path) {
154         return reader.readOperationalData(path);
155     }
156
157     @Override
158     public D readConfigurationData(P path) {
159         return reader.readConfigurationData(path);
160     }
161
162     private D getConfigurationOriginal(P path) {
163         D data = configurationOriginal.get(path);
164         if (data != null) {
165             return data;
166         }
167         data = reader.readConfigurationData(path);
168         if (data != null) {
169             configurationOriginal.putIfAbsent(path, data);
170             return data;
171         }
172         return null;
173     }
174
175     private D getOperationalOriginal(P path) {
176         D data = operationalOriginal.get(path);
177         if (data != null) {
178             return data;
179         }
180         data = reader.readOperationalData(path);
181         if (data != null) {
182             operationalOriginal.putIfAbsent(path, data);
183             return data;
184         }
185         return null;
186     }
187
188     protected D mergeOperationalData(P path,D stored, D modified) {
189         return modified;
190     }
191
192     protected D mergeConfigurationData(P path,D stored, D modified) {
193         return modified;
194     }
195 }