Remove @(Not)ThreadSafe annotation
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / shard / ForeignShardModificationContext.java
1 /*
2  * Copyright (c) 2016 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.mdsal.dom.spi.shard;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18 import org.opendaylight.yangtools.concepts.Mutable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * A handle for a transaction being done on a different shard. This class is not thread-safe.
24  */
25 @Beta
26 public final class ForeignShardModificationContext implements Identifiable<DOMDataTreeIdentifier>, Mutable {
27     private static final Logger LOG = LoggerFactory.getLogger(ForeignShardModificationContext.class);
28
29     private final DOMDataTreeIdentifier identifier;
30     private final DOMDataTreeShardProducer producer;
31
32     private DOMDataTreeShardWriteTransaction tx;
33     private DOMDataTreeWriteCursor cursor;
34
35     private volatile boolean ready = false;
36
37     public ForeignShardModificationContext(final DOMDataTreeIdentifier identifier,
38                                            final DOMDataTreeShardProducer producer) {
39         this.identifier = requireNonNull(identifier);
40         this.producer = requireNonNull(producer);
41     }
42
43     public DOMDataTreeWriteCursor getCursor() {
44         checkState(!ready, "Context %s has been readied", this);
45
46         if (cursor == null) {
47             if (tx == null) {
48                 tx = producer.createTransaction();
49             }
50             cursor = tx.createCursor(getIdentifier());
51         }
52         return cursor;
53     }
54
55     public boolean isModified() {
56         return tx != null;
57     }
58
59     public void ready() {
60         if (ready) {
61             // Idempotent, but emit a debug
62             LOG.debug("Duplicate ready() of context {}", this);
63             return;
64         }
65
66         ready = true;
67         if (cursor != null) {
68             cursor.close();
69             cursor = null;
70         }
71         if (tx != null) {
72             tx.ready();
73             // TODO: it would be nice if we could clear this reference
74             // tx = null;
75         }
76     }
77
78     @Override
79     public DOMDataTreeIdentifier getIdentifier() {
80         return identifier;
81     }
82
83     public DOMDataTreeShardProducer getProducer() {
84         return producer;
85     }
86
87     public ListenableFuture<Boolean> validate() {
88         return tx.validate();
89     }
90
91     public ListenableFuture<Void> prepare() {
92         return tx.prepare();
93     }
94
95     public ListenableFuture<Void> submit() {
96         checkState(ready, "Modification context %s has to be ready before submit", this);
97         final ListenableFuture<Void> commit = tx.commit();
98         ready = false;
99         tx = null;
100         return commit;
101     }
102
103     public void closeForeignTransaction() {
104         if (cursor != null) {
105             cursor.close();
106             cursor = null;
107         }
108
109         if (tx != null) {
110             tx.close();
111             tx = null;
112         }
113     }
114 }