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