blob: 9425b7e09ac626f711fcc4dc72519416c4c5bb88 [file] [log] [blame]
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07001package net.onrc.onos.ofcontroller.util.serializers;
2
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;
Toshio Koideeb90d912014-02-18 21:30:22 -080010import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koidedf2eab92014-02-20 11:24:59 -080011import net.onrc.onos.intent.ErrorIntent;
Toshio Koideeb90d912014-02-18 21:30:22 -080012import net.onrc.onos.intent.Intent;
13import net.onrc.onos.intent.IntentOperation;
14import net.onrc.onos.intent.IntentOperationList;
15import net.onrc.onos.intent.PathIntent;
16import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koide066506e2014-02-20 19:52:09 -080017import net.onrc.onos.intent.runtime.IntentStateList;
TeruU7feef8a2014-04-03 00:15:49 -070018import net.onrc.onos.ofcontroller.devicemanager.OnosDevice;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080019import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
20import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koidebc116be2014-02-19 23:56:48 -080021import net.onrc.onos.ofcontroller.networkgraph.Path;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080022import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080023import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
24import net.onrc.onos.ofcontroller.networkgraph.TopologyEvent;
TeruU7feef8a2014-04-03 00:15:49 -070025import net.onrc.onos.ofcontroller.proxyarp.ArpReplyNotification;
26import net.onrc.onos.ofcontroller.proxyarp.BroadcastPacketOutNotification;
27import net.onrc.onos.ofcontroller.proxyarp.PacketOutNotification;
28import net.onrc.onos.ofcontroller.proxyarp.SinglePacketOutNotification;
Jonathan Hartd3003252013-11-15 09:44:46 -080029import net.onrc.onos.ofcontroller.util.CallerId;
30import net.onrc.onos.ofcontroller.util.DataPath;
31import net.onrc.onos.ofcontroller.util.DataPathEndpoints;
32import net.onrc.onos.ofcontroller.util.Dpid;
33import net.onrc.onos.ofcontroller.util.FlowEntry;
34import net.onrc.onos.ofcontroller.util.FlowEntryAction;
35import net.onrc.onos.ofcontroller.util.FlowEntryActions;
36import net.onrc.onos.ofcontroller.util.FlowEntryErrorState;
37import net.onrc.onos.ofcontroller.util.FlowEntryId;
38import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
39import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
40import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
41import net.onrc.onos.ofcontroller.util.FlowId;
42import net.onrc.onos.ofcontroller.util.FlowPath;
43import net.onrc.onos.ofcontroller.util.FlowPathFlags;
44import net.onrc.onos.ofcontroller.util.FlowPathType;
45import net.onrc.onos.ofcontroller.util.FlowPathUserState;
46import net.onrc.onos.ofcontroller.util.IPv4;
47import net.onrc.onos.ofcontroller.util.IPv4Net;
48import net.onrc.onos.ofcontroller.util.IPv6;
49import net.onrc.onos.ofcontroller.util.IPv6Net;
50import net.onrc.onos.ofcontroller.util.Port;
51import net.onrc.onos.ofcontroller.util.Switch;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080052// import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartd3003252013-11-15 09:44:46 -080053
Toshio Koide066506e2014-02-20 19:52:09 -080054
TeruUd1c5b652014-03-24 13:58:46 -070055
56
TeruU7feef8a2014-04-03 00:15:49 -070057
58
59
60
61
62
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080063import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070064
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070065/**
66 * Class factory for allocating Kryo instances for
67 * serialization/deserialization of classes.
68 */
69public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070070 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070071 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
72
73 /**
74 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070075 *
76 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070077 */
78 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070079 this(DEFAULT_PREALLOCATIONS);
80 }
81
82 /**
83 * Constructor to explicitly specify number of Kryo instances to pool
84 *
85 * @param initialCapacity number of Kryo instance to preallocate
86 */
87 public KryoFactory(final int initialCapacity) {
88 // Preallocate
89 kryoList.ensureCapacity(initialCapacity);
90 for (int i = 0; i < initialCapacity; i++) {
91 Kryo kryo = newKryoImpl();
92 kryoList.add(kryo);
93 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070094 }
95
96 /**
97 * Create and initialize a new Kryo object.
98 *
99 * @return the created Kryo object.
100 */
101 public Kryo newKryo() {
102 return newDeleteKryo(null);
103 }
104
105 /**
106 * Delete an existing Kryo object.
107 *
108 * @param deleteKryo the object to delete.
109 */
110 public void deleteKryo(Kryo deleteKryo) {
111 newDeleteKryo(deleteKryo);
112 }
113
114 /**
115 * Create or delete a Kryo object.
116 *
117 * @param deleteKryo if null, then allocate and return a new object,
118 * otherwise delete the provided object.
119 * @return a new Kryo object if needed, otherwise null.
120 */
121 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
122 if (deleteKryo != null) {
123 // Delete an entry by moving it back to the buffer
124 kryoList.add(deleteKryo);
125 return null;
126 } else {
127 Kryo kryo = null;
128 if (kryoList.isEmpty()) {
129 // Preallocate
130 for (int i = 0; i < 100; i++) {
131 kryo = newKryoImpl();
132 kryoList.add(kryo);
133 }
134 }
135
136 kryo = kryoList.remove(kryoList.size() - 1);
137 return kryo;
138 }
139 }
140
141 /**
142 * Create and initialize a new Kryo object.
143 *
144 * @return the created Kryo object.
145 */
146 private Kryo newKryoImpl() {
147 Kryo kryo = new Kryo();
148 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700149 //
150 // WARNING: Order of register() calls affects serialized bytes.
151 // - Do no insert new entry in the middle, always add to the end.
152 // - Do not simply remove existing entry
153 //
154
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700155 // kryo.setReferences(false);
156 //
157 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700158
159 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700160 kryo.register(CallerId.class);
161 kryo.register(DataPath.class);
162 kryo.register(DataPathEndpoints.class);
163 kryo.register(Dpid.class);
164 kryo.register(FlowEntryAction.class);
165 kryo.register(FlowEntryAction.ActionEnqueue.class);
166 kryo.register(FlowEntryAction.ActionOutput.class);
167 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
168 kryo.register(FlowEntryAction.ActionSetIpToS.class);
169 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
170 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
171 kryo.register(FlowEntryAction.ActionSetVlanId.class);
172 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
173 kryo.register(FlowEntryAction.ActionStripVlan.class);
174 kryo.register(FlowEntryAction.ActionValues.class);
175 kryo.register(FlowEntryActions.class);
176 kryo.register(FlowEntryErrorState.class);
177 kryo.register(FlowEntryId.class);
178 kryo.register(FlowEntry.class);
179 kryo.register(FlowEntryMatch.class);
180 kryo.register(FlowEntryMatch.Field.class);
181 kryo.register(FlowEntrySwitchState.class);
182 kryo.register(FlowEntryUserState.class);
183 kryo.register(FlowId.class);
184 kryo.register(FlowPath.class);
185 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700186 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700187 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700188 kryo.register(IPv4.class);
189 kryo.register(IPv4Net.class);
190 kryo.register(IPv6.class);
191 kryo.register(IPv6Net.class);
192 kryo.register(byte[].class);
193 kryo.register(MACAddress.class);
194 kryo.register(Port.class);
195 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800196 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700197
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800198 // New data model-related classes
199 kryo.register(DeviceEvent.class);
200 kryo.register(InetAddress.class);
201 kryo.register(LinkEvent.class);
202 kryo.register(PortEvent.class);
203 kryo.register(PortEvent.SwitchPort.class);
204 kryo.register(SwitchEvent.class);
205 kryo.register(TopologyEvent.class);
206
Toshio Koideeb90d912014-02-18 21:30:22 -0800207 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800208 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800209 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800210 kryo.register(Intent.IntentState.class);
211 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800212 kryo.register(ShortestPathIntent.class);
213 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800214 kryo.register(ErrorIntent.class);
215 kryo.register(ErrorIntent.ErrorType.class);
216 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800217 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800218 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800219 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800220
TeruUd1c5b652014-03-24 13:58:46 -0700221 // Device-related classes
222 kryo.register(HashSet.class);
223 kryo.register(Inet4Address.class);
TeruU7feef8a2014-04-03 00:15:49 -0700224 kryo.register(OnosDevice.class);
225 kryo.register(Date.class);
226
227 // ProxyArp-related classes
228 kryo.register(BroadcastPacketOutNotification.class);
229 kryo.register(SinglePacketOutNotification.class);
230 kryo.register(ArpReplyNotification.class);
TeruUd1c5b652014-03-24 13:58:46 -0700231
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700232 return kryo;
233 }
234}