Cleanup AbstractDataTransaction state changes
[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.impl.AbstractDataModification;
15 import org.opendaylight.yangtools.concepts.Path;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.base.Preconditions;
21
22 public abstract class AbstractDataTransaction<P extends Path<P>, D extends Object> extends
23         AbstractDataModification<P, D> {
24     private final static Logger LOG = LoggerFactory.getLogger(AbstractDataTransaction.class);
25
26     private final Object identifier;
27     private final long allocationTime;
28     private long readyTime = 0;
29     private long completeTime = 0;
30
31     private TransactionStatus status = TransactionStatus.NEW;
32
33     private final AbstractDataBroker<P, D, ? extends Object> broker;
34
35     protected AbstractDataTransaction(final Object identifier,
36             final AbstractDataBroker<P, D, ? extends Object> dataBroker) {
37         super(dataBroker);
38         this.identifier = Preconditions.checkNotNull(identifier);
39         this.broker = Preconditions.checkNotNull(dataBroker);
40         this.allocationTime = System.nanoTime();
41         LOG.debug("Transaction {} Allocated.", identifier);
42     }
43
44     @Override
45     public Object getIdentifier() {
46         return this.identifier;
47     }
48
49     @Override
50     public Future<RpcResult<TransactionStatus>> commit() {
51         readyTime = System.nanoTime();
52         LOG.debug("Transaction {} Ready after {}ms.", identifier, TimeUnit.NANOSECONDS.toMillis(readyTime - allocationTime));
53         changeStatus(TransactionStatus.SUBMITED);
54
55         return this.broker.commit(this);
56     }
57
58     @Override
59     public D readConfigurationData(final P path) {
60         final D local = getUpdatedConfigurationData().get(path);
61         if (local != null) {
62             return local;
63         }
64         return this.broker.readConfigurationData(path);
65     }
66
67     @Override
68     public D readOperationalData(final P path) {
69         final D local = this.getUpdatedOperationalData().get(path);
70         if (local != null) {
71             return local;
72         }
73         return this.broker.readOperationalData(path);
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 31;
79         int result = 1;
80         result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
81         return result;
82     }
83
84     @Override
85     public boolean equals(Object obj) {
86         if (this == obj)
87             return true;
88         if (obj == null)
89             return false;
90         if (getClass() != obj.getClass())
91             return false;
92         AbstractDataTransaction<?, ?> other = (AbstractDataTransaction<?, ?>) obj;
93         if (identifier == null) {
94             if (other.identifier != null)
95                 return false;
96         } else if (!identifier.equals(other.identifier))
97             return false;
98         return true;
99     }
100
101     @Override
102     public TransactionStatus getStatus() {
103         return this.status;
104     }
105
106     protected abstract void onStatusChange(final TransactionStatus status);
107
108     public void succeeded() {
109         this.completeTime = System.nanoTime();
110         LOG.debug("Transaction {} Committed after {}ms.", identifier, TimeUnit.NANOSECONDS.toMillis(completeTime - readyTime));
111         changeStatus(TransactionStatus.COMMITED);
112     }
113
114     public void failed() {
115         this.completeTime = System.nanoTime();
116         LOG.debug("Transaction {} Failed after {}ms.", identifier, TimeUnit.NANOSECONDS.toMillis(completeTime - readyTime));
117         changeStatus(TransactionStatus.FAILED);
118     }
119
120     private void changeStatus(final TransactionStatus status) {
121         LOG.debug("Transaction {} transitioned from {} to {}", getIdentifier(), this.status, status);
122         this.status = status;
123         this.onStatusChange(status);
124     }
125 }