blob: 4341e4ac7727a31205b0025c13b8ef0fe16233c3 [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;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070019import net.onrc.onos.api.newintent.AbstractIntent;
20import net.onrc.onos.api.newintent.ConnectivityIntent;
21import net.onrc.onos.api.newintent.IntentEvent;
22import net.onrc.onos.api.newintent.IntentId;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070023import net.onrc.onos.api.newintent.IntentState;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070024import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent;
25import net.onrc.onos.api.newintent.OpticalConnectivityIntent;
26import net.onrc.onos.api.newintent.PacketConnectivityIntent;
27import net.onrc.onos.api.newintent.PointToPointIntent;
28import net.onrc.onos.api.newintent.SinglePointToMultiPointIntent;
TeruU3c049c42014-04-15 10:13:25 -070029import net.onrc.onos.apps.proxyarp.ArpCacheNotification;
Jonathan Hart0961fe82014-04-03 09:56:25 -070030import net.onrc.onos.apps.proxyarp.ArpReplyNotification;
Jonathan Hart03102132014-07-01 23:22:04 -070031import net.onrc.onos.core.hostmanager.Host;
Jonathan Hartaa380972014-04-03 10:24:46 -070032import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
33import net.onrc.onos.core.intent.ErrorIntent;
34import net.onrc.onos.core.intent.Intent;
35import net.onrc.onos.core.intent.IntentOperation;
36import net.onrc.onos.core.intent.IntentOperationList;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -070037import net.onrc.onos.core.intent.Path;
Jonathan Hartaa380972014-04-03 10:24:46 -070038import net.onrc.onos.core.intent.PathIntent;
39import net.onrc.onos.core.intent.ShortestPathIntent;
40import net.onrc.onos.core.intent.runtime.IntentStateList;
Sho SHIMIZU07ace752014-08-21 09:04:41 -070041import net.onrc.onos.core.newintent.IntentCompilationResult;
Sho SHIMIZU1674fb32014-08-20 14:44:31 -070042import net.onrc.onos.core.newintent.PathFlowIntent;
43import net.onrc.onos.core.newintent.SingleDstTreeFlowIntent;
44import net.onrc.onos.core.newintent.SingleSrcTreeFlowIntent;
Sho SHIMIZU95d53c82014-08-21 09:18:47 -070045import net.onrc.onos.core.newintent.TestIntent;
46import net.onrc.onos.core.newintent.TestSubclassIntent;
Jonathan Hart313fdf02014-04-10 14:09:46 -070047import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification;
Jonathan Harte6e63732014-04-16 14:29:49 -070048import net.onrc.onos.core.packetservice.PacketOutNotification;
Jonathan Hart313fdf02014-04-10 14:09:46 -070049import net.onrc.onos.core.packetservice.SinglePacketOutNotification;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070050import net.onrc.onos.core.topology.AdminStatus;
51import net.onrc.onos.core.topology.ConfigState;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070052import net.onrc.onos.core.topology.HostEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070053import net.onrc.onos.core.topology.LinkEvent;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070054import net.onrc.onos.core.topology.MastershipEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070055import net.onrc.onos.core.topology.PortEvent;
56import net.onrc.onos.core.topology.SwitchEvent;
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -070057import net.onrc.onos.core.topology.TopologyBatchOperation;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070058import net.onrc.onos.core.topology.TopologyElement;
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -070059import net.onrc.onos.core.topology.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070060import net.onrc.onos.core.util.Dpid;
Jonathan Hart23701d12014-04-03 10:45:48 -070061import net.onrc.onos.core.util.IPv4;
62import net.onrc.onos.core.util.IPv4Net;
63import net.onrc.onos.core.util.IPv6;
64import net.onrc.onos.core.util.IPv6Net;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070065import net.onrc.onos.core.util.LinkTuple;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070066import net.onrc.onos.core.util.OnosInstanceId;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070067import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070068import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart23701d12014-04-03 10:45:48 -070069
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080070import com.esotericsoftware.kryo.Kryo;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070071import com.esotericsoftware.kryo.io.Input;
72import com.esotericsoftware.kryo.io.Output;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070073
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070074/**
75 * Class factory for allocating Kryo instances for
76 * serialization/deserialization of classes.
77 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070078@ThreadSafe
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070079public class KryoFactory {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070080
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070081 private static final int DEFAULT_PREALLOCATIONS = 100;
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -070082 /**
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 Radoslavove0a643e2013-10-24 13:33:39 -070092
93 /**
94 * Default constructor.
Ray Milkey269ffb92014-04-03 14:43:30 -070095 * <p/>
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070096 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070097 */
98 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070099 this(DEFAULT_PREALLOCATIONS);
100 }
101
102 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700103 * Constructor to explicitly specify number of Kryo instances to pool.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700104 *
105 * @param initialCapacity number of Kryo instance to preallocate
106 */
107 public KryoFactory(final int initialCapacity) {
108 // Preallocate
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700109 for (int i = 0; i < initialCapacity; i++) {
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700110 Kryo kryo = newKryoObject();
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700111 kryoList.add(kryo);
112 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700113 }
114
115 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700116 * Gets a new Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700117 *
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700118 * @return the Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700119 */
120 public Kryo newKryo() {
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700121 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 Radoslavove0a643e2013-10-24 13:33:39 -0700130 }
131
132 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700133 * Deletes an existing Kryo object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700134 *
135 * @param deleteKryo the object to delete.
136 */
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700137 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 Milkey269ffb92014-04-03 14:43:30 -0700143 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700144 kryoList.addAll(kryos);
Ray Milkey269ffb92014-04-03 14:43:30 -0700145 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700146
147 // recycle
148 kryoList.add(deleteKryo);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700149 }
150
151 /**
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700152 * 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 Radoslavove0a643e2013-10-24 13:33:39 -0700158 *
159 * @return the created Kryo object.
160 */
Pavlin Radoslavov9c7b14c2014-07-03 17:24:27 -0700161 public static Kryo newKryoObject() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 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 HIGUCHI0af53d22014-03-31 14:01:35 -0700169
Ray Milkey269ffb92014-04-03 14:43:30 -0700170 // kryo.setReferences(false);
171 //
172 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700173
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 // FlowPath and related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700175 kryo.register(Dpid.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 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 HIGUCHIfb564502014-06-16 21:29:00 -0700182 kryo.register(PortNumber.class);
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700183 kryo.register(SwitchPort.class);
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -0700184 kryo.register(LinkTuple.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700185
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 // New data model-related classes
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700187 kryo.register(AdminStatus.class);
188 kryo.register(ConcurrentHashMap.class);
189 kryo.register(ConfigState.class);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700190 kryo.register(HostEvent.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700191 kryo.register(LinkedList.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 kryo.register(LinkEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700193 kryo.register(MastershipEvent.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700194 kryo.register(OnosInstanceId.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 kryo.register(PortEvent.class);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700196 kryo.register(Role.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 kryo.register(SwitchEvent.class);
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700198 kryo.register(TopologyBatchOperation.class);
199 kryo.register(TopologyBatchOperation.Operator.class);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700200 kryo.register(TopologyElement.class);
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700201 kryo.register(TopologyEvent.class);
Pavlin Radoslavov31f85102014-08-15 13:55:44 -0700202 kryo.register(TopologyEvent.Type.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800203
Ray Milkey269ffb92014-04-03 14:43:30 -0700204 // 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);
TeruUf9111652014-05-14 23:10:35 -0700217 kryo.register(HashMap.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800218
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -0700219 // New intent-related classes
220 kryo.register(BatchOperationEntry.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700221 kryo.register(IntentId.class);
222 kryo.register(IntentEvent.class);
Sho SHIMIZU07ace752014-08-21 09:04:41 -0700223 kryo.register(IntentState.class);
224 kryo.register(IntentCompilationResult.class);
Sho SHIMIZU1674fb32014-08-20 14:44:31 -0700225 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 SHIMIZU95d53c82014-08-21 09:18:47 -0700236 // 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 Radoslavovdd08e8c2014-08-14 11:02:57 -0700242
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700243 // Host-related classes
Ray Milkey269ffb92014-04-03 14:43:30 -0700244 kryo.register(HashSet.class);
Jonathan Hart03102132014-07-01 23:22:04 -0700245 kryo.register(Host.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700246 kryo.register(Date.class);
TeruU7feef8a2014-04-03 00:15:49 -0700247
Ray Milkey269ffb92014-04-03 14:43:30 -0700248 // ProxyArp-related classes
Jonathan Harte6e63732014-04-16 14:29:49 -0700249 kryo.register(PacketOutNotification.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700250 kryo.register(BroadcastPacketOutNotification.class);
251 kryo.register(SinglePacketOutNotification.class);
252 kryo.register(ArpReplyNotification.class);
TeruU3c049c42014-04-15 10:13:25 -0700253 kryo.register(ArpCacheNotification.class);
Yuta HIGUCHIc25861a2014-06-17 23:17:09 -0700254 // 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);
TeruUd1c5b652014-03-24 13:58:46 -0700258
Ray Milkey269ffb92014-04-03 14:43:30 -0700259 return kryo;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700260 }
Yuta HIGUCHIe57e10e2014-08-20 14:25:30 -0700261
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 Radoslavove0a643e2013-10-24 13:33:39 -0700310}