blob: e8cf7d32806a4623f17c05a2adf1c656ec30fdc3 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.cluster.messaging;
tom1d416c52014-09-29 20:55:24 -070017
Jordan Halterman1cf4f842019-05-21 11:27:50 -070018import java.time.Duration;
Jordan Halterman28183ee2017-10-17 17:29:10 -070019import java.util.Set;
20import java.util.concurrent.CompletableFuture;
21import java.util.concurrent.Executor;
22import java.util.concurrent.ExecutorService;
23import java.util.function.Consumer;
24import java.util.function.Function;
25
26import org.onosproject.cluster.NodeId;
27
tom1d416c52014-09-29 20:55:24 -070028/**
29 * Service for assisting communications between controller cluster nodes.
30 */
Jordan Halterman28183ee2017-10-17 17:29:10 -070031public interface ClusterCommunicationService {
32
33 /**
34 * Adds a new subscriber for the specified message subject.
35 *
36 * @param subject message subject
37 * @param subscriber message subscriber
38 * @param executor executor to use for running handler.
39 * @deprecated in Cardinal Release
40 */
41 @Deprecated
42 void addSubscriber(MessageSubject subject, ClusterMessageHandler subscriber, ExecutorService executor);
43
44 /**
45 * Broadcasts a message to all controller nodes.
46 *
47 * @param message message to send
48 * @param subject message subject
49 * @param encoder function for encoding message to byte[]
50 * @param <M> message type
51 */
52 <M> void broadcast(M message,
53 MessageSubject subject,
54 Function<M, byte[]> encoder);
55
56 /**
57 * Broadcasts a message to all controller nodes including self.
58 *
59 * @param message message to send
60 * @param subject message subject
61 * @param encoder function for encoding message to byte[]
62 * @param <M> message type
63 */
64 <M> void broadcastIncludeSelf(M message,
65 MessageSubject subject,
66 Function<M, byte[]> encoder);
67
68 /**
69 * Sends a message to the specified controller node.
70 *
71 * @param message message to send
72 * @param subject message subject
73 * @param encoder function for encoding message to byte[]
74 * @param toNodeId destination node identifier
75 * @param <M> message type
76 * @return future that is completed when the message is sent
77 */
78 <M> CompletableFuture<Void> unicast(M message,
79 MessageSubject subject,
80 Function<M, byte[]> encoder,
81 NodeId toNodeId);
82
83 /**
84 * Multicasts a message to a set of controller nodes.
85 *
86 * @param message message to send
87 * @param subject message subject
88 * @param encoder function for encoding message to byte[]
89 * @param nodeIds recipient node identifiers
90 * @param <M> message type
91 */
92 <M> void multicast(M message,
93 MessageSubject subject,
94 Function<M, byte[]> encoder,
95 Set<NodeId> nodeIds);
96
97 /**
98 * Sends a message and expects a reply.
99 *
100 * @param message message to send
101 * @param subject message subject
102 * @param encoder function for encoding request to byte[]
103 * @param decoder function for decoding response from byte[]
104 * @param toNodeId recipient node identifier
105 * @param <M> request type
106 * @param <R> reply type
107 * @return reply future
108 */
Jordan Halterman1cf4f842019-05-21 11:27:50 -0700109 default <M, R> CompletableFuture<R> sendAndReceive(M message,
110 MessageSubject subject,
111 Function<M, byte[]> encoder,
112 Function<byte[], R> decoder,
113 NodeId toNodeId) {
pier3daed982020-02-19 20:44:23 +0100114 return sendAndReceive(message, subject, encoder, decoder, toNodeId, null);
Jordan Halterman1cf4f842019-05-21 11:27:50 -0700115 }
116
117 /**
118 * Sends a message and expects a reply.
119 *
120 * @param message message to send
121 * @param subject message subject
122 * @param encoder function for encoding request to byte[]
123 * @param decoder function for decoding response from byte[]
124 * @param toNodeId recipient node identifier
125 * @param timeout the message timeout
126 * @param <M> request type
127 * @param <R> reply type
128 * @return reply future
129 */
Jordan Halterman28183ee2017-10-17 17:29:10 -0700130 <M, R> CompletableFuture<R> sendAndReceive(M message,
131 MessageSubject subject,
132 Function<M, byte[]> encoder,
133 Function<byte[], R> decoder,
Jordan Halterman1cf4f842019-05-21 11:27:50 -0700134 NodeId toNodeId,
135 Duration timeout);
Jordan Halterman28183ee2017-10-17 17:29:10 -0700136
137 /**
138 * Adds a new subscriber for the specified message subject.
139 *
140 * @param subject message subject
141 * @param decoder decoder for resurrecting incoming message
142 * @param handler handler function that processes the incoming message and produces a reply
143 * @param encoder encoder for serializing reply
144 * @param executor executor to run this handler on
145 * @param <M> incoming message type
146 * @param <R> reply message type
147 */
148 <M, R> void addSubscriber(MessageSubject subject,
149 Function<byte[], M> decoder,
150 Function<M, R> handler,
151 Function<R, byte[]> encoder,
152 Executor executor);
153
154 /**
155 * Adds a new subscriber for the specified message subject.
156 *
157 * @param subject message subject
158 * @param decoder decoder for resurrecting incoming message
159 * @param handler handler function that processes the incoming message and produces a reply
160 * @param encoder encoder for serializing reply
161 * @param <M> incoming message type
162 * @param <R> reply message type
163 */
164 <M, R> void addSubscriber(MessageSubject subject,
165 Function<byte[], M> decoder,
166 Function<M, CompletableFuture<R>> handler,
167 Function<R, byte[]> encoder);
168
169 /**
170 * Adds a new subscriber for the specified message subject.
171 *
172 * @param subject message subject
173 * @param decoder decoder to resurrecting incoming message
174 * @param handler handler for handling message
175 * @param executor executor to run this handler on
176 * @param <M> incoming message type
177 */
178 <M> void addSubscriber(MessageSubject subject,
179 Function<byte[], M> decoder,
180 Consumer<M> handler,
181 Executor executor);
182
183 /**
184 * Removes a subscriber for the specified message subject.
185 *
186 * @param subject message subject
187 */
188 void removeSubscriber(MessageSubject subject);
tom1d416c52014-09-29 20:55:24 -0700189}