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.dom.broker.impl;
10 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
14 import com.google.common.base.Function;
15 import com.google.common.base.Preconditions;
19 * Utility exception mapper which translates {@link Exception}
20 * to {@link TransactionCommitFailedException}.
22 * This mapper is intended to be used with {@link com.google.common.util.concurrent.Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
24 * <li>if exception is {@link TransactionCommitFailedException} or one of its subclasses returns original exception.
25 * <li>if exception is {@link ExecutionException} and cause is {@link TransactionCommitFailedException} return cause
26 * <li>otherwise returns {@link TransactionCommitFailedException} with original exception as a cause.
31 final class TransactionCommitFailedExceptionMapper implements
32 Function<Exception, TransactionCommitFailedException> {
34 static final TransactionCommitFailedExceptionMapper PRE_COMMIT_MAPPER = create("canCommit");
36 static final TransactionCommitFailedExceptionMapper CAN_COMMIT_ERROR_MAPPER = create("preCommit");
38 static final TransactionCommitFailedExceptionMapper COMMIT_ERROR_MAPPER = create("commit");
40 private final String opName;
42 private TransactionCommitFailedExceptionMapper(final String opName) {
43 this.opName = Preconditions.checkNotNull(opName);
46 public static final TransactionCommitFailedExceptionMapper create(final String opName) {
47 return new TransactionCommitFailedExceptionMapper(opName);
51 public TransactionCommitFailedException apply(final Exception e) {
52 // If excetion is TransactionCommitFailedException
53 // we reuse it directly.
54 if (e instanceof TransactionCommitFailedException) {
55 return (TransactionCommitFailedException) e;
57 // If error is ExecutionException which was caused by cause of
58 // TransactionCommitFailedException
59 // we reuse original cause
60 if (e instanceof ExecutionException && e.getCause() instanceof TransactionCommitFailedException) {
61 return (TransactionCommitFailedException) e.getCause();
63 if (e instanceof InterruptedException) {
64 return new TransactionCommitFailedException(opName + " failed - DOMStore was interupted.", e);
66 // Otherwise we are using new exception, with original cause
67 return new TransactionCommitFailedException(opName + " failed", e);