Merge "Initial commit of the akka based distributed-datastore"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / TransactionCommitFailedExceptionMapper.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.dom.broker.impl;
9
10 import java.util.concurrent.ExecutionException;
11
12 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
13
14 import com.google.common.base.Function;
15 import com.google.common.base.Preconditions;
16 import com.google.common.util.concurrent.Futures;
17
18 /**
19  *
20  * Utility exception mapper which translates {@link Exception}
21  * to {@link TransactionCommitFailedException}.
22  *
23  * This mapper is intended to be used with {@link Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
24  * <ul>
25  * <li>if exception is {@link TransactionCommitFailedException} or one of its subclasses returns original exception.
26  * <li>if exception is {@link ExecutionException} and cause is  {@link TransactionCommitFailedException} return cause
27  * <li>otherwise returns {@link TransactionCommitFailedException} with original exception as a cause.
28  * </ul>
29  *
30  */
31 final class TransactionCommitFailedExceptionMapper implements
32         Function<Exception, TransactionCommitFailedException> {
33
34     static final TransactionCommitFailedExceptionMapper PRE_COMMIT_MAPPER = create("canCommit");
35
36     static final TransactionCommitFailedExceptionMapper CAN_COMMIT_ERROR_MAPPER = create("preCommit");
37
38     static final TransactionCommitFailedExceptionMapper COMMIT_ERROR_MAPPER = create("commit");
39
40     private final String opName;
41
42     private TransactionCommitFailedExceptionMapper(final String opName) {
43         this.opName = Preconditions.checkNotNull(opName);
44     }
45
46     public static final TransactionCommitFailedExceptionMapper create(final String opName) {
47         return new TransactionCommitFailedExceptionMapper(opName);
48     }
49
50     @Override
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;
56         }
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();
62         }
63         if (e instanceof InterruptedException) {
64             return new TransactionCommitFailedException(opName + " failed - DOMStore was interupted.", e);
65         }
66         // Otherwise we are using new exception, with original cause
67         return new TransactionCommitFailedException(opName + " failed", e);
68     }
69 }