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