blob: 0837a91c99e123a77c9d1f74999943719b3fc32d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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.serializers;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070017
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080018import java.io.InputStream;
19import java.io.OutputStream;
tomf5d85d42014-10-02 05:27:56 -070020import java.nio.ByteBuffer;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070021
Jonathan Hart4f60f982014-10-27 08:11:17 -070022import org.onlab.util.KryoNamespace;
23
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080024import com.google.common.base.MoreObjects;
25
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070026/**
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070027 * StoreSerializer implementation using Kryo.
HIGUCHI Yutae7290652016-05-18 11:29:01 -070028 *
29 * @deprecated in Goldeneye (1.6.0)
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070030 */
HIGUCHI Yutae7290652016-05-18 11:29:01 -070031@Deprecated
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070032public class KryoSerializer implements StoreSerializer {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070033
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070034 protected KryoNamespace serializerPool;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070035
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070036 public KryoSerializer() {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070037 setupKryoPool();
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070038 }
39
40 /**
Jonathan Hart4f60f982014-10-27 08:11:17 -070041 * Sets up the common serializers pool.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070042 */
43 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070044 serializerPool = KryoNamespace.newBuilder()
45 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080046 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
47 .build();
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070048 }
49
50 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070051 public byte[] encode(final Object obj) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070052 return serializerPool.serialize(obj);
53 }
54
55 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070056 public <T> T decode(final byte[] bytes) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070057 if (bytes == null) {
58 return null;
59 }
60 return serializerPool.deserialize(bytes);
61 }
62
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070063 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070064 public void encode(Object obj, ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070065 serializerPool.serialize(obj, buffer);
66 }
67
68 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070069 public <T> T decode(ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070070 return serializerPool.deserialize(buffer);
71 }
72
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080073 @Override
74 public void encode(Object obj, OutputStream stream) {
75 serializerPool.serialize(obj, stream);
76 }
77
78 @Override
79 public <T> T decode(InputStream stream) {
80 return serializerPool.deserialize(stream);
81 }
82
83 @Override
Madan Jampanidc012972016-04-25 11:13:26 -070084 public <T> T copy(T object) {
85 return decode(encode(object));
86 }
87
88 @Override
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080089 public String toString() {
90 return MoreObjects.toStringHelper(getClass())
91 .add("serializerPool", serializerPool)
92 .toString();
93 }
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070094}