Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.util.serializers; |
| 2 | |
| 3 | import java.util.ArrayList; |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 4 | import java.util.TreeMap; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 5 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 6 | import com.esotericsoftware.kryo2.Kryo; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 7 | |
| 8 | import net.floodlightcontroller.util.MACAddress; |
| 9 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 10 | import net.onrc.onos.ofcontroller.util.*; |
| 11 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
| 12 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 13 | /** |
| 14 | * Class factory for allocating Kryo instances for |
| 15 | * serialization/deserialization of classes. |
| 16 | */ |
| 17 | public class KryoFactory { |
| 18 | private ArrayList<Kryo> kryoList = new ArrayList<Kryo>(); |
| 19 | |
| 20 | /** |
| 21 | * Default constructor. |
| 22 | */ |
| 23 | public KryoFactory() { |
| 24 | Kryo kryo; |
| 25 | // Preallocate |
| 26 | for (int i = 0; i < 100; i++) { |
| 27 | kryo = newKryoImpl(); |
| 28 | kryoList.add(kryo); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Create and initialize a new Kryo object. |
| 34 | * |
| 35 | * @return the created Kryo object. |
| 36 | */ |
| 37 | public Kryo newKryo() { |
| 38 | return newDeleteKryo(null); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Delete an existing Kryo object. |
| 43 | * |
| 44 | * @param deleteKryo the object to delete. |
| 45 | */ |
| 46 | public void deleteKryo(Kryo deleteKryo) { |
| 47 | newDeleteKryo(deleteKryo); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Create or delete a Kryo object. |
| 52 | * |
| 53 | * @param deleteKryo if null, then allocate and return a new object, |
| 54 | * otherwise delete the provided object. |
| 55 | * @return a new Kryo object if needed, otherwise null. |
| 56 | */ |
| 57 | synchronized private Kryo newDeleteKryo(Kryo deleteKryo) { |
| 58 | if (deleteKryo != null) { |
| 59 | // Delete an entry by moving it back to the buffer |
| 60 | kryoList.add(deleteKryo); |
| 61 | return null; |
| 62 | } else { |
| 63 | Kryo kryo = null; |
| 64 | if (kryoList.isEmpty()) { |
| 65 | // Preallocate |
| 66 | for (int i = 0; i < 100; i++) { |
| 67 | kryo = newKryoImpl(); |
| 68 | kryoList.add(kryo); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | kryo = kryoList.remove(kryoList.size() - 1); |
| 73 | return kryo; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Create and initialize a new Kryo object. |
| 79 | * |
| 80 | * @return the created Kryo object. |
| 81 | */ |
| 82 | private Kryo newKryoImpl() { |
| 83 | Kryo kryo = new Kryo(); |
| 84 | kryo.setRegistrationRequired(true); |
| 85 | // kryo.setReferences(false); |
| 86 | // |
| 87 | kryo.register(ArrayList.class); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 88 | |
| 89 | // FlowPath and related classes |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 90 | kryo.register(CallerId.class); |
| 91 | kryo.register(DataPath.class); |
| 92 | kryo.register(DataPathEndpoints.class); |
| 93 | kryo.register(Dpid.class); |
| 94 | kryo.register(FlowEntryAction.class); |
| 95 | kryo.register(FlowEntryAction.ActionEnqueue.class); |
| 96 | kryo.register(FlowEntryAction.ActionOutput.class); |
| 97 | kryo.register(FlowEntryAction.ActionSetEthernetAddr.class); |
| 98 | kryo.register(FlowEntryAction.ActionSetIpToS.class); |
| 99 | kryo.register(FlowEntryAction.ActionSetIPv4Addr.class); |
| 100 | kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class); |
| 101 | kryo.register(FlowEntryAction.ActionSetVlanId.class); |
| 102 | kryo.register(FlowEntryAction.ActionSetVlanPriority.class); |
| 103 | kryo.register(FlowEntryAction.ActionStripVlan.class); |
| 104 | kryo.register(FlowEntryAction.ActionValues.class); |
| 105 | kryo.register(FlowEntryActions.class); |
| 106 | kryo.register(FlowEntryErrorState.class); |
| 107 | kryo.register(FlowEntryId.class); |
| 108 | kryo.register(FlowEntry.class); |
| 109 | kryo.register(FlowEntryMatch.class); |
| 110 | kryo.register(FlowEntryMatch.Field.class); |
| 111 | kryo.register(FlowEntrySwitchState.class); |
| 112 | kryo.register(FlowEntryUserState.class); |
| 113 | kryo.register(FlowId.class); |
| 114 | kryo.register(FlowPath.class); |
| 115 | kryo.register(FlowPathFlags.class); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 116 | kryo.register(FlowPathType.class); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 117 | kryo.register(FlowPathUserState.class); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 118 | kryo.register(IPv4.class); |
| 119 | kryo.register(IPv4Net.class); |
| 120 | kryo.register(IPv6.class); |
| 121 | kryo.register(IPv6Net.class); |
| 122 | kryo.register(byte[].class); |
| 123 | kryo.register(MACAddress.class); |
| 124 | kryo.register(Port.class); |
| 125 | kryo.register(Switch.class); |
| 126 | kryo.register(SwitchPort.class); |
| 127 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 128 | // Topology-related classes |
| 129 | kryo.register(TopologyElement.class); |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 130 | kryo.register(TopologyElement.Type.class); |
| 131 | kryo.register(TreeMap.class); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 132 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 133 | return kryo; |
| 134 | } |
| 135 | } |