Remove SupressWarnings("all")
[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
12 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
13 import org.opendaylight.controller.md.sal.common.impl.AbstractDataModification;
14 import org.opendaylight.yangtools.concepts.Path;
15 import org.opendaylight.yangtools.yang.common.RpcResult;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public abstract class AbstractDataTransaction<P extends Path<P>, D extends Object> extends
20         AbstractDataModification<P, D> {
21     private final static Logger LOG = LoggerFactory.getLogger(AbstractDataTransaction.class);
22
23     private final Object identifier;
24
25     @Override
26     public Object getIdentifier() {
27         return this.identifier;
28     }
29
30     private TransactionStatus status;
31
32     private final AbstractDataBroker<P, D, ? extends Object> broker;
33
34     protected AbstractDataTransaction(final Object identifier,
35             final AbstractDataBroker<P, D, ? extends Object> dataBroker) {
36         super(dataBroker);
37         this.identifier = identifier;
38         this.broker = dataBroker;
39         this.status = TransactionStatus.NEW;
40         AbstractDataTransaction.LOG.debug("Transaction {} Allocated.", identifier);
41     }
42
43     @Override
44     public Future<RpcResult<TransactionStatus>> commit() {
45         return this.broker.commit(this);
46     }
47
48     @Override
49     public D readConfigurationData(final P path) {
50         final D local = getUpdatedConfigurationData().get(path);
51         if (local != null) {
52             return local;
53         }
54         return this.broker.readConfigurationData(path);
55     }
56
57     @Override
58     public D readOperationalData(final P path) {
59         final D local = this.getUpdatedOperationalData().get(path);
60         if (local != null) {
61             return local;
62         }
63         return this.broker.readOperationalData(path);
64     }
65
66
67
68     @Override
69     public int hashCode() {
70         final int prime = 31;
71         int result = 1;
72         result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
73         return result;
74     }
75
76     @Override
77     public boolean equals(Object obj) {
78         if (this == obj)
79             return true;
80         if (obj == null)
81             return false;
82         if (getClass() != obj.getClass())
83             return false;
84         AbstractDataTransaction<?, ?> other = (AbstractDataTransaction<?, ?>) obj;
85         if (identifier == null) {
86             if (other.identifier != null)
87                 return false;
88         } else if (!identifier.equals(other.identifier))
89             return false;
90         return true;
91     }
92
93     @Override
94     public TransactionStatus getStatus() {
95         return this.status;
96     }
97
98     protected abstract void onStatusChange(final TransactionStatus status);
99
100     public void changeStatus(final TransactionStatus status) {
101         Object _identifier = this.getIdentifier();
102         AbstractDataTransaction.LOG
103                 .debug("Transaction {} transitioned from {} to {}", _identifier, this.status, status);
104         this.status = status;
105         this.onStatusChange(status);
106     }
107 }