blob: 3590f672b8f9b148e927f8b96ed8c60510fe50cc [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;
Jonathan Hart0961fe82014-04-03 09:56:25 -070010import net.onrc.onos.apps.proxyarp.ArpReplyNotification;
11import net.onrc.onos.apps.proxyarp.BroadcastPacketOutNotification;
Jonathan Hart0961fe82014-04-03 09:56:25 -070012import net.onrc.onos.apps.proxyarp.SinglePacketOutNotification;
Jonathan Hart23701d12014-04-03 10:45:48 -070013import net.onrc.onos.core.devicemanager.OnosDevice;
Jonathan Hartaa380972014-04-03 10:24:46 -070014import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
15import net.onrc.onos.core.intent.ErrorIntent;
16import net.onrc.onos.core.intent.Intent;
17import net.onrc.onos.core.intent.IntentOperation;
18import net.onrc.onos.core.intent.IntentOperationList;
19import net.onrc.onos.core.intent.PathIntent;
20import net.onrc.onos.core.intent.ShortestPathIntent;
21import net.onrc.onos.core.intent.runtime.IntentStateList;
Jonathan Hart472062d2014-04-03 10:56:48 -070022import net.onrc.onos.core.topology.DeviceEvent;
23import net.onrc.onos.core.topology.LinkEvent;
24import net.onrc.onos.core.topology.Path;
25import net.onrc.onos.core.topology.PortEvent;
26import net.onrc.onos.core.topology.SwitchEvent;
27import net.onrc.onos.core.topology.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070028import net.onrc.onos.core.util.CallerId;
29import net.onrc.onos.core.util.DataPath;
30import net.onrc.onos.core.util.DataPathEndpoints;
31import net.onrc.onos.core.util.Dpid;
32import net.onrc.onos.core.util.FlowEntry;
33import net.onrc.onos.core.util.FlowEntryAction;
34import net.onrc.onos.core.util.FlowEntryActions;
35import net.onrc.onos.core.util.FlowEntryErrorState;
36import net.onrc.onos.core.util.FlowEntryId;
37import net.onrc.onos.core.util.FlowEntryMatch;
38import net.onrc.onos.core.util.FlowEntrySwitchState;
39import net.onrc.onos.core.util.FlowEntryUserState;
40import net.onrc.onos.core.util.FlowId;
41import net.onrc.onos.core.util.FlowPath;
42import net.onrc.onos.core.util.FlowPathFlags;
43import net.onrc.onos.core.util.FlowPathType;
44import net.onrc.onos.core.util.FlowPathUserState;
45import net.onrc.onos.core.util.IPv4;
46import net.onrc.onos.core.util.IPv4Net;
47import net.onrc.onos.core.util.IPv6;
48import net.onrc.onos.core.util.IPv6Net;
49import net.onrc.onos.core.util.Port;
50import net.onrc.onos.core.util.Switch;
Jonathan Hart23701d12014-04-03 10:45:48 -070051// import net.onrc.onos.core.util.SwitchPort;
52
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080053import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070054
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070055/**
56 * Class factory for allocating Kryo instances for
57 * serialization/deserialization of classes.
58 */
59public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070060 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070061 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
62
63 /**
64 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070065 *
66 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070067 */
68 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070069 this(DEFAULT_PREALLOCATIONS);
70 }
71
72 /**
73 * Constructor to explicitly specify number of Kryo instances to pool
74 *
75 * @param initialCapacity number of Kryo instance to preallocate
76 */
77 public KryoFactory(final int initialCapacity) {
78 // Preallocate
79 kryoList.ensureCapacity(initialCapacity);
80 for (int i = 0; i < initialCapacity; i++) {
81 Kryo kryo = newKryoImpl();
82 kryoList.add(kryo);
83 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070084 }
85
86 /**
87 * Create and initialize a new Kryo object.
88 *
89 * @return the created Kryo object.
90 */
91 public Kryo newKryo() {
92 return newDeleteKryo(null);
93 }
94
95 /**
96 * Delete an existing Kryo object.
97 *
98 * @param deleteKryo the object to delete.
99 */
100 public void deleteKryo(Kryo deleteKryo) {
101 newDeleteKryo(deleteKryo);
102 }
103
104 /**
105 * Create or delete a Kryo object.
106 *
107 * @param deleteKryo if null, then allocate and return a new object,
108 * otherwise delete the provided object.
109 * @return a new Kryo object if needed, otherwise null.
110 */
111 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
112 if (deleteKryo != null) {
113 // Delete an entry by moving it back to the buffer
114 kryoList.add(deleteKryo);
115 return null;
116 } else {
117 Kryo kryo = null;
118 if (kryoList.isEmpty()) {
119 // Preallocate
120 for (int i = 0; i < 100; i++) {
121 kryo = newKryoImpl();
122 kryoList.add(kryo);
123 }
124 }
125
126 kryo = kryoList.remove(kryoList.size() - 1);
127 return kryo;
128 }
129 }
130
131 /**
132 * Create and initialize a new Kryo object.
133 *
134 * @return the created Kryo object.
135 */
136 private Kryo newKryoImpl() {
137 Kryo kryo = new Kryo();
138 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700139 //
140 // WARNING: Order of register() calls affects serialized bytes.
141 // - Do no insert new entry in the middle, always add to the end.
142 // - Do not simply remove existing entry
143 //
144
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700145 // kryo.setReferences(false);
146 //
147 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700148
149 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700150 kryo.register(CallerId.class);
151 kryo.register(DataPath.class);
152 kryo.register(DataPathEndpoints.class);
153 kryo.register(Dpid.class);
154 kryo.register(FlowEntryAction.class);
155 kryo.register(FlowEntryAction.ActionEnqueue.class);
156 kryo.register(FlowEntryAction.ActionOutput.class);
157 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
158 kryo.register(FlowEntryAction.ActionSetIpToS.class);
159 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
160 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
161 kryo.register(FlowEntryAction.ActionSetVlanId.class);
162 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
163 kryo.register(FlowEntryAction.ActionStripVlan.class);
164 kryo.register(FlowEntryAction.ActionValues.class);
165 kryo.register(FlowEntryActions.class);
166 kryo.register(FlowEntryErrorState.class);
167 kryo.register(FlowEntryId.class);
168 kryo.register(FlowEntry.class);
169 kryo.register(FlowEntryMatch.class);
170 kryo.register(FlowEntryMatch.Field.class);
171 kryo.register(FlowEntrySwitchState.class);
172 kryo.register(FlowEntryUserState.class);
173 kryo.register(FlowId.class);
174 kryo.register(FlowPath.class);
175 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700176 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700177 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700178 kryo.register(IPv4.class);
179 kryo.register(IPv4Net.class);
180 kryo.register(IPv6.class);
181 kryo.register(IPv6Net.class);
182 kryo.register(byte[].class);
183 kryo.register(MACAddress.class);
184 kryo.register(Port.class);
185 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800186 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700187
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800188 // New data model-related classes
189 kryo.register(DeviceEvent.class);
190 kryo.register(InetAddress.class);
191 kryo.register(LinkEvent.class);
192 kryo.register(PortEvent.class);
193 kryo.register(PortEvent.SwitchPort.class);
194 kryo.register(SwitchEvent.class);
195 kryo.register(TopologyEvent.class);
196
Toshio Koideeb90d912014-02-18 21:30:22 -0800197 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800198 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800199 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800200 kryo.register(Intent.IntentState.class);
201 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800202 kryo.register(ShortestPathIntent.class);
203 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800204 kryo.register(ErrorIntent.class);
205 kryo.register(ErrorIntent.ErrorType.class);
206 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800207 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800208 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800209 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800210
TeruUd1c5b652014-03-24 13:58:46 -0700211 // Device-related classes
212 kryo.register(HashSet.class);
213 kryo.register(Inet4Address.class);
TeruU7feef8a2014-04-03 00:15:49 -0700214 kryo.register(OnosDevice.class);
215 kryo.register(Date.class);
216
217 // ProxyArp-related classes
218 kryo.register(BroadcastPacketOutNotification.class);
219 kryo.register(SinglePacketOutNotification.class);
220 kryo.register(ArpReplyNotification.class);
TeruUd1c5b652014-03-24 13:58:46 -0700221
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700222 return kryo;
223 }
224}