blob: f547a476ec0e180f3ebecb7a338a001c50ca3b05 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
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
Madan Jampani2bfa94c2015-04-11 05:03:49 -070018import java.util.Set;
19import java.util.concurrent.CompletableFuture;
Madan Jampaniec5ae342015-04-13 15:43:10 -070020import java.util.concurrent.Executor;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070021import java.util.concurrent.ExecutorService;
22import java.util.function.Consumer;
23import java.util.function.Function;
24
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cluster.NodeId;
Madan Jampani890bc352014-10-01 22:35:29 -070026
tom1d416c52014-09-29 20:55:24 -070027/**
28 * Service for assisting communications between controller cluster nodes.
29 */
30public interface ClusterCommunicationService {
31
32 /**
Madan Jampani2af244a2015-02-22 13:12:01 -080033 * 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 */
Madan Jampani2bfa94c2015-04-11 05:03:49 -070039 @Deprecated
Madan Jampani2af244a2015-02-22 13:12:01 -080040 void addSubscriber(MessageSubject subject, ClusterMessageHandler subscriber, ExecutorService executor);
41
42 /**
Madan Jampani2bfa94c2015-04-11 05:03:49 -070043 * Broadcasts a message to all controller nodes.
44 *
45 * @param message message to send
46 * @param subject message subject
47 * @param encoder function for encoding message to byte[]
48 * @param <M> message type
49 */
50 <M> void broadcast(M message,
51 MessageSubject subject,
52 Function<M, byte[]> encoder);
53
54 /**
55 * Broadcasts a message to all controller nodes including self.
56 *
57 * @param message message to send
58 * @param subject message subject
59 * @param encoder function for encoding message to byte[]
60 * @param <M> message type
61 */
62 <M> void broadcastIncludeSelf(M message,
63 MessageSubject subject,
64 Function<M, byte[]> encoder);
65
66 /**
67 * Sends a message to the specified controller node.
68 *
69 * @param message message to send
70 * @param subject message subject
71 * @param encoder function for encoding message to byte[]
72 * @param toNodeId destination node identifier
73 * @param <M> message type
Madan Jampani175e8fd2015-05-20 14:10:45 -070074 * @return future that is completed when the message is sent
Madan Jampani2bfa94c2015-04-11 05:03:49 -070075 */
Madan Jampani175e8fd2015-05-20 14:10:45 -070076 <M> CompletableFuture<Void> unicast(M message,
Madan Jampani2bfa94c2015-04-11 05:03:49 -070077 MessageSubject subject,
78 Function<M, byte[]> encoder,
79 NodeId toNodeId);
80
81 /**
82 * Multicasts a message to a set of controller nodes.
83 *
84 * @param message message to send
85 * @param subject message subject
86 * @param encoder function for encoding message to byte[]
87 * @param nodeIds recipient node identifiers
88 * @param <M> message type
89 */
90 <M> void multicast(M message,
91 MessageSubject subject,
92 Function<M, byte[]> encoder,
93 Set<NodeId> nodeIds);
94
95 /**
96 * Sends a message and expects a reply.
97 *
98 * @param message message to send
99 * @param subject message subject
100 * @param encoder function for encoding request to byte[]
101 * @param decoder function for decoding response from byte[]
102 * @param toNodeId recipient node identifier
103 * @param <M> request type
104 * @param <R> reply type
105 * @return reply future
106 */
107 <M, R> CompletableFuture<R> sendAndReceive(M message,
108 MessageSubject subject,
109 Function<M, byte[]> encoder,
110 Function<byte[], R> decoder,
111 NodeId toNodeId);
112
113 /**
114 * Adds a new subscriber for the specified message subject.
115 *
116 * @param subject message subject
117 * @param decoder decoder for resurrecting incoming message
Madan Jampani27b69c62015-05-15 15:49:02 -0700118 * @param handler handler function that processes the incoming message and produces a reply
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700119 * @param encoder encoder for serializing reply
120 * @param executor executor to run this handler on
121 * @param <M> incoming message type
122 * @param <R> reply message type
123 */
124 <M, R> void addSubscriber(MessageSubject subject,
125 Function<byte[], M> decoder,
126 Function<M, R> handler,
127 Function<R, byte[]> encoder,
Madan Jampaniec5ae342015-04-13 15:43:10 -0700128 Executor executor);
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700129
130 /**
131 * Adds a new subscriber for the specified message subject.
132 *
133 * @param subject message subject
Madan Jampani27b69c62015-05-15 15:49:02 -0700134 * @param decoder decoder for resurrecting incoming message
135 * @param handler handler function that processes the incoming message and produces a reply
136 * @param encoder encoder for serializing reply
137 * @param <M> incoming message type
138 * @param <R> reply message type
139 */
140 <M, R> void addSubscriber(MessageSubject subject,
141 Function<byte[], M> decoder,
142 Function<M, CompletableFuture<R>> handler,
143 Function<R, byte[]> encoder);
144
145 /**
146 * Adds a new subscriber for the specified message subject.
147 *
148 * @param subject message subject
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700149 * @param decoder decoder to resurrecting incoming message
150 * @param handler handler for handling message
151 * @param executor executor to run this handler on
152 * @param <M> incoming message type
153 */
154 <M> void addSubscriber(MessageSubject subject,
155 Function<byte[], M> decoder,
156 Consumer<M> handler,
Madan Jampaniec5ae342015-04-13 15:43:10 -0700157 Executor executor);
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700158
159 /**
Yuta HIGUCHI76b54bf2014-11-07 01:56:55 -0800160 * Removes a subscriber for the specified message subject.
161 *
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700162 * @param subject message subject
Yuta HIGUCHI76b54bf2014-11-07 01:56:55 -0800163 */
164 void removeSubscriber(MessageSubject subject);
tom1d416c52014-09-29 20:55:24 -0700165}