blob: 059287751512e44fe25ed5f2aa4c1e407fb8364e [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;
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;
Toshio Koideeb90d912014-02-18 21:30:22 -080014import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koidedf2eab92014-02-20 11:24:59 -080015import net.onrc.onos.intent.ErrorIntent;
Toshio Koideeb90d912014-02-18 21:30:22 -080016import net.onrc.onos.intent.Intent;
17import net.onrc.onos.intent.IntentOperation;
18import net.onrc.onos.intent.IntentOperationList;
19import net.onrc.onos.intent.PathIntent;
20import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koide066506e2014-02-20 19:52:09 -080021import net.onrc.onos.intent.runtime.IntentStateList;
TeruU7feef8a2014-04-03 00:15:49 -070022import net.onrc.onos.ofcontroller.devicemanager.OnosDevice;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080023import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
24import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koidebc116be2014-02-19 23:56:48 -080025import net.onrc.onos.ofcontroller.networkgraph.Path;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080026import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080027import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
28import net.onrc.onos.ofcontroller.networkgraph.TopologyEvent;
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
Jonathan Hart0961fe82014-04-03 09:56:25 -070063
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080064import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070065
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070066/**
67 * Class factory for allocating Kryo instances for
68 * serialization/deserialization of classes.
69 */
70public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070071 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070072 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
73
74 /**
75 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070076 *
77 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070078 */
79 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070080 this(DEFAULT_PREALLOCATIONS);
81 }
82
83 /**
84 * Constructor to explicitly specify number of Kryo instances to pool
85 *
86 * @param initialCapacity number of Kryo instance to preallocate
87 */
88 public KryoFactory(final int initialCapacity) {
89 // Preallocate
90 kryoList.ensureCapacity(initialCapacity);
91 for (int i = 0; i < initialCapacity; i++) {
92 Kryo kryo = newKryoImpl();
93 kryoList.add(kryo);
94 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070095 }
96
97 /**
98 * Create and initialize a new Kryo object.
99 *
100 * @return the created Kryo object.
101 */
102 public Kryo newKryo() {
103 return newDeleteKryo(null);
104 }
105
106 /**
107 * Delete an existing Kryo object.
108 *
109 * @param deleteKryo the object to delete.
110 */
111 public void deleteKryo(Kryo deleteKryo) {
112 newDeleteKryo(deleteKryo);
113 }
114
115 /**
116 * Create or delete a Kryo object.
117 *
118 * @param deleteKryo if null, then allocate and return a new object,
119 * otherwise delete the provided object.
120 * @return a new Kryo object if needed, otherwise null.
121 */
122 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
123 if (deleteKryo != null) {
124 // Delete an entry by moving it back to the buffer
125 kryoList.add(deleteKryo);
126 return null;
127 } else {
128 Kryo kryo = null;
129 if (kryoList.isEmpty()) {
130 // Preallocate
131 for (int i = 0; i < 100; i++) {
132 kryo = newKryoImpl();
133 kryoList.add(kryo);
134 }
135 }
136
137 kryo = kryoList.remove(kryoList.size() - 1);
138 return kryo;
139 }
140 }
141
142 /**
143 * Create and initialize a new Kryo object.
144 *
145 * @return the created Kryo object.
146 */
147 private Kryo newKryoImpl() {
148 Kryo kryo = new Kryo();
149 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700150 //
151 // WARNING: Order of register() calls affects serialized bytes.
152 // - Do no insert new entry in the middle, always add to the end.
153 // - Do not simply remove existing entry
154 //
155
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700156 // kryo.setReferences(false);
157 //
158 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700159
160 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700161 kryo.register(CallerId.class);
162 kryo.register(DataPath.class);
163 kryo.register(DataPathEndpoints.class);
164 kryo.register(Dpid.class);
165 kryo.register(FlowEntryAction.class);
166 kryo.register(FlowEntryAction.ActionEnqueue.class);
167 kryo.register(FlowEntryAction.ActionOutput.class);
168 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
169 kryo.register(FlowEntryAction.ActionSetIpToS.class);
170 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
171 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
172 kryo.register(FlowEntryAction.ActionSetVlanId.class);
173 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
174 kryo.register(FlowEntryAction.ActionStripVlan.class);
175 kryo.register(FlowEntryAction.ActionValues.class);
176 kryo.register(FlowEntryActions.class);
177 kryo.register(FlowEntryErrorState.class);
178 kryo.register(FlowEntryId.class);
179 kryo.register(FlowEntry.class);
180 kryo.register(FlowEntryMatch.class);
181 kryo.register(FlowEntryMatch.Field.class);
182 kryo.register(FlowEntrySwitchState.class);
183 kryo.register(FlowEntryUserState.class);
184 kryo.register(FlowId.class);
185 kryo.register(FlowPath.class);
186 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700187 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700188 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700189 kryo.register(IPv4.class);
190 kryo.register(IPv4Net.class);
191 kryo.register(IPv6.class);
192 kryo.register(IPv6Net.class);
193 kryo.register(byte[].class);
194 kryo.register(MACAddress.class);
195 kryo.register(Port.class);
196 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800197 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700198
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800199 // New data model-related classes
200 kryo.register(DeviceEvent.class);
201 kryo.register(InetAddress.class);
202 kryo.register(LinkEvent.class);
203 kryo.register(PortEvent.class);
204 kryo.register(PortEvent.SwitchPort.class);
205 kryo.register(SwitchEvent.class);
206 kryo.register(TopologyEvent.class);
207
Toshio Koideeb90d912014-02-18 21:30:22 -0800208 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800209 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800210 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800211 kryo.register(Intent.IntentState.class);
212 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800213 kryo.register(ShortestPathIntent.class);
214 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800215 kryo.register(ErrorIntent.class);
216 kryo.register(ErrorIntent.ErrorType.class);
217 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800218 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800219 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800220 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800221
TeruUd1c5b652014-03-24 13:58:46 -0700222 // Device-related classes
223 kryo.register(HashSet.class);
224 kryo.register(Inet4Address.class);
TeruU7feef8a2014-04-03 00:15:49 -0700225 kryo.register(OnosDevice.class);
226 kryo.register(Date.class);
227
228 // ProxyArp-related classes
229 kryo.register(BroadcastPacketOutNotification.class);
230 kryo.register(SinglePacketOutNotification.class);
231 kryo.register(ArpReplyNotification.class);
TeruUd1c5b652014-03-24 13:58:46 -0700232
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700233 return kryo;
234 }
235}