blob: 1117ab9ba290102df21235a4529a9a74a5288b29 [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman2bf177c2017-06-29 01:49:08 -07003 *
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 */
16package org.onosproject.store.primitives.impl;
17
18import java.net.ConnectException;
19import java.util.concurrent.CompletableFuture;
20import java.util.concurrent.CompletionException;
21
22import io.atomix.protocols.raft.RaftException;
23import io.atomix.protocols.raft.cluster.MemberId;
24import org.onosproject.cluster.NodeId;
Jordan Halterman28183ee2017-10-17 17:29:10 -070025import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070026import org.onosproject.store.cluster.messaging.MessageSubject;
27import org.onosproject.store.cluster.messaging.MessagingException;
28import org.onosproject.store.service.Serializer;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Abstract base class for Raft protocol client/server.
34 */
35public abstract class RaftCommunicator {
36 protected final RaftMessageContext context;
37 protected final Serializer serializer;
Jordan Halterman28183ee2017-10-17 17:29:10 -070038 protected final ClusterCommunicationService clusterCommunicator;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070039
40 public RaftCommunicator(
41 RaftMessageContext context,
42 Serializer serializer,
Jordan Halterman28183ee2017-10-17 17:29:10 -070043 ClusterCommunicationService clusterCommunicator) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070044 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}