blob: a1c472822c76d9dbf3fe6e85549897e84fe0c996 [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;
TeruUd1c5b652014-03-24 13:58:46 -07007import java.util.HashSet;
TeruU7feef8a2014-04-03 00:15:49 -07008
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07009import net.floodlightcontroller.util.MACAddress;
TeruU3c049c42014-04-15 10:13:25 -070010import net.onrc.onos.apps.proxyarp.ArpCacheNotification;
Jonathan Hart0961fe82014-04-03 09:56:25 -070011import net.onrc.onos.apps.proxyarp.ArpReplyNotification;
Jonathan Hart23701d12014-04-03 10:45:48 -070012import net.onrc.onos.core.devicemanager.OnosDevice;
Jonathan Hartaa380972014-04-03 10:24:46 -070013import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
14import net.onrc.onos.core.intent.ErrorIntent;
15import net.onrc.onos.core.intent.Intent;
16import net.onrc.onos.core.intent.IntentOperation;
17import net.onrc.onos.core.intent.IntentOperationList;
18import net.onrc.onos.core.intent.PathIntent;
19import net.onrc.onos.core.intent.ShortestPathIntent;
20import net.onrc.onos.core.intent.runtime.IntentStateList;
Jonathan Hart313fdf02014-04-10 14:09:46 -070021import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification;
Jonathan Harte6e63732014-04-16 14:29:49 -070022import net.onrc.onos.core.packetservice.PacketOutNotification;
Jonathan Hart313fdf02014-04-10 14:09:46 -070023import net.onrc.onos.core.packetservice.SinglePacketOutNotification;
Jonathan Hart472062d2014-04-03 10:56:48 -070024import net.onrc.onos.core.topology.DeviceEvent;
25import net.onrc.onos.core.topology.LinkEvent;
26import net.onrc.onos.core.topology.Path;
27import net.onrc.onos.core.topology.PortEvent;
28import net.onrc.onos.core.topology.SwitchEvent;
29import net.onrc.onos.core.topology.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070030import net.onrc.onos.core.util.CallerId;
31import net.onrc.onos.core.util.DataPath;
32import net.onrc.onos.core.util.DataPathEndpoints;
33import net.onrc.onos.core.util.Dpid;
34import net.onrc.onos.core.util.FlowEntry;
35import net.onrc.onos.core.util.FlowEntryAction;
36import net.onrc.onos.core.util.FlowEntryActions;
37import net.onrc.onos.core.util.FlowEntryErrorState;
38import net.onrc.onos.core.util.FlowEntryId;
39import net.onrc.onos.core.util.FlowEntryMatch;
40import net.onrc.onos.core.util.FlowEntrySwitchState;
41import net.onrc.onos.core.util.FlowEntryUserState;
42import net.onrc.onos.core.util.FlowId;
43import net.onrc.onos.core.util.FlowPath;
44import net.onrc.onos.core.util.FlowPathFlags;
45import net.onrc.onos.core.util.FlowPathType;
46import net.onrc.onos.core.util.FlowPathUserState;
47import net.onrc.onos.core.util.IPv4;
48import net.onrc.onos.core.util.IPv4Net;
49import net.onrc.onos.core.util.IPv6;
50import net.onrc.onos.core.util.IPv6Net;
51import net.onrc.onos.core.util.Port;
52import net.onrc.onos.core.util.Switch;
Jonathan Hart23701d12014-04-03 10:45:48 -070053// import net.onrc.onos.core.util.SwitchPort;
54
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080055import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070056
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070057/**
58 * Class factory for allocating Kryo instances for
59 * serialization/deserialization of classes.
60 */
61public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070062 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070063 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
64
65 /**
66 * Default constructor.
Ray Milkey269ffb92014-04-03 14:43:30 -070067 * <p/>
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070068 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070069 */
70 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070071 this(DEFAULT_PREALLOCATIONS);
72 }
73
74 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070075 * Constructor to explicitly specify number of Kryo instances to pool.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070076 *
77 * @param initialCapacity number of Kryo instance to preallocate
78 */
79 public KryoFactory(final int initialCapacity) {
80 // Preallocate
81 kryoList.ensureCapacity(initialCapacity);
82 for (int i = 0; i < initialCapacity; i++) {
83 Kryo kryo = newKryoImpl();
84 kryoList.add(kryo);
85 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070086 }
87
88 /**
89 * Create and initialize a new Kryo object.
90 *
91 * @return the created Kryo object.
92 */
93 public Kryo newKryo() {
Ray Milkey269ffb92014-04-03 14:43:30 -070094 return newDeleteKryo(null);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070095 }
96
97 /**
98 * Delete an existing Kryo object.
99 *
100 * @param deleteKryo the object to delete.
101 */
102 public void deleteKryo(Kryo deleteKryo) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 newDeleteKryo(deleteKryo);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700104 }
105
106 /**
107 * Create or delete a Kryo object.
108 *
109 * @param deleteKryo if null, then allocate and return a new object,
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 * otherwise delete the provided object.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700111 * @return a new Kryo object if needed, otherwise null.
112 */
Ray Milkeyec838942014-04-09 11:28:43 -0700113 private synchronized Kryo newDeleteKryo(Kryo deleteKryo) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 if (deleteKryo != null) {
115 // Delete an entry by moving it back to the buffer
116 kryoList.add(deleteKryo);
117 return null;
118 } else {
119 Kryo kryo = null;
120 if (kryoList.isEmpty()) {
121 // Preallocate
122 for (int i = 0; i < 100; i++) {
123 kryo = newKryoImpl();
124 kryoList.add(kryo);
125 }
126 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700127
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 kryo = kryoList.remove(kryoList.size() - 1);
129 return kryo;
130 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700131 }
132
133 /**
134 * Create and initialize a new Kryo object.
135 *
136 * @return the created Kryo object.
137 */
138 private Kryo newKryoImpl() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 Kryo kryo = new Kryo();
140 kryo.setRegistrationRequired(true);
141 //
142 // WARNING: Order of register() calls affects serialized bytes.
143 // - Do no insert new entry in the middle, always add to the end.
144 // - Do not simply remove existing entry
145 //
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700146
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 // kryo.setReferences(false);
148 //
149 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700150
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 // FlowPath and related classes
152 kryo.register(CallerId.class);
153 kryo.register(DataPath.class);
154 kryo.register(DataPathEndpoints.class);
155 kryo.register(Dpid.class);
156 kryo.register(FlowEntryAction.class);
157 kryo.register(FlowEntryAction.ActionEnqueue.class);
158 kryo.register(FlowEntryAction.ActionOutput.class);
159 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
160 kryo.register(FlowEntryAction.ActionSetIpToS.class);
161 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
162 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
163 kryo.register(FlowEntryAction.ActionSetVlanId.class);
164 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
165 kryo.register(FlowEntryAction.ActionStripVlan.class);
166 kryo.register(FlowEntryAction.ActionValues.class);
167 kryo.register(FlowEntryActions.class);
168 kryo.register(FlowEntryErrorState.class);
169 kryo.register(FlowEntryId.class);
170 kryo.register(FlowEntry.class);
171 kryo.register(FlowEntryMatch.class);
172 kryo.register(FlowEntryMatch.Field.class);
173 kryo.register(FlowEntrySwitchState.class);
174 kryo.register(FlowEntryUserState.class);
175 kryo.register(FlowId.class);
176 kryo.register(FlowPath.class);
177 kryo.register(FlowPathFlags.class);
178 kryo.register(FlowPathType.class);
179 kryo.register(FlowPathUserState.class);
180 kryo.register(IPv4.class);
181 kryo.register(IPv4Net.class);
182 kryo.register(IPv6.class);
183 kryo.register(IPv6Net.class);
184 kryo.register(byte[].class);
185 kryo.register(MACAddress.class);
186 kryo.register(Port.class);
187 kryo.register(Switch.class);
188 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700189
Ray Milkey269ffb92014-04-03 14:43:30 -0700190 // New data model-related classes
191 kryo.register(DeviceEvent.class);
192 kryo.register(InetAddress.class);
193 kryo.register(LinkEvent.class);
194 kryo.register(PortEvent.class);
195 kryo.register(PortEvent.SwitchPort.class);
196 kryo.register(SwitchEvent.class);
197 kryo.register(TopologyEvent.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800198
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 // Intent-related classes
200 kryo.register(Path.class);
201 kryo.register(Intent.class);
202 kryo.register(Intent.IntentState.class);
203 kryo.register(PathIntent.class);
204 kryo.register(ShortestPathIntent.class);
205 kryo.register(ConstrainedShortestPathIntent.class);
206 kryo.register(ErrorIntent.class);
207 kryo.register(ErrorIntent.ErrorType.class);
208 kryo.register(IntentOperation.class);
209 kryo.register(IntentOperation.Operator.class);
210 kryo.register(IntentOperationList.class);
211 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800212
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 // Device-related classes
214 kryo.register(HashSet.class);
215 kryo.register(Inet4Address.class);
216 kryo.register(OnosDevice.class);
217 kryo.register(Date.class);
TeruU7feef8a2014-04-03 00:15:49 -0700218
Ray Milkey269ffb92014-04-03 14:43:30 -0700219 // ProxyArp-related classes
Jonathan Harte6e63732014-04-16 14:29:49 -0700220 kryo.register(PacketOutNotification.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700221 kryo.register(BroadcastPacketOutNotification.class);
222 kryo.register(SinglePacketOutNotification.class);
223 kryo.register(ArpReplyNotification.class);
TeruU3c049c42014-04-15 10:13:25 -0700224 kryo.register(ArpCacheNotification.class);
TeruUd1c5b652014-03-24 13:58:46 -0700225
Ray Milkey269ffb92014-04-03 14:43:30 -0700226 return kryo;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700227 }
228}