Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util.serializers; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 2 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 3 | import java.net.Inet4Address; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 4 | import java.net.InetAddress; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 5 | import java.util.ArrayList; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 6 | import java.util.Date; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 7 | import java.util.HashSet; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 8 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 9 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 10 | import net.onrc.onos.apps.proxyarp.ArpReplyNotification; |
| 11 | import net.onrc.onos.apps.proxyarp.BroadcastPacketOutNotification; |
| 12 | import net.onrc.onos.apps.proxyarp.PacketOutNotification; |
| 13 | import net.onrc.onos.apps.proxyarp.SinglePacketOutNotification; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.devicemanager.OnosDevice; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.intent.ConstrainedShortestPathIntent; |
| 16 | import net.onrc.onos.core.intent.ErrorIntent; |
| 17 | import net.onrc.onos.core.intent.Intent; |
| 18 | import net.onrc.onos.core.intent.IntentOperation; |
| 19 | import net.onrc.onos.core.intent.IntentOperationList; |
| 20 | import net.onrc.onos.core.intent.PathIntent; |
| 21 | import net.onrc.onos.core.intent.ShortestPathIntent; |
| 22 | import net.onrc.onos.core.intent.runtime.IntentStateList; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame^] | 23 | import net.onrc.onos.core.topology.DeviceEvent; |
| 24 | import net.onrc.onos.core.topology.LinkEvent; |
| 25 | import net.onrc.onos.core.topology.Path; |
| 26 | import net.onrc.onos.core.topology.PortEvent; |
| 27 | import net.onrc.onos.core.topology.SwitchEvent; |
| 28 | import net.onrc.onos.core.topology.TopologyEvent; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.util.CallerId; |
| 30 | import net.onrc.onos.core.util.DataPath; |
| 31 | import net.onrc.onos.core.util.DataPathEndpoints; |
| 32 | import net.onrc.onos.core.util.Dpid; |
| 33 | import net.onrc.onos.core.util.FlowEntry; |
| 34 | import net.onrc.onos.core.util.FlowEntryAction; |
| 35 | import net.onrc.onos.core.util.FlowEntryActions; |
| 36 | import net.onrc.onos.core.util.FlowEntryErrorState; |
| 37 | import net.onrc.onos.core.util.FlowEntryId; |
| 38 | import net.onrc.onos.core.util.FlowEntryMatch; |
| 39 | import net.onrc.onos.core.util.FlowEntrySwitchState; |
| 40 | import net.onrc.onos.core.util.FlowEntryUserState; |
| 41 | import net.onrc.onos.core.util.FlowId; |
| 42 | import net.onrc.onos.core.util.FlowPath; |
| 43 | import net.onrc.onos.core.util.FlowPathFlags; |
| 44 | import net.onrc.onos.core.util.FlowPathType; |
| 45 | import net.onrc.onos.core.util.FlowPathUserState; |
| 46 | import net.onrc.onos.core.util.IPv4; |
| 47 | import net.onrc.onos.core.util.IPv4Net; |
| 48 | import net.onrc.onos.core.util.IPv6; |
| 49 | import net.onrc.onos.core.util.IPv6Net; |
| 50 | import net.onrc.onos.core.util.Port; |
| 51 | import net.onrc.onos.core.util.Switch; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 52 | // import net.onrc.onos.core.util.SwitchPort; |
| 53 | |
| 54 | |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 55 | |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 56 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 57 | |
| 58 | |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 65 | |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 66 | |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame^] | 67 | |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 68 | import com.esotericsoftware.kryo.Kryo; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 69 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Class factory for allocating Kryo instances for |
| 72 | * serialization/deserialization of classes. |
| 73 | */ |
| 74 | public class KryoFactory { |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 75 | private static final int DEFAULT_PREALLOCATIONS = 100; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 76 | private ArrayList<Kryo> kryoList = new ArrayList<Kryo>(); |
| 77 | |
| 78 | /** |
| 79 | * Default constructor. |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 80 | * |
| 81 | * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 82 | */ |
| 83 | public KryoFactory() { |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 84 | 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 Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 99 | } |
| 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 HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 154 | // |
| 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 Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 160 | // kryo.setReferences(false); |
| 161 | // |
| 162 | kryo.register(ArrayList.class); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 163 | |
| 164 | // FlowPath and related classes |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 165 | 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 Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 191 | kryo.register(FlowPathType.class); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 192 | kryo.register(FlowPathUserState.class); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 193 | 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 Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 201 | // kryo.register(SwitchPort.class); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 202 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 203 | // 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 Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 212 | // Intent-related classes |
Toshio Koide | fece1ce | 2014-02-26 17:12:10 -0800 | [diff] [blame] | 213 | kryo.register(Path.class); |
Toshio Koide | b39c9d3 | 2014-02-20 01:21:47 -0800 | [diff] [blame] | 214 | kryo.register(Intent.class); |
Toshio Koide | fece1ce | 2014-02-26 17:12:10 -0800 | [diff] [blame] | 215 | kryo.register(Intent.IntentState.class); |
| 216 | kryo.register(PathIntent.class); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 217 | kryo.register(ShortestPathIntent.class); |
| 218 | kryo.register(ConstrainedShortestPathIntent.class); |
Toshio Koide | fece1ce | 2014-02-26 17:12:10 -0800 | [diff] [blame] | 219 | kryo.register(ErrorIntent.class); |
| 220 | kryo.register(ErrorIntent.ErrorType.class); |
| 221 | kryo.register(IntentOperation.class); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 222 | kryo.register(IntentOperation.Operator.class); |
Toshio Koide | fece1ce | 2014-02-26 17:12:10 -0800 | [diff] [blame] | 223 | kryo.register(IntentOperationList.class); |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 224 | kryo.register(IntentStateList.class); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 225 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 226 | // Device-related classes |
| 227 | kryo.register(HashSet.class); |
| 228 | kryo.register(Inet4Address.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 229 | 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); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 236 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 237 | return kryo; |
| 238 | } |
| 239 | } |