blob: c9c3c7bccf371f100e208d9914eb6dc830d9a5e4 [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;
Toshio Koideb7a578c2014-08-22 18:00:54 -070021import net.onrc.onos.api.flowmanager.FlowState;
Toshio Koide2c67a2d2014-08-27 11:30:56 -070022import net.onrc.onos.api.flowmanager.OpticalPathFlow;
23import net.onrc.onos.api.flowmanager.PacketPathFlow;
24import net.onrc.onos.api.flowmanager.SingleDstTreeFlow;
25import net.onrc.onos.api.flowmanager.Tree;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070026import net.onrc.onos.api.newintent.AbstractIntent;
27import net.onrc.onos.api.newintent.ConnectivityIntent;
28import net.onrc.onos.api.newintent.IntentEvent;
29import net.onrc.onos.api.newintent.IntentId;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070030import net.onrc.onos.api.newintent.IntentState;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070031import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent;
32import net.onrc.onos.api.newintent.OpticalConnectivityIntent;
33import net.onrc.onos.api.newintent.PacketConnectivityIntent;
34import net.onrc.onos.api.newintent.PointToPointIntent;
35import net.onrc.onos.api.newintent.SinglePointToMultiPointIntent;
TeruU3c049c42014-04-15 10:13:25 -070036import net.onrc.onos.apps.proxyarp.ArpCacheNotification;
Jonathan Hart0961fe82014-04-03 09:56:25 -070037import net.onrc.onos.apps.proxyarp.ArpReplyNotification;
Jonathan Hart03102132014-07-01 23:22:04 -070038import net.onrc.onos.core.hostmanager.Host;
Jonathan Hartaa380972014-04-03 10:24:46 -070039import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
40import net.onrc.onos.core.intent.ErrorIntent;
41import net.onrc.onos.core.intent.Intent;
42import net.onrc.onos.core.intent.IntentOperation;
43import net.onrc.onos.core.intent.IntentOperationList;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -070044import net.onrc.onos.core.intent.Path;
Jonathan Hartaa380972014-04-03 10:24:46 -070045import net.onrc.onos.core.intent.PathIntent;
46import net.onrc.onos.core.intent.ShortestPathIntent;
47import net.onrc.onos.core.intent.runtime.IntentStateList;
Toshio Koide2c67a2d2014-08-27 11:30:56 -070048import net.onrc.onos.core.matchaction.action.ModifyDstMacAction;
49import net.onrc.onos.core.matchaction.action.ModifySrcMacAction;
50import net.onrc.onos.core.matchaction.action.OutputAction;
51import net.onrc.onos.core.matchaction.match.PacketMatch;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070052import net.onrc.onos.core.newintent.IntentCompilationResult;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070053import net.onrc.onos.core.newintent.PathFlowIntent;
54import net.onrc.onos.core.newintent.SingleDstTreeFlowIntent;
55import net.onrc.onos.core.newintent.SingleSrcTreeFlowIntent;
Sho SHIMIZU95d53c82014-08-21 09:18:47 -070056import net.onrc.onos.core.newintent.TestIntent;
57import net.onrc.onos.core.newintent.TestSubclassIntent;
Jonathan Hart313fdf02014-04-10 14:09:46 -070058import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification;
Jonathan Harte6e63732014-04-16 14:29:49 -070059import net.onrc.onos.core.packetservice.PacketOutNotification;
Jonathan Hart313fdf02014-04-10 14:09:46 -070060import net.onrc.onos.core.packetservice.SinglePacketOutNotification;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070061import net.onrc.onos.core.topology.AdminStatus;
62import net.onrc.onos.core.topology.ConfigState;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070063import net.onrc.onos.core.topology.HostEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070064import net.onrc.onos.core.topology.LinkEvent;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070065import net.onrc.onos.core.topology.MastershipEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070066import net.onrc.onos.core.topology.PortEvent;
67import net.onrc.onos.core.topology.SwitchEvent;
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -070068import net.onrc.onos.core.topology.TopologyBatchOperation;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070069import net.onrc.onos.core.topology.TopologyElement;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070070import net.onrc.onos.core.topology.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070071import net.onrc.onos.core.util.Dpid;
Jonathan Hart23701d12014-04-03 10:45:48 -070072import net.onrc.onos.core.util.IPv4;
73import net.onrc.onos.core.util.IPv4Net;
74import net.onrc.onos.core.util.IPv6;
75import net.onrc.onos.core.util.IPv6Net;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070076import net.onrc.onos.core.util.LinkTuple;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070077import net.onrc.onos.core.util.OnosInstanceId;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070078import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070079import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart23701d12014-04-03 10:45:48 -070080
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080081import com.esotericsoftware.kryo.Kryo;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070082import com.esotericsoftware.kryo.io.Input;
83import com.esotericsoftware.kryo.io.Output;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070084
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070085/**
86 * Class factory for allocating Kryo instances for
87 * serialization/deserialization of classes.
88 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070089@ThreadSafe
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070090public class KryoFactory {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070091
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070092 private static final int DEFAULT_PREALLOCATIONS = 100;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070093 /**
94 * Default buffer size used for serialization.
95 *
96 * @see #serialize(Object)
97 */
98 public static final int DEFAULT_BUFFER_SIZE = 1 * 1000 * 1000;
99
100 private static final KryoFactory GLOBAL_POOL = new KryoFactory();
101
102 private ConcurrentLinkedQueue<Kryo> kryoList = new ConcurrentLinkedQueue<Kryo>();
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700103
104 /**
105 * Default constructor.
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 * <p/>
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700107 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700108 */
109 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700110 this(DEFAULT_PREALLOCATIONS);
111 }
112
113 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700114 * Constructor to explicitly specify number of Kryo instances to pool.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700115 *
116 * @param initialCapacity number of Kryo instance to preallocate
117 */
118 public KryoFactory(final int initialCapacity) {
119 // Preallocate
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700120 for (int i = 0; i < initialCapacity; i++) {
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700121 Kryo kryo = newKryoObject();
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700122 kryoList.add(kryo);
123 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700124 }
125
126 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700127 * Gets a new Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700128 *
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700129 * @return the Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700130 */
131 public Kryo newKryo() {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700132 Kryo kryo = kryoList.poll();
133 if (kryo == null) {
134 // Will defer additional allocation until deleteKryo().
135 // It is more likely that it is no longer latency sensitive
136 // by the time caller is recycling Kryo instance.
137 return newKryoObject();
138 } else {
139 return kryo;
140 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700141 }
142
143 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700144 * Deletes an existing Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700145 *
146 * @param deleteKryo the object to delete.
147 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700148 public void deleteKryo(final Kryo deleteKryo) {
149 if (kryoList.isEmpty()) {
150 // buffer extra if kryo instance pool has exhausted.
151 List<Kryo> kryos = new ArrayList<>(DEFAULT_PREALLOCATIONS);
152 for (int i = 0; i < DEFAULT_PREALLOCATIONS; ++i) {
153 kryos.add(newKryoObject());
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700155 kryoList.addAll(kryos);
Ray Milkey269ffb92014-04-03 14:43:30 -0700156 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700157
158 // recycle
159 kryoList.add(deleteKryo);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700160 }
161
162 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700163 * Creates and initializes a new Kryo object.
164 *<p>
165 * NOTE: This operation can be slow and should be used only if the
166 * application needs a single Kryo instance (e.g., during startup).
167 * For faster allocation, the application should use #newKryo()
168 * and #deleteKryo() factory methods.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700169 *
170 * @return the created Kryo object.
171 */
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700172 public static Kryo newKryoObject() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700173 Kryo kryo = new Kryo();
174 kryo.setRegistrationRequired(true);
175 //
176 // WARNING: Order of register() calls affects serialized bytes.
177 // - Do no insert new entry in the middle, always add to the end.
178 // - Do not simply remove existing entry
179 //
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700180
Ray Milkey269ffb92014-04-03 14:43:30 -0700181 // kryo.setReferences(false);
182 //
183 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700184
Ray Milkey269ffb92014-04-03 14:43:30 -0700185 // FlowPath and related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 kryo.register(Dpid.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 kryo.register(IPv4.class);
188 kryo.register(IPv4Net.class);
189 kryo.register(IPv6.class);
190 kryo.register(IPv6Net.class);
191 kryo.register(byte[].class);
192 kryo.register(MACAddress.class);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700193 kryo.register(PortNumber.class);
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700194 kryo.register(SwitchPort.class);
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700195 kryo.register(LinkTuple.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700196
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 // New data model-related classes
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700198 kryo.register(AdminStatus.class);
199 kryo.register(ConcurrentHashMap.class);
200 kryo.register(ConfigState.class);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700201 kryo.register(HostEvent.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700202 kryo.register(LinkedList.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700203 kryo.register(LinkEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700204 kryo.register(MastershipEvent.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700205 kryo.register(OnosInstanceId.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700206 kryo.register(PortEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700207 kryo.register(Role.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700208 kryo.register(SwitchEvent.class);
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700209 kryo.register(TopologyBatchOperation.class);
210 kryo.register(TopologyBatchOperation.Operator.class);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700211 kryo.register(TopologyElement.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700212 kryo.register(TopologyEvent.class);
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700213 kryo.register(TopologyEvent.Type.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800214
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 // Intent-related classes
216 kryo.register(Path.class);
217 kryo.register(Intent.class);
218 kryo.register(Intent.IntentState.class);
219 kryo.register(PathIntent.class);
220 kryo.register(ShortestPathIntent.class);
221 kryo.register(ConstrainedShortestPathIntent.class);
222 kryo.register(ErrorIntent.class);
223 kryo.register(ErrorIntent.ErrorType.class);
224 kryo.register(IntentOperation.class);
225 kryo.register(IntentOperation.Operator.class);
226 kryo.register(IntentOperationList.class);
227 kryo.register(IntentStateList.class);
TeruUf9111652014-05-14 23:10:35 -0700228 kryo.register(HashMap.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800229
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700230 // New intent-related classes
231 kryo.register(BatchOperationEntry.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700232 kryo.register(IntentId.class);
233 kryo.register(IntentEvent.class);
Sho SHIMIZU07ace752014-08-21 09:04:41 -0700234 kryo.register(IntentState.class);
235 kryo.register(IntentCompilationResult.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700236 kryo.register(AbstractIntent.class);
237 kryo.register(ConnectivityIntent.class);
238 kryo.register(PointToPointIntent.class);
239 kryo.register(MultiPointToSinglePointIntent.class);
240 kryo.register(SinglePointToMultiPointIntent.class);
241 kryo.register(net.onrc.onos.api.newintent.PathIntent.class);
242 kryo.register(PathFlowIntent.class);
243 kryo.register(SingleSrcTreeFlowIntent.class);
244 kryo.register(SingleDstTreeFlowIntent.class);
245 kryo.register(PacketConnectivityIntent.class);
246 kryo.register(OpticalConnectivityIntent.class);
Sho SHIMIZU95d53c82014-08-21 09:18:47 -0700247 // FIXME: due to lack of functionality to register a serializer
248 // in API user side, we added the following two classes.
249 // Theoretically the classes are only for test. we should create
250 // a way to register serializer without editing source code
251 kryo.register(TestIntent.class);
252 kryo.register(TestSubclassIntent.class);
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700253
Toshio Koide2c67a2d2014-08-27 11:30:56 -0700254 // New flow manager related classes
255 kryo.register(FlowId.class);
Toshio Koideb7a578c2014-08-22 18:00:54 -0700256 kryo.register(FlowState.class);
Toshio Koide2c67a2d2014-08-27 11:30:56 -0700257 kryo.register(net.onrc.onos.api.flowmanager.Path.class);
258 kryo.register(Tree.class);
259 kryo.register(FlowLink.class);
260 kryo.register(OpticalPathFlow.class);
261 kryo.register(PacketPathFlow.class);
262 kryo.register(SingleDstTreeFlow.class);
263
264 // New match action related classes
265 kryo.register(PacketMatch.class);
266 kryo.register(OutputAction.class);
267 kryo.register(ModifyDstMacAction.class);
268 kryo.register(ModifySrcMacAction.class);
269
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700270 // Host-related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700271 kryo.register(HashSet.class);
Jonathan Hart03102132014-07-01 23:22:04 -0700272 kryo.register(Host.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700273 kryo.register(Date.class);
TeruU7feef8a2014-04-03 00:15:49 -0700274
Ray Milkey269ffb92014-04-03 14:43:30 -0700275 // ProxyArp-related classes
Jonathan Harte6e63732014-04-16 14:29:49 -0700276 kryo.register(PacketOutNotification.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700277 kryo.register(BroadcastPacketOutNotification.class);
278 kryo.register(SinglePacketOutNotification.class);
279 kryo.register(ArpReplyNotification.class);
TeruU3c049c42014-04-15 10:13:25 -0700280 kryo.register(ArpCacheNotification.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700281 // TODO check if InetAddress related is still used
282 // TODO check if InetAddress can be correctly serialized
283 kryo.register(InetAddress.class);
284 kryo.register(Inet4Address.class);
TeruUd1c5b652014-03-24 13:58:46 -0700285
Ray Milkey269ffb92014-04-03 14:43:30 -0700286 return kryo;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700287 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700288
289 /**
290 * Serializes given object to byte array using Kryo instance in global pool.
291 * <p>
292 * Note: Serialized bytes must be smaller than DEFAULT_BUFFER_SIZE (=1MB).
293 *
294 * @param obj Object to serialize (Class must be registered to KryoFactory)
295 * @return serialized bytes
296 */
297 public static byte[] serialize(final Object obj) {
298 return serialize(obj, DEFAULT_BUFFER_SIZE);
299 }
300
301 /**
302 * Serializes given object to byte array using Kryo instance in global pool.
303 *
304 * @param obj Object to serialize (Class must be registered to KryoFactory)
305 * @param bufferSize maximum size of serialized bytes
306 * @return serialized bytes
307 */
308 public static byte[] serialize(final Object obj, final int bufferSize) {
309 Output out = new Output(bufferSize);
310 Kryo kryo = GLOBAL_POOL.newKryo();
311 try {
312 kryo.writeClassAndObject(out, obj);
313 return out.toBytes();
314 } finally {
315 GLOBAL_POOL.deleteKryo(kryo);
316 }
317 }
318
319 /**
320 * Deserializes given byte array to Object using Kryo instance in global pool.
321 *
322 * @param bytes serialized bytes
323 * @param <T> deserialized Object type
324 * @return deserialized Object (Class must be registered to KryoFactory)
325 */
326 public static <T> T deserialize(final byte[] bytes) {
327 Input in = new Input(bytes);
328 Kryo kryo = GLOBAL_POOL.newKryo();
329 try {
330 @SuppressWarnings("unchecked")
331 T obj = (T) kryo.readClassAndObject(in);
332 return obj;
333 } finally {
334 GLOBAL_POOL.deleteKryo(kryo);
335 }
336 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700337}