Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 1 | package net.onrc.onos.datagrid; |
| 2 | |
| 3 | import java.io.FileNotFoundException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
| 6 | import java.util.HashMap; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 7 | import java.util.LinkedList; |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 8 | import java.util.List; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 9 | import java.util.Map; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 10 | import java.util.Set; |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 11 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 12 | |
| 13 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 14 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 15 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 17 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 18 | import net.floodlightcontroller.restserver.IRestApiService; |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 19 | import net.onrc.onos.datagrid.web.DatagridWebRoutable; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.devicemanager.IDeviceEventHandler; |
| 21 | import net.onrc.onos.ofcontroller.devicemanager.OnosDevice; |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.proxyarp.ArpReplyNotification; |
| 23 | import net.onrc.onos.ofcontroller.proxyarp.IArpReplyEventHandler; |
| 24 | import net.onrc.onos.ofcontroller.proxyarp.IPacketOutEventHandler; |
| 25 | import net.onrc.onos.ofcontroller.proxyarp.PacketOutNotification; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.util.serializers.KryoFactory; |
| 27 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 31 | import com.esotericsoftware.kryo.Kryo; |
| 32 | import com.esotericsoftware.kryo.io.Input; |
| 33 | import com.esotericsoftware.kryo.io.Output; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 34 | import com.hazelcast.config.Config; |
| 35 | import com.hazelcast.config.FileSystemXmlConfig; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 36 | import com.hazelcast.core.EntryEvent; |
| 37 | import com.hazelcast.core.EntryListener; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 38 | import com.hazelcast.core.Hazelcast; |
| 39 | import com.hazelcast.core.HazelcastInstance; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 40 | import com.hazelcast.core.IList; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 41 | import com.hazelcast.core.IMap; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 42 | import com.hazelcast.instance.GroupProperties; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 43 | import net.onrc.onos.intent.Intent; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * A datagrid service that uses Hazelcast as a datagrid. |
| 47 | * The relevant data is stored in the Hazelcast datagrid and shared as |
| 48 | * appropriate in a multi-node cluster. |
| 49 | */ |
| 50 | public class HazelcastDatagrid implements IFloodlightModule, IDatagridService { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 51 | private static final int MAX_BUFFER_SIZE = 64 * 1024; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 52 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 53 | static final Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class); |
| 54 | private IRestApiService restApi; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 55 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 56 | static final String HAZELCAST_CONFIG_FILE = "datagridConfig"; |
| 57 | private HazelcastInstance hazelcastInstance; |
| 58 | private Config hazelcastConfig; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 59 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 60 | private final KryoFactory kryoFactory = new KryoFactory(); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 61 | |
Pavlin Radoslavov | 07af5f2 | 2014-03-21 15:17:58 -0700 | [diff] [blame] | 62 | // |
| 63 | // NOTE: eventChannels is kept thread safe by using explicit "synchronized" |
| 64 | // blocks below. Those are needed to protect the integrity of each entry |
| 65 | // instance, and avoid preemption during channel creation/startup. |
| 66 | // |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 67 | private final Map<String, IEventChannel<?, ?>> eventChannels = new HashMap<>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 68 | |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 69 | // State related to the packet out map |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 70 | private static final String PACKET_OUT_MAP_NAME = "packetOutMap"; |
| 71 | private IMap<PacketOutNotification, byte[]> packetOutMap; |
| 72 | private final List<IPacketOutEventHandler> packetOutEventHandlers = new ArrayList<>(); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 73 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 74 | private final byte[] dummyByte = {0}; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 75 | |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 76 | // State related to the ARP reply map |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 77 | private static final String ARP_REPLY_MAP_NAME = "arpReplyMap"; |
| 78 | private IMap<ArpReplyNotification, byte[]> arpReplyMap; |
| 79 | private final List<IArpReplyEventHandler> arpReplyEventHandlers = new ArrayList<>(); |
| 80 | |
| 81 | |
| 82 | private static final String INTENT_LIST_NAME = "intentList"; |
| 83 | private IList<Intent> intentList; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | public void registerIntent(Collection<Intent> intents) { |
| 87 | intentList.addAll(intents); |
| 88 | } |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 89 | |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 90 | |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 91 | // State related to the Network Device map |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 92 | private static final String MAP_DEVICE_NAME = "mapDevice"; |
| 93 | private IMap<Long, OnosDevice> mapDevice; |
| 94 | private final List<IDeviceEventHandler> deviceEventHandlers = new ArrayList<>(); |
| 95 | |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 96 | /** |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 97 | * MapDeviceListener - reacts to Device related events. |
| 98 | */ |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 99 | class MapDeviceListener implements EntryListener<Long, OnosDevice> { |
| 100 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public void entryAdded(EntryEvent<Long, OnosDevice> event) { |
| 103 | for (IDeviceEventHandler deviceEventHandler : deviceEventHandlers) { |
| 104 | deviceEventHandler.addDeviceEvent(event.getKey(), event.getValue()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void entryRemoved(EntryEvent<Long, OnosDevice> event) { |
| 110 | for (IDeviceEventHandler deviceEventHandler : deviceEventHandlers) { |
| 111 | deviceEventHandler.deleteDeviceEvent(event.getKey(), event.getValue()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void entryUpdated(EntryEvent<Long, OnosDevice> event) { |
| 117 | for (IDeviceEventHandler deviceEventHandler : deviceEventHandlers) { |
| 118 | deviceEventHandler.updateDeviceEvent(event.getKey(), event.getValue()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void entryEvicted(EntryEvent<Long, OnosDevice> arg0) { |
| 124 | //Not used. |
| 125 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 126 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 127 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 128 | /** |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 129 | * Class for receiving notifications for sending packet-outs. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 130 | * <p/> |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 131 | * The datagrid map is: |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 132 | * - Key: Packet-out to send (PacketOutNotification) |
| 133 | * - Value: dummy value (we only need the key) (byte[]) |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 134 | */ |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 135 | class PacketOutMapListener implements EntryListener<PacketOutNotification, byte[]> { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Receive a notification that an entry is added. |
| 138 | * |
| 139 | * @param event the notification event for the entry. |
| 140 | */ |
| 141 | @Override |
| 142 | public void entryAdded(EntryEvent<PacketOutNotification, byte[]> event) { |
| 143 | for (IPacketOutEventHandler packetOutEventHandler : packetOutEventHandlers) { |
| 144 | packetOutEventHandler.packetOutNotification(event.getKey()); |
| 145 | } |
| 146 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 147 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Receive a notification that an entry is removed. |
| 150 | * |
| 151 | * @param event the notification event for the entry. |
| 152 | */ |
| 153 | @Override |
| 154 | public void entryRemoved(EntryEvent<PacketOutNotification, byte[]> event) { |
| 155 | // Not used |
| 156 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 157 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Receive a notification that an entry is updated. |
| 160 | * |
| 161 | * @param event the notification event for the entry. |
| 162 | */ |
| 163 | @Override |
| 164 | public void entryUpdated(EntryEvent<PacketOutNotification, byte[]> event) { |
| 165 | // Not used |
| 166 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 167 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Receive a notification that an entry is evicted. |
| 170 | * |
| 171 | * @param event the notification event for the entry. |
| 172 | */ |
| 173 | @Override |
| 174 | public void entryEvicted(EntryEvent<PacketOutNotification, byte[]> event) { |
| 175 | // Not used |
| 176 | } |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 177 | } |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 180 | * Class for receiving notifications for sending packet-outs. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 181 | * <p/> |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 182 | * The datagrid map is: |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 183 | * - Key: Packet-out to send (PacketOutNotification) |
| 184 | * - Value: dummy value (we only need the key) (byte[]) |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 185 | */ |
| 186 | class ArpReplyMapListener implements EntryListener<ArpReplyNotification, byte[]> { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 187 | /** |
| 188 | * Receive a notification that an entry is added. |
| 189 | * |
| 190 | * @param event the notification event for the entry. |
| 191 | */ |
| 192 | @Override |
| 193 | public void entryAdded(EntryEvent<ArpReplyNotification, byte[]> event) { |
| 194 | triggerEventHandler(event.getKey()); |
| 195 | } |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 196 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public void entryUpdated(EntryEvent<ArpReplyNotification, byte[]> event) { |
| 199 | triggerEventHandler(event.getKey()); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void entryRemoved(EntryEvent<ArpReplyNotification, byte[]> event) { |
| 204 | // Not used for ARP replies |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void entryEvicted(EntryEvent<ArpReplyNotification, byte[]> event) { |
| 209 | // Not used for ARP replies |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Handle an event. |
| 214 | * @param notification notification |
| 215 | */ |
| 216 | private void triggerEventHandler(ArpReplyNotification notification) { |
| 217 | for (IArpReplyEventHandler arpReplyEventHandler : arpReplyEventHandlers) { |
| 218 | arpReplyEventHandler.arpReplyEvent(notification); |
| 219 | } |
| 220 | } |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 221 | } |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 222 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Initialize the Hazelcast Datagrid operation. |
| 225 | * |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 226 | * @param configFilename the configuration filename. |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 227 | */ |
| 228 | public void init(String configFilename) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 229 | /* |
| 230 | System.setProperty("hazelcast.socket.receive.buffer.size", "32"); |
| 231 | System.setProperty("hazelcast.socket.send.buffer.size", "32"); |
| 232 | */ |
| 233 | // System.setProperty("hazelcast.heartbeat.interval.seconds", "100"); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 234 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 235 | // Init from configuration file |
| 236 | try { |
| 237 | hazelcastConfig = new FileSystemXmlConfig(configFilename); |
| 238 | } catch (FileNotFoundException e) { |
| 239 | log.error("Error opening Hazelcast XML configuration. File not found: " + configFilename, e); |
| 240 | } |
| 241 | /* |
| 242 | hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1"); |
| 243 | hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1"); |
| 244 | hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_THREAD_COUNT, "1"); |
| 245 | */ |
| 246 | // |
| 247 | hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_QUEUE_CAPACITY, "4000000"); |
| 248 | hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_RECEIVE_BUFFER_SIZE, "4096"); |
| 249 | hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_SEND_BUFFER_SIZE, "4096"); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Shutdown the Hazelcast Datagrid operation. |
| 254 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 255 | @Override |
| 256 | protected void finalize() { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 257 | close(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Shutdown the Hazelcast Datagrid operation. |
| 262 | */ |
| 263 | public void close() { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 264 | Hazelcast.shutdownAll(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get the collection of offered module services. |
| 269 | * |
| 270 | * @return the collection of offered module services. |
| 271 | */ |
| 272 | @Override |
| 273 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 274 | Collection<Class<? extends IFloodlightService>> l = |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 275 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 276 | l.add(IDatagridService.class); |
| 277 | return l; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Get the collection of implemented services. |
| 282 | * |
| 283 | * @return the collection of implemented services. |
| 284 | */ |
| 285 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 286 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 287 | getServiceImpls() { |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 288 | Map<Class<? extends IFloodlightService>, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 289 | IFloodlightService> m = |
| 290 | new HashMap<Class<? extends IFloodlightService>, |
| 291 | IFloodlightService>(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 292 | m.put(IDatagridService.class, this); |
| 293 | return m; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Get the collection of modules this module depends on. |
| 298 | * |
| 299 | * @return the collection of modules this module depends on. |
| 300 | */ |
| 301 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 302 | public Collection<Class<? extends IFloodlightService>> |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 303 | getModuleDependencies() { |
| 304 | Collection<Class<? extends IFloodlightService>> l = |
| 305 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 306 | l.add(IFloodlightProviderService.class); |
| 307 | l.add(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 308 | return l; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Initialize the module. |
| 313 | * |
| 314 | * @param context the module context to use for the initialization. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 315 | * @throws FloodlightModuleException on error |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 316 | */ |
| 317 | @Override |
| 318 | public void init(FloodlightModuleContext context) |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 319 | throws FloodlightModuleException { |
| 320 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 321 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 322 | // Get the configuration file name and configure the Datagrid |
| 323 | Map<String, String> configMap = context.getConfigParams(this); |
| 324 | String configFilename = configMap.get(HAZELCAST_CONFIG_FILE); |
| 325 | this.init(configFilename); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Startup module operation. |
| 330 | * |
| 331 | * @param context the module context to use for the startup. |
| 332 | */ |
| 333 | @Override |
| 334 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 335 | hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig); |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 336 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 337 | restApi.addRestletRoutable(new DatagridWebRoutable()); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 338 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 339 | packetOutMap = hazelcastInstance.getMap(PACKET_OUT_MAP_NAME); |
| 340 | packetOutMap.addEntryListener(new PacketOutMapListener(), true); |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 341 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 342 | arpReplyMap = hazelcastInstance.getMap(ARP_REPLY_MAP_NAME); |
| 343 | arpReplyMap.addEntryListener(new ArpReplyMapListener(), true); |
| 344 | intentList = hazelcastInstance.getList(INTENT_LIST_NAME); |
| 345 | |
| 346 | mapDevice = hazelcastInstance.getMap(MAP_DEVICE_NAME); |
| 347 | mapDevice.addEntryListener(new MapDeviceListener(), true); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 351 | * Create an event channel. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 352 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 353 | * If the channel already exists, just return it. |
| 354 | * NOTE: The channel is started automatically. |
| 355 | * |
| 356 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 357 | * @param <K> the type of the Key in the Key-Value store. |
| 358 | * @param <V> the type of the Value in the Key-Value store. |
| 359 | * @param typeK the type of the Key in the Key-Value store. |
| 360 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 361 | * @return the event channel for the channel name. |
| 362 | */ |
| 363 | @Override |
| 364 | public <K, V> IEventChannel<K, V> createChannel(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 365 | Class<K> typeK, Class<V> typeV) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 366 | synchronized (eventChannels) { |
| 367 | IEventChannel<K, V> eventChannel = |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 368 | createChannelImpl(channelName, typeK, typeV); |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 369 | eventChannel.startup(); |
| 370 | return eventChannel; |
| 371 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Create an event channel implementation. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 376 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 377 | * If the channel already exists, just return it. |
| 378 | * NOTE: The caller must call IEventChannel.startup() to startup the |
| 379 | * channel operation. |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 380 | * NOTE: The caller must own the lock on "eventChannels". |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 381 | * |
| 382 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 383 | * @param <K> the type of the Key in the Key-Value store. |
| 384 | * @param <V> the type of the Value in the Key-Value store. |
| 385 | * @param typeK the type of the Key in the Key-Value store. |
| 386 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 387 | * @return the event channel for the channel name. |
| 388 | */ |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 389 | private <K, V> IEventChannel<K, V> createChannelImpl( |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 390 | String channelName, |
| 391 | Class<K> typeK, Class<V> typeV) { |
| 392 | IEventChannel<K, V> castedEventChannel; |
| 393 | IEventChannel<?, ?> genericEventChannel = |
| 394 | eventChannels.get(channelName); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 395 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 396 | // Add the channel if the first listener |
| 397 | if (genericEventChannel == null) { |
| 398 | castedEventChannel = |
| 399 | new HazelcastEventChannel<K, V>(hazelcastInstance, |
| 400 | channelName, typeK, typeV); |
| 401 | eventChannels.put(channelName, castedEventChannel); |
| 402 | } else { |
| 403 | // |
| 404 | // TODO: Find if we can use Java internal support to check for |
| 405 | // type mismatch. |
| 406 | // |
| 407 | if (!genericEventChannel.verifyKeyValueTypes(typeK, typeV)) { |
| 408 | throw new ClassCastException("Key-value type mismatch for event channel " + channelName); |
| 409 | } |
| 410 | castedEventChannel = (IEventChannel<K, V>) genericEventChannel; |
| 411 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 412 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 413 | return castedEventChannel; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Add event channel listener. |
| 418 | * |
| 419 | * NOTE: The channel is started automatically right after the listener |
| 420 | * is added. |
| 421 | * |
| 422 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 423 | * @param listener the listener to add. |
| 424 | * @param <K> the type of the Key in the Key-Value store. |
| 425 | * @param <V> the type of the Value in the Key-Value store. |
| 426 | * @param typeK the type of the Key in the Key-Value store. |
| 427 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 428 | * @return the event channel for the channel name. |
| 429 | */ |
| 430 | @Override |
| 431 | public <K, V> IEventChannel<K, V> addListener(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 432 | IEventChannelListener<K, V> listener, |
| 433 | Class<K> typeK, Class<V> typeV) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 434 | synchronized (eventChannels) { |
| 435 | IEventChannel<K, V> eventChannel = |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 436 | createChannelImpl(channelName, typeK, typeV); |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 437 | eventChannel.addListener(listener); |
| 438 | eventChannel.startup(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 439 | |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 440 | return eventChannel; |
| 441 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Remove event channel listener. |
| 446 | * |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 447 | * @param <K> the type of the Key in the Key-Value store. |
| 448 | * @param <V> the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 449 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 450 | * @param listener the listener to remove. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 451 | */ |
| 452 | @Override |
| 453 | public <K, V> void removeListener(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 454 | IEventChannelListener<K, V> listener) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 455 | synchronized (eventChannels) { |
| 456 | IEventChannel<?, ?> genericEventChannel = |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 457 | eventChannels.get(channelName); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 458 | |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 459 | if (genericEventChannel != null) { |
| 460 | // |
| 461 | // TODO: Find if we can use Java internal support to check for |
| 462 | // type mismatch. |
| 463 | // NOTE: Using "ClassCastException" exception below doesn't |
| 464 | // work. |
| 465 | // |
| 466 | IEventChannel<K, V> castedEventChannel = |
| 467 | (IEventChannel<K, V>) genericEventChannel; |
| 468 | castedEventChannel.removeListener(listener); |
| 469 | } |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 470 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 473 | @Override |
Jonathan Hart | c632562 | 2014-01-14 16:37:50 -0800 | [diff] [blame] | 474 | public void registerPacketOutEventHandler(IPacketOutEventHandler packetOutEventHandler) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 475 | if (packetOutEventHandler != null) { |
| 476 | packetOutEventHandlers.add(packetOutEventHandler); |
| 477 | } |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 478 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 479 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 480 | @Override |
Jonathan Hart | c632562 | 2014-01-14 16:37:50 -0800 | [diff] [blame] | 481 | public void deregisterPacketOutEventHandler(IPacketOutEventHandler packetOutEventHandler) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 482 | packetOutEventHandlers.remove(packetOutEventHandler); |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 483 | } |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 484 | |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 485 | @Override |
| 486 | public void registerArpReplyEventHandler(IArpReplyEventHandler arpReplyEventHandler) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 487 | if (arpReplyEventHandler != null) { |
| 488 | arpReplyEventHandlers.add(arpReplyEventHandler); |
| 489 | } |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 490 | } |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 491 | |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 492 | @Override |
| 493 | public void deregisterArpReplyEventHandler(IArpReplyEventHandler arpReplyEventHandler) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 494 | arpReplyEventHandlers.remove(arpReplyEventHandler); |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 495 | } |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 496 | |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 497 | @Override |
| 498 | public void registerMapDeviceEventHandler(IDeviceEventHandler deviceEventHandler) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 499 | if (deviceEventHandler != null) { |
| 500 | deviceEventHandlers.add(deviceEventHandler); |
| 501 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public void deregisterMapDeviceEventHandler(IDeviceEventHandler deviceEventHandler) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 506 | deviceEventHandlers.remove(deviceEventHandler); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 507 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 508 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 509 | @Override |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 510 | public void sendPacketOutNotification(PacketOutNotification packetOutNotification) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 511 | packetOutMap.putAsync(packetOutNotification, dummyByte, 1L, TimeUnit.MILLISECONDS); |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 512 | } |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 513 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 514 | @Override |
| 515 | public void sendArpReplyNotification(ArpReplyNotification arpReply) { |
| 516 | arpReplyMap.putAsync(arpReply, dummyByte, 1L, TimeUnit.MILLISECONDS); |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void sendNotificationDeviceAdded(Long mac, OnosDevice dev) { |
| 521 | log.debug("DeviceAdded in datagrid. mac {}", dev.getMacAddress()); |
| 522 | mapDevice.putAsync(mac, dev); |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public void sendNotificationDeviceDeleted(OnosDevice dev) { |
| 527 | long mac = dev.getMacAddress().toLong(); |
| 528 | if (mapDevice.containsKey(mac)) { |
| 529 | log.debug("DeviceDeleted in datagrid. mac {}", dev.getMacAddress()); |
| 530 | mapDevice.removeAsync(mac); |
| 531 | } |
| 532 | } |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 533 | } |