blob: 1e47a00532e93000a4f9bc548938b86ecbda066a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Madan Jampani890bc352014-10-01 22:35:29 -070016package org.onlab.onos.store.cluster.messaging.impl;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.io.IOException;
Madan Jampani890bc352014-10-01 22:35:29 -070021import java.util.Set;
Madan Jampani24f9efb2014-10-24 18:56:23 -070022
Madan Jampani890bc352014-10-01 22:35:29 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Madan Jampania5d0d782014-10-07 14:36:00 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Madan Jampani890bc352014-10-01 22:35:29 -070028import org.apache.felix.scr.annotations.Service;
Madan Jampania5d0d782014-10-07 14:36:00 -070029import org.onlab.onos.cluster.ClusterService;
Madan Jampani890bc352014-10-01 22:35:29 -070030import org.onlab.onos.cluster.ControllerNode;
31import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHIc057c632014-10-06 18:38:14 -070032import org.onlab.onos.store.cluster.impl.ClusterMembershipEvent;
Madan Jampani890bc352014-10-01 22:35:29 -070033import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
34import org.onlab.onos.store.cluster.messaging.ClusterMessage;
35import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler;
36import org.onlab.onos.store.cluster.messaging.MessageSubject;
Madan Jampanie0ec3292014-10-20 16:10:51 -070037import org.onlab.onos.store.serializers.ClusterMessageSerializer;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070038import org.onlab.onos.store.serializers.KryoNamespaces;
Madan Jampani53e44e62014-10-07 12:39:51 -070039import org.onlab.onos.store.serializers.KryoSerializer;
Madan Jampanie0ec3292014-10-20 16:10:51 -070040import org.onlab.onos.store.serializers.MessageSubjectSerializer;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070041import org.onlab.util.KryoNamespace;
Madan Jampanic9ed9be2014-10-02 16:13:11 -070042import org.onlab.netty.Endpoint;
43import org.onlab.netty.Message;
44import org.onlab.netty.MessageHandler;
45import org.onlab.netty.MessagingService;
Madan Jampanida1a6b02014-10-07 14:16:15 -070046import org.onlab.netty.NettyMessagingService;
Madan Jampani890bc352014-10-01 22:35:29 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
Madan Jampani24f9efb2014-10-24 18:56:23 -070050import com.google.common.util.concurrent.ListenableFuture;
51
Madan Jampani890bc352014-10-01 22:35:29 -070052@Component(immediate = true)
53@Service
Madan Jampani3b0dfd52014-10-02 16:48:13 -070054public class ClusterCommunicationManager
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070055 implements ClusterCommunicationService {
Madan Jampani890bc352014-10-01 22:35:29 -070056
57 private final Logger log = LoggerFactory.getLogger(getClass());
58
Madan Jampania5d0d782014-10-07 14:36:00 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 private ClusterService clusterService;
61
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070062 // TODO: This probably should not be a OSGi service.
Madan Jampani890bc352014-10-01 22:35:29 -070063 private MessagingService messagingService;
64
Madan Jampani53e44e62014-10-07 12:39:51 -070065 private static final KryoSerializer SERIALIZER = new KryoSerializer() {
Yuta HIGUCHI7a8d4aa2014-10-07 17:37:11 -070066 @Override
Madan Jampani53e44e62014-10-07 12:39:51 -070067 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070068 serializerPool = KryoNamespace.newBuilder()
69 .register(KryoNamespaces.API)
Yuta HIGUCHI7a8d4aa2014-10-07 17:37:11 -070070 .register(ClusterMessage.class, new ClusterMessageSerializer())
Madan Jampani53e44e62014-10-07 12:39:51 -070071 .register(ClusterMembershipEvent.class)
Yuta HIGUCHI7a8d4aa2014-10-07 17:37:11 -070072 .register(byte[].class)
Yuta HIGUCHI3e0b6512014-10-07 17:43:56 -070073 .register(MessageSubject.class, new MessageSubjectSerializer())
Madan Jampani53e44e62014-10-07 12:39:51 -070074 .build()
75 .populate(1);
76 }
77
78 };
79
Madan Jampani890bc352014-10-01 22:35:29 -070080 @Activate
81 public void activate() {
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070082 ControllerNode localNode = clusterService.getLocalNode();
Madan Jampani87100932014-10-21 16:46:12 -070083 NettyMessagingService netty = new NettyMessagingService(localNode.ip().toString(), localNode.tcpPort());
Yuta HIGUCHI7a8d4aa2014-10-07 17:37:11 -070084 // FIXME: workaround until it becomes a service.
85 try {
86 netty.activate();
87 } catch (Exception e) {
Yuta HIGUCHI7a8d4aa2014-10-07 17:37:11 -070088 log.error("NettyMessagingService#activate", e);
89 }
90 messagingService = netty;
Yuta HIGUCHI1f8cd5f2014-11-04 23:48:55 -080091 log.info("Started on {}:{}", localNode.ip(), localNode.tcpPort());
Madan Jampani890bc352014-10-01 22:35:29 -070092 }
93
94 @Deactivate
95 public void deactivate() {
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070096 // TODO: cleanup messageingService if needed.
Yuta HIGUCHI6c6fb9c2014-10-31 20:21:15 -070097 // FIXME: workaround until it becomes a service.
98 try {
99 ((NettyMessagingService) messagingService).deactivate();
100 } catch (Exception e) {
101 log.error("NettyMessagingService#deactivate", e);
102 }
Madan Jampani890bc352014-10-01 22:35:29 -0700103 log.info("Stopped");
104 }
105
106 @Override
Yuta HIGUCHI9cff53b2014-10-13 15:21:16 -0700107 public boolean broadcast(ClusterMessage message) throws IOException {
Madan Jampani890bc352014-10-01 22:35:29 -0700108 boolean ok = true;
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -0700109 final ControllerNode localNode = clusterService.getLocalNode();
Yuta HIGUCHI7a8d4aa2014-10-07 17:37:11 -0700110 for (ControllerNode node : clusterService.getNodes()) {
Madan Jampani890bc352014-10-01 22:35:29 -0700111 if (!node.equals(localNode)) {
112 ok = unicast(message, node.id()) && ok;
113 }
114 }
115 return ok;
116 }
117
118 @Override
Yuta HIGUCHI9cff53b2014-10-13 15:21:16 -0700119 public boolean multicast(ClusterMessage message, Set<NodeId> nodes) throws IOException {
Madan Jampani890bc352014-10-01 22:35:29 -0700120 boolean ok = true;
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -0700121 final ControllerNode localNode = clusterService.getLocalNode();
Madan Jampani890bc352014-10-01 22:35:29 -0700122 for (NodeId nodeId : nodes) {
123 if (!nodeId.equals(localNode.id())) {
Madan Jampani98c17602014-10-23 15:33:23 -0700124 ok = unicastUnchecked(message, nodeId) && ok;
Madan Jampani890bc352014-10-01 22:35:29 -0700125 }
126 }
127 return ok;
128 }
129
130 @Override
Yuta HIGUCHI9cff53b2014-10-13 15:21:16 -0700131 public boolean unicast(ClusterMessage message, NodeId toNodeId) throws IOException {
Yuta HIGUCHI7a8d4aa2014-10-07 17:37:11 -0700132 ControllerNode node = clusterService.getNode(toNodeId);
Madan Jampani890bc352014-10-01 22:35:29 -0700133 checkArgument(node != null, "Unknown nodeId: %s", toNodeId);
134 Endpoint nodeEp = new Endpoint(node.ip().toString(), node.tcpPort());
135 try {
Yuta HIGUCHI053b7d72014-10-07 23:03:20 -0700136 messagingService.sendAsync(nodeEp,
Yuta HIGUCHI68d90472014-10-07 17:55:50 -0700137 message.subject().value(), SERIALIZER.encode(message));
Madan Jampani890bc352014-10-01 22:35:29 -0700138 return true;
Yuta HIGUCHI053b7d72014-10-07 23:03:20 -0700139 } catch (IOException e) {
Yuta HIGUCHIa58451f2014-10-16 15:44:59 -0700140 log.trace("Failed to send cluster message to nodeId: " + toNodeId, e);
Yuta HIGUCHI9cff53b2014-10-13 15:21:16 -0700141 throw e;
Madan Jampani890bc352014-10-01 22:35:29 -0700142 }
Madan Jampani890bc352014-10-01 22:35:29 -0700143 }
144
Madan Jampani98c17602014-10-23 15:33:23 -0700145 private boolean unicastUnchecked(ClusterMessage message, NodeId toNodeId) throws IOException {
146 try {
147 return unicast(message, toNodeId);
148 } catch (IOException e) {
149 return false;
150 }
151 }
152
Madan Jampani890bc352014-10-01 22:35:29 -0700153 @Override
Madan Jampani24f9efb2014-10-24 18:56:23 -0700154 public ListenableFuture<byte[]> sendAndReceive(ClusterMessage message, NodeId toNodeId) throws IOException {
Madan Jampani4a9cb6d2014-10-17 10:48:50 -0700155 ControllerNode node = clusterService.getNode(toNodeId);
156 checkArgument(node != null, "Unknown nodeId: %s", toNodeId);
157 Endpoint nodeEp = new Endpoint(node.ip().toString(), node.tcpPort());
158 try {
Madan Jampani24f9efb2014-10-24 18:56:23 -0700159 return messagingService.sendAndReceive(nodeEp, message.subject().value(), SERIALIZER.encode(message));
Madan Jampani4a9cb6d2014-10-17 10:48:50 -0700160
161 } catch (IOException e) {
Yuta HIGUCHI39ae5502014-11-05 16:42:12 -0800162 log.trace("Failed interaction with remote nodeId: " + toNodeId, e);
Madan Jampani4a9cb6d2014-10-17 10:48:50 -0700163 throw e;
164 }
165 }
166
167 @Override
Madan Jampani890bc352014-10-01 22:35:29 -0700168 public void addSubscriber(MessageSubject subject,
Yuta HIGUCHI76b54bf2014-11-07 01:56:55 -0800169 ClusterMessageHandler subscriber) {
Madan Jampani890bc352014-10-01 22:35:29 -0700170 messagingService.registerHandler(subject.value(), new InternalClusterMessageHandler(subscriber));
171 }
172
Yuta HIGUCHI76b54bf2014-11-07 01:56:55 -0800173 @Override
174 public void removeSubscriber(MessageSubject subject) {
175 messagingService.unregisterHandler(subject.value());
176 }
177
Yuta HIGUCHI053b7d72014-10-07 23:03:20 -0700178 private final class InternalClusterMessageHandler implements MessageHandler {
Madan Jampani890bc352014-10-01 22:35:29 -0700179
180 private final ClusterMessageHandler handler;
181
182 public InternalClusterMessageHandler(ClusterMessageHandler handler) {
183 this.handler = handler;
184 }
185
186 @Override
187 public void handle(Message message) {
Yuta HIGUCHI38bd1452014-10-07 17:37:25 -0700188 try {
Yuta HIGUCHI38bd1452014-10-07 17:37:25 -0700189 ClusterMessage clusterMessage = SERIALIZER.decode(message.payload());
Madan Jampani8a895092014-10-17 16:55:50 -0700190 handler.handle(new InternalClusterMessage(clusterMessage, message));
Yuta HIGUCHI38bd1452014-10-07 17:37:25 -0700191 } catch (Exception e) {
Yuta HIGUCHI053b7d72014-10-07 23:03:20 -0700192 log.error("Exception caught during ClusterMessageHandler", e);
193 throw e;
Yuta HIGUCHI38bd1452014-10-07 17:37:25 -0700194 }
Madan Jampani890bc352014-10-01 22:35:29 -0700195 }
196 }
Madan Jampani4a9cb6d2014-10-17 10:48:50 -0700197
Madan Jampani8a895092014-10-17 16:55:50 -0700198 public static final class InternalClusterMessage extends ClusterMessage {
199
200 private final Message rawMessage;
201
202 public InternalClusterMessage(ClusterMessage clusterMessage, Message rawMessage) {
203 super(clusterMessage.sender(), clusterMessage.subject(), clusterMessage.payload());
204 this.rawMessage = rawMessage;
205 }
206
207 @Override
208 public void respond(byte[] response) throws IOException {
209 rawMessage.respond(response);
210 }
211 }
Madan Jampani24f9efb2014-10-24 18:56:23 -0700212}