blob: 5fec5b203897f32dce97ab1f2e2f703cbbe204d8 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util.serializers;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07002
TeruUd1c5b652014-03-24 13:58:46 -07003import java.net.Inet4Address;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08004import java.net.InetAddress;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07005import java.util.ArrayList;
TeruU7feef8a2014-04-03 00:15:49 -07006import java.util.Date;
TeruUf9111652014-05-14 23:10:35 -07007import java.util.HashMap;
TeruUd1c5b652014-03-24 13:58:46 -07008import java.util.HashSet;
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -07009import java.util.LinkedList;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070010import java.util.List;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070011import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070012import java.util.concurrent.ConcurrentLinkedQueue;
13
14import javax.annotation.concurrent.ThreadSafe;
TeruU7feef8a2014-04-03 00:15:49 -070015
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070016import net.floodlightcontroller.core.IFloodlightProviderService.Role;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070017import net.floodlightcontroller.util.MACAddress;
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -070018import net.onrc.onos.api.batchoperation.BatchOperationEntry;
Toshio Koide2c67a2d2014-08-27 11:30:56 -070019import net.onrc.onos.api.flowmanager.FlowId;
20import net.onrc.onos.api.flowmanager.FlowLink;
21import net.onrc.onos.api.flowmanager.OpticalPathFlow;
22import net.onrc.onos.api.flowmanager.PacketPathFlow;
23import net.onrc.onos.api.flowmanager.SingleDstTreeFlow;
24import net.onrc.onos.api.flowmanager.Tree;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070025import net.onrc.onos.api.newintent.AbstractIntent;
26import net.onrc.onos.api.newintent.ConnectivityIntent;
27import net.onrc.onos.api.newintent.IntentEvent;
28import net.onrc.onos.api.newintent.IntentId;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070029import net.onrc.onos.api.newintent.IntentState;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070030import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent;
31import net.onrc.onos.api.newintent.OpticalConnectivityIntent;
32import net.onrc.onos.api.newintent.PacketConnectivityIntent;
33import net.onrc.onos.api.newintent.PointToPointIntent;
34import net.onrc.onos.api.newintent.SinglePointToMultiPointIntent;
TeruU3c049c42014-04-15 10:13:25 -070035import net.onrc.onos.apps.proxyarp.ArpCacheNotification;
Jonathan Hart0961fe82014-04-03 09:56:25 -070036import net.onrc.onos.apps.proxyarp.ArpReplyNotification;
Jonathan Hart03102132014-07-01 23:22:04 -070037import net.onrc.onos.core.hostmanager.Host;
Jonathan Hartaa380972014-04-03 10:24:46 -070038import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
39import net.onrc.onos.core.intent.ErrorIntent;
40import net.onrc.onos.core.intent.Intent;
41import net.onrc.onos.core.intent.IntentOperation;
42import net.onrc.onos.core.intent.IntentOperationList;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -070043import net.onrc.onos.core.intent.Path;
Jonathan Hartaa380972014-04-03 10:24:46 -070044import net.onrc.onos.core.intent.PathIntent;
45import net.onrc.onos.core.intent.ShortestPathIntent;
46import net.onrc.onos.core.intent.runtime.IntentStateList;
Toshio Koide2c67a2d2014-08-27 11:30:56 -070047import net.onrc.onos.core.matchaction.action.ModifyDstMacAction;
48import net.onrc.onos.core.matchaction.action.ModifySrcMacAction;
49import net.onrc.onos.core.matchaction.action.OutputAction;
50import net.onrc.onos.core.matchaction.match.PacketMatch;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070051import net.onrc.onos.core.newintent.IntentCompilationResult;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070052import net.onrc.onos.core.newintent.PathFlowIntent;
53import net.onrc.onos.core.newintent.SingleDstTreeFlowIntent;
54import net.onrc.onos.core.newintent.SingleSrcTreeFlowIntent;
Sho SHIMIZU95d53c82014-08-21 09:18:47 -070055import net.onrc.onos.core.newintent.TestIntent;
56import net.onrc.onos.core.newintent.TestSubclassIntent;
Jonathan Hart313fdf02014-04-10 14:09:46 -070057import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification;
Jonathan Harte6e63732014-04-16 14:29:49 -070058import net.onrc.onos.core.packetservice.PacketOutNotification;
Jonathan Hart313fdf02014-04-10 14:09:46 -070059import net.onrc.onos.core.packetservice.SinglePacketOutNotification;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070060import net.onrc.onos.core.topology.AdminStatus;
61import net.onrc.onos.core.topology.ConfigState;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070062import net.onrc.onos.core.topology.HostEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070063import net.onrc.onos.core.topology.LinkEvent;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070064import net.onrc.onos.core.topology.MastershipEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070065import net.onrc.onos.core.topology.PortEvent;
66import net.onrc.onos.core.topology.SwitchEvent;
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -070067import net.onrc.onos.core.topology.TopologyBatchOperation;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070068import net.onrc.onos.core.topology.TopologyElement;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070069import net.onrc.onos.core.topology.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070070import net.onrc.onos.core.util.Dpid;
Jonathan Hart23701d12014-04-03 10:45:48 -070071import net.onrc.onos.core.util.IPv4;
72import net.onrc.onos.core.util.IPv4Net;
73import net.onrc.onos.core.util.IPv6;
74import net.onrc.onos.core.util.IPv6Net;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070075import net.onrc.onos.core.util.LinkTuple;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070076import net.onrc.onos.core.util.OnosInstanceId;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070077import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070078import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart23701d12014-04-03 10:45:48 -070079
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080080import com.esotericsoftware.kryo.Kryo;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070081import com.esotericsoftware.kryo.io.Input;
82import com.esotericsoftware.kryo.io.Output;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070083
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070084/**
85 * Class factory for allocating Kryo instances for
86 * serialization/deserialization of classes.
87 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070088@ThreadSafe
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070089public class KryoFactory {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070090
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070091 private static final int DEFAULT_PREALLOCATIONS = 100;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070092 /**
93 * Default buffer size used for serialization.
94 *
95 * @see #serialize(Object)
96 */
97 public static final int DEFAULT_BUFFER_SIZE = 1 * 1000 * 1000;
98
99 private static final KryoFactory GLOBAL_POOL = new KryoFactory();
100
101 private ConcurrentLinkedQueue<Kryo> kryoList = new ConcurrentLinkedQueue<Kryo>();
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700102
103 /**
104 * Default constructor.
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 * <p/>
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700106 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700107 */
108 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700109 this(DEFAULT_PREALLOCATIONS);
110 }
111
112 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700113 * Constructor to explicitly specify number of Kryo instances to pool.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700114 *
115 * @param initialCapacity number of Kryo instance to preallocate
116 */
117 public KryoFactory(final int initialCapacity) {
118 // Preallocate
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700119 for (int i = 0; i < initialCapacity; i++) {
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700120 Kryo kryo = newKryoObject();
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700121 kryoList.add(kryo);
122 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700123 }
124
125 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700126 * Gets a new Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700127 *
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700128 * @return the Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700129 */
130 public Kryo newKryo() {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700131 Kryo kryo = kryoList.poll();
132 if (kryo == null) {
133 // Will defer additional allocation until deleteKryo().
134 // It is more likely that it is no longer latency sensitive
135 // by the time caller is recycling Kryo instance.
136 return newKryoObject();
137 } else {
138 return kryo;
139 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700140 }
141
142 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700143 * Deletes an existing Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700144 *
145 * @param deleteKryo the object to delete.
146 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700147 public void deleteKryo(final Kryo deleteKryo) {
148 if (kryoList.isEmpty()) {
149 // buffer extra if kryo instance pool has exhausted.
150 List<Kryo> kryos = new ArrayList<>(DEFAULT_PREALLOCATIONS);
151 for (int i = 0; i < DEFAULT_PREALLOCATIONS; ++i) {
152 kryos.add(newKryoObject());
Ray Milkey269ffb92014-04-03 14:43:30 -0700153 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700154 kryoList.addAll(kryos);
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700156
157 // recycle
158 kryoList.add(deleteKryo);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700159 }
160
161 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700162 * Creates and initializes a new Kryo object.
163 *<p>
164 * NOTE: This operation can be slow and should be used only if the
165 * application needs a single Kryo instance (e.g., during startup).
166 * For faster allocation, the application should use #newKryo()
167 * and #deleteKryo() factory methods.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700168 *
169 * @return the created Kryo object.
170 */
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700171 public static Kryo newKryoObject() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700172 Kryo kryo = new Kryo();
173 kryo.setRegistrationRequired(true);
174 //
175 // WARNING: Order of register() calls affects serialized bytes.
176 // - Do no insert new entry in the middle, always add to the end.
177 // - Do not simply remove existing entry
178 //
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700179
Ray Milkey269ffb92014-04-03 14:43:30 -0700180 // kryo.setReferences(false);
181 //
182 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700183
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 // FlowPath and related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700185 kryo.register(Dpid.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 kryo.register(IPv4.class);
187 kryo.register(IPv4Net.class);
188 kryo.register(IPv6.class);
189 kryo.register(IPv6Net.class);
190 kryo.register(byte[].class);
191 kryo.register(MACAddress.class);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700192 kryo.register(PortNumber.class);
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700193 kryo.register(SwitchPort.class);
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700194 kryo.register(LinkTuple.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700195
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 // New data model-related classes
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700197 kryo.register(AdminStatus.class);
198 kryo.register(ConcurrentHashMap.class);
199 kryo.register(ConfigState.class);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700200 kryo.register(HostEvent.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700201 kryo.register(LinkedList.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700202 kryo.register(LinkEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700203 kryo.register(MastershipEvent.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700204 kryo.register(OnosInstanceId.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700205 kryo.register(PortEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700206 kryo.register(Role.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 kryo.register(SwitchEvent.class);
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700208 kryo.register(TopologyBatchOperation.class);
209 kryo.register(TopologyBatchOperation.Operator.class);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700210 kryo.register(TopologyElement.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700211 kryo.register(TopologyEvent.class);
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700212 kryo.register(TopologyEvent.Type.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800213
Ray Milkey269ffb92014-04-03 14:43:30 -0700214 // Intent-related classes
215 kryo.register(Path.class);
216 kryo.register(Intent.class);
217 kryo.register(Intent.IntentState.class);
218 kryo.register(PathIntent.class);
219 kryo.register(ShortestPathIntent.class);
220 kryo.register(ConstrainedShortestPathIntent.class);
221 kryo.register(ErrorIntent.class);
222 kryo.register(ErrorIntent.ErrorType.class);
223 kryo.register(IntentOperation.class);
224 kryo.register(IntentOperation.Operator.class);
225 kryo.register(IntentOperationList.class);
226 kryo.register(IntentStateList.class);
TeruUf9111652014-05-14 23:10:35 -0700227 kryo.register(HashMap.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800228
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700229 // New intent-related classes
230 kryo.register(BatchOperationEntry.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700231 kryo.register(IntentId.class);
232 kryo.register(IntentEvent.class);
Sho SHIMIZU07ace752014-08-21 09:04:41 -0700233 kryo.register(IntentState.class);
234 kryo.register(IntentCompilationResult.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700235 kryo.register(AbstractIntent.class);
236 kryo.register(ConnectivityIntent.class);
237 kryo.register(PointToPointIntent.class);
238 kryo.register(MultiPointToSinglePointIntent.class);
239 kryo.register(SinglePointToMultiPointIntent.class);
240 kryo.register(net.onrc.onos.api.newintent.PathIntent.class);
241 kryo.register(PathFlowIntent.class);
242 kryo.register(SingleSrcTreeFlowIntent.class);
243 kryo.register(SingleDstTreeFlowIntent.class);
244 kryo.register(PacketConnectivityIntent.class);
245 kryo.register(OpticalConnectivityIntent.class);
Sho SHIMIZU95d53c82014-08-21 09:18:47 -0700246 // FIXME: due to lack of functionality to register a serializer
247 // in API user side, we added the following two classes.
248 // Theoretically the classes are only for test. we should create
249 // a way to register serializer without editing source code
250 kryo.register(TestIntent.class);
251 kryo.register(TestSubclassIntent.class);
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700252
Toshio Koide2c67a2d2014-08-27 11:30:56 -0700253 // New flow manager related classes
254 kryo.register(FlowId.class);
255 kryo.register(net.onrc.onos.api.flowmanager.Path.class);
256 kryo.register(Tree.class);
257 kryo.register(FlowLink.class);
258 kryo.register(OpticalPathFlow.class);
259 kryo.register(PacketPathFlow.class);
260 kryo.register(SingleDstTreeFlow.class);
261
262 // New match action related classes
263 kryo.register(PacketMatch.class);
264 kryo.register(OutputAction.class);
265 kryo.register(ModifyDstMacAction.class);
266 kryo.register(ModifySrcMacAction.class);
267
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700268 // Host-related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700269 kryo.register(HashSet.class);
Jonathan Hart03102132014-07-01 23:22:04 -0700270 kryo.register(Host.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700271 kryo.register(Date.class);
TeruU7feef8a2014-04-03 00:15:49 -0700272
Ray Milkey269ffb92014-04-03 14:43:30 -0700273 // ProxyArp-related classes
Jonathan Harte6e63732014-04-16 14:29:49 -0700274 kryo.register(PacketOutNotification.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700275 kryo.register(BroadcastPacketOutNotification.class);
276 kryo.register(SinglePacketOutNotification.class);
277 kryo.register(ArpReplyNotification.class);
TeruU3c049c42014-04-15 10:13:25 -0700278 kryo.register(ArpCacheNotification.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700279 // TODO check if InetAddress related is still used
280 // TODO check if InetAddress can be correctly serialized
281 kryo.register(InetAddress.class);
282 kryo.register(Inet4Address.class);
TeruUd1c5b652014-03-24 13:58:46 -0700283
Ray Milkey269ffb92014-04-03 14:43:30 -0700284 return kryo;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700285 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700286
287 /**
288 * Serializes given object to byte array using Kryo instance in global pool.
289 * <p>
290 * Note: Serialized bytes must be smaller than DEFAULT_BUFFER_SIZE (=1MB).
291 *
292 * @param obj Object to serialize (Class must be registered to KryoFactory)
293 * @return serialized bytes
294 */
295 public static byte[] serialize(final Object obj) {
296 return serialize(obj, DEFAULT_BUFFER_SIZE);
297 }
298
299 /**
300 * Serializes given object to byte array using Kryo instance in global pool.
301 *
302 * @param obj Object to serialize (Class must be registered to KryoFactory)
303 * @param bufferSize maximum size of serialized bytes
304 * @return serialized bytes
305 */
306 public static byte[] serialize(final Object obj, final int bufferSize) {
307 Output out = new Output(bufferSize);
308 Kryo kryo = GLOBAL_POOL.newKryo();
309 try {
310 kryo.writeClassAndObject(out, obj);
311 return out.toBytes();
312 } finally {
313 GLOBAL_POOL.deleteKryo(kryo);
314 }
315 }
316
317 /**
318 * Deserializes given byte array to Object using Kryo instance in global pool.
319 *
320 * @param bytes serialized bytes
321 * @param <T> deserialized Object type
322 * @return deserialized Object (Class must be registered to KryoFactory)
323 */
324 public static <T> T deserialize(final byte[] bytes) {
325 Input in = new Input(bytes);
326 Kryo kryo = GLOBAL_POOL.newKryo();
327 try {
328 @SuppressWarnings("unchecked")
329 T obj = (T) kryo.readClassAndObject(in);
330 return obj;
331 } finally {
332 GLOBAL_POOL.deleteKryo(kryo);
333 }
334 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700335}