2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.common.impl;
10 import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.NEW;
12 import java.util.Collections;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
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;
22 public abstract class AbstractDataModification<P extends Path<P>, D> implements DataModification<P, D> {
24 private final ConcurrentMap<P, D> operationalOriginal;
25 private final ConcurrentMap<P, D> configurationOriginal;
27 private final ConcurrentMap<P, D> operationalCreated;
28 private final ConcurrentMap<P, D> configurationCreated;
30 private final ConcurrentMap<P, D> configurationUpdate;
31 private final ConcurrentMap<P, D> operationalUpdate;
33 private final ConcurrentMap<P, P> configurationRemove;
34 private final ConcurrentMap<P, P> operationalRemove;
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;
46 public AbstractDataModification(DataReader<P, D> reader) {
48 this.configurationUpdate = new ConcurrentHashMap<>();
49 this.operationalUpdate = new ConcurrentHashMap<>();
50 this.configurationRemove = new ConcurrentHashMap<>();
51 this.operationalRemove = new ConcurrentHashMap<>();
53 this.configurationOriginal = new ConcurrentHashMap<>();
54 this.operationalOriginal = new ConcurrentHashMap<>();
56 this.configurationCreated = new ConcurrentHashMap<>();
57 this.operationalCreated = new ConcurrentHashMap<>();
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());
70 public final void putConfigurationData(P path, D data) {
73 if ((original = getConfigurationOriginal(path)) == null) {
74 configurationCreated.put(path, data);
77 configurationUpdate.put(path, mergeConfigurationData(path,original, data));
81 public final void putOperationalData(P path, D data) {
84 if ((original = getOperationalOriginal(path)) == null) {
85 operationalCreated.put(path, data);
87 operationalUpdate.put(path, mergeOperationalData(path,original,data));
91 public final void removeOperationalData(P path) {
93 getOperationalOriginal(path);
94 operationalUpdate.remove(path);
95 operationalRemove.put(path, path);
99 public final void removeConfigurationData(P path) {
101 getConfigurationOriginal(path);
102 configurationUpdate.remove(path);
103 configurationRemove.put(path, path);
106 private final void checkMutable() {
107 if (!NEW.equals(this.getStatus()))
108 throw new IllegalStateException("Transaction was already submitted");
112 public final Map<P, D> getUpdatedConfigurationData() {
114 return unmodifiable_configurationUpdate;
118 public final Map<P, D> getUpdatedOperationalData() {
119 return unmodifiable_operationalUpdate;
123 public final Set<P> getRemovedConfigurationData() {
124 return unmodifiable_configurationRemove;
128 public final Set<P> getRemovedOperationalData() {
129 return unmodifiable_OperationalRemove;
133 public Map<P, D> getCreatedConfigurationData() {
134 return unmodifiable_configurationCreated;
138 public Map<P, D> getCreatedOperationalData() {
139 return unmodifiable_operationalCreated;
143 public Map<P, D> getOriginalConfigurationData() {
144 return unmodifiable_configurationOriginal;
148 public Map<P, D> getOriginalOperationalData() {
149 return unmodifiable_operationalOriginal;
153 public D readOperationalData(P path) {
154 return reader.readOperationalData(path);
158 public D readConfigurationData(P path) {
159 return reader.readConfigurationData(path);
162 private D getConfigurationOriginal(P path) {
163 D data = configurationOriginal.get(path);
167 data = reader.readConfigurationData(path);
169 configurationOriginal.putIfAbsent(path, data);
175 private D getOperationalOriginal(P path) {
176 D data = operationalOriginal.get(path);
180 data = reader.readOperationalData(path);
182 operationalOriginal.putIfAbsent(path, data);
188 protected D mergeOperationalData(P path,D stored, D modified) {
192 protected D mergeConfigurationData(P path,D stored, D modified) {