Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util.serializers; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 2 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 3 | import java.net.Inet4Address; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 4 | import java.net.InetAddress; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 5 | import java.util.ArrayList; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 6 | import java.util.Date; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 7 | import java.util.HashMap; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 8 | import java.util.HashSet; |
Yuta HIGUCHI | c25861a | 2014-06-17 23:17:09 -0700 | [diff] [blame] | 9 | import java.util.LinkedList; |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 10 | import java.util.List; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 11 | import java.util.concurrent.ConcurrentHashMap; |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 12 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 13 | |
| 14 | import javax.annotation.concurrent.ThreadSafe; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 15 | |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 16 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 17 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame] | 18 | import net.onrc.onos.api.batchoperation.BatchOperationEntry; |
Sho SHIMIZU | 1674fb3 | 2014-08-20 14:44:31 -0700 | [diff] [blame] | 19 | import net.onrc.onos.api.newintent.AbstractIntent; |
| 20 | import net.onrc.onos.api.newintent.ConnectivityIntent; |
| 21 | import net.onrc.onos.api.newintent.IntentEvent; |
| 22 | import net.onrc.onos.api.newintent.IntentId; |
Sho SHIMIZU | 07ace75 | 2014-08-21 09:04:41 -0700 | [diff] [blame] | 23 | import net.onrc.onos.api.newintent.IntentState; |
Sho SHIMIZU | 1674fb3 | 2014-08-20 14:44:31 -0700 | [diff] [blame] | 24 | import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent; |
| 25 | import net.onrc.onos.api.newintent.OpticalConnectivityIntent; |
| 26 | import net.onrc.onos.api.newintent.PacketConnectivityIntent; |
| 27 | import net.onrc.onos.api.newintent.PointToPointIntent; |
| 28 | import net.onrc.onos.api.newintent.SinglePointToMultiPointIntent; |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 29 | import net.onrc.onos.apps.proxyarp.ArpCacheNotification; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 30 | import net.onrc.onos.apps.proxyarp.ArpReplyNotification; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.hostmanager.Host; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.intent.ConstrainedShortestPathIntent; |
| 33 | import net.onrc.onos.core.intent.ErrorIntent; |
| 34 | import net.onrc.onos.core.intent.Intent; |
| 35 | import net.onrc.onos.core.intent.IntentOperation; |
| 36 | import net.onrc.onos.core.intent.IntentOperationList; |
Yuta HIGUCHI | 1fc395e | 2014-05-13 14:06:28 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.intent.Path; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.intent.PathIntent; |
| 39 | import net.onrc.onos.core.intent.ShortestPathIntent; |
| 40 | import net.onrc.onos.core.intent.runtime.IntentStateList; |
Sho SHIMIZU | 07ace75 | 2014-08-21 09:04:41 -0700 | [diff] [blame] | 41 | import net.onrc.onos.core.newintent.IntentCompilationResult; |
Sho SHIMIZU | 1674fb3 | 2014-08-20 14:44:31 -0700 | [diff] [blame] | 42 | import net.onrc.onos.core.newintent.PathFlowIntent; |
| 43 | import net.onrc.onos.core.newintent.SingleDstTreeFlowIntent; |
| 44 | import net.onrc.onos.core.newintent.SingleSrcTreeFlowIntent; |
Sho SHIMIZU | 95d53c8 | 2014-08-21 09:18:47 -0700 | [diff] [blame] | 45 | import net.onrc.onos.core.newintent.TestIntent; |
| 46 | import net.onrc.onos.core.newintent.TestSubclassIntent; |
Jonathan Hart | 313fdf0 | 2014-04-10 14:09:46 -0700 | [diff] [blame] | 47 | import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 48 | import net.onrc.onos.core.packetservice.PacketOutNotification; |
Jonathan Hart | 313fdf0 | 2014-04-10 14:09:46 -0700 | [diff] [blame] | 49 | import net.onrc.onos.core.packetservice.SinglePacketOutNotification; |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 50 | import net.onrc.onos.core.topology.AdminStatus; |
| 51 | import net.onrc.onos.core.topology.ConfigState; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 52 | import net.onrc.onos.core.topology.HostEvent; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 53 | import net.onrc.onos.core.topology.LinkEvent; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 54 | import net.onrc.onos.core.topology.MastershipEvent; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 55 | import net.onrc.onos.core.topology.PortEvent; |
| 56 | import net.onrc.onos.core.topology.SwitchEvent; |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame] | 57 | import net.onrc.onos.core.topology.TopologyBatchOperation; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 58 | import net.onrc.onos.core.topology.TopologyElement; |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 59 | import net.onrc.onos.core.topology.TopologyEvent; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 60 | import net.onrc.onos.core.util.Dpid; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 61 | import net.onrc.onos.core.util.IPv4; |
| 62 | import net.onrc.onos.core.util.IPv4Net; |
| 63 | import net.onrc.onos.core.util.IPv6; |
| 64 | import net.onrc.onos.core.util.IPv6Net; |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 65 | import net.onrc.onos.core.util.LinkTuple; |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 66 | import net.onrc.onos.core.util.OnosInstanceId; |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 67 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 68 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 69 | |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 70 | import com.esotericsoftware.kryo.Kryo; |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 71 | import com.esotericsoftware.kryo.io.Input; |
| 72 | import com.esotericsoftware.kryo.io.Output; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 73 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Class factory for allocating Kryo instances for |
| 76 | * serialization/deserialization of classes. |
| 77 | */ |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 78 | @ThreadSafe |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 79 | public class KryoFactory { |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 80 | |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 81 | private static final int DEFAULT_PREALLOCATIONS = 100; |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 82 | /** |
| 83 | * Default buffer size used for serialization. |
| 84 | * |
| 85 | * @see #serialize(Object) |
| 86 | */ |
| 87 | public static final int DEFAULT_BUFFER_SIZE = 1 * 1000 * 1000; |
| 88 | |
| 89 | private static final KryoFactory GLOBAL_POOL = new KryoFactory(); |
| 90 | |
| 91 | private ConcurrentLinkedQueue<Kryo> kryoList = new ConcurrentLinkedQueue<Kryo>(); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Default constructor. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | * <p/> |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 96 | * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 97 | */ |
| 98 | public KryoFactory() { |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 99 | this(DEFAULT_PREALLOCATIONS); |
| 100 | } |
| 101 | |
| 102 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 103 | * Constructor to explicitly specify number of Kryo instances to pool. |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 104 | * |
| 105 | * @param initialCapacity number of Kryo instance to preallocate |
| 106 | */ |
| 107 | public KryoFactory(final int initialCapacity) { |
| 108 | // Preallocate |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 109 | for (int i = 0; i < initialCapacity; i++) { |
Pavlin Radoslavov | 9c7b14c | 2014-07-03 17:24:27 -0700 | [diff] [blame] | 110 | Kryo kryo = newKryoObject(); |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 111 | kryoList.add(kryo); |
| 112 | } |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Pavlin Radoslavov | 9c7b14c | 2014-07-03 17:24:27 -0700 | [diff] [blame] | 116 | * Gets a new Kryo object. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 117 | * |
Pavlin Radoslavov | 9c7b14c | 2014-07-03 17:24:27 -0700 | [diff] [blame] | 118 | * @return the Kryo object. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 119 | */ |
| 120 | public Kryo newKryo() { |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 121 | Kryo kryo = kryoList.poll(); |
| 122 | if (kryo == null) { |
| 123 | // Will defer additional allocation until deleteKryo(). |
| 124 | // It is more likely that it is no longer latency sensitive |
| 125 | // by the time caller is recycling Kryo instance. |
| 126 | return newKryoObject(); |
| 127 | } else { |
| 128 | return kryo; |
| 129 | } |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Pavlin Radoslavov | 9c7b14c | 2014-07-03 17:24:27 -0700 | [diff] [blame] | 133 | * Deletes an existing Kryo object. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 134 | * |
| 135 | * @param deleteKryo the object to delete. |
| 136 | */ |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 137 | public void deleteKryo(final Kryo deleteKryo) { |
| 138 | if (kryoList.isEmpty()) { |
| 139 | // buffer extra if kryo instance pool has exhausted. |
| 140 | List<Kryo> kryos = new ArrayList<>(DEFAULT_PREALLOCATIONS); |
| 141 | for (int i = 0; i < DEFAULT_PREALLOCATIONS; ++i) { |
| 142 | kryos.add(newKryoObject()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | } |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 144 | kryoList.addAll(kryos); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | } |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 146 | |
| 147 | // recycle |
| 148 | kryoList.add(deleteKryo); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Pavlin Radoslavov | 9c7b14c | 2014-07-03 17:24:27 -0700 | [diff] [blame] | 152 | * Creates and initializes a new Kryo object. |
| 153 | *<p> |
| 154 | * NOTE: This operation can be slow and should be used only if the |
| 155 | * application needs a single Kryo instance (e.g., during startup). |
| 156 | * For faster allocation, the application should use #newKryo() |
| 157 | * and #deleteKryo() factory methods. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 158 | * |
| 159 | * @return the created Kryo object. |
| 160 | */ |
Pavlin Radoslavov | 9c7b14c | 2014-07-03 17:24:27 -0700 | [diff] [blame] | 161 | public static Kryo newKryoObject() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | Kryo kryo = new Kryo(); |
| 163 | kryo.setRegistrationRequired(true); |
| 164 | // |
| 165 | // WARNING: Order of register() calls affects serialized bytes. |
| 166 | // - Do no insert new entry in the middle, always add to the end. |
| 167 | // - Do not simply remove existing entry |
| 168 | // |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 169 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | // kryo.setReferences(false); |
| 171 | // |
| 172 | kryo.register(ArrayList.class); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 173 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | // FlowPath and related classes |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | kryo.register(Dpid.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | kryo.register(IPv4.class); |
| 177 | kryo.register(IPv4Net.class); |
| 178 | kryo.register(IPv6.class); |
| 179 | kryo.register(IPv6Net.class); |
| 180 | kryo.register(byte[].class); |
| 181 | kryo.register(MACAddress.class); |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 182 | kryo.register(PortNumber.class); |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 183 | kryo.register(SwitchPort.class); |
Yuta HIGUCHI | 02ccb8c | 2014-07-10 11:29:45 -0700 | [diff] [blame] | 184 | kryo.register(LinkTuple.class); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 185 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | // New data model-related classes |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 187 | kryo.register(AdminStatus.class); |
| 188 | kryo.register(ConcurrentHashMap.class); |
| 189 | kryo.register(ConfigState.class); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 190 | kryo.register(HostEvent.class); |
Yuta HIGUCHI | c25861a | 2014-06-17 23:17:09 -0700 | [diff] [blame] | 191 | kryo.register(LinkedList.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | kryo.register(LinkEvent.class); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 193 | kryo.register(MastershipEvent.class); |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 194 | kryo.register(OnosInstanceId.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | kryo.register(PortEvent.class); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 196 | kryo.register(Role.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | kryo.register(SwitchEvent.class); |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame] | 198 | kryo.register(TopologyBatchOperation.class); |
| 199 | kryo.register(TopologyBatchOperation.Operator.class); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 200 | kryo.register(TopologyElement.class); |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 201 | kryo.register(TopologyEvent.class); |
Pavlin Radoslavov | 31f8510 | 2014-08-15 13:55:44 -0700 | [diff] [blame] | 202 | kryo.register(TopologyEvent.Type.class); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 203 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 204 | // Intent-related classes |
| 205 | kryo.register(Path.class); |
| 206 | kryo.register(Intent.class); |
| 207 | kryo.register(Intent.IntentState.class); |
| 208 | kryo.register(PathIntent.class); |
| 209 | kryo.register(ShortestPathIntent.class); |
| 210 | kryo.register(ConstrainedShortestPathIntent.class); |
| 211 | kryo.register(ErrorIntent.class); |
| 212 | kryo.register(ErrorIntent.ErrorType.class); |
| 213 | kryo.register(IntentOperation.class); |
| 214 | kryo.register(IntentOperation.Operator.class); |
| 215 | kryo.register(IntentOperationList.class); |
| 216 | kryo.register(IntentStateList.class); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 217 | kryo.register(HashMap.class); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 218 | |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame] | 219 | // New intent-related classes |
| 220 | kryo.register(BatchOperationEntry.class); |
Sho SHIMIZU | 1674fb3 | 2014-08-20 14:44:31 -0700 | [diff] [blame] | 221 | kryo.register(IntentId.class); |
| 222 | kryo.register(IntentEvent.class); |
Sho SHIMIZU | 07ace75 | 2014-08-21 09:04:41 -0700 | [diff] [blame] | 223 | kryo.register(IntentState.class); |
| 224 | kryo.register(IntentCompilationResult.class); |
Sho SHIMIZU | 1674fb3 | 2014-08-20 14:44:31 -0700 | [diff] [blame] | 225 | kryo.register(AbstractIntent.class); |
| 226 | kryo.register(ConnectivityIntent.class); |
| 227 | kryo.register(PointToPointIntent.class); |
| 228 | kryo.register(MultiPointToSinglePointIntent.class); |
| 229 | kryo.register(SinglePointToMultiPointIntent.class); |
| 230 | kryo.register(net.onrc.onos.api.newintent.PathIntent.class); |
| 231 | kryo.register(PathFlowIntent.class); |
| 232 | kryo.register(SingleSrcTreeFlowIntent.class); |
| 233 | kryo.register(SingleDstTreeFlowIntent.class); |
| 234 | kryo.register(PacketConnectivityIntent.class); |
| 235 | kryo.register(OpticalConnectivityIntent.class); |
Sho SHIMIZU | 95d53c8 | 2014-08-21 09:18:47 -0700 | [diff] [blame] | 236 | // FIXME: due to lack of functionality to register a serializer |
| 237 | // in API user side, we added the following two classes. |
| 238 | // Theoretically the classes are only for test. we should create |
| 239 | // a way to register serializer without editing source code |
| 240 | kryo.register(TestIntent.class); |
| 241 | kryo.register(TestSubclassIntent.class); |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame] | 242 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 243 | // Host-related classes |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 244 | kryo.register(HashSet.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 245 | kryo.register(Host.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 246 | kryo.register(Date.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 247 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 248 | // ProxyArp-related classes |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 249 | kryo.register(PacketOutNotification.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 250 | kryo.register(BroadcastPacketOutNotification.class); |
| 251 | kryo.register(SinglePacketOutNotification.class); |
| 252 | kryo.register(ArpReplyNotification.class); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 253 | kryo.register(ArpCacheNotification.class); |
Yuta HIGUCHI | c25861a | 2014-06-17 23:17:09 -0700 | [diff] [blame] | 254 | // TODO check if InetAddress related is still used |
| 255 | // TODO check if InetAddress can be correctly serialized |
| 256 | kryo.register(InetAddress.class); |
| 257 | kryo.register(Inet4Address.class); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 258 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 259 | return kryo; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 260 | } |
Yuta HIGUCHI | e57e10e | 2014-08-20 14:25:30 -0700 | [diff] [blame^] | 261 | |
| 262 | /** |
| 263 | * Serializes given object to byte array using Kryo instance in global pool. |
| 264 | * <p> |
| 265 | * Note: Serialized bytes must be smaller than DEFAULT_BUFFER_SIZE (=1MB). |
| 266 | * |
| 267 | * @param obj Object to serialize (Class must be registered to KryoFactory) |
| 268 | * @return serialized bytes |
| 269 | */ |
| 270 | public static byte[] serialize(final Object obj) { |
| 271 | return serialize(obj, DEFAULT_BUFFER_SIZE); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Serializes given object to byte array using Kryo instance in global pool. |
| 276 | * |
| 277 | * @param obj Object to serialize (Class must be registered to KryoFactory) |
| 278 | * @param bufferSize maximum size of serialized bytes |
| 279 | * @return serialized bytes |
| 280 | */ |
| 281 | public static byte[] serialize(final Object obj, final int bufferSize) { |
| 282 | Output out = new Output(bufferSize); |
| 283 | Kryo kryo = GLOBAL_POOL.newKryo(); |
| 284 | try { |
| 285 | kryo.writeClassAndObject(out, obj); |
| 286 | return out.toBytes(); |
| 287 | } finally { |
| 288 | GLOBAL_POOL.deleteKryo(kryo); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Deserializes given byte array to Object using Kryo instance in global pool. |
| 294 | * |
| 295 | * @param bytes serialized bytes |
| 296 | * @param <T> deserialized Object type |
| 297 | * @return deserialized Object (Class must be registered to KryoFactory) |
| 298 | */ |
| 299 | public static <T> T deserialize(final byte[] bytes) { |
| 300 | Input in = new Input(bytes); |
| 301 | Kryo kryo = GLOBAL_POOL.newKryo(); |
| 302 | try { |
| 303 | @SuppressWarnings("unchecked") |
| 304 | T obj = (T) kryo.readClassAndObject(in); |
| 305 | return obj; |
| 306 | } finally { |
| 307 | GLOBAL_POOL.deleteKryo(kryo); |
| 308 | } |
| 309 | } |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 310 | } |