blob: 9eb4e10421b7e98f5e11643592f6e121312dc411 [file] [log] [blame]
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07001package net.onrc.onos.ofcontroller.util.serializers;
2
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -08003import java.net.InetAddress;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07004import java.util.ArrayList;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -07005
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07006import net.floodlightcontroller.util.MACAddress;
Toshio Koideeb90d912014-02-18 21:30:22 -08007import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koidedf2eab92014-02-20 11:24:59 -08008import net.onrc.onos.intent.ErrorIntent;
Toshio Koideeb90d912014-02-18 21:30:22 -08009import net.onrc.onos.intent.Intent;
10import net.onrc.onos.intent.IntentOperation;
11import net.onrc.onos.intent.IntentOperationList;
12import net.onrc.onos.intent.PathIntent;
13import net.onrc.onos.intent.ShortestPathIntent;
Toshio Koide066506e2014-02-20 19:52:09 -080014import net.onrc.onos.intent.runtime.IntentStateList;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080015import net.onrc.onos.ofcontroller.networkgraph.DeviceEvent;
16import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
Toshio Koidebc116be2014-02-19 23:56:48 -080017import net.onrc.onos.ofcontroller.networkgraph.Path;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080018import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080019import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
20import net.onrc.onos.ofcontroller.networkgraph.TopologyEvent;
Jonathan Hartd3003252013-11-15 09:44:46 -080021import net.onrc.onos.ofcontroller.util.CallerId;
22import net.onrc.onos.ofcontroller.util.DataPath;
23import net.onrc.onos.ofcontroller.util.DataPathEndpoints;
24import net.onrc.onos.ofcontroller.util.Dpid;
25import net.onrc.onos.ofcontroller.util.FlowEntry;
26import net.onrc.onos.ofcontroller.util.FlowEntryAction;
27import net.onrc.onos.ofcontroller.util.FlowEntryActions;
28import net.onrc.onos.ofcontroller.util.FlowEntryErrorState;
29import net.onrc.onos.ofcontroller.util.FlowEntryId;
30import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
31import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
32import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
33import net.onrc.onos.ofcontroller.util.FlowId;
34import net.onrc.onos.ofcontroller.util.FlowPath;
35import net.onrc.onos.ofcontroller.util.FlowPathFlags;
36import net.onrc.onos.ofcontroller.util.FlowPathType;
37import net.onrc.onos.ofcontroller.util.FlowPathUserState;
38import net.onrc.onos.ofcontroller.util.IPv4;
39import net.onrc.onos.ofcontroller.util.IPv4Net;
40import net.onrc.onos.ofcontroller.util.IPv6;
41import net.onrc.onos.ofcontroller.util.IPv6Net;
42import net.onrc.onos.ofcontroller.util.Port;
43import net.onrc.onos.ofcontroller.util.Switch;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080044// import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartd3003252013-11-15 09:44:46 -080045
Toshio Koide066506e2014-02-20 19:52:09 -080046
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080047import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070048
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070049/**
50 * Class factory for allocating Kryo instances for
51 * serialization/deserialization of classes.
52 */
53public class KryoFactory {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070054 private static final int DEFAULT_PREALLOCATIONS = 100;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070055 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
56
57 /**
58 * Default constructor.
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070059 *
60 * Preallocates {@code DEFAULT_PREALLOCATIONS} Kryo instances.
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070061 */
62 public KryoFactory() {
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -070063 this(DEFAULT_PREALLOCATIONS);
64 }
65
66 /**
67 * Constructor to explicitly specify number of Kryo instances to pool
68 *
69 * @param initialCapacity number of Kryo instance to preallocate
70 */
71 public KryoFactory(final int initialCapacity) {
72 // Preallocate
73 kryoList.ensureCapacity(initialCapacity);
74 for (int i = 0; i < initialCapacity; i++) {
75 Kryo kryo = newKryoImpl();
76 kryoList.add(kryo);
77 }
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070078 }
79
80 /**
81 * Create and initialize a new Kryo object.
82 *
83 * @return the created Kryo object.
84 */
85 public Kryo newKryo() {
86 return newDeleteKryo(null);
87 }
88
89 /**
90 * Delete an existing Kryo object.
91 *
92 * @param deleteKryo the object to delete.
93 */
94 public void deleteKryo(Kryo deleteKryo) {
95 newDeleteKryo(deleteKryo);
96 }
97
98 /**
99 * Create or delete a Kryo object.
100 *
101 * @param deleteKryo if null, then allocate and return a new object,
102 * otherwise delete the provided object.
103 * @return a new Kryo object if needed, otherwise null.
104 */
105 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
106 if (deleteKryo != null) {
107 // Delete an entry by moving it back to the buffer
108 kryoList.add(deleteKryo);
109 return null;
110 } else {
111 Kryo kryo = null;
112 if (kryoList.isEmpty()) {
113 // Preallocate
114 for (int i = 0; i < 100; i++) {
115 kryo = newKryoImpl();
116 kryoList.add(kryo);
117 }
118 }
119
120 kryo = kryoList.remove(kryoList.size() - 1);
121 return kryo;
122 }
123 }
124
125 /**
126 * Create and initialize a new Kryo object.
127 *
128 * @return the created Kryo object.
129 */
130 private Kryo newKryoImpl() {
131 Kryo kryo = new Kryo();
132 kryo.setRegistrationRequired(true);
Yuta HIGUCHI0af53d22014-03-31 14:01:35 -0700133 //
134 // WARNING: Order of register() calls affects serialized bytes.
135 // - Do no insert new entry in the middle, always add to the end.
136 // - Do not simply remove existing entry
137 //
138
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700139 // kryo.setReferences(false);
140 //
141 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700142
143 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700144 kryo.register(CallerId.class);
145 kryo.register(DataPath.class);
146 kryo.register(DataPathEndpoints.class);
147 kryo.register(Dpid.class);
148 kryo.register(FlowEntryAction.class);
149 kryo.register(FlowEntryAction.ActionEnqueue.class);
150 kryo.register(FlowEntryAction.ActionOutput.class);
151 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
152 kryo.register(FlowEntryAction.ActionSetIpToS.class);
153 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
154 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
155 kryo.register(FlowEntryAction.ActionSetVlanId.class);
156 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
157 kryo.register(FlowEntryAction.ActionStripVlan.class);
158 kryo.register(FlowEntryAction.ActionValues.class);
159 kryo.register(FlowEntryActions.class);
160 kryo.register(FlowEntryErrorState.class);
161 kryo.register(FlowEntryId.class);
162 kryo.register(FlowEntry.class);
163 kryo.register(FlowEntryMatch.class);
164 kryo.register(FlowEntryMatch.Field.class);
165 kryo.register(FlowEntrySwitchState.class);
166 kryo.register(FlowEntryUserState.class);
167 kryo.register(FlowId.class);
168 kryo.register(FlowPath.class);
169 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700170 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700171 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700172 kryo.register(IPv4.class);
173 kryo.register(IPv4Net.class);
174 kryo.register(IPv6.class);
175 kryo.register(IPv6Net.class);
176 kryo.register(byte[].class);
177 kryo.register(MACAddress.class);
178 kryo.register(Port.class);
179 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800180 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700181
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800182 // New data model-related classes
183 kryo.register(DeviceEvent.class);
184 kryo.register(InetAddress.class);
185 kryo.register(LinkEvent.class);
186 kryo.register(PortEvent.class);
187 kryo.register(PortEvent.SwitchPort.class);
188 kryo.register(SwitchEvent.class);
189 kryo.register(TopologyEvent.class);
190
Toshio Koideeb90d912014-02-18 21:30:22 -0800191 // Intent-related classes
Toshio Koidefece1ce2014-02-26 17:12:10 -0800192 kryo.register(Path.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800193 kryo.register(Intent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800194 kryo.register(Intent.IntentState.class);
195 kryo.register(PathIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800196 kryo.register(ShortestPathIntent.class);
197 kryo.register(ConstrainedShortestPathIntent.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800198 kryo.register(ErrorIntent.class);
199 kryo.register(ErrorIntent.ErrorType.class);
200 kryo.register(IntentOperation.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800201 kryo.register(IntentOperation.Operator.class);
Toshio Koidefece1ce2014-02-26 17:12:10 -0800202 kryo.register(IntentOperationList.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800203 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800204
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700205 return kryo;
206 }
207}