Jordan Halterman | 6cf60c3 | 2018-08-15 01:22:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.atomix.primitives.impl; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | |
| 20 | import com.google.common.base.Throwables; |
| 21 | import org.onosproject.store.service.ConsistentMapException; |
| 22 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 23 | import org.onosproject.store.service.NoSuchDocumentPathException; |
| 24 | import org.onosproject.store.service.StorageException; |
| 25 | |
| 26 | /** |
| 27 | * Utility class for adapting Atomix futures. |
| 28 | */ |
| 29 | final class AtomixFutures { |
| 30 | |
| 31 | /** |
| 32 | * Adapts the given Atomix future to ONOS. |
| 33 | * |
| 34 | * @param future the future to adapt |
| 35 | * @param <T> the future result type |
| 36 | * @return the adapted future |
| 37 | */ |
| 38 | static <T> CompletableFuture<T> adaptFuture(CompletableFuture<T> future) { |
| 39 | CompletableFuture<T> newFuture = new CompletableFuture<>(); |
| 40 | future.whenComplete((result, error) -> { |
| 41 | if (error == null) { |
| 42 | newFuture.complete(result); |
| 43 | } else { |
| 44 | Throwable cause = Throwables.getRootCause(error); |
| 45 | if (cause instanceof io.atomix.primitive.PrimitiveException.ConcurrentModification) { |
| 46 | newFuture.completeExceptionally(new StorageException.ConcurrentModification()); |
| 47 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Timeout) { |
| 48 | newFuture.completeExceptionally(new StorageException.Timeout()); |
| 49 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Interrupted) { |
| 50 | newFuture.completeExceptionally(new StorageException.Interrupted()); |
| 51 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Unavailable) { |
| 52 | newFuture.completeExceptionally(new StorageException.Unavailable()); |
| 53 | } else if (cause instanceof io.atomix.primitive.PrimitiveException) { |
| 54 | newFuture.completeExceptionally(new StorageException(cause.getMessage())); |
| 55 | } else { |
| 56 | newFuture.completeExceptionally(cause); |
| 57 | } |
| 58 | } |
| 59 | }); |
| 60 | return newFuture; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Adapts the given Atomix future to map exceptions. |
| 65 | * |
| 66 | * @param future the future to adapt |
| 67 | * @param <T> the future result type |
| 68 | * @return the adapted future |
| 69 | */ |
| 70 | static <T> CompletableFuture<T> adaptMapFuture(CompletableFuture<T> future) { |
| 71 | CompletableFuture<T> newFuture = new CompletableFuture<>(); |
| 72 | future.whenComplete((result, error) -> { |
| 73 | if (error == null) { |
| 74 | newFuture.complete(result); |
| 75 | } else { |
| 76 | Throwable cause = Throwables.getRootCause(error); |
| 77 | if (cause instanceof io.atomix.primitive.PrimitiveException.ConcurrentModification) { |
| 78 | newFuture.completeExceptionally( |
| 79 | new ConsistentMapException.ConcurrentModification(cause.getMessage())); |
| 80 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Timeout) { |
| 81 | newFuture.completeExceptionally(new ConsistentMapException.Timeout(cause.getMessage())); |
| 82 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Interrupted) { |
| 83 | newFuture.completeExceptionally(new ConsistentMapException.Interrupted()); |
| 84 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Unavailable) { |
| 85 | newFuture.completeExceptionally(new ConsistentMapException.Unavailable()); |
| 86 | } else if (cause instanceof io.atomix.primitive.PrimitiveException) { |
| 87 | newFuture.completeExceptionally(new ConsistentMapException(cause.getMessage())); |
| 88 | } else { |
| 89 | newFuture.completeExceptionally(cause); |
| 90 | } |
| 91 | } |
| 92 | }); |
| 93 | return newFuture; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Adapts the given Atomix future to document tree exceptions. |
| 98 | * |
| 99 | * @param future the future to adapt |
| 100 | * @param <T> the future result type |
| 101 | * @return the adapted future |
| 102 | */ |
| 103 | static <T> CompletableFuture<T> adaptTreeFuture(CompletableFuture<T> future) { |
| 104 | CompletableFuture<T> newFuture = new CompletableFuture<>(); |
| 105 | future.whenComplete((result, error) -> { |
| 106 | if (error == null) { |
| 107 | newFuture.complete(result); |
| 108 | } else { |
| 109 | Throwable cause = Throwables.getRootCause(error); |
| 110 | if (cause instanceof io.atomix.core.tree.NoSuchDocumentPathException) { |
| 111 | newFuture.completeExceptionally(new NoSuchDocumentPathException()); |
| 112 | } else if (cause instanceof io.atomix.core.tree.IllegalDocumentModificationException) { |
| 113 | newFuture.completeExceptionally(new IllegalDocumentModificationException()); |
| 114 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.ConcurrentModification) { |
| 115 | newFuture.completeExceptionally(new StorageException.ConcurrentModification()); |
| 116 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Timeout) { |
| 117 | newFuture.completeExceptionally(new StorageException.Timeout()); |
| 118 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Interrupted) { |
| 119 | newFuture.completeExceptionally(new StorageException.Interrupted()); |
| 120 | } else if (cause instanceof io.atomix.primitive.PrimitiveException.Unavailable) { |
| 121 | newFuture.completeExceptionally(new StorageException.Unavailable()); |
| 122 | } else if (cause instanceof io.atomix.primitive.PrimitiveException) { |
| 123 | newFuture.completeExceptionally(new StorageException(cause.getMessage())); |
| 124 | } else { |
| 125 | newFuture.completeExceptionally(cause); |
| 126 | } |
| 127 | } |
| 128 | }); |
| 129 | return newFuture; |
| 130 | } |
| 131 | |
| 132 | private AtomixFutures() { |
| 133 | } |
| 134 | } |