blob: 3ff773ed658d4fa6031165e196d90a7448765c35 [file] [log] [blame]
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -07001package net.onrc.onos.datagrid;
2
3import java.io.FileNotFoundException;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashMap;
7import java.util.Map;
8
9import net.floodlightcontroller.core.IFloodlightProviderService;
10import net.floodlightcontroller.core.module.FloodlightModuleContext;
11import net.floodlightcontroller.core.module.FloodlightModuleException;
12import net.floodlightcontroller.core.module.IFloodlightModule;
13import net.floodlightcontroller.core.module.IFloodlightService;
14
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
18import com.hazelcast.config.Config;
19import com.hazelcast.config.FileSystemXmlConfig;
20import com.hazelcast.core.Hazelcast;
21import com.hazelcast.core.HazelcastInstance;
22import com.hazelcast.instance.GroupProperties;
23
24/**
25 * A datagrid service that uses Hazelcast as a datagrid.
26 * The relevant data is stored in the Hazelcast datagrid and shared as
27 * appropriate in a multi-node cluster.
28 */
29public class HazelcastDatagrid implements IFloodlightModule, IDatagridService {
Pavlin Radoslavov27da7532013-10-18 18:41:50 -070030 protected static Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class);
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -070031 protected IFloodlightProviderService floodlightProvider;
32
33 protected static final String HazelcastConfigFile = "datagridConfig";
34 private HazelcastInstance hazelcast = null;
35 private Config hazelcastConfig = null;
36
37 /**
38 * Initialize the Hazelcast Datagrid operation.
39 *
40 * @param conf the configuration filename.
41 */
42 public void init(String configFilename) {
43 /*
44 System.setProperty("hazelcast.socket.receive.buffer.size", "32");
45 System.setProperty("hazelcast.socket.send.buffer.size", "32");
46 */
47 // System.setProperty("hazelcast.heartbeat.interval.seconds", "100");
48
49 // Init from configuration file
50 try {
51 hazelcastConfig = new FileSystemXmlConfig(configFilename);
52 } catch (FileNotFoundException e) {
53 log.error("Error opening Hazelcast XML configuration. File not found: " + configFilename, e);
54 }
55 /*
56 hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1");
57 hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1");
58 hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_THREAD_COUNT, "1");
59 */
60 //
61 hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_QUEUE_CAPACITY, "4000000");
62 hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_RECEIVE_BUFFER_SIZE, "4096");
63 hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_SEND_BUFFER_SIZE, "4096");
64 }
65
66 /**
67 * Shutdown the Hazelcast Datagrid operation.
68 */
69 public void finalize() {
70 close();
71 }
72
73 /**
74 * Shutdown the Hazelcast Datagrid operation.
75 */
76 public void close() {
77 Hazelcast.shutdownAll();
78 }
79
80 /**
81 * Get the collection of offered module services.
82 *
83 * @return the collection of offered module services.
84 */
85 @Override
86 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
87 Collection<Class<? extends IFloodlightService>> l =
88 new ArrayList<Class<? extends IFloodlightService>>();
89 l.add(IDatagridService.class);
90 return l;
91 }
92
93 /**
94 * Get the collection of implemented services.
95 *
96 * @return the collection of implemented services.
97 */
98 @Override
99 public Map<Class<? extends IFloodlightService>, IFloodlightService>
100 getServiceImpls() {
101 Map<Class<? extends IFloodlightService>,
Pavlin Radoslavov27da7532013-10-18 18:41:50 -0700102 IFloodlightService> m =
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700103 new HashMap<Class<? extends IFloodlightService>,
104 IFloodlightService>();
105 m.put(IDatagridService.class, this);
106 return m;
107 }
108
109 /**
110 * Get the collection of modules this module depends on.
111 *
112 * @return the collection of modules this module depends on.
113 */
114 @Override
115 public Collection<Class<? extends IFloodlightService>>
116 getModuleDependencies() {
117 Collection<Class<? extends IFloodlightService>> l =
118 new ArrayList<Class<? extends IFloodlightService>>();
119 l.add(IFloodlightProviderService.class);
120 return l;
121 }
122
123 /**
124 * Initialize the module.
125 *
126 * @param context the module context to use for the initialization.
127 */
128 @Override
129 public void init(FloodlightModuleContext context)
130 throws FloodlightModuleException {
131 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
132
133 // Get the configuration file name and configure the Datagrid
134 Map<String, String> configMap = context.getConfigParams(this);
135 String configFilename = configMap.get(HazelcastConfigFile);
136 this.init(configFilename);
137 }
138
139 /**
140 * Startup module operation.
141 *
142 * @param context the module context to use for the startup.
143 */
144 @Override
145 public void startUp(FloodlightModuleContext context) {
146 hazelcast = Hazelcast.newHazelcastInstance(hazelcastConfig);
147 }
148}