Use ImmutableMap instead of Collections.emptyMap()
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / 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
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 @NotThreadSafe
20 final class ForeignShardModificationContext {
21     private static final Logger LOG = LoggerFactory.getLogger(ForeignShardModificationContext.class);
22     private final DOMDataTreeIdentifier identifier;
23     private final DOMDataTreeShardProducer producer;
24     private DOMDataTreeShardWriteTransaction tx;
25     private DOMDataTreeWriteCursor cursor;
26     private volatile boolean notReady = true;
27
28     ForeignShardModificationContext(final DOMDataTreeIdentifier identifier, final DOMDataTreeShardProducer producer) {
29         this.identifier = Preconditions.checkNotNull(identifier);
30         this.producer = Preconditions.checkNotNull(producer);
31     }
32
33     DOMDataTreeWriteCursor getCursor() {
34         Preconditions.checkState(notReady, "Context %s has been readied", this);
35
36         if (cursor == null) {
37             if (tx == null) {
38                 tx = producer.createTransaction();
39             }
40             cursor = tx.createCursor(getIdentifier());
41         }
42         return cursor;
43     }
44
45     boolean isModified() {
46         return tx != null;
47     }
48
49     void ready() {
50         if (!notReady) {
51             // Idempotent, but emit a debug
52             LOG.debug("Duplicate ready() of context {}", this);
53             return;
54         }
55
56         notReady = true;
57         if (cursor != null) {
58             cursor.close();
59             cursor = null;
60         }
61         if (tx != null) {
62             tx.ready();
63             // TODO: it would be nice if we could clear this reference
64             // tx = null;
65         }
66     }
67
68     DOMDataTreeIdentifier getIdentifier() {
69         return identifier;
70     }
71
72     ListenableFuture<Boolean> validate() {
73         return tx.validate();
74     }
75
76     ListenableFuture<Void> prepare() {
77         return tx.prepare();
78     }
79
80     ListenableFuture<Void> submit() {
81         final ListenableFuture<Void> commit = tx.commit();
82         tx = null;
83         return commit;
84     }
85
86     void closeForeignTransaction() {
87         if (cursor != null) {
88             cursor.close();
89             cursor = null;
90         }
91
92         if (tx != null) {
93             tx.close();
94             tx = null;
95         }
96     }
97 }