blob: 4475a7288137550835623ef50bbc1ee1ee1f2374 [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 Hart472062d2014-04-03 10:56:48 -070023import net.onrc.onos.core.topology.DeviceEvent;
24import net.onrc.onos.core.topology.LinkEvent;
25import net.onrc.onos.core.topology.Path;
26import net.onrc.onos.core.topology.PortEvent;
27import net.onrc.onos.core.topology.SwitchEvent;
28import net.onrc.onos.core.topology.TopologyEvent;
Jonathan Hart23701d12014-04-03 10:45:48 -070029import net.onrc.onos.core.util.CallerId;
30import net.onrc.onos.core.util.DataPath;
31import net.onrc.onos.core.util.DataPathEndpoints;
32import net.onrc.onos.core.util.Dpid;
33import net.onrc.onos.core.util.FlowEntry;
34import net.onrc.onos.core.util.FlowEntryAction;
35import net.onrc.onos.core.util.FlowEntryActions;
36import net.onrc.onos.core.util.FlowEntryErrorState;
37import net.onrc.onos.core.util.FlowEntryId;
38import net.onrc.onos.core.util.FlowEntryMatch;
39import net.onrc.onos.core.util.FlowEntrySwitchState;
40import net.onrc.onos.core.util.FlowEntryUserState;
41import net.onrc.onos.core.util.FlowId;
42import net.onrc.onos.core.util.FlowPath;
43import net.onrc.onos.core.util.FlowPathFlags;
44import net.onrc.onos.core.util.FlowPathType;
45import net.onrc.onos.core.util.FlowPathUserState;
46import net.onrc.onos.core.util.IPv4;
47import net.onrc.onos.core.util.IPv4Net;
48import net.onrc.onos.core.util.IPv6;
49import net.onrc.onos.core.util.IPv6Net;
50import net.onrc.onos.core.util.Port;
51import net.onrc.onos.core.util.Switch;
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
Jonathan Hart472062d2014-04-03 10:56:48 -070067
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080068import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070069
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070070/**
71 * Class factory for allocating Kryo instances for
72 * serialization/deserialization of classes.
73 */
74public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070075 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070076 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
77
78 /**
79 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070080 *
81 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070082 */
83 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070084 this(DEFAULT_PREALLOCATIONS);
85 }
86
87 /**
88 * Constructor to explicitly specify number of Kryo instances to pool
89 *
90 * @param initialCapacity number of Kryo instance to preallocate
91 */
92 public KryoFactory(final int initialCapacity) {
93 // Preallocate
94 kryoList.ensureCapacity(initialCapacity);
95 for (int i = 0; i < initialCapacity; i++) {
96 Kryo kryo = newKryoImpl();
97 kryoList.add(kryo);
98 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070099 }
100
101 /**
102 * Create and initialize a new Kryo object.
103 *
104 * @return the created Kryo object.
105 */
106 public Kryo newKryo() {
107 return newDeleteKryo(null);
108 }
109
110 /**
111 * Delete an existing Kryo object.
112 *
113 * @param deleteKryo the object to delete.
114 */
115 public void deleteKryo(Kryo deleteKryo) {
116 newDeleteKryo(deleteKryo);
117 }
118
119 /**
120 * Create or delete a Kryo object.
121 *
122 * @param deleteKryo if null, then allocate and return a new object,
123 * otherwise delete the provided object.
124 * @return a new Kryo object if needed, otherwise null.
125 */
126 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
127 if (deleteKryo != null) {
128 // Delete an entry by moving it back to the buffer
129 kryoList.add(deleteKryo);
130 return null;
131 } else {
132 Kryo kryo = null;
133 if (kryoList.isEmpty()) {
134 // Preallocate
135 for (int i = 0; i < 100; i++) {
136 kryo = newKryoImpl();
137 kryoList.add(kryo);
138 }
139 }
140
141 kryo = kryoList.remove(kryoList.size() - 1);
142 return kryo;
143 }
144 }
145
146 /**
147 * Create and initialize a new Kryo object.
148 *
149 * @return the created Kryo object.
150 */
151 private Kryo newKryoImpl() {
152 Kryo kryo = new Kryo();
153 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700154 //
155 // WARNING: Order of register() calls affects serialized bytes.
156 // - Do no insert new entry in the middle, always add to the end.
157 // - Do not simply remove existing entry
158 //
159
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700160 // kryo.setReferences(false);
161 //
162 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700163
164 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700165 kryo.register(CallerId.class);
166 kryo.register(DataPath.class);
167 kryo.register(DataPathEndpoints.class);
168 kryo.register(Dpid.class);
169 kryo.register(FlowEntryAction.class);
170 kryo.register(FlowEntryAction.ActionEnqueue.class);
171 kryo.register(FlowEntryAction.ActionOutput.class);
172 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
173 kryo.register(FlowEntryAction.ActionSetIpToS.class);
174 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
175 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
176 kryo.register(FlowEntryAction.ActionSetVlanId.class);
177 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
178 kryo.register(FlowEntryAction.ActionStripVlan.class);
179 kryo.register(FlowEntryAction.ActionValues.class);
180 kryo.register(FlowEntryActions.class);
181 kryo.register(FlowEntryErrorState.class);
182 kryo.register(FlowEntryId.class);
183 kryo.register(FlowEntry.class);
184 kryo.register(FlowEntryMatch.class);
185 kryo.register(FlowEntryMatch.Field.class);
186 kryo.register(FlowEntrySwitchState.class);
187 kryo.register(FlowEntryUserState.class);
188 kryo.register(FlowId.class);
189 kryo.register(FlowPath.class);
190 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700191 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700192 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700193 kryo.register(IPv4.class);
194 kryo.register(IPv4Net.class);
195 kryo.register(IPv6.class);
196 kryo.register(IPv6Net.class);
197 kryo.register(byte[].class);
198 kryo.register(MACAddress.class);
199 kryo.register(Port.class);
200 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800201 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700202
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800203 // New data model-related classes
204 kryo.register(DeviceEvent.class);
205 kryo.register(InetAddress.class);
206 kryo.register(LinkEvent.class);
207 kryo.register(PortEvent.class);
208 kryo.register(PortEvent.SwitchPort.class);
209 kryo.register(SwitchEvent.class);
210 kryo.register(TopologyEvent.class);
211
Toshio Koideeb90d912014-02-18 21:30:22 -0800212 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800213 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800214 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800215 kryo.register(Intent.IntentState.class);
216 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800217 kryo.register(ShortestPathIntent.class);
218 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800219 kryo.register(ErrorIntent.class);
220 kryo.register(ErrorIntent.ErrorType.class);
221 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800222 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800223 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800224 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800225
TeruUd1c5b652014-03-24 13:58:46 -0700226 // Device-related classes
227 kryo.register(HashSet.class);
228 kryo.register(Inet4Address.class);
TeruU7feef8a2014-04-03 00:15:49 -0700229 kryo.register(OnosDevice.class);
230 kryo.register(Date.class);
231
232 // ProxyArp-related classes
233 kryo.register(BroadcastPacketOutNotification.class);
234 kryo.register(SinglePacketOutNotification.class);
235 kryo.register(ArpReplyNotification.class);
TeruUd1c5b652014-03-24 13:58:46 -0700236
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700237 return kryo;
238 }
239}