Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 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.primitives.impl; |
| 17 | |
| 18 | import java.net.ConnectException; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.concurrent.CompletionException; |
| 21 | |
| 22 | import io.atomix.protocols.raft.RaftException; |
| 23 | import io.atomix.protocols.raft.cluster.MemberId; |
| 24 | import org.onosproject.cluster.NodeId; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 25 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 26 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 27 | import org.onosproject.store.cluster.messaging.MessagingException; |
| 28 | import org.onosproject.store.service.Serializer; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Abstract base class for Raft protocol client/server. |
| 34 | */ |
| 35 | public abstract class RaftCommunicator { |
| 36 | protected final RaftMessageContext context; |
| 37 | protected final Serializer serializer; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 38 | protected final ClusterCommunicationService clusterCommunicator; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 39 | |
| 40 | public RaftCommunicator( |
| 41 | RaftMessageContext context, |
| 42 | Serializer serializer, |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 43 | ClusterCommunicationService clusterCommunicator) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 44 | this.context = checkNotNull(context, "context cannot be null"); |
| 45 | this.serializer = checkNotNull(serializer, "serializer cannot be null"); |
| 46 | this.clusterCommunicator = checkNotNull(clusterCommunicator, "clusterCommunicator cannot be null"); |
| 47 | } |
| 48 | |
| 49 | protected <T, U> CompletableFuture<U> sendAndReceive(MessageSubject subject, T request, MemberId memberId) { |
| 50 | CompletableFuture<U> future = new CompletableFuture<>(); |
| 51 | clusterCommunicator.<T, U>sendAndReceive( |
| 52 | request, subject, serializer::encode, serializer::decode, NodeId.nodeId(memberId.id())) |
| 53 | .whenComplete((result, error) -> { |
| 54 | if (error == null) { |
| 55 | future.complete(result); |
| 56 | } else { |
| 57 | if (error instanceof CompletionException) { |
| 58 | error = error.getCause(); |
| 59 | } |
| 60 | if (error instanceof MessagingException.NoRemoteHandler) { |
| 61 | error = new ConnectException(error.getMessage()); |
| 62 | } else if (error instanceof MessagingException.RemoteHandlerFailure |
| 63 | || error instanceof MessagingException.ProtocolException) { |
| 64 | error = new RaftException.ProtocolException(error.getMessage()); |
| 65 | } |
| 66 | future.completeExceptionally(error); |
| 67 | } |
| 68 | }); |
| 69 | return future; |
| 70 | } |
| 71 | } |