79f7ce22bc64af22e8fd5ae635713f6d68bbbb76
[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  * @deprecated This interface is scheduled for removal in the next major release.
26  */
27 @Deprecated(forRemoval = true)
28 @Beta
29 public final class ForeignShardModificationContext implements Identifiable<DOMDataTreeIdentifier>, Mutable {
30     private static final Logger LOG = LoggerFactory.getLogger(ForeignShardModificationContext.class);
31
32     private final DOMDataTreeIdentifier identifier;
33     private final DOMDataTreeShardProducer producer;
34
35     private DOMDataTreeShardWriteTransaction tx;
36     private DOMDataTreeWriteCursor cursor;
37
38     private volatile boolean ready = false;
39
40     public ForeignShardModificationContext(final DOMDataTreeIdentifier identifier,
41                                            final DOMDataTreeShardProducer producer) {
42         this.identifier = requireNonNull(identifier);
43         this.producer = requireNonNull(producer);
44     }
45
46     public DOMDataTreeWriteCursor getCursor() {
47         checkState(!ready, "Context %s has been readied", this);
48
49         if (cursor == null) {
50             if (tx == null) {
51                 tx = producer.createTransaction();
52             }
53             cursor = tx.createCursor(getIdentifier());
54         }
55         return cursor;
56     }
57
58     public boolean isModified() {
59         return tx != null;
60     }
61
62     public void ready() {
63         if (ready) {
64             // Idempotent, but emit a debug
65             LOG.debug("Duplicate ready() of context {}", this);
66             return;
67         }
68
69         ready = true;
70         if (cursor != null) {
71             cursor.close();
72             cursor = null;
73         }
74         if (tx != null) {
75             tx.ready();
76             // TODO: it would be nice if we could clear this reference
77             // tx = null;
78         }
79     }
80
81     @Override
82     public DOMDataTreeIdentifier getIdentifier() {
83         return identifier;
84     }
85
86     public DOMDataTreeShardProducer getProducer() {
87         return producer;
88     }
89
90     public ListenableFuture<Boolean> validate() {
91         return tx.validate();
92     }
93
94     public ListenableFuture<Void> prepare() {
95         return tx.prepare();
96     }
97
98     public ListenableFuture<Void> submit() {
99         checkState(ready, "Modification context %s has to be ready before submit", this);
100         final ListenableFuture<Void> commit = tx.commit();
101         ready = false;
102         tx = null;
103         return commit;
104     }
105
106     public void closeForeignTransaction() {
107         if (cursor != null) {
108             cursor.close();
109             cursor = null;
110         }
111
112         if (tx != null) {
113             tx.close();
114             tx = null;
115         }
116     }
117 }