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