Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 17 | package org.onosproject.store.service; |
| 18 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 19 | import java.util.Arrays; |
| 20 | import java.util.List; |
| 21 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
| 23 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Lists; |
| 25 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 26 | /** |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 27 | * Interface for serialization of store artifacts. |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 28 | */ |
| 29 | public interface Serializer { |
| 30 | /** |
| 31 | * Serialize the specified object. |
| 32 | * @param object object to serialize. |
| 33 | * @return serialized bytes. |
| 34 | * @param <T> encoded type |
| 35 | */ |
| 36 | <T> byte[] encode(T object); |
| 37 | |
| 38 | /** |
| 39 | * Deserialize the specified bytes. |
| 40 | * @param bytes byte array to deserialize. |
| 41 | * @return deserialized object. |
| 42 | * @param <T> decoded type |
| 43 | */ |
| 44 | <T> T decode(byte[] bytes); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Creates a new Serializer instance from a KryoNamespace. |
| 48 | * |
| 49 | * @param kryo kryo namespace |
| 50 | * @return Serializer instance |
| 51 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 52 | static Serializer using(KryoNamespace kryo) { |
HIGUCHI Yuta | c299d07 | 2016-05-18 15:18:45 -0700 | [diff] [blame] | 53 | return new Serializer() { |
| 54 | |
| 55 | @Override |
| 56 | public <T> byte[] encode(T object) { |
| 57 | return kryo.serialize(object); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public <T> T decode(byte[] bytes) { |
| 62 | return kryo.deserialize(bytes); |
| 63 | } |
| 64 | }; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Madan Jampani | 17ed48b | 2015-12-09 10:56:35 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Creates a new Serializer instance from a KryoNamespace and some additional classes. |
| 69 | * |
| 70 | * @param namespace kryo namespace |
| 71 | * @param classes variable length array of classes to register |
| 72 | * @return Serializer instance |
| 73 | */ |
| 74 | static Serializer using(KryoNamespace namespace, Class<?>... classes) { |
| 75 | return using(Arrays.asList(namespace), classes); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates a new Serializer instance from a list of KryoNamespaces and some additional classes. |
| 80 | * |
| 81 | * @param namespaces kryo namespaces |
| 82 | * @param classes variable length array of classes to register |
| 83 | * @return Serializer instance |
| 84 | */ |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 85 | static Serializer using(List<KryoNamespace> namespaces, Class<?>... classes) { |
| 86 | KryoNamespace.Builder builder = new KryoNamespace.Builder(); |
| 87 | namespaces.forEach(builder::register); |
| 88 | Lists.newArrayList(classes).forEach(builder::register); |
Jon Hall | d46280c | 2015-07-01 10:30:59 -0700 | [diff] [blame] | 89 | builder.register(MapEvent.class, MapEvent.Type.class, Versioned.class); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 90 | KryoNamespace namespace = builder.build(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 91 | return new Serializer() { |
| 92 | @Override |
| 93 | public <T> byte[] encode(T object) { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 94 | return namespace.serialize(object); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public <T> T decode(byte[] bytes) { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 99 | return namespace.deserialize(bytes); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 100 | } |
| 101 | }; |
| 102 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 103 | |
| 104 | static Serializer forTypes(Class<?>... classes) { |
| 105 | return using(KryoNamespace.newBuilder() |
| 106 | .register(classes) |
| 107 | .register(MapEvent.class, MapEvent.Type.class) |
| 108 | .build()); |
| 109 | } |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 110 | } |