blob: f5bf01d6bde95d7972ffc40b9a0ec481fa2335e2 [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 Koided6cbec32014-08-27 15:23:44 -070019import net.onrc.onos.api.flowmanager.FlowBatchOperation;
20import net.onrc.onos.api.flowmanager.FlowBatchState;
Toshio Koide2c67a2d2014-08-27 11:30:56 -070021import net.onrc.onos.api.flowmanager.FlowId;
22import net.onrc.onos.api.flowmanager.FlowLink;
Toshio Koideb7a578c2014-08-22 18:00:54 -070023import net.onrc.onos.api.flowmanager.FlowState;
Toshio Koide2c67a2d2014-08-27 11:30:56 -070024import net.onrc.onos.api.flowmanager.OpticalPathFlow;
25import net.onrc.onos.api.flowmanager.PacketPathFlow;
26import net.onrc.onos.api.flowmanager.SingleDstTreeFlow;
27import net.onrc.onos.api.flowmanager.Tree;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070028import net.onrc.onos.api.newintent.AbstractIntent;
29import net.onrc.onos.api.newintent.ConnectivityIntent;
30import net.onrc.onos.api.newintent.IntentEvent;
31import net.onrc.onos.api.newintent.IntentId;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070032import net.onrc.onos.api.newintent.IntentState;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070033import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent;
34import net.onrc.onos.api.newintent.OpticalConnectivityIntent;
35import net.onrc.onos.api.newintent.PacketConnectivityIntent;
36import net.onrc.onos.api.newintent.PointToPointIntent;
37import net.onrc.onos.api.newintent.SinglePointToMultiPointIntent;
TeruU3c049c42014-04-15 10:13:25 -070038import net.onrc.onos.apps.proxyarp.ArpCacheNotification;
Jonathan Hart0961fe82014-04-03 09:56:25 -070039import net.onrc.onos.apps.proxyarp.ArpReplyNotification;
Jonathan Hart03102132014-07-01 23:22:04 -070040import net.onrc.onos.core.hostmanager.Host;
Jonathan Hartaa380972014-04-03 10:24:46 -070041import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
42import net.onrc.onos.core.intent.ErrorIntent;
43import net.onrc.onos.core.intent.Intent;
44import net.onrc.onos.core.intent.IntentOperation;
45import net.onrc.onos.core.intent.IntentOperationList;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -070046import net.onrc.onos.core.intent.Path;
Jonathan Hartaa380972014-04-03 10:24:46 -070047import net.onrc.onos.core.intent.PathIntent;
48import net.onrc.onos.core.intent.ShortestPathIntent;
49import net.onrc.onos.core.intent.runtime.IntentStateList;
Toshio Koide2c67a2d2014-08-27 11:30:56 -070050import net.onrc.onos.core.matchaction.action.ModifyDstMacAction;
51import net.onrc.onos.core.matchaction.action.ModifySrcMacAction;
52import net.onrc.onos.core.matchaction.action.OutputAction;
53import net.onrc.onos.core.matchaction.match.PacketMatch;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070054import net.onrc.onos.core.newintent.IntentCompilationResult;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070055import net.onrc.onos.core.newintent.PathFlowIntent;
56import net.onrc.onos.core.newintent.SingleDstTreeFlowIntent;
57import net.onrc.onos.core.newintent.SingleSrcTreeFlowIntent;
Sho SHIMIZU95d53c82014-08-21 09:18:47 -070058import net.onrc.onos.core.newintent.TestIntent;
59import net.onrc.onos.core.newintent.TestSubclassIntent;
Jonathan Hart313fdf02014-04-10 14:09:46 -070060import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification;
Jonathan Harte6e63732014-04-16 14:29:49 -070061import net.onrc.onos.core.packetservice.PacketOutNotification;
Jonathan Hart313fdf02014-04-10 14:09:46 -070062import net.onrc.onos.core.packetservice.SinglePacketOutNotification;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070063import net.onrc.onos.core.topology.AdminStatus;
64import net.onrc.onos.core.topology.ConfigState;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070065import net.onrc.onos.core.topology.HostEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070066import net.onrc.onos.core.topology.LinkEvent;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070067import net.onrc.onos.core.topology.MastershipEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070068import net.onrc.onos.core.topology.PortEvent;
69import net.onrc.onos.core.topology.SwitchEvent;
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -070070import net.onrc.onos.core.topology.TopologyBatchOperation;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070071import net.onrc.onos.core.topology.TopologyElement;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070072import net.onrc.onos.core.topology.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070073import net.onrc.onos.core.util.Dpid;
Jonathan Hart23701d12014-04-03 10:45:48 -070074import net.onrc.onos.core.util.IPv4;
75import net.onrc.onos.core.util.IPv4Net;
76import net.onrc.onos.core.util.IPv6;
77import net.onrc.onos.core.util.IPv6Net;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070078import net.onrc.onos.core.util.LinkTuple;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070079import net.onrc.onos.core.util.OnosInstanceId;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070080import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070081import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart23701d12014-04-03 10:45:48 -070082
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080083import com.esotericsoftware.kryo.Kryo;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070084import com.esotericsoftware.kryo.io.Input;
85import com.esotericsoftware.kryo.io.Output;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070086
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070087/**
88 * Class factory for allocating Kryo instances for
89 * serialization/deserialization of classes.
90 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070091@ThreadSafe
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070092public class KryoFactory {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070093
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070094 private static final int DEFAULT_PREALLOCATIONS = 100;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070095 /**
96 * Default buffer size used for serialization.
97 *
98 * @see #serialize(Object)
99 */
100 public static final int DEFAULT_BUFFER_SIZE = 1 * 1000 * 1000;
101
102 private static final KryoFactory GLOBAL_POOL = new KryoFactory();
103
104 private ConcurrentLinkedQueue<Kryo> kryoList = new ConcurrentLinkedQueue<Kryo>();
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700105
106 /**
107 * Default constructor.
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 * <p/>
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700109 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700110 */
111 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700112 this(DEFAULT_PREALLOCATIONS);
113 }
114
115 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700116 * Constructor to explicitly specify number of Kryo instances to pool.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700117 *
118 * @param initialCapacity number of Kryo instance to preallocate
119 */
120 public KryoFactory(final int initialCapacity) {
121 // Preallocate
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700122 for (int i = 0; i < initialCapacity; i++) {
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700123 Kryo kryo = newKryoObject();
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700124 kryoList.add(kryo);
125 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700126 }
127
128 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700129 * Gets a new Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700130 *
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700131 * @return the Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700132 */
133 public Kryo newKryo() {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700134 Kryo kryo = kryoList.poll();
135 if (kryo == null) {
136 // Will defer additional allocation until deleteKryo().
137 // It is more likely that it is no longer latency sensitive
138 // by the time caller is recycling Kryo instance.
139 return newKryoObject();
140 } else {
141 return kryo;
142 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700143 }
144
145 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700146 * Deletes an existing Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700147 *
148 * @param deleteKryo the object to delete.
149 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700150 public void deleteKryo(final Kryo deleteKryo) {
151 if (kryoList.isEmpty()) {
152 // buffer extra if kryo instance pool has exhausted.
153 List<Kryo> kryos = new ArrayList<>(DEFAULT_PREALLOCATIONS);
154 for (int i = 0; i < DEFAULT_PREALLOCATIONS; ++i) {
155 kryos.add(newKryoObject());
Ray Milkey269ffb92014-04-03 14:43:30 -0700156 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700157 kryoList.addAll(kryos);
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700159
160 // recycle
161 kryoList.add(deleteKryo);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700162 }
163
164 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700165 * Creates and initializes a new Kryo object.
166 *<p>
167 * NOTE: This operation can be slow and should be used only if the
168 * application needs a single Kryo instance (e.g., during startup).
169 * For faster allocation, the application should use #newKryo()
170 * and #deleteKryo() factory methods.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700171 *
172 * @return the created Kryo object.
173 */
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700174 public static Kryo newKryoObject() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700175 Kryo kryo = new Kryo();
176 kryo.setRegistrationRequired(true);
177 //
178 // WARNING: Order of register() calls affects serialized bytes.
179 // - Do no insert new entry in the middle, always add to the end.
180 // - Do not simply remove existing entry
181 //
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700182
Ray Milkey269ffb92014-04-03 14:43:30 -0700183 // kryo.setReferences(false);
184 //
185 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700186
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 // FlowPath and related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700188 kryo.register(Dpid.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700189 kryo.register(IPv4.class);
190 kryo.register(IPv4Net.class);
191 kryo.register(IPv6.class);
192 kryo.register(IPv6Net.class);
193 kryo.register(byte[].class);
194 kryo.register(MACAddress.class);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700195 kryo.register(PortNumber.class);
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700196 kryo.register(SwitchPort.class);
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700197 kryo.register(LinkTuple.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700198
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 // New data model-related classes
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700200 kryo.register(AdminStatus.class);
201 kryo.register(ConcurrentHashMap.class);
202 kryo.register(ConfigState.class);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700203 kryo.register(HostEvent.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700204 kryo.register(LinkedList.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700205 kryo.register(LinkEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700206 kryo.register(MastershipEvent.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700207 kryo.register(OnosInstanceId.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700208 kryo.register(PortEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700209 kryo.register(Role.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700210 kryo.register(SwitchEvent.class);
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700211 kryo.register(TopologyBatchOperation.class);
212 kryo.register(TopologyBatchOperation.Operator.class);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700213 kryo.register(TopologyElement.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700214 kryo.register(TopologyEvent.class);
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700215 kryo.register(TopologyEvent.Type.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800216
Ray Milkey269ffb92014-04-03 14:43:30 -0700217 // Intent-related classes
218 kryo.register(Path.class);
219 kryo.register(Intent.class);
220 kryo.register(Intent.IntentState.class);
221 kryo.register(PathIntent.class);
222 kryo.register(ShortestPathIntent.class);
223 kryo.register(ConstrainedShortestPathIntent.class);
224 kryo.register(ErrorIntent.class);
225 kryo.register(ErrorIntent.ErrorType.class);
226 kryo.register(IntentOperation.class);
227 kryo.register(IntentOperation.Operator.class);
228 kryo.register(IntentOperationList.class);
229 kryo.register(IntentStateList.class);
TeruUf9111652014-05-14 23:10:35 -0700230 kryo.register(HashMap.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800231
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700232 // New intent-related classes
233 kryo.register(BatchOperationEntry.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700234 kryo.register(IntentId.class);
235 kryo.register(IntentEvent.class);
Sho SHIMIZU07ace752014-08-21 09:04:41 -0700236 kryo.register(IntentState.class);
237 kryo.register(IntentCompilationResult.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700238 kryo.register(AbstractIntent.class);
239 kryo.register(ConnectivityIntent.class);
240 kryo.register(PointToPointIntent.class);
241 kryo.register(MultiPointToSinglePointIntent.class);
242 kryo.register(SinglePointToMultiPointIntent.class);
243 kryo.register(net.onrc.onos.api.newintent.PathIntent.class);
244 kryo.register(PathFlowIntent.class);
245 kryo.register(SingleSrcTreeFlowIntent.class);
246 kryo.register(SingleDstTreeFlowIntent.class);
247 kryo.register(PacketConnectivityIntent.class);
248 kryo.register(OpticalConnectivityIntent.class);
Sho SHIMIZU95d53c82014-08-21 09:18:47 -0700249 // FIXME: due to lack of functionality to register a serializer
250 // in API user side, we added the following two classes.
251 // Theoretically the classes are only for test. we should create
252 // a way to register serializer without editing source code
253 kryo.register(TestIntent.class);
254 kryo.register(TestSubclassIntent.class);
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700255
Toshio Koide2c67a2d2014-08-27 11:30:56 -0700256 // New flow manager related classes
257 kryo.register(FlowId.class);
Toshio Koideb7a578c2014-08-22 18:00:54 -0700258 kryo.register(FlowState.class);
Toshio Koided6cbec32014-08-27 15:23:44 -0700259 kryo.register(FlowBatchOperation.class);
260 kryo.register(FlowBatchOperation.Operator.class);
261 kryo.register(FlowBatchState.class);
Toshio Koide2c67a2d2014-08-27 11:30:56 -0700262 kryo.register(net.onrc.onos.api.flowmanager.Path.class);
263 kryo.register(Tree.class);
264 kryo.register(FlowLink.class);
265 kryo.register(OpticalPathFlow.class);
266 kryo.register(PacketPathFlow.class);
267 kryo.register(SingleDstTreeFlow.class);
268
269 // New match action related classes
270 kryo.register(PacketMatch.class);
271 kryo.register(OutputAction.class);
272 kryo.register(ModifyDstMacAction.class);
273 kryo.register(ModifySrcMacAction.class);
274
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700275 // Host-related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700276 kryo.register(HashSet.class);
Jonathan Hart03102132014-07-01 23:22:04 -0700277 kryo.register(Host.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700278 kryo.register(Date.class);
TeruU7feef8a2014-04-03 00:15:49 -0700279
Ray Milkey269ffb92014-04-03 14:43:30 -0700280 // ProxyArp-related classes
Jonathan Harte6e63732014-04-16 14:29:49 -0700281 kryo.register(PacketOutNotification.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700282 kryo.register(BroadcastPacketOutNotification.class);
283 kryo.register(SinglePacketOutNotification.class);
284 kryo.register(ArpReplyNotification.class);
TeruU3c049c42014-04-15 10:13:25 -0700285 kryo.register(ArpCacheNotification.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700286 // TODO check if InetAddress related is still used
287 // TODO check if InetAddress can be correctly serialized
288 kryo.register(InetAddress.class);
289 kryo.register(Inet4Address.class);
TeruUd1c5b652014-03-24 13:58:46 -0700290
Ray Milkey269ffb92014-04-03 14:43:30 -0700291 return kryo;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700292 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700293
294 /**
295 * Serializes given object to byte array using Kryo instance in global pool.
296 * <p>
297 * Note: Serialized bytes must be smaller than DEFAULT_BUFFER_SIZE (=1MB).
298 *
299 * @param obj Object to serialize (Class must be registered to KryoFactory)
300 * @return serialized bytes
301 */
302 public static byte[] serialize(final Object obj) {
303 return serialize(obj, DEFAULT_BUFFER_SIZE);
304 }
305
306 /**
307 * Serializes given object to byte array using Kryo instance in global pool.
308 *
309 * @param obj Object to serialize (Class must be registered to KryoFactory)
310 * @param bufferSize maximum size of serialized bytes
311 * @return serialized bytes
312 */
313 public static byte[] serialize(final Object obj, final int bufferSize) {
314 Output out = new Output(bufferSize);
315 Kryo kryo = GLOBAL_POOL.newKryo();
316 try {
317 kryo.writeClassAndObject(out, obj);
318 return out.toBytes();
319 } finally {
320 GLOBAL_POOL.deleteKryo(kryo);
321 }
322 }
323
324 /**
325 * Deserializes given byte array to Object using Kryo instance in global pool.
326 *
327 * @param bytes serialized bytes
328 * @param <T> deserialized Object type
329 * @return deserialized Object (Class must be registered to KryoFactory)
330 */
331 public static <T> T deserialize(final byte[] bytes) {
332 Input in = new Input(bytes);
333 Kryo kryo = GLOBAL_POOL.newKryo();
334 try {
335 @SuppressWarnings("unchecked")
336 T obj = (T) kryo.readClassAndObject(in);
337 return obj;
338 } finally {
339 GLOBAL_POOL.deleteKryo(kryo);
340 }
341 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700342}