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 | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 7 | import java.util.HashMap; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 8 | import java.util.HashSet; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 9 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 10 | import net.floodlightcontroller.util.MACAddress; |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 11 | import net.onrc.onos.apps.proxyarp.ArpCacheNotification; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 12 | import net.onrc.onos.apps.proxyarp.ArpReplyNotification; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.devicemanager.OnosDevice; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.intent.ConstrainedShortestPathIntent; |
| 15 | import net.onrc.onos.core.intent.ErrorIntent; |
| 16 | import net.onrc.onos.core.intent.Intent; |
| 17 | import net.onrc.onos.core.intent.IntentOperation; |
| 18 | import net.onrc.onos.core.intent.IntentOperationList; |
Yuta HIGUCHI | 1fc395e | 2014-05-13 14:06:28 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.intent.Path; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 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 | 313fdf0 | 2014-04-10 14:09:46 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification; |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.packetservice.PacketOutNotification; |
Jonathan Hart | 313fdf0 | 2014-04-10 14:09:46 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.packetservice.SinglePacketOutNotification; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.topology.DeviceEvent; |
| 27 | import net.onrc.onos.core.topology.LinkEvent; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.topology.PortEvent; |
| 29 | import net.onrc.onos.core.topology.SwitchEvent; |
| 30 | import net.onrc.onos.core.topology.TopologyEvent; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.util.CallerId; |
| 32 | import net.onrc.onos.core.util.DataPath; |
| 33 | import net.onrc.onos.core.util.DataPathEndpoints; |
| 34 | import net.onrc.onos.core.util.Dpid; |
| 35 | import net.onrc.onos.core.util.FlowEntry; |
| 36 | import net.onrc.onos.core.util.FlowEntryAction; |
| 37 | import net.onrc.onos.core.util.FlowEntryActions; |
| 38 | import net.onrc.onos.core.util.FlowEntryErrorState; |
| 39 | import net.onrc.onos.core.util.FlowEntryId; |
| 40 | import net.onrc.onos.core.util.FlowEntryMatch; |
| 41 | import net.onrc.onos.core.util.FlowEntrySwitchState; |
| 42 | import net.onrc.onos.core.util.FlowEntryUserState; |
| 43 | import net.onrc.onos.core.util.FlowId; |
| 44 | import net.onrc.onos.core.util.FlowPath; |
| 45 | import net.onrc.onos.core.util.FlowPathFlags; |
| 46 | import net.onrc.onos.core.util.FlowPathType; |
| 47 | import net.onrc.onos.core.util.FlowPathUserState; |
| 48 | import net.onrc.onos.core.util.IPv4; |
| 49 | import net.onrc.onos.core.util.IPv4Net; |
| 50 | import net.onrc.onos.core.util.IPv6; |
| 51 | import net.onrc.onos.core.util.IPv6Net; |
| 52 | import net.onrc.onos.core.util.Port; |
| 53 | import net.onrc.onos.core.util.Switch; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 54 | // import net.onrc.onos.core.util.SwitchPort; |
| 55 | |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 56 | import com.esotericsoftware.kryo.Kryo; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 57 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Class factory for allocating Kryo instances for |
| 60 | * serialization/deserialization of classes. |
| 61 | */ |
| 62 | public class KryoFactory { |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 63 | private static final int DEFAULT_PREALLOCATIONS = 100; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 64 | private ArrayList<Kryo> kryoList = new ArrayList<Kryo>(); |
| 65 | |
| 66 | /** |
| 67 | * Default constructor. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | * <p/> |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 69 | * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 70 | */ |
| 71 | public KryoFactory() { |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 72 | this(DEFAULT_PREALLOCATIONS); |
| 73 | } |
| 74 | |
| 75 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 76 | * Constructor to explicitly specify number of Kryo instances to pool. |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 77 | * |
| 78 | * @param initialCapacity number of Kryo instance to preallocate |
| 79 | */ |
| 80 | public KryoFactory(final int initialCapacity) { |
| 81 | // Preallocate |
| 82 | kryoList.ensureCapacity(initialCapacity); |
| 83 | for (int i = 0; i < initialCapacity; i++) { |
| 84 | Kryo kryo = newKryoImpl(); |
| 85 | kryoList.add(kryo); |
| 86 | } |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create and initialize a new Kryo object. |
| 91 | * |
| 92 | * @return the created Kryo object. |
| 93 | */ |
| 94 | public Kryo newKryo() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | return newDeleteKryo(null); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Delete an existing Kryo object. |
| 100 | * |
| 101 | * @param deleteKryo the object to delete. |
| 102 | */ |
| 103 | public void deleteKryo(Kryo deleteKryo) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | newDeleteKryo(deleteKryo); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Create or delete a Kryo object. |
| 109 | * |
| 110 | * @param deleteKryo if null, then allocate and return a new object, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | * otherwise delete the provided object. |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 112 | * @return a new Kryo object if needed, otherwise null. |
| 113 | */ |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 114 | private synchronized Kryo newDeleteKryo(Kryo deleteKryo) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | if (deleteKryo != null) { |
| 116 | // Delete an entry by moving it back to the buffer |
| 117 | kryoList.add(deleteKryo); |
| 118 | return null; |
| 119 | } else { |
| 120 | Kryo kryo = null; |
| 121 | if (kryoList.isEmpty()) { |
| 122 | // Preallocate |
| 123 | for (int i = 0; i < 100; i++) { |
| 124 | kryo = newKryoImpl(); |
| 125 | kryoList.add(kryo); |
| 126 | } |
| 127 | } |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 128 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | kryo = kryoList.remove(kryoList.size() - 1); |
| 130 | return kryo; |
| 131 | } |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Create and initialize a new Kryo object. |
| 136 | * |
| 137 | * @return the created Kryo object. |
| 138 | */ |
| 139 | private Kryo newKryoImpl() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | Kryo kryo = new Kryo(); |
| 141 | kryo.setRegistrationRequired(true); |
| 142 | // |
| 143 | // WARNING: Order of register() calls affects serialized bytes. |
| 144 | // - Do no insert new entry in the middle, always add to the end. |
| 145 | // - Do not simply remove existing entry |
| 146 | // |
Yuta HIGUCHI | 0af53d2 | 2014-03-31 14:01:35 -0700 | [diff] [blame] | 147 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | // kryo.setReferences(false); |
| 149 | // |
| 150 | kryo.register(ArrayList.class); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 151 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | // FlowPath and related classes |
| 153 | kryo.register(CallerId.class); |
| 154 | kryo.register(DataPath.class); |
| 155 | kryo.register(DataPathEndpoints.class); |
| 156 | kryo.register(Dpid.class); |
| 157 | kryo.register(FlowEntryAction.class); |
| 158 | kryo.register(FlowEntryAction.ActionEnqueue.class); |
| 159 | kryo.register(FlowEntryAction.ActionOutput.class); |
| 160 | kryo.register(FlowEntryAction.ActionSetEthernetAddr.class); |
| 161 | kryo.register(FlowEntryAction.ActionSetIpToS.class); |
| 162 | kryo.register(FlowEntryAction.ActionSetIPv4Addr.class); |
| 163 | kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class); |
| 164 | kryo.register(FlowEntryAction.ActionSetVlanId.class); |
| 165 | kryo.register(FlowEntryAction.ActionSetVlanPriority.class); |
| 166 | kryo.register(FlowEntryAction.ActionStripVlan.class); |
| 167 | kryo.register(FlowEntryAction.ActionValues.class); |
| 168 | kryo.register(FlowEntryActions.class); |
| 169 | kryo.register(FlowEntryErrorState.class); |
| 170 | kryo.register(FlowEntryId.class); |
| 171 | kryo.register(FlowEntry.class); |
| 172 | kryo.register(FlowEntryMatch.class); |
| 173 | kryo.register(FlowEntryMatch.Field.class); |
| 174 | kryo.register(FlowEntrySwitchState.class); |
| 175 | kryo.register(FlowEntryUserState.class); |
| 176 | kryo.register(FlowId.class); |
| 177 | kryo.register(FlowPath.class); |
| 178 | kryo.register(FlowPathFlags.class); |
| 179 | kryo.register(FlowPathType.class); |
| 180 | kryo.register(FlowPathUserState.class); |
| 181 | kryo.register(IPv4.class); |
| 182 | kryo.register(IPv4Net.class); |
| 183 | kryo.register(IPv6.class); |
| 184 | kryo.register(IPv6Net.class); |
| 185 | kryo.register(byte[].class); |
| 186 | kryo.register(MACAddress.class); |
| 187 | kryo.register(Port.class); |
| 188 | kryo.register(Switch.class); |
| 189 | // kryo.register(SwitchPort.class); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 190 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | // New data model-related classes |
| 192 | kryo.register(DeviceEvent.class); |
| 193 | kryo.register(InetAddress.class); |
| 194 | kryo.register(LinkEvent.class); |
| 195 | kryo.register(PortEvent.class); |
| 196 | kryo.register(PortEvent.SwitchPort.class); |
| 197 | kryo.register(SwitchEvent.class); |
| 198 | kryo.register(TopologyEvent.class); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 199 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 200 | // Intent-related classes |
| 201 | kryo.register(Path.class); |
| 202 | kryo.register(Intent.class); |
| 203 | kryo.register(Intent.IntentState.class); |
| 204 | kryo.register(PathIntent.class); |
| 205 | kryo.register(ShortestPathIntent.class); |
| 206 | kryo.register(ConstrainedShortestPathIntent.class); |
| 207 | kryo.register(ErrorIntent.class); |
| 208 | kryo.register(ErrorIntent.ErrorType.class); |
| 209 | kryo.register(IntentOperation.class); |
| 210 | kryo.register(IntentOperation.Operator.class); |
| 211 | kryo.register(IntentOperationList.class); |
| 212 | kryo.register(IntentStateList.class); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 213 | kryo.register(HashMap.class); |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 214 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 215 | // Device-related classes |
| 216 | kryo.register(HashSet.class); |
| 217 | kryo.register(Inet4Address.class); |
| 218 | kryo.register(OnosDevice.class); |
| 219 | kryo.register(Date.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 220 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 221 | // ProxyArp-related classes |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 222 | kryo.register(PacketOutNotification.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 223 | kryo.register(BroadcastPacketOutNotification.class); |
| 224 | kryo.register(SinglePacketOutNotification.class); |
| 225 | kryo.register(ArpReplyNotification.class); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 226 | kryo.register(ArpCacheNotification.class); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 227 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 228 | return kryo; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 229 | } |
| 230 | } |