blob: 4cd4a00ff9e7f4e534423728f46930bf99803a17 [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;
12import net.onrc.onos.apps.proxyarp.PacketOutNotification;
13import net.onrc.onos.apps.proxyarp.SinglePacketOutNotification;
Jonathan Hart23701d12014-04-03 10:45:48 -070014import net.onrc.onos.core.devicemanager.OnosDevice;
Jonathan Hartaa380972014-04-03 10:24:46 -070015import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
16import net.onrc.onos.core.intent.ErrorIntent;
17import net.onrc.onos.core.intent.Intent;
18import net.onrc.onos.core.intent.IntentOperation;
19import net.onrc.onos.core.intent.IntentOperationList;
20import net.onrc.onos.core.intent.PathIntent;
21import net.onrc.onos.core.intent.ShortestPathIntent;
22import net.onrc.onos.core.intent.runtime.IntentStateList;
Jonathan Hart23701d12014-04-03 10:45:48 -070023import net.onrc.onos.core.util.CallerId;
24import net.onrc.onos.core.util.DataPath;
25import net.onrc.onos.core.util.DataPathEndpoints;
26import net.onrc.onos.core.util.Dpid;
27import net.onrc.onos.core.util.FlowEntry;
28import net.onrc.onos.core.util.FlowEntryAction;
29import net.onrc.onos.core.util.FlowEntryActions;
30import net.onrc.onos.core.util.FlowEntryErrorState;
31import net.onrc.onos.core.util.FlowEntryId;
32import net.onrc.onos.core.util.FlowEntryMatch;
33import net.onrc.onos.core.util.FlowEntrySwitchState;
34import net.onrc.onos.core.util.FlowEntryUserState;
35import net.onrc.onos.core.util.FlowId;
36import net.onrc.onos.core.util.FlowPath;
37import net.onrc.onos.core.util.FlowPathFlags;
38import net.onrc.onos.core.util.FlowPathType;
39import net.onrc.onos.core.util.FlowPathUserState;
40import net.onrc.onos.core.util.IPv4;
41import net.onrc.onos.core.util.IPv4Net;
42import net.onrc.onos.core.util.IPv6;
43import net.onrc.onos.core.util.IPv6Net;
44import net.onrc.onos.core.util.Port;
45import net.onrc.onos.core.util.Switch;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080046import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
47import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koidebc116be2014-02-19 23:56:48 -080048import net.onrc.onos.ofcontroller.networkgraph.Path;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080049import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080050import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
51import net.onrc.onos.ofcontroller.networkgraph.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070052// import net.onrc.onos.core.util.SwitchPort;
53
54
Jonathan Hartd3003252013-11-15 09:44:46 -080055
Toshio Koide066506e2014-02-20 19:52:09 -080056
TeruUd1c5b652014-03-24 13:58:46 -070057
58
TeruU7feef8a2014-04-03 00:15:49 -070059
60
61
62
63
64
Jonathan Hart0961fe82014-04-03 09:56:25 -070065
Jonathan Hartaa380972014-04-03 10:24:46 -070066
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080067import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070068
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070069/**
70 * Class factory for allocating Kryo instances for
71 * serialization/deserialization of classes.
72 */
73public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070074 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070075 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
76
77 /**
78 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070079 *
80 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070081 */
82 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070083 this(DEFAULT_PREALLOCATIONS);
84 }
85
86 /**
87 * Constructor to explicitly specify number of Kryo instances to pool
88 *
89 * @param initialCapacity number of Kryo instance to preallocate
90 */
91 public KryoFactory(final int initialCapacity) {
92 // Preallocate
93 kryoList.ensureCapacity(initialCapacity);
94 for (int i = 0; i < initialCapacity; i++) {
95 Kryo kryo = newKryoImpl();
96 kryoList.add(kryo);
97 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070098 }
99
100 /**
101 * Create and initialize a new Kryo object.
102 *
103 * @return the created Kryo object.
104 */
105 public Kryo newKryo() {
106 return newDeleteKryo(null);
107 }
108
109 /**
110 * Delete an existing Kryo object.
111 *
112 * @param deleteKryo the object to delete.
113 */
114 public void deleteKryo(Kryo deleteKryo) {
115 newDeleteKryo(deleteKryo);
116 }
117
118 /**
119 * Create or delete a Kryo object.
120 *
121 * @param deleteKryo if null, then allocate and return a new object,
122 * otherwise delete the provided object.
123 * @return a new Kryo object if needed, otherwise null.
124 */
125 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
126 if (deleteKryo != null) {
127 // Delete an entry by moving it back to the buffer
128 kryoList.add(deleteKryo);
129 return null;
130 } else {
131 Kryo kryo = null;
132 if (kryoList.isEmpty()) {
133 // Preallocate
134 for (int i = 0; i < 100; i++) {
135 kryo = newKryoImpl();
136 kryoList.add(kryo);
137 }
138 }
139
140 kryo = kryoList.remove(kryoList.size() - 1);
141 return kryo;
142 }
143 }
144
145 /**
146 * Create and initialize a new Kryo object.
147 *
148 * @return the created Kryo object.
149 */
150 private Kryo newKryoImpl() {
151 Kryo kryo = new Kryo();
152 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700153 //
154 // WARNING: Order of register() calls affects serialized bytes.
155 // - Do no insert new entry in the middle, always add to the end.
156 // - Do not simply remove existing entry
157 //
158
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700159 // kryo.setReferences(false);
160 //
161 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700162
163 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700164 kryo.register(CallerId.class);
165 kryo.register(DataPath.class);
166 kryo.register(DataPathEndpoints.class);
167 kryo.register(Dpid.class);
168 kryo.register(FlowEntryAction.class);
169 kryo.register(FlowEntryAction.ActionEnqueue.class);
170 kryo.register(FlowEntryAction.ActionOutput.class);
171 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
172 kryo.register(FlowEntryAction.ActionSetIpToS.class);
173 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
174 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
175 kryo.register(FlowEntryAction.ActionSetVlanId.class);
176 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
177 kryo.register(FlowEntryAction.ActionStripVlan.class);
178 kryo.register(FlowEntryAction.ActionValues.class);
179 kryo.register(FlowEntryActions.class);
180 kryo.register(FlowEntryErrorState.class);
181 kryo.register(FlowEntryId.class);
182 kryo.register(FlowEntry.class);
183 kryo.register(FlowEntryMatch.class);
184 kryo.register(FlowEntryMatch.Field.class);
185 kryo.register(FlowEntrySwitchState.class);
186 kryo.register(FlowEntryUserState.class);
187 kryo.register(FlowId.class);
188 kryo.register(FlowPath.class);
189 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700190 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700191 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700192 kryo.register(IPv4.class);
193 kryo.register(IPv4Net.class);
194 kryo.register(IPv6.class);
195 kryo.register(IPv6Net.class);
196 kryo.register(byte[].class);
197 kryo.register(MACAddress.class);
198 kryo.register(Port.class);
199 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800200 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700201
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800202 // New data model-related classes
203 kryo.register(DeviceEvent.class);
204 kryo.register(InetAddress.class);
205 kryo.register(LinkEvent.class);
206 kryo.register(PortEvent.class);
207 kryo.register(PortEvent.SwitchPort.class);
208 kryo.register(SwitchEvent.class);
209 kryo.register(TopologyEvent.class);
210
Toshio Koideeb90d912014-02-18 21:30:22 -0800211 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800212 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800213 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800214 kryo.register(Intent.IntentState.class);
215 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800216 kryo.register(ShortestPathIntent.class);
217 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800218 kryo.register(ErrorIntent.class);
219 kryo.register(ErrorIntent.ErrorType.class);
220 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800221 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800222 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800223 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800224
TeruUd1c5b652014-03-24 13:58:46 -0700225 // Device-related classes
226 kryo.register(HashSet.class);
227 kryo.register(Inet4Address.class);
TeruU7feef8a2014-04-03 00:15:49 -0700228 kryo.register(OnosDevice.class);
229 kryo.register(Date.class);
230
231 // ProxyArp-related classes
232 kryo.register(BroadcastPacketOutNotification.class);
233 kryo.register(SinglePacketOutNotification.class);
234 kryo.register(ArpReplyNotification.class);
TeruUd1c5b652014-03-24 13:58:46 -0700235
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700236 return kryo;
237 }
238}