blob: 45a43bf0debd755e02bc9752da3c8811657f2a6f [file] [log] [blame]
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07001package net.onrc.onos.ofcontroller.util.serializers;
2
3import java.util.ArrayList;
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -07004import java.util.TreeMap;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -07005
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07006import com.esotericsoftware.kryo2.Kryo;
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -07007
8import net.floodlightcontroller.util.MACAddress;
9
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070010import net.onrc.onos.ofcontroller.util.*;
11import net.onrc.onos.ofcontroller.topology.TopologyElement;
12
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070013/**
14 * Class factory for allocating Kryo instances for
15 * serialization/deserialization of classes.
16 */
17public class KryoFactory {
18 private ArrayList<Kryo> kryoList = new ArrayList<Kryo>();
19
20 /**
21 * Default constructor.
22 */
23 public KryoFactory() {
24 Kryo kryo;
25 // Preallocate
26 for (int i = 0; i < 100; i++) {
27 kryo = newKryoImpl();
28 kryoList.add(kryo);
29 }
30 }
31
32 /**
33 * Create and initialize a new Kryo object.
34 *
35 * @return the created Kryo object.
36 */
37 public Kryo newKryo() {
38 return newDeleteKryo(null);
39 }
40
41 /**
42 * Delete an existing Kryo object.
43 *
44 * @param deleteKryo the object to delete.
45 */
46 public void deleteKryo(Kryo deleteKryo) {
47 newDeleteKryo(deleteKryo);
48 }
49
50 /**
51 * Create or delete a Kryo object.
52 *
53 * @param deleteKryo if null, then allocate and return a new object,
54 * otherwise delete the provided object.
55 * @return a new Kryo object if needed, otherwise null.
56 */
57 synchronized private Kryo newDeleteKryo(Kryo deleteKryo) {
58 if (deleteKryo != null) {
59 // Delete an entry by moving it back to the buffer
60 kryoList.add(deleteKryo);
61 return null;
62 } else {
63 Kryo kryo = null;
64 if (kryoList.isEmpty()) {
65 // Preallocate
66 for (int i = 0; i < 100; i++) {
67 kryo = newKryoImpl();
68 kryoList.add(kryo);
69 }
70 }
71
72 kryo = kryoList.remove(kryoList.size() - 1);
73 return kryo;
74 }
75 }
76
77 /**
78 * Create and initialize a new Kryo object.
79 *
80 * @return the created Kryo object.
81 */
82 private Kryo newKryoImpl() {
83 Kryo kryo = new Kryo();
84 kryo.setRegistrationRequired(true);
85 // kryo.setReferences(false);
86 //
87 kryo.register(ArrayList.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070088
89 // FlowPath and related classes
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -070090 kryo.register(CallerId.class);
91 kryo.register(DataPath.class);
92 kryo.register(DataPathEndpoints.class);
93 kryo.register(Dpid.class);
94 kryo.register(FlowEntryAction.class);
95 kryo.register(FlowEntryAction.ActionEnqueue.class);
96 kryo.register(FlowEntryAction.ActionOutput.class);
97 kryo.register(FlowEntryAction.ActionSetEthernetAddr.class);
98 kryo.register(FlowEntryAction.ActionSetIpToS.class);
99 kryo.register(FlowEntryAction.ActionSetIPv4Addr.class);
100 kryo.register(FlowEntryAction.ActionSetTcpUdpPort.class);
101 kryo.register(FlowEntryAction.ActionSetVlanId.class);
102 kryo.register(FlowEntryAction.ActionSetVlanPriority.class);
103 kryo.register(FlowEntryAction.ActionStripVlan.class);
104 kryo.register(FlowEntryAction.ActionValues.class);
105 kryo.register(FlowEntryActions.class);
106 kryo.register(FlowEntryErrorState.class);
107 kryo.register(FlowEntryId.class);
108 kryo.register(FlowEntry.class);
109 kryo.register(FlowEntryMatch.class);
110 kryo.register(FlowEntryMatch.Field.class);
111 kryo.register(FlowEntrySwitchState.class);
112 kryo.register(FlowEntryUserState.class);
113 kryo.register(FlowId.class);
114 kryo.register(FlowPath.class);
115 kryo.register(FlowPathFlags.class);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700116 kryo.register(FlowPathType.class);
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700117 kryo.register(IPv4.class);
118 kryo.register(IPv4Net.class);
119 kryo.register(IPv6.class);
120 kryo.register(IPv6Net.class);
121 kryo.register(byte[].class);
122 kryo.register(MACAddress.class);
123 kryo.register(Port.class);
124 kryo.register(Switch.class);
125 kryo.register(SwitchPort.class);
126
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700127 // Topology-related classes
128 kryo.register(TopologyElement.class);
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700129 kryo.register(TopologyElement.Type.class);
130 kryo.register(TreeMap.class);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700131
Pavlin Radoslavove0a643e2013-10-24 13:33:39 -0700132 return kryo;
133 }
134}