blob: 748749f805b56f23608841c21b1963cfbc7176b6 [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;
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;
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
Jonathan Hartaa380972014-04-03 10:24:46 -070064
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080065import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070066
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070067/**
68 * Class factory for allocating Kryo instances for
69 * serialization/deserialization of classes.
70 */
71public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070072 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070073 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
74
75 /**
76 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070077 *
78 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070079 */
80 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070081 this(DEFAULT_PREALLOCATIONS);
82 }
83
84 /**
85 * Constructor to explicitly specify number of Kryo instances to pool
86 *
87 * @param initialCapacity number of Kryo instance to preallocate
88 */
89 public KryoFactory(final int initialCapacity) {
90 // Preallocate
91 kryoList.ensureCapacity(initialCapacity);
92 for (int i = 0; i < initialCapacity; i++) {
93 Kryo kryo = newKryoImpl();
94 kryoList.add(kryo);
95 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070096 }
97
98 /**
99 * Create and initialize a new Kryo object.
100 *
101 * @return the created Kryo object.
102 */
103 public Kryo newKryo() {
104 return newDeleteKryo(null);
105 }
106
107 /**
108 * Delete an existing Kryo object.
109 *
110 * @param deleteKryo the object to delete.
111 */
112 public void deleteKryo(Kryo deleteKryo) {
113 newDeleteKryo(deleteKryo);
114 }
115
116 /**
117 * Create or delete a Kryo object.
118 *
119 * @param deleteKryo if null, then allocate and return a new object,
120 * otherwise delete the provided object.
121 * @return a new Kryo object if needed, otherwise null.
122 */
123 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
124 if (deleteKryo != null) {
125 // Delete an entry by moving it back to the buffer
126 kryoList.add(deleteKryo);
127 return null;
128 } else {
129 Kryo kryo = null;
130 if (kryoList.isEmpty()) {
131 // Preallocate
132 for (int i = 0; i < 100; i++) {
133 kryo = newKryoImpl();
134 kryoList.add(kryo);
135 }
136 }
137
138 kryo = kryoList.remove(kryoList.size() - 1);
139 return kryo;
140 }
141 }
142
143 /**
144 * Create and initialize a new Kryo object.
145 *
146 * @return the created Kryo object.
147 */
148 private Kryo newKryoImpl() {
149 Kryo kryo = new Kryo();
150 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700151 //
152 // WARNING: Order of register() calls affects serialized bytes.
153 // - Do no insert new entry in the middle, always add to the end.
154 // - Do not simply remove existing entry
155 //
156
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700157 // kryo.setReferences(false);
158 //
159 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700160
161 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700162 kryo.register(CallerId.class);
163 kryo.register(DataPath.class);
164 kryo.register(DataPathEndpoints.class);
165 kryo.register(Dpid.class);
166 kryo.register(FlowEntryAction.class);
167 kryo.register(FlowEntryAction.ActionEnqueue.class);
168 kryo.register(FlowEntryAction.ActionOutput.class);
169 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
170 kryo.register(FlowEntryAction.ActionSetIpToS.class);
171 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
172 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
173 kryo.register(FlowEntryAction.ActionSetVlanId.class);
174 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
175 kryo.register(FlowEntryAction.ActionStripVlan.class);
176 kryo.register(FlowEntryAction.ActionValues.class);
177 kryo.register(FlowEntryActions.class);
178 kryo.register(FlowEntryErrorState.class);
179 kryo.register(FlowEntryId.class);
180 kryo.register(FlowEntry.class);
181 kryo.register(FlowEntryMatch.class);
182 kryo.register(FlowEntryMatch.Field.class);
183 kryo.register(FlowEntrySwitchState.class);
184 kryo.register(FlowEntryUserState.class);
185 kryo.register(FlowId.class);
186 kryo.register(FlowPath.class);
187 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700188 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700189 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700190 kryo.register(IPv4.class);
191 kryo.register(IPv4Net.class);
192 kryo.register(IPv6.class);
193 kryo.register(IPv6Net.class);
194 kryo.register(byte[].class);
195 kryo.register(MACAddress.class);
196 kryo.register(Port.class);
197 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800198 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700199
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800200 // New data model-related classes
201 kryo.register(DeviceEvent.class);
202 kryo.register(InetAddress.class);
203 kryo.register(LinkEvent.class);
204 kryo.register(PortEvent.class);
205 kryo.register(PortEvent.SwitchPort.class);
206 kryo.register(SwitchEvent.class);
207 kryo.register(TopologyEvent.class);
208
Toshio Koideeb90d912014-02-18 21:30:22 -0800209 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800210 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800211 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800212 kryo.register(Intent.IntentState.class);
213 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800214 kryo.register(ShortestPathIntent.class);
215 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800216 kryo.register(ErrorIntent.class);
217 kryo.register(ErrorIntent.ErrorType.class);
218 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800219 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800220 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800221 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800222
TeruUd1c5b652014-03-24 13:58:46 -0700223 // Device-related classes
224 kryo.register(HashSet.class);
225 kryo.register(Inet4Address.class);
TeruU7feef8a2014-04-03 00:15:49 -0700226 kryo.register(OnosDevice.class);
227 kryo.register(Date.class);
228
229 // ProxyArp-related classes
230 kryo.register(BroadcastPacketOutNotification.class);
231 kryo.register(SinglePacketOutNotification.class);
232 kryo.register(ArpReplyNotification.class);
TeruUd1c5b652014-03-24 13:58:46 -0700233
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700234 return kryo;
235 }
236}