blob: 435225cf00fe6561fd9b24b93dc03a81a8ea3c16 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Madan Jampaniab6d3112014-10-02 16:30:14 -070016package org.onlab.netty;
17
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070018import org.onlab.util.KryoNamespace;
Madan Jampaniab6d3112014-10-02 16:30:14 -070019
Madan Jampani86ed0552014-10-03 16:45:42 -070020import java.nio.ByteBuffer;
Madan Jampaniab6d3112014-10-02 16:30:14 -070021import java.util.ArrayList;
22import java.util.HashMap;
23
Yuta HIGUCHIcdda17f2014-10-06 23:35:19 -070024//FIXME: Should be move out to test or app
Madan Jampaniab6d3112014-10-02 16:30:14 -070025/**
26 * Kryo Serializer.
27 */
Madan Jampani53e44e62014-10-07 12:39:51 -070028public class KryoSerializer {
Madan Jampaniab6d3112014-10-02 16:30:14 -070029
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070030 private KryoNamespace serializerPool;
Madan Jampaniab6d3112014-10-02 16:30:14 -070031
32 public KryoSerializer() {
33 setupKryoPool();
34 }
35
36 /**
37 * Sets up the common serialzers pool.
38 */
39 protected void setupKryoPool() {
40 // FIXME Slice out types used in common to separate pool/namespace.
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070041 serializerPool = KryoNamespace.newBuilder()
Madan Jampaniab6d3112014-10-02 16:30:14 -070042 .register(ArrayList.class,
43 HashMap.class,
Madan Jampani86ed0552014-10-03 16:45:42 -070044 ArrayList.class,
45 InternalMessage.class,
Madan Jampani53e44e62014-10-07 12:39:51 -070046 Endpoint.class,
47 byte[].class
Madan Jampaniab6d3112014-10-02 16:30:14 -070048 )
49 .build()
50 .populate(1);
51 }
52
53
Madan Jampani86ed0552014-10-03 16:45:42 -070054 public <T> T decode(byte[] data) {
Madan Jampaniab6d3112014-10-02 16:30:14 -070055 return serializerPool.deserialize(data);
56 }
57
Madan Jampaniab6d3112014-10-02 16:30:14 -070058 public byte[] encode(Object payload) {
59 return serializerPool.serialize(payload);
60 }
Madan Jampani86ed0552014-10-03 16:45:42 -070061
Yuta HIGUCHIcad78e92014-10-06 17:58:36 -070062 public <T> T decode(ByteBuffer buffer) {
Madan Jampani86ed0552014-10-03 16:45:42 -070063 return serializerPool.deserialize(buffer);
64 }
65
Yuta HIGUCHIcad78e92014-10-06 17:58:36 -070066 public void encode(Object obj, ByteBuffer buffer) {
Madan Jampani86ed0552014-10-03 16:45:42 -070067 serializerPool.serialize(obj, buffer);
68 }
Yuta HIGUCHI92626c02014-10-06 15:46:18 -070069}