b345bc76fbda4d9cf30b5f2a159acbfa23fd9322
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / infra / WriteTrackingTypedReadWriteTransactionImpl.java
1 /*
2  * Copyright © 2018 Red Hat, Inc. and others.
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.genius.infra;
9
10 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
11 import org.opendaylight.yangtools.yang.binding.DataObject;
12 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
13
14 /**
15  * Read-write typed transaction which keeps track of writes.
16  */
17 class WriteTrackingTypedReadWriteTransactionImpl<D extends Datastore> extends TypedReadWriteTransactionImpl<D>
18         implements WriteTrackingTransaction {
19
20     // This is volatile to ensure we get the latest value; transactions aren't supposed to be used in multiple threads,
21     // but the cost here is tiny (one read penalty at the end of a transaction) so we play it safe
22     private volatile boolean written;
23
24     WriteTrackingTypedReadWriteTransactionImpl(Class<D> datastoreType, ReadWriteTransaction realTx) {
25         super(datastoreType, realTx);
26     }
27
28     @Override
29     public <T extends DataObject> void put(InstanceIdentifier<T> path, T data) {
30         super.put(path, data);
31         written = true;
32     }
33
34     @Override
35     public <T extends DataObject> void put(InstanceIdentifier<T> path, T data, boolean createMissingParents) {
36         super.put(path, data, createMissingParents);
37         written = true;
38     }
39
40     @Override
41     public <T extends DataObject> void merge(InstanceIdentifier<T> path, T data) {
42         super.merge(path, data);
43         written = true;
44     }
45
46     @Override
47     public <T extends DataObject> void merge(InstanceIdentifier<T> path, T data, boolean createMissingParents) {
48         super.merge(path, data, createMissingParents);
49         written = true;
50     }
51
52     @Override
53     public void delete(InstanceIdentifier<?> path) {
54         super.delete(path);
55         written = true;
56     }
57
58     @Override
59     public boolean isWritten() {
60         return written;
61     }
62 }