2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.common.impl.service;
10 import java.util.concurrent.Future;
11 import java.util.concurrent.TimeUnit;
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;
20 import com.google.common.base.Preconditions;
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);
26 private final Object identifier;
27 private final long allocationTime;
28 private long readyTime = 0;
29 private long completeTime = 0;
31 private TransactionStatus status = TransactionStatus.NEW;
33 private final AbstractDataBroker<P, D, ? extends Object> broker;
35 protected AbstractDataTransaction(final Object identifier,
36 final AbstractDataBroker<P, D, ? extends Object> dataBroker) {
38 this.identifier = Preconditions.checkNotNull(identifier);
39 this.broker = Preconditions.checkNotNull(dataBroker);
40 this.allocationTime = System.nanoTime();
41 LOG.debug("Transaction {} Allocated.", identifier);
45 public Object getIdentifier() {
46 return this.identifier;
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);
55 return this.broker.commit(this);
59 public D readConfigurationData(final P path) {
60 final D local = getUpdatedConfigurationData().get(path);
64 return this.broker.readConfigurationData(path);
68 public D readOperationalData(final P path) {
69 final D local = this.getUpdatedOperationalData().get(path);
73 return this.broker.readOperationalData(path);
77 public int hashCode() {
80 result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
85 public boolean equals(Object obj) {
90 if (getClass() != obj.getClass())
92 AbstractDataTransaction<?, ?> other = (AbstractDataTransaction<?, ?>) obj;
93 if (identifier == null) {
94 if (other.identifier != null)
96 } else if (!identifier.equals(other.identifier))
102 public TransactionStatus getStatus() {
106 protected abstract void onStatusChange(final TransactionStatus status);
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);
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);
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);