Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.util.serializers; |
| 2 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 3 | import java.net.InetAddress; |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 4 | import java.util.ArrayList; |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 5 | import java.util.TreeMap; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 6 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 7 | import net.floodlightcontroller.util.MACAddress; |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 8 | import net.onrc.onos.intent.ConstrainedShortestPathIntent; |
| 9 | import net.onrc.onos.intent.Intent; |
| 10 | import net.onrc.onos.intent.IntentOperation; |
| 11 | import net.onrc.onos.intent.IntentOperationList; |
| 12 | import net.onrc.onos.intent.PathIntent; |
| 13 | import net.onrc.onos.intent.ShortestPathIntent; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 14 | import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent; |
| 15 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
| 16 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent; |
| 17 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort; |
| 18 | import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent; |
| 19 | import net.onrc.onos.ofcontroller.networkgraph.TopologyEvent; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.util.CallerId; |
| 22 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 23 | import net.onrc.onos.ofcontroller.util.DataPathEndpoints; |
| 24 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 25 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 26 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 27 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
| 28 | import net.onrc.onos.ofcontroller.util.FlowEntryErrorState; |
| 29 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 30 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 31 | import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState; |
| 32 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
| 33 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 34 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 35 | import net.onrc.onos.ofcontroller.util.FlowPathFlags; |
| 36 | import net.onrc.onos.ofcontroller.util.FlowPathType; |
| 37 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
| 38 | import net.onrc.onos.ofcontroller.util.IPv4; |
| 39 | import net.onrc.onos.ofcontroller.util.IPv4Net; |
| 40 | import net.onrc.onos.ofcontroller.util.IPv6; |
| 41 | import net.onrc.onos.ofcontroller.util.IPv6Net; |
| 42 | import net.onrc.onos.ofcontroller.util.Port; |
| 43 | import net.onrc.onos.ofcontroller.util.Switch; |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 44 | // import net.onrc.onos.ofcontroller.util.SwitchPort; |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 45 | |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 46 | |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 47 | import com.esotericsoftware.kryo.Kryo; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 48 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Class factory for allocating Kryo instances for |
| 51 | * serialization/deserialization of classes. |
| 52 | */ |
| 53 | public class KryoFactory { |
| 54 | private ArrayList<Kryo> kryoList = new ArrayList<Kryo>(); |
| 55 | |
| 56 | /** |
| 57 | * Default constructor. |
| 58 | */ |
| 59 | public KryoFactory() { |
| 60 | Kryo kryo; |
| 61 | // Preallocate |
| 62 | for (int i = 0; i < 100; i++) { |
| 63 | kryo = newKryoImpl(); |
| 64 | kryoList.add(kryo); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create and initialize a new Kryo object. |
| 70 | * |
| 71 | * @return the created Kryo object. |
| 72 | */ |
| 73 | public Kryo newKryo() { |
| 74 | return newDeleteKryo(null); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Delete an existing Kryo object. |
| 79 | * |
| 80 | * @param deleteKryo the object to delete. |
| 81 | */ |
| 82 | public void deleteKryo(Kryo deleteKryo) { |
| 83 | newDeleteKryo(deleteKryo); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create or delete a Kryo object. |
| 88 | * |
| 89 | * @param deleteKryo if null, then allocate and return a new object, |
| 90 | * otherwise delete the provided object. |
| 91 | * @return a new Kryo object if needed, otherwise null. |
| 92 | */ |
| 93 | synchronized private Kryo newDeleteKryo(Kryo deleteKryo) { |
| 94 | if (deleteKryo != null) { |
| 95 | // Delete an entry by moving it back to the buffer |
| 96 | kryoList.add(deleteKryo); |
| 97 | return null; |
| 98 | } else { |
| 99 | Kryo kryo = null; |
| 100 | if (kryoList.isEmpty()) { |
| 101 | // Preallocate |
| 102 | for (int i = 0; i < 100; i++) { |
| 103 | kryo = newKryoImpl(); |
| 104 | kryoList.add(kryo); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | kryo = kryoList.remove(kryoList.size() - 1); |
| 109 | return kryo; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Create and initialize a new Kryo object. |
| 115 | * |
| 116 | * @return the created Kryo object. |
| 117 | */ |
| 118 | private Kryo newKryoImpl() { |
| 119 | Kryo kryo = new Kryo(); |
| 120 | kryo.setRegistrationRequired(true); |
| 121 | // kryo.setReferences(false); |
| 122 | // |
| 123 | kryo.register(ArrayList.class); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 124 | |
| 125 | // FlowPath and related classes |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 126 | kryo.register(CallerId.class); |
| 127 | kryo.register(DataPath.class); |
| 128 | kryo.register(DataPathEndpoints.class); |
| 129 | kryo.register(Dpid.class); |
| 130 | kryo.register(FlowEntryAction.class); |
| 131 | kryo.register(FlowEntryAction.ActionEnqueue.class); |
| 132 | kryo.register(FlowEntryAction.ActionOutput.class); |
| 133 | kryo.register(FlowEntryAction.ActionSetEthernetAddr.class); |
| 134 | kryo.register(FlowEntryAction.ActionSetIpToS.class); |
| 135 | kryo.register(FlowEntryAction.ActionSetIPv4Addr.class); |
| 136 | kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class); |
| 137 | kryo.register(FlowEntryAction.ActionSetVlanId.class); |
| 138 | kryo.register(FlowEntryAction.ActionSetVlanPriority.class); |
| 139 | kryo.register(FlowEntryAction.ActionStripVlan.class); |
| 140 | kryo.register(FlowEntryAction.ActionValues.class); |
| 141 | kryo.register(FlowEntryActions.class); |
| 142 | kryo.register(FlowEntryErrorState.class); |
| 143 | kryo.register(FlowEntryId.class); |
| 144 | kryo.register(FlowEntry.class); |
| 145 | kryo.register(FlowEntryMatch.class); |
| 146 | kryo.register(FlowEntryMatch.Field.class); |
| 147 | kryo.register(FlowEntrySwitchState.class); |
| 148 | kryo.register(FlowEntryUserState.class); |
| 149 | kryo.register(FlowId.class); |
| 150 | kryo.register(FlowPath.class); |
| 151 | kryo.register(FlowPathFlags.class); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 152 | kryo.register(FlowPathType.class); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 153 | kryo.register(FlowPathUserState.class); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 154 | kryo.register(IPv4.class); |
| 155 | kryo.register(IPv4Net.class); |
| 156 | kryo.register(IPv6.class); |
| 157 | kryo.register(IPv6Net.class); |
| 158 | kryo.register(byte[].class); |
| 159 | kryo.register(MACAddress.class); |
| 160 | kryo.register(Port.class); |
| 161 | kryo.register(Switch.class); |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 162 | // kryo.register(SwitchPort.class); |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 163 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 164 | // Topology-related classes |
| 165 | kryo.register(TopologyElement.class); |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 166 | kryo.register(TopologyElement.Type.class); |
| 167 | kryo.register(TreeMap.class); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 168 | |
Pavlin Radoslavov | 45ec04b | 2014-02-14 23:29:33 -0800 | [diff] [blame] | 169 | // New data model-related classes |
| 170 | kryo.register(DeviceEvent.class); |
| 171 | kryo.register(InetAddress.class); |
| 172 | kryo.register(LinkEvent.class); |
| 173 | kryo.register(PortEvent.class); |
| 174 | kryo.register(PortEvent.SwitchPort.class); |
| 175 | kryo.register(SwitchEvent.class); |
| 176 | kryo.register(TopologyEvent.class); |
| 177 | |
Toshio Koide | eb90d91 | 2014-02-18 21:30:22 -0800 | [diff] [blame] | 178 | // Intent-related classes |
| 179 | kryo.register(IntentOperationList.class); |
| 180 | kryo.register(IntentOperation.class); |
| 181 | kryo.register(PathIntent.class); |
| 182 | kryo.register(ShortestPathIntent.class); |
| 183 | kryo.register(ConstrainedShortestPathIntent.class); |
| 184 | kryo.register(Intent.IntentState.class); |
| 185 | kryo.register(long[].class); |
| 186 | kryo.register(IntentOperation.Operator.class); |
| 187 | |
Pavlin Radoslavov | e0a643e | 2013-10-24 13:33:39 -0700 | [diff] [blame] | 188 | return kryo; |
| 189 | } |
| 190 | } |