Bug 3195: Cleanup on error paths and error handling
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContext.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.cluster.datastore;
9
10 import akka.actor.ActorSelection;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import scala.concurrent.Future;
23
24 /**
25  * Processes front-end transaction operations locally before being committed to the destination shard.
26  * Instances of this class are used when the destination shard is local to the caller.
27  *
28  * @author Thomas Pantelis
29  */
30 abstract class LocalTransactionContext extends AbstractTransactionContext {
31     private final DOMStoreTransaction txDelegate;
32     private final LocalTransactionReadySupport readySupport;
33     private Exception operationError;
34
35     LocalTransactionContext(DOMStoreTransaction txDelegate, TransactionIdentifier identifier,
36             LocalTransactionReadySupport readySupport) {
37         super(identifier);
38         this.txDelegate = Preconditions.checkNotNull(txDelegate);
39         this.readySupport = readySupport;
40     }
41
42     protected abstract DOMStoreWriteTransaction getWriteDelegate();
43
44     protected abstract DOMStoreReadTransaction getReadDelegate();
45
46     @Override
47     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
48         incrementModificationCount();
49         if(operationError == null) {
50             try {
51                 getWriteDelegate().write(path, data);
52             } catch (Exception e) {
53                 operationError = e;
54             }
55         }
56
57     }
58
59     @Override
60     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
61         incrementModificationCount();
62         if(operationError == null) {
63             try {
64                 getWriteDelegate().merge(path, data);
65             } catch (Exception e) {
66                 operationError = e;
67             }
68         }
69     }
70
71     @Override
72     public void deleteData(YangInstanceIdentifier path) {
73         incrementModificationCount();
74         if(operationError == null) {
75             try {
76                 getWriteDelegate().delete(path);
77             } catch (Exception e) {
78                 operationError = e;
79             }
80         }
81     }
82
83     @Override
84     public void readData(YangInstanceIdentifier path, final SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture) {
85         Futures.addCallback(getReadDelegate().read(path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
86             @Override
87             public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
88                 proxyFuture.set(result);
89             }
90
91             @Override
92             public void onFailure(final Throwable t) {
93                 proxyFuture.setException(t);
94             }
95         });
96     }
97
98     @Override
99     public void dataExists(YangInstanceIdentifier path, final SettableFuture<Boolean> proxyFuture) {
100         Futures.addCallback(getReadDelegate().exists(path), new FutureCallback<Boolean>() {
101             @Override
102             public void onSuccess(final Boolean result) {
103                 proxyFuture.set(result);
104             }
105
106             @Override
107             public void onFailure(final Throwable t) {
108                 proxyFuture.setException(t);
109             }
110         });
111     }
112
113     private LocalThreePhaseCommitCohort ready() {
114         logModificationCount();
115         LocalThreePhaseCommitCohort cohort = readySupport.onTransactionReady(getWriteDelegate());
116         cohort.setOperationError(operationError);
117         return cohort;
118     }
119
120     @Override
121     public Future<ActorSelection> readyTransaction() {
122         final LocalThreePhaseCommitCohort cohort = ready();
123         return cohort.initiateCoordinatedCommit();
124     }
125
126     @Override
127     public Future<Object> directCommit() {
128         final LocalThreePhaseCommitCohort cohort = ready();
129         return cohort.initiateDirectCommit();
130     }
131
132     @Override
133     public boolean supportsDirectCommit() {
134         return true;
135     }
136
137     @Override
138     public void closeTransaction() {
139         txDelegate.close();
140     }
141 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.