Bug 1362: New AsyncWriteTransaction#submit method
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / service / AbstractDataTransaction.java
1 /**
2  * Copyright (c) 2014 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.controller.md.sal.common.impl.service;
9
10 import java.util.concurrent.Future;
11 import java.util.concurrent.TimeUnit;
12
13 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
15 import org.opendaylight.controller.md.sal.common.impl.AbstractDataModification;
16 import org.opendaylight.yangtools.concepts.Path;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.base.Preconditions;
23 import com.google.common.util.concurrent.AsyncFunction;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
27
28 public abstract class AbstractDataTransaction<P extends Path<P>, D extends Object> extends
29         AbstractDataModification<P, D> {
30     private final static Logger LOG = LoggerFactory.getLogger(AbstractDataTransaction.class);
31
32     private final Object identifier;
33     private final long allocationTime;
34     private long readyTime = 0;
35     private long completeTime = 0;
36
37     private TransactionStatus status = TransactionStatus.NEW;
38
39     private final AbstractDataBroker<P, D, ? extends Object> broker;
40
41     protected AbstractDataTransaction(final Object identifier,
42             final AbstractDataBroker<P, D, ? extends Object> dataBroker) {
43         super(dataBroker);
44         this.identifier = Preconditions.checkNotNull(identifier);
45         this.broker = Preconditions.checkNotNull(dataBroker);
46         this.allocationTime = System.nanoTime();
47         LOG.debug("Transaction {} Allocated.", identifier);
48     }
49
50     @Override
51     public Object getIdentifier() {
52         return this.identifier;
53     }
54
55     @Override
56     public Future<RpcResult<TransactionStatus>> commit() {
57         readyTime = System.nanoTime();
58         LOG.debug("Transaction {} Ready after {}ms.", identifier, TimeUnit.NANOSECONDS.toMillis(readyTime - allocationTime));
59         changeStatus(TransactionStatus.SUBMITED);
60
61         return this.broker.commit(this);
62     }
63
64     @Override
65     public D readConfigurationData(final P path) {
66         final D local = getUpdatedConfigurationData().get(path);
67         if (local != null) {
68             return local;
69         }
70         return this.broker.readConfigurationData(path);
71     }
72
73     @Override
74     public D readOperationalData(final P path) {
75         final D local = this.getUpdatedOperationalData().get(path);
76         if (local != null) {
77             return local;
78         }
79         return this.broker.readOperationalData(path);
80     }
81
82     @Override
83     public int hashCode() {
84         final int prime = 31;
85         int result = 1;
86         result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
87         return result;
88     }
89
90     @Override
91     public boolean equals(Object obj) {
92         if (this == obj) {
93             return true;
94         }
95         if (obj == null) {
96             return false;
97         }
98         if (getClass() != obj.getClass()) {
99             return false;
100         }
101         AbstractDataTransaction<?, ?> other = (AbstractDataTransaction<?, ?>) obj;
102         if (identifier == null) {
103             if (other.identifier != null) {
104                 return false;
105             }
106         } else if (!identifier.equals(other.identifier)) {
107             return false;
108         }
109         return true;
110     }
111
112     @Override
113     public TransactionStatus getStatus() {
114         return this.status;
115     }
116
117     protected abstract void onStatusChange(final TransactionStatus status);
118
119     public void succeeded() {
120         this.completeTime = System.nanoTime();
121         LOG.debug("Transaction {} Committed after {}ms.", identifier, TimeUnit.NANOSECONDS.toMillis(completeTime - readyTime));
122         changeStatus(TransactionStatus.COMMITED);
123     }
124
125     public void failed() {
126         this.completeTime = System.nanoTime();
127         LOG.debug("Transaction {} Failed after {}ms.", identifier, TimeUnit.NANOSECONDS.toMillis(completeTime - readyTime));
128         changeStatus(TransactionStatus.FAILED);
129     }
130
131     private void changeStatus(final TransactionStatus status) {
132         LOG.debug("Transaction {} transitioned from {} to {}", getIdentifier(), this.status, status);
133         this.status = status;
134         this.onStatusChange(status);
135     }
136
137     public static ListenableFuture<RpcResult<TransactionStatus>> convertToLegacyCommitFuture(
138                                         CheckedFuture<Void,TransactionCommitFailedException> from ) {
139         return Futures.transform(from, new AsyncFunction<Void, RpcResult<TransactionStatus>>() {
140             @Override
141             public ListenableFuture<RpcResult<TransactionStatus>> apply(Void input) throws Exception {
142                 return Futures.immediateFuture(RpcResultBuilder.<TransactionStatus>
143                                                               success(TransactionStatus.COMMITED).build());
144             }
145         } );
146     }
147 }