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