blob: 42a3a4d84e1ab76df2fb1b7db1506bf4feee5d9d [file] [log] [blame]
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07001package net.onrc.onos.ofcontroller.util.serializers;
2
TeruUd1c5b652014-03-24 13:58:46 -07003import java.net.Inet4Address;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08004import java.net.InetAddress;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07005import java.util.ArrayList;
TeruUd1c5b652014-03-24 13:58:46 -07006import java.util.HashSet;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07007import net.floodlightcontroller.util.MACAddress;
Toshio Koideeb90d912014-02-18 21:30:22 -08008import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koidedf2eab92014-02-20 11:24:59 -08009import net.onrc.onos.intent.ErrorIntent;
Toshio Koideeb90d912014-02-18 21:30:22 -080010import net.onrc.onos.intent.Intent;
11import net.onrc.onos.intent.IntentOperation;
12import net.onrc.onos.intent.IntentOperationList;
13import net.onrc.onos.intent.PathIntent;
14import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koide066506e2014-02-20 19:52:09 -080015import net.onrc.onos.intent.runtime.IntentStateList;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080016import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
17import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koidebc116be2014-02-19 23:56:48 -080018import net.onrc.onos.ofcontroller.networkgraph.Path;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080019import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080020import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
21import net.onrc.onos.ofcontroller.networkgraph.TopologyEvent;
Jonathan Hartd3003252013-11-15 09:44:46 -080022import net.onrc.onos.ofcontroller.util.CallerId;
23import net.onrc.onos.ofcontroller.util.DataPath;
24import net.onrc.onos.ofcontroller.util.DataPathEndpoints;
25import net.onrc.onos.ofcontroller.util.Dpid;
26import net.onrc.onos.ofcontroller.util.FlowEntry;
27import net.onrc.onos.ofcontroller.util.FlowEntryAction;
28import net.onrc.onos.ofcontroller.util.FlowEntryActions;
29import net.onrc.onos.ofcontroller.util.FlowEntryErrorState;
30import net.onrc.onos.ofcontroller.util.FlowEntryId;
31import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
32import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
33import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
34import net.onrc.onos.ofcontroller.util.FlowId;
35import net.onrc.onos.ofcontroller.util.FlowPath;
36import net.onrc.onos.ofcontroller.util.FlowPathFlags;
37import net.onrc.onos.ofcontroller.util.FlowPathType;
38import net.onrc.onos.ofcontroller.util.FlowPathUserState;
39import net.onrc.onos.ofcontroller.util.IPv4;
40import net.onrc.onos.ofcontroller.util.IPv4Net;
41import net.onrc.onos.ofcontroller.util.IPv6;
42import net.onrc.onos.ofcontroller.util.IPv6Net;
43import net.onrc.onos.ofcontroller.util.Port;
44import net.onrc.onos.ofcontroller.util.Switch;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080045// import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartd3003252013-11-15 09:44:46 -080046
Toshio Koide066506e2014-02-20 19:52:09 -080047
TeruUd1c5b652014-03-24 13:58:46 -070048
49
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080050import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070051
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070052/**
53 * Class factory for allocating Kryo instances for
54 * serialization/deserialization of classes.
55 */
56public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070057 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070058 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
59
60 /**
61 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070062 *
63 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070064 */
65 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070066 this(DEFAULT_PREALLOCATIONS);
67 }
68
69 /**
70 * Constructor to explicitly specify number of Kryo instances to pool
71 *
72 * @param initialCapacity number of Kryo instance to preallocate
73 */
74 public KryoFactory(final int initialCapacity) {
75 // Preallocate
76 kryoList.ensureCapacity(initialCapacity);
77 for (int i = 0; i < initialCapacity; i++) {
78 Kryo kryo = newKryoImpl();
79 kryoList.add(kryo);
80 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070081 }
82
83 /**
84 * Create and initialize a new Kryo object.
85 *
86 * @return the created Kryo object.
87 */
88 public Kryo newKryo() {
89 return newDeleteKryo(null);
90 }
91
92 /**
93 * Delete an existing Kryo object.
94 *
95 * @param deleteKryo the object to delete.
96 */
97 public void deleteKryo(Kryo deleteKryo) {
98 newDeleteKryo(deleteKryo);
99 }
100
101 /**
102 * Create or delete a Kryo object.
103 *
104 * @param deleteKryo if null, then allocate and return a new object,
105 * otherwise delete the provided object.
106 * @return a new Kryo object if needed, otherwise null.
107 */
108 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
109 if (deleteKryo != null) {
110 // Delete an entry by moving it back to the buffer
111 kryoList.add(deleteKryo);
112 return null;
113 } else {
114 Kryo kryo = null;
115 if (kryoList.isEmpty()) {
116 // Preallocate
117 for (int i = 0; i < 100; i++) {
118 kryo = newKryoImpl();
119 kryoList.add(kryo);
120 }
121 }
122
123 kryo = kryoList.remove(kryoList.size() - 1);
124 return kryo;
125 }
126 }
127
128 /**
129 * Create and initialize a new Kryo object.
130 *
131 * @return the created Kryo object.
132 */
133 private Kryo newKryoImpl() {
134 Kryo kryo = new Kryo();
135 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700136 //
137 // WARNING: Order of register() calls affects serialized bytes.
138 // - Do no insert new entry in the middle, always add to the end.
139 // - Do not simply remove existing entry
140 //
141
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700142 // kryo.setReferences(false);
143 //
144 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700145
146 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700147 kryo.register(CallerId.class);
148 kryo.register(DataPath.class);
149 kryo.register(DataPathEndpoints.class);
150 kryo.register(Dpid.class);
151 kryo.register(FlowEntryAction.class);
152 kryo.register(FlowEntryAction.ActionEnqueue.class);
153 kryo.register(FlowEntryAction.ActionOutput.class);
154 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
155 kryo.register(FlowEntryAction.ActionSetIpToS.class);
156 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
157 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
158 kryo.register(FlowEntryAction.ActionSetVlanId.class);
159 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
160 kryo.register(FlowEntryAction.ActionStripVlan.class);
161 kryo.register(FlowEntryAction.ActionValues.class);
162 kryo.register(FlowEntryActions.class);
163 kryo.register(FlowEntryErrorState.class);
164 kryo.register(FlowEntryId.class);
165 kryo.register(FlowEntry.class);
166 kryo.register(FlowEntryMatch.class);
167 kryo.register(FlowEntryMatch.Field.class);
168 kryo.register(FlowEntrySwitchState.class);
169 kryo.register(FlowEntryUserState.class);
170 kryo.register(FlowId.class);
171 kryo.register(FlowPath.class);
172 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700173 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700174 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700175 kryo.register(IPv4.class);
176 kryo.register(IPv4Net.class);
177 kryo.register(IPv6.class);
178 kryo.register(IPv6Net.class);
179 kryo.register(byte[].class);
180 kryo.register(MACAddress.class);
181 kryo.register(Port.class);
182 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800183 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700184
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800185 // New data model-related classes
186 kryo.register(DeviceEvent.class);
187 kryo.register(InetAddress.class);
188 kryo.register(LinkEvent.class);
189 kryo.register(PortEvent.class);
190 kryo.register(PortEvent.SwitchPort.class);
191 kryo.register(SwitchEvent.class);
192 kryo.register(TopologyEvent.class);
193
Toshio Koideeb90d912014-02-18 21:30:22 -0800194 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800195 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800196 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800197 kryo.register(Intent.IntentState.class);
198 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800199 kryo.register(ShortestPathIntent.class);
200 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800201 kryo.register(ErrorIntent.class);
202 kryo.register(ErrorIntent.ErrorType.class);
203 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800204 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800205 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800206 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800207
TeruUd1c5b652014-03-24 13:58:46 -0700208 // Device-related classes
209 kryo.register(HashSet.class);
210 kryo.register(Inet4Address.class);
211
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700212 return kryo;
213 }
214}