blob: 7d2480e1e9c162c3835bbce51a607675161fb700 [file] [log] [blame]
Jordan Halterman980a8c12017-09-22 18:01:19 -07001/*
2 * Copyright 2017-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 */
16package org.onosproject.store.cluster.messaging;
17
18import java.util.Set;
19import java.util.concurrent.CompletableFuture;
20import java.util.concurrent.Executor;
21import java.util.concurrent.ExecutorService;
22import java.util.function.Consumer;
23import java.util.function.Function;
24
25import org.onosproject.cluster.NodeId;
26
27/**
28 * Service for assisting communications between controller cluster nodes.
29 */
30public interface ClusterCommunicator {
31
32 /**
33 * Adds a new subscriber for the specified message subject.
34 *
35 * @param subject message subject
36 * @param subscriber message subscriber
37 * @param executor executor to use for running handler.
38 * @deprecated in Cardinal Release
39 */
40 @Deprecated
41 void addSubscriber(MessageSubject subject, ClusterMessageHandler subscriber, ExecutorService executor);
42
43 /**
44 * Broadcasts a message to all controller nodes.
45 *
46 * @param message message to send
47 * @param subject message subject
48 * @param encoder function for encoding message to byte[]
49 * @param <M> message type
50 */
51 <M> void broadcast(M message,
52 MessageSubject subject,
53 Function<M, byte[]> encoder);
54
55 /**
56 * Broadcasts a message to all controller nodes including self.
57 *
58 * @param message message to send
59 * @param subject message subject
60 * @param encoder function for encoding message to byte[]
61 * @param <M> message type
62 */
63 <M> void broadcastIncludeSelf(M message,
64 MessageSubject subject,
65 Function<M, byte[]> encoder);
66
67 /**
68 * Sends a message to the specified controller node.
69 *
70 * @param message message to send
71 * @param subject message subject
72 * @param encoder function for encoding message to byte[]
73 * @param toNodeId destination node identifier
74 * @param <M> message type
75 * @return future that is completed when the message is sent
76 */
77 <M> CompletableFuture<Void> unicast(M message,
78 MessageSubject subject,
79 Function<M, byte[]> encoder,
80 NodeId toNodeId);
81
82 /**
83 * Multicasts a message to a set of controller nodes.
84 *
85 * @param message message to send
86 * @param subject message subject
87 * @param encoder function for encoding message to byte[]
88 * @param nodeIds recipient node identifiers
89 * @param <M> message type
90 */
91 <M> void multicast(M message,
92 MessageSubject subject,
93 Function<M, byte[]> encoder,
94 Set<NodeId> nodeIds);
95
96 /**
97 * Sends a message and expects a reply.
98 *
99 * @param message message to send
100 * @param subject message subject
101 * @param encoder function for encoding request to byte[]
102 * @param decoder function for decoding response from byte[]
103 * @param toNodeId recipient node identifier
104 * @param <M> request type
105 * @param <R> reply type
106 * @return reply future
107 */
108 <M, R> CompletableFuture<R> sendAndReceive(M message,
109 MessageSubject subject,
110 Function<M, byte[]> encoder,
111 Function<byte[], R> decoder,
112 NodeId toNodeId);
113
114 /**
115 * Adds a new subscriber for the specified message subject.
116 *
117 * @param subject message subject
118 * @param decoder decoder for resurrecting incoming message
119 * @param handler handler function that processes the incoming message and produces a reply
120 * @param encoder encoder for serializing reply
121 * @param executor executor to run this handler on
122 * @param <M> incoming message type
123 * @param <R> reply message type
124 */
125 <M, R> void addSubscriber(MessageSubject subject,
126 Function<byte[], M> decoder,
127 Function<M, R> handler,
128 Function<R, byte[]> encoder,
129 Executor executor);
130
131 /**
132 * Adds a new subscriber for the specified message subject.
133 *
134 * @param subject message subject
135 * @param decoder decoder for resurrecting incoming message
136 * @param handler handler function that processes the incoming message and produces a reply
137 * @param encoder encoder for serializing reply
138 * @param <M> incoming message type
139 * @param <R> reply message type
140 */
141 <M, R> void addSubscriber(MessageSubject subject,
142 Function<byte[], M> decoder,
143 Function<M, CompletableFuture<R>> handler,
144 Function<R, byte[]> encoder);
145
146 /**
147 * Adds a new subscriber for the specified message subject.
148 *
149 * @param subject message subject
150 * @param decoder decoder to resurrecting incoming message
151 * @param handler handler for handling message
152 * @param executor executor to run this handler on
153 * @param <M> incoming message type
154 */
155 <M> void addSubscriber(MessageSubject subject,
156 Function<byte[], M> decoder,
157 Consumer<M> handler,
158 Executor executor);
159
160 /**
161 * Removes a subscriber for the specified message subject.
162 *
163 * @param subject message subject
164 */
165 void removeSubscriber(MessageSubject subject);
166}