blob: 07b173de1e5a907a7e2431051fb5b8757b6f951e [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;
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;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070021import net.onrc.onos.ofcontroller.topology.TopologyElement;
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
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 {
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 Radoslavovaaace7f2013-10-25 19:42:00 -0700124
125 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700126 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 Radoslavovd28cf7c2013-10-26 11:27:43 -0700152 kryo.register(FlowPathType.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700153 kryo.register(FlowPathUserState.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700154 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 Radoslavov45ec04b2014-02-14 23:29:33 -0800162 // kryo.register(SwitchPort.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700163
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700164 // Topology-related classes
165 kryo.register(TopologyElement.class);
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700166 kryo.register(TopologyElement.Type.class);
167 kryo.register(TreeMap.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700168
Pavlin Radoslavov45ec04b2014-02-14 23:29:33 -0800169 // 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 Koideeb90d912014-02-18 21:30:22 -0800178 // Intent-related classes
179 kryo.register(IntentOperationList.class);
180 kryo.register(IntentOperation.class);
181 kryo.register(PathIntent.class);
Toshio Koideb39c9d32014-02-20 01:21:47 -0800182 kryo.register(Intent.class);
Toshio Koidedf2eab92014-02-20 11:24:59 -0800183 kryo.register(ErrorIntent.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800184 kryo.register(ShortestPathIntent.class);
185 kryo.register(ConstrainedShortestPathIntent.class);
186 kryo.register(Intent.IntentState.class);
Toshio Koidebc116be2014-02-19 23:56:48 -0800187 kryo.register(Path.class);
Toshio Koideeb90d912014-02-18 21:30:22 -0800188 kryo.register(IntentOperation.Operator.class);
189
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700190 return kryo;
191 }
192}