blob: c0fae2821fc7f4ef05cc27696d03a3c13b5069ad [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Madan Jampaniab6d3112014-10-02 16:30:14 -070019package org.onlab.netty;
20
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070021import org.onlab.util.KryoNamespace;
Madan Jampaniab6d3112014-10-02 16:30:14 -070022
Madan Jampani86ed0552014-10-03 16:45:42 -070023import java.nio.ByteBuffer;
Madan Jampaniab6d3112014-10-02 16:30:14 -070024import java.util.ArrayList;
25import java.util.HashMap;
26
Yuta HIGUCHIcdda17f2014-10-06 23:35:19 -070027//FIXME: Should be move out to test or app
Madan Jampaniab6d3112014-10-02 16:30:14 -070028/**
29 * Kryo Serializer.
30 */
Madan Jampani53e44e62014-10-07 12:39:51 -070031public class KryoSerializer {
Madan Jampaniab6d3112014-10-02 16:30:14 -070032
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070033 private KryoNamespace serializerPool;
Madan Jampaniab6d3112014-10-02 16:30:14 -070034
35 public KryoSerializer() {
36 setupKryoPool();
37 }
38
39 /**
40 * Sets up the common serialzers pool.
41 */
42 protected void setupKryoPool() {
43 // FIXME Slice out types used in common to separate pool/namespace.
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070044 serializerPool = KryoNamespace.newBuilder()
Madan Jampaniab6d3112014-10-02 16:30:14 -070045 .register(ArrayList.class,
46 HashMap.class,
Madan Jampani86ed0552014-10-03 16:45:42 -070047 ArrayList.class,
48 InternalMessage.class,
Madan Jampani53e44e62014-10-07 12:39:51 -070049 Endpoint.class,
50 byte[].class
Madan Jampaniab6d3112014-10-02 16:30:14 -070051 )
52 .build()
53 .populate(1);
54 }
55
56
Madan Jampani86ed0552014-10-03 16:45:42 -070057 public <T> T decode(byte[] data) {
Madan Jampaniab6d3112014-10-02 16:30:14 -070058 return serializerPool.deserialize(data);
59 }
60
Madan Jampaniab6d3112014-10-02 16:30:14 -070061 public byte[] encode(Object payload) {
62 return serializerPool.serialize(payload);
63 }
Madan Jampani86ed0552014-10-03 16:45:42 -070064
Yuta HIGUCHIcad78e92014-10-06 17:58:36 -070065 public <T> T decode(ByteBuffer buffer) {
Madan Jampani86ed0552014-10-03 16:45:42 -070066 return serializerPool.deserialize(buffer);
67 }
68
Yuta HIGUCHIcad78e92014-10-06 17:58:36 -070069 public void encode(Object obj, ByteBuffer buffer) {
Madan Jampani86ed0552014-10-03 16:45:42 -070070 serializerPool.serialize(obj, buffer);
71 }
Yuta HIGUCHI92626c02014-10-06 15:46:18 -070072}