blob: df93614fdb667aaeba5b8d2dfeb623ae7cb2c9b0 [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 Radoslavov2a091bf2013-10-25 21:37:12 -07005import java.util.TreeMap;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -07006
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;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070022import net.onrc.onos.ofcontroller.topology.TopologyElement;
Jonathan Hartd3003252013-11-15 09:44:46 -080023import net.onrc.onos.ofcontroller.util.CallerId;
24import net.onrc.onos.ofcontroller.util.DataPath;
25import net.onrc.onos.ofcontroller.util.DataPathEndpoints;
26import net.onrc.onos.ofcontroller.util.Dpid;
27import net.onrc.onos.ofcontroller.util.FlowEntry;
28import net.onrc.onos.ofcontroller.util.FlowEntryAction;
29import net.onrc.onos.ofcontroller.util.FlowEntryActions;
30import net.onrc.onos.ofcontroller.util.FlowEntryErrorState;
31import net.onrc.onos.ofcontroller.util.FlowEntryId;
32import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
33import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
34import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
35import net.onrc.onos.ofcontroller.util.FlowId;
36import net.onrc.onos.ofcontroller.util.FlowPath;
37import net.onrc.onos.ofcontroller.util.FlowPathFlags;
38import net.onrc.onos.ofcontroller.util.FlowPathType;
39import net.onrc.onos.ofcontroller.util.FlowPathUserState;
40import net.onrc.onos.ofcontroller.util.IPv4;
41import net.onrc.onos.ofcontroller.util.IPv4Net;
42import net.onrc.onos.ofcontroller.util.IPv6;
43import net.onrc.onos.ofcontroller.util.IPv6Net;
44import net.onrc.onos.ofcontroller.util.Port;
45import net.onrc.onos.ofcontroller.util.Switch;
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -080046// import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartd3003252013-11-15 09:44:46 -080047
Toshio Koide066506e2014-02-20 19:52:09 -080048
Yuta HIGUCHI2d5ac522014-01-22 10:21:41 -080049import com.esotericsoftware.kryo.Kryo;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070050
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070051/**
52 * Class factory for allocating Kryo instances for
53 * serialization/deserialization of classes.
54 */
55public class KryoFactory {
56 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
57
58 /**
59 * Default constructor.
60 */
61 public KryoFactory() {
62 Kryo kryo;
63 // Preallocate
64 for (int i = 0; i < 100; i++) {
65 kryo = newKryoImpl();
66 kryoList.add(kryo);
67 }
68 }
69
70 /**
71 * Create and initialize a new Kryo object.
72 *
73 * @return the created Kryo object.
74 */
75 public Kryo newKryo() {
76 return newDeleteKryo(null);
77 }
78
79 /**
80 * Delete an existing Kryo object.
81 *
82 * @param deleteKryo the object to delete.
83 */
84 public void deleteKryo(Kryo deleteKryo) {
85 newDeleteKryo(deleteKryo);
86 }
87
88 /**
89 * Create or delete a Kryo object.
90 *
91 * @param deleteKryo if null, then allocate and return a new object,
92 * otherwise delete the provided object.
93 * @return a new Kryo object if needed, otherwise null.
94 */
95 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
96 if (deleteKryo != null) {
97 // Delete an entry by moving it back to the buffer
98 kryoList.add(deleteKryo);
99 return null;
100 } else {
101 Kryo kryo = null;
102 if (kryoList.isEmpty()) {
103 // Preallocate
104 for (int i = 0; i < 100; i++) {
105 kryo = newKryoImpl();
106 kryoList.add(kryo);
107 }
108 }
109
110 kryo = kryoList.remove(kryoList.size() - 1);
111 return kryo;
112 }
113 }
114
115 /**
116 * Create and initialize a new Kryo object.
117 *
118 * @return the created Kryo object.
119 */
120 private Kryo newKryoImpl() {
121 Kryo kryo = new Kryo();
122 kryo.setRegistrationRequired(true);
123 // kryo.setReferences(false);
124 //
125 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700126
127 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700128 kryo.register(CallerId.class);
129 kryo.register(DataPath.class);
130 kryo.register(DataPathEndpoints.class);
131 kryo.register(Dpid.class);
132 kryo.register(FlowEntryAction.class);
133 kryo.register(FlowEntryAction.ActionEnqueue.class);
134 kryo.register(FlowEntryAction.ActionOutput.class);
135 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
136 kryo.register(FlowEntryAction.ActionSetIpToS.class);
137 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
138 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
139 kryo.register(FlowEntryAction.ActionSetVlanId.class);
140 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
141 kryo.register(FlowEntryAction.ActionStripVlan.class);
142 kryo.register(FlowEntryAction.ActionValues.class);
143 kryo.register(FlowEntryActions.class);
144 kryo.register(FlowEntryErrorState.class);
145 kryo.register(FlowEntryId.class);
146 kryo.register(FlowEntry.class);
147 kryo.register(FlowEntryMatch.class);
148 kryo.register(FlowEntryMatch.Field.class);
149 kryo.register(FlowEntrySwitchState.class);
150 kryo.register(FlowEntryUserState.class);
151 kryo.register(FlowId.class);
152 kryo.register(FlowPath.class);
153 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700154 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700155 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700156 kryo.register(IPv4.class);
157 kryo.register(IPv4Net.class);
158 kryo.register(IPv6.class);
159 kryo.register(IPv6Net.class);
160 kryo.register(byte[].class);
161 kryo.register(MACAddress.class);
162 kryo.register(Port.class);
163 kryo.register(Switch.class);
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800164 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700165
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700166 // Topology-related classes
167 kryo.register(TopologyElement.class);
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700168 kryo.register(TopologyElement.Type.class);
169 kryo.register(TreeMap.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700170
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800171 // New data model-related classes
172 kryo.register(DeviceEvent.class);
173 kryo.register(InetAddress.class);
174 kryo.register(LinkEvent.class);
175 kryo.register(PortEvent.class);
176 kryo.register(PortEvent.SwitchPort.class);
177 kryo.register(SwitchEvent.class);
178 kryo.register(TopologyEvent.class);
179
Toshio Koideeb90d912014-02-18 21:30:22 -0800180 // Intent-related classes
181 kryo.register(IntentOperationList.class);
182 kryo.register(IntentOperation.class);
183 kryo.register(PathIntent.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800184 kryo.register(Intent.class);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800185 kryo.register(ErrorIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800186 kryo.register(ShortestPathIntent.class);
187 kryo.register(ConstrainedShortestPathIntent.class);
188 kryo.register(Intent.IntentState.class);
Toshio Koidebc116be2014-02-19 23:56:48 -0800189 kryo.register(Path.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800190 kryo.register(IntentOperation.Operator.class);
Toshio Koide066506e2014-02-20 19:52:09 -0800191 kryo.register(IntentStateList.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800192
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700193 return kryo;
194 }
195}