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