blob: 7096bd915701d5205c229b5db316847cdd9bc605 [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datagrid;
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -07002
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;
Pavlin Radoslavovda7ef612013-10-30 16:12:14 -070014import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hart6df90172014-04-03 10:13:11 -070015import net.onrc.onos.core.datagrid.web.DatagridWebRoutable;
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -070016import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18
19import com.hazelcast.config.Config;
20import com.hazelcast.config.FileSystemXmlConfig;
21import com.hazelcast.core.Hazelcast;
22import com.hazelcast.core.HazelcastInstance;
23import com.hazelcast.instance.GroupProperties;
24
25/**
26 * A datagrid service that uses Hazelcast as a datagrid.
27 * The relevant data is stored in the Hazelcast datagrid and shared as
28 * appropriate in a multi-node cluster.
29 */
30public class HazelcastDatagrid implements IFloodlightModule, IDatagridService {
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070031 static final Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class);
Yuta HIGUCHI6dfba392014-05-28 15:45:44 -070032
33 /**
34 * The name of Hazelcast instance in this JVM.
35 */
36 public static final String ONOS_HAZELCAST_INSTANCE = "ONOS_HazelcastInstance";
37
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070038 private IRestApiService restApi;
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -070039
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070040 static final String HAZELCAST_CONFIG_FILE = "datagridConfig";
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -070041 private static final String HAZELCAST_DEFAULT_XML = "conf/hazelcast.default.xml";
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070042 private HazelcastInstance hazelcastInstance;
43 private Config hazelcastConfig;
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -070044
Pavlin Radoslavov07af5f22014-03-21 15:17:58 -070045 //
46 // NOTE: eventChannels is kept thread safe by using explicit "synchronized"
47 // blocks below. Those are needed to protect the integrity of each entry
48 // instance, and avoid preemption during channel creation/startup.
49 //
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070050 private final Map<String, IEventChannel<?, ?>> eventChannels = new HashMap<>();
Pavlin Radoslavov7940b652014-02-13 19:42:05 -080051
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070052 /**
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -070053 * Load the Hazelcast Datagrid configuration file.
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -070054 *
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070055 * @param configFilename the configuration filename.
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -070056 */
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -070057 public void loadHazelcastConfig(String configFilename) {
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070058 /*
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070059 System.setProperty("hazelcast.socket.receive.buffer.size", "32");
60 System.setProperty("hazelcast.socket.send.buffer.size", "32");
61 */
62 // System.setProperty("hazelcast.heartbeat.interval.seconds", "100");
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080063
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070064 // Init from configuration file
65 try {
66 hazelcastConfig = new FileSystemXmlConfig(configFilename);
67 } catch (FileNotFoundException e) {
68 log.error("Error opening Hazelcast XML configuration. File not found: " + configFilename, e);
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -070069
70 // Fallback mechanism to support running unit test without setup.
71 log.error("Falling back to default Hazelcast XML {}", HAZELCAST_DEFAULT_XML);
72 try {
73 hazelcastConfig = new FileSystemXmlConfig(HAZELCAST_DEFAULT_XML);
74 } catch (FileNotFoundException e2) {
75 log.error("Error opening fall back Hazelcast XML configuration. "
76 + "File not found: " + HAZELCAST_DEFAULT_XML, e2);
77 // XXX probably should throw some exception to kill ONOS instead.
78 }
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070079 }
Yuta HIGUCHI6dfba392014-05-28 15:45:44 -070080
81 // set the name of Hazelcast instance in this JVM.
82 hazelcastConfig.setInstanceName(ONOS_HAZELCAST_INSTANCE);
83
Ray Milkey0ab2d8a2014-03-20 14:30:10 -070084 /*
85 hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1");
86 hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1");
87 hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_THREAD_COUNT, "1");
88 */
89 //
90 hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_QUEUE_CAPACITY, "4000000");
91 hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_RECEIVE_BUFFER_SIZE, "4096");
92 hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_SEND_BUFFER_SIZE, "4096");
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -070093 }
94
95 /**
96 * Shutdown the Hazelcast Datagrid operation.
97 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080098 @Override
99 protected void finalize() {
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700100 close();
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700101 }
102
103 /**
104 * Shutdown the Hazelcast Datagrid operation.
105 */
106 public void close() {
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700107 Hazelcast.shutdownAll();
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700108 }
109
110 /**
111 * Get the collection of offered module services.
112 *
113 * @return the collection of offered module services.
114 */
115 @Override
116 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800117 Collection<Class<? extends IFloodlightService>> l =
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700118 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700119 l.add(IDatagridService.class);
120 return l;
121 }
122
123 /**
124 * Get the collection of implemented services.
125 *
126 * @return the collection of implemented services.
127 */
128 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800129 public Map<Class<? extends IFloodlightService>, IFloodlightService>
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700130 getServiceImpls() {
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700131 Map<Class<? extends IFloodlightService>,
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700132 IFloodlightService> m =
133 new HashMap<Class<? extends IFloodlightService>,
134 IFloodlightService>();
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700135 m.put(IDatagridService.class, this);
136 return m;
137 }
138
139 /**
140 * Get the collection of modules this module depends on.
141 *
142 * @return the collection of modules this module depends on.
143 */
144 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800145 public Collection<Class<? extends IFloodlightService>>
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700146 getModuleDependencies() {
147 Collection<Class<? extends IFloodlightService>> l =
148 new ArrayList<Class<? extends IFloodlightService>>();
149 l.add(IFloodlightProviderService.class);
150 l.add(IRestApiService.class);
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700151 return l;
152 }
153
154 /**
155 * Initialize the module.
156 *
157 * @param context the module context to use for the initialization.
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700158 * @throws FloodlightModuleException on error
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700159 */
160 @Override
161 public void init(FloodlightModuleContext context)
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700162 throws FloodlightModuleException {
163 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700164
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700165 // Get the configuration file name and configure the Datagrid
166 Map<String, String> configMap = context.getConfigParams(this);
167 String configFilename = configMap.get(HAZELCAST_CONFIG_FILE);
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700168 this.loadHazelcastConfig(configFilename);
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700169 }
170
171 /**
172 * Startup module operation.
173 *
174 * @param context the module context to use for the startup.
175 */
176 @Override
177 public void startUp(FloodlightModuleContext context) {
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700178 hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig);
Pavlin Radoslavovda7ef612013-10-30 16:12:14 -0700179
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700180 restApi.addRestletRoutable(new DatagridWebRoutable());
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700181 }
182
183 /**
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800184 * Create an event channel.
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700185 * <p/>
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800186 * If the channel already exists, just return it.
187 * NOTE: The channel is started automatically.
188 *
189 * @param channelName the event channel name.
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700190 * @param <K> the type of the Key in the Key-Value store.
191 * @param <V> the type of the Value in the Key-Value store.
192 * @param typeK the type of the Key in the Key-Value store.
193 * @param typeV the type of the Value in the Key-Value store.
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800194 * @return the event channel for the channel name.
195 */
196 @Override
197 public <K, V> IEventChannel<K, V> createChannel(String channelName,
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700198 Class<K> typeK, Class<V> typeV) {
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700199 synchronized (eventChannels) {
200 IEventChannel<K, V> eventChannel =
Ray Milkey9c8a2132014-04-02 15:16:42 -0700201 createChannelImpl(channelName, typeK, typeV);
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700202 eventChannel.startup();
203 return eventChannel;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700204 }
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800205 }
206
207 /**
208 * Create an event channel implementation.
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700209 * <p/>
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800210 * If the channel already exists, just return it.
211 * NOTE: The caller must call IEventChannel.startup() to startup the
212 * channel operation.
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700213 * NOTE: The caller must own the lock on "eventChannels".
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800214 *
215 * @param channelName the event channel name.
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700216 * @param <K> the type of the Key in the Key-Value store.
217 * @param <V> the type of the Value in the Key-Value store.
218 * @param typeK the type of the Key in the Key-Value store.
219 * @param typeV the type of the Value in the Key-Value store.
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800220 * @return the event channel for the channel name.
221 */
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700222 private <K, V> IEventChannel<K, V> createChannelImpl(
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700223 String channelName,
224 Class<K> typeK, Class<V> typeV) {
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700225 IEventChannel<?, ?> genericEventChannel =
226 eventChannels.get(channelName);
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800227
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700228 // Add the channel if the first listener
229 if (genericEventChannel == null) {
Pavlin Radoslavove561a4c2014-04-01 14:10:55 -0700230 IEventChannel<K, V> castedEventChannel =
Ray Milkey9c8a2132014-04-02 15:16:42 -0700231 new HazelcastEventChannel<K, V>(hazelcastInstance,
232 channelName, typeK, typeV);
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700233 eventChannels.put(channelName, castedEventChannel);
Pavlin Radoslavove561a4c2014-04-01 14:10:55 -0700234 return castedEventChannel;
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700235 }
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800236
Pavlin Radoslavove561a4c2014-04-01 14:10:55 -0700237 //
238 // TODO: Find if we can use Java internal support to check for
239 // type mismatch.
240 //
241 if (!genericEventChannel.verifyKeyValueTypes(typeK, typeV)) {
242 throw new ClassCastException("Key-value type mismatch for event channel " + channelName);
243 }
244 @SuppressWarnings("unchecked")
245 IEventChannel<K, V> castedEventChannel =
Ray Milkey9c8a2132014-04-02 15:16:42 -0700246 (IEventChannel<K, V>) genericEventChannel;
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700247 return castedEventChannel;
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800248 }
249
250 /**
251 * Add event channel listener.
Ray Milkey9c8a2132014-04-02 15:16:42 -0700252 * <p/>
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800253 * NOTE: The channel is started automatically right after the listener
254 * is added.
255 *
256 * @param channelName the event channel name.
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700257 * @param listener the listener to add.
258 * @param <K> the type of the Key in the Key-Value store.
259 * @param <V> the type of the Value in the Key-Value store.
260 * @param typeK the type of the Key in the Key-Value store.
261 * @param typeV the type of the Value in the Key-Value store.
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800262 * @return the event channel for the channel name.
263 */
264 @Override
265 public <K, V> IEventChannel<K, V> addListener(String channelName,
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700266 IEventChannelListener<K, V> listener,
267 Class<K> typeK, Class<V> typeV) {
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700268 synchronized (eventChannels) {
269 IEventChannel<K, V> eventChannel =
Ray Milkey9c8a2132014-04-02 15:16:42 -0700270 createChannelImpl(channelName, typeK, typeV);
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700271 eventChannel.addListener(listener);
272 eventChannel.startup();
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800273
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700274 return eventChannel;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700275 }
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800276 }
277
278 /**
279 * Remove event channel listener.
280 *
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700281 * @param <K> the type of the Key in the Key-Value store.
282 * @param <V> the type of the Value in the Key-Value store.
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800283 * @param channelName the event channel name.
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700284 * @param listener the listener to remove.
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800285 */
286 @Override
287 public <K, V> void removeListener(String channelName,
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700288 IEventChannelListener<K, V> listener) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700289 synchronized (eventChannels) {
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700290 IEventChannel<?, ?> genericEventChannel =
Ray Milkey9c8a2132014-04-02 15:16:42 -0700291 eventChannels.get(channelName);
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800292
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700293 if (genericEventChannel != null) {
294 //
295 // TODO: Find if we can use Java internal support to check for
296 // type mismatch.
297 // NOTE: Using "ClassCastException" exception below doesn't
298 // work.
299 //
Pavlin Radoslavove561a4c2014-04-01 14:10:55 -0700300 @SuppressWarnings("unchecked")
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700301 IEventChannel<K, V> castedEventChannel =
Ray Milkey9c8a2132014-04-02 15:16:42 -0700302 (IEventChannel<K, V>) genericEventChannel;
Pavlin Radoslavov00fad592014-03-21 11:32:34 -0700303 castedEventChannel.removeListener(listener);
304 }
Ray Milkey0ab2d8a2014-03-20 14:30:10 -0700305 }
Pavlin Radoslavov7940b652014-02-13 19:42:05 -0800306 }
Pavlin Radoslavov1eee2c82013-10-15 02:30:32 -0700307}