Madan Jampani

Introduced a serializer interface and minimal kyro based implementation as a pre…

…cursor to moving netty messaging out to onos-utils
1 +package org.onlab.onos.store.messaging.impl;
2 +
3 +import org.onlab.util.KryoPool;
4 +import org.slf4j.Logger;
5 +import org.slf4j.LoggerFactory;
6 +
7 +import java.util.ArrayList;
8 +import java.util.HashMap;
9 +
10 +/**
11 + * Kryo Serializer.
12 + */
13 +public class KryoSerializer implements Serializer {
14 +
15 + private final Logger log = LoggerFactory.getLogger(getClass());
16 +
17 + private KryoPool serializerPool;
18 +
19 + public KryoSerializer() {
20 + setupKryoPool();
21 + }
22 +
23 + /**
24 + * Sets up the common serialzers pool.
25 + */
26 + protected void setupKryoPool() {
27 + // FIXME Slice out types used in common to separate pool/namespace.
28 + serializerPool = KryoPool.newBuilder()
29 + .register(ArrayList.class,
30 + HashMap.class,
31 + ArrayList.class
32 + )
33 + .build()
34 + .populate(1);
35 + }
36 +
37 +
38 + @Override
39 + public Object decode(byte[] data) {
40 + return serializerPool.deserialize(data);
41 + }
42 +
43 + @Override
44 + public byte[] encode(Object payload) {
45 + return serializerPool.serialize(payload);
46 + }
47 +}
...@@ -5,7 +5,6 @@ import java.util.List; ...@@ -5,7 +5,6 @@ import java.util.List;
5 5
6 import static com.google.common.base.Preconditions.checkState; 6 import static com.google.common.base.Preconditions.checkState;
7 7
8 -import org.onlab.onos.store.cluster.messaging.SerializationService;
9 import org.onlab.onos.store.messaging.Endpoint; 8 import org.onlab.onos.store.messaging.Endpoint;
10 9
11 import io.netty.buffer.ByteBuf; 10 import io.netty.buffer.ByteBuf;
...@@ -18,11 +17,11 @@ import io.netty.handler.codec.ByteToMessageDecoder; ...@@ -18,11 +17,11 @@ import io.netty.handler.codec.ByteToMessageDecoder;
18 public class MessageDecoder extends ByteToMessageDecoder { 17 public class MessageDecoder extends ByteToMessageDecoder {
19 18
20 private final NettyMessagingService messagingService; 19 private final NettyMessagingService messagingService;
21 - private final SerializationService serializationService; 20 + private final Serializer serializer;
22 21
23 - public MessageDecoder(NettyMessagingService messagingService, SerializationService serializationService) { 22 + public MessageDecoder(NettyMessagingService messagingService, Serializer serializer) {
24 this.messagingService = messagingService; 23 this.messagingService = messagingService;
25 - this.serializationService = serializationService; 24 + this.serializer = serializer;
26 } 25 }
27 26
28 @Override 27 @Override
...@@ -47,7 +46,7 @@ public class MessageDecoder extends ByteToMessageDecoder { ...@@ -47,7 +46,7 @@ public class MessageDecoder extends ByteToMessageDecoder {
47 Endpoint sender = new Endpoint(host, port); 46 Endpoint sender = new Endpoint(host, port);
48 47
49 // read message payload; first read size and then bytes. 48 // read message payload; first read size and then bytes.
50 - Object payload = serializationService.decode(in.readBytes(in.readInt()).array()); 49 + Object payload = serializer.decode(in.readBytes(in.readInt()).array());
51 50
52 InternalMessage message = new InternalMessage.Builder(messagingService) 51 InternalMessage message = new InternalMessage.Builder(messagingService)
53 .withId(id) 52 .withId(id)
......
1 package org.onlab.onos.store.messaging.impl; 1 package org.onlab.onos.store.messaging.impl;
2 2
3 -import org.onlab.onos.store.cluster.messaging.SerializationService;
4 -
5 import io.netty.buffer.ByteBuf; 3 import io.netty.buffer.ByteBuf;
6 import io.netty.channel.ChannelHandlerContext; 4 import io.netty.channel.ChannelHandlerContext;
7 import io.netty.handler.codec.MessageToByteEncoder; 5 import io.netty.handler.codec.MessageToByteEncoder;
...@@ -14,10 +12,10 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> { ...@@ -14,10 +12,10 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> {
14 // onosiscool in ascii 12 // onosiscool in ascii
15 public static final byte[] PREAMBLE = "onosiscool".getBytes(); 13 public static final byte[] PREAMBLE = "onosiscool".getBytes();
16 14
17 - private final SerializationService serializationService; 15 + private final Serializer serializer;
18 16
19 - public MessageEncoder(SerializationService serializationService) { 17 + public MessageEncoder(Serializer serializer) {
20 - this.serializationService = serializationService; 18 + this.serializer = serializer;
21 } 19 }
22 20
23 @Override 21 @Override
...@@ -46,12 +44,12 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> { ...@@ -46,12 +44,12 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> {
46 out.writeInt(message.sender().port()); 44 out.writeInt(message.sender().port());
47 45
48 try { 46 try {
49 - serializationService.encode(message.payload()); 47 + serializer.encode(message.payload());
50 } catch (Exception e) { 48 } catch (Exception e) {
51 e.printStackTrace(); 49 e.printStackTrace();
52 } 50 }
53 51
54 - byte[] payload = serializationService.encode(message.payload()); 52 + byte[] payload = serializer.encode(message.payload());
55 53
56 // write payload length. 54 // write payload length.
57 out.writeInt(payload.length); 55 out.writeInt(payload.length);
......
...@@ -31,7 +31,6 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -31,7 +31,6 @@ import org.apache.felix.scr.annotations.Deactivate;
31 import org.apache.felix.scr.annotations.Reference; 31 import org.apache.felix.scr.annotations.Reference;
32 import org.apache.felix.scr.annotations.ReferenceCardinality; 32 import org.apache.felix.scr.annotations.ReferenceCardinality;
33 import org.apache.felix.scr.annotations.Service; 33 import org.apache.felix.scr.annotations.Service;
34 -import org.onlab.onos.store.cluster.messaging.SerializationService;
35 import org.onlab.onos.store.messaging.Endpoint; 34 import org.onlab.onos.store.messaging.Endpoint;
36 import org.onlab.onos.store.messaging.MessageHandler; 35 import org.onlab.onos.store.messaging.MessageHandler;
37 import org.onlab.onos.store.messaging.MessagingService; 36 import org.onlab.onos.store.messaging.MessagingService;
...@@ -61,7 +60,7 @@ public class NettyMessagingService implements MessagingService { ...@@ -61,7 +60,7 @@ public class NettyMessagingService implements MessagingService {
61 private final Endpoint localEp; 60 private final Endpoint localEp;
62 61
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 - protected SerializationService serializationService; 63 + protected Serializer serializer;
65 64
66 public NettyMessagingService() { 65 public NettyMessagingService() {
67 // TODO: Default port should be configurable. 66 // TODO: Default port should be configurable.
...@@ -213,8 +212,8 @@ public class NettyMessagingService implements MessagingService { ...@@ -213,8 +212,8 @@ public class NettyMessagingService implements MessagingService {
213 @Override 212 @Override
214 protected void initChannel(SocketChannel channel) throws Exception { 213 protected void initChannel(SocketChannel channel) throws Exception {
215 channel.pipeline() 214 channel.pipeline()
216 - .addLast(new MessageEncoder(serializationService)) 215 + .addLast(new MessageEncoder(serializer))
217 - .addLast(new MessageDecoder(NettyMessagingService.this, serializationService)) 216 + .addLast(new MessageDecoder(NettyMessagingService.this, serializer))
218 .addLast(new NettyMessagingService.InboundMessageDispatcher()); 217 .addLast(new NettyMessagingService.InboundMessageDispatcher());
219 } 218 }
220 } 219 }
......
1 +package org.onlab.onos.store.messaging.impl;
2 +
3 +/**
4 + * Interface for encoding/decoding message payloads.
5 + */
6 +public interface Serializer {
7 +
8 + /**
9 + * Decodes the specified byte array to a POJO.
10 + *
11 + * @param data byte array.
12 + * @return POJO
13 + */
14 + Object decode(byte[] data);
15 +
16 + /**
17 + * Encodes the specified POJO into a byte array.
18 + *
19 + * @param data POJO to be encoded
20 + * @return byte array.
21 + */
22 + byte[] encode(Object message);
23 +
24 +}
...@@ -2,7 +2,6 @@ package org.onlab.onos.store.messaging.impl; ...@@ -2,7 +2,6 @@ package org.onlab.onos.store.messaging.impl;
2 2
3 import java.util.concurrent.TimeUnit; 3 import java.util.concurrent.TimeUnit;
4 4
5 -import org.onlab.onos.store.cluster.impl.MessageSerializer;
6 import org.onlab.onos.store.messaging.Endpoint; 5 import org.onlab.onos.store.messaging.Endpoint;
7 import org.onlab.onos.store.messaging.Response; 6 import org.onlab.onos.store.messaging.Response;
8 7
...@@ -21,9 +20,8 @@ public final class SimpleClient { ...@@ -21,9 +20,8 @@ public final class SimpleClient {
21 public static class TestNettyMessagingService extends NettyMessagingService { 20 public static class TestNettyMessagingService extends NettyMessagingService {
22 public TestNettyMessagingService(int port) throws Exception { 21 public TestNettyMessagingService(int port) throws Exception {
23 super(port); 22 super(port);
24 - MessageSerializer mgr = new MessageSerializer(); 23 + Serializer serializer = new KryoSerializer();
25 - mgr.activate(); 24 + this.serializer = serializer;
26 - this.serializationService = mgr;
27 } 25 }
28 } 26 }
29 } 27 }
......
1 package org.onlab.onos.store.messaging.impl; 1 package org.onlab.onos.store.messaging.impl;
2 2
3 -import org.onlab.onos.store.cluster.impl.MessageSerializer;
4 -
5 public final class SimpleServer { 3 public final class SimpleServer {
6 private SimpleServer() {} 4 private SimpleServer() {}
7 5
...@@ -14,9 +12,8 @@ public final class SimpleServer { ...@@ -14,9 +12,8 @@ public final class SimpleServer {
14 12
15 public static class TestNettyMessagingService extends NettyMessagingService { 13 public static class TestNettyMessagingService extends NettyMessagingService {
16 protected TestNettyMessagingService() { 14 protected TestNettyMessagingService() {
17 - MessageSerializer mgr = new MessageSerializer(); 15 + Serializer serializer = new KryoSerializer();
18 - mgr.activate(); 16 + this.serializer = serializer;
19 - this.serializationService = mgr;
20 } 17 }
21 } 18 }
22 } 19 }
......