blob: 7736880edda10215bc10664fe58965d6db7a5bff [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 {
30 protected static Logger log =
31 LoggerFactory.getLogger(HazelcastDatagrid.class);
32
33 protected IFloodlightProviderService floodlightProvider;
34
35 protected static final String HazelcastConfigFile = "datagridConfig";
36 private HazelcastInstance hazelcast = null;
37 private Config hazelcastConfig = null;
38
39 /**
40 * Initialize the Hazelcast Datagrid operation.
41 *
42 * @param conf the configuration filename.
43 */
44 public void init(String configFilename) {
45 /*
46 System.setProperty("hazelcast.socket.receive.buffer.size", "32");
47 System.setProperty("hazelcast.socket.send.buffer.size", "32");
48 */
49 // System.setProperty("hazelcast.heartbeat.interval.seconds", "100");
50
51 // Init from configuration file
52 try {
53 hazelcastConfig = new FileSystemXmlConfig(configFilename);
54 } catch (FileNotFoundException e) {
55 log.error("Error opening Hazelcast XML configuration. File not found: " + configFilename, e);
56 }
57 /*
58 hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1");
59 hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1");
60 hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_THREAD_COUNT, "1");
61 */
62 //
63 hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_QUEUE_CAPACITY, "4000000");
64 hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_RECEIVE_BUFFER_SIZE, "4096");
65 hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_SEND_BUFFER_SIZE, "4096");
66 }
67
68 /**
69 * Shutdown the Hazelcast Datagrid operation.
70 */
71 public void finalize() {
72 close();
73 }
74
75 /**
76 * Shutdown the Hazelcast Datagrid operation.
77 */
78 public void close() {
79 Hazelcast.shutdownAll();
80 }
81
82 /**
83 * Get the collection of offered module services.
84 *
85 * @return the collection of offered module services.
86 */
87 @Override
88 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
89 Collection<Class<? extends IFloodlightService>> l =
90 new ArrayList<Class<? extends IFloodlightService>>();
91 l.add(IDatagridService.class);
92 return l;
93 }
94
95 /**
96 * Get the collection of implemented services.
97 *
98 * @return the collection of implemented services.
99 */
100 @Override
101 public Map<Class<? extends IFloodlightService>, IFloodlightService>
102 getServiceImpls() {
103 Map<Class<? extends IFloodlightService>,
104 IFloodlightService> m =
105 new HashMap<Class<? extends IFloodlightService>,
106 IFloodlightService>();
107 m.put(IDatagridService.class, this);
108 return m;
109 }
110
111 /**
112 * Get the collection of modules this module depends on.
113 *
114 * @return the collection of modules this module depends on.
115 */
116 @Override
117 public Collection<Class<? extends IFloodlightService>>
118 getModuleDependencies() {
119 Collection<Class<? extends IFloodlightService>> l =
120 new ArrayList<Class<? extends IFloodlightService>>();
121 l.add(IFloodlightProviderService.class);
122 return l;
123 }
124
125 /**
126 * Initialize the module.
127 *
128 * @param context the module context to use for the initialization.
129 */
130 @Override
131 public void init(FloodlightModuleContext context)
132 throws FloodlightModuleException {
133 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
134
135 // Get the configuration file name and configure the Datagrid
136 Map<String, String> configMap = context.getConfigParams(this);
137 String configFilename = configMap.get(HazelcastConfigFile);
138 this.init(configFilename);
139 }
140
141 /**
142 * Startup module operation.
143 *
144 * @param context the module context to use for the startup.
145 */
146 @Override
147 public void startUp(FloodlightModuleContext context) {
148 hazelcast = Hazelcast.newHazelcastInstance(hazelcastConfig);
149 }
150}