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; |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.flowmanager.IFlowEventHandlerService; |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.proxyarp.ArpMessage; |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.proxyarp.IArpEventHandler; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 24 | import net.onrc.onos.ofcontroller.util.Dpid; |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 26 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 28 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 29 | import net.onrc.onos.ofcontroller.util.serializers.KryoFactory; |
| 30 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 34 | import com.esotericsoftware.kryo2.Kryo; |
| 35 | import com.esotericsoftware.kryo2.io.Input; |
| 36 | import com.esotericsoftware.kryo2.io.Output; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 37 | import com.hazelcast.config.Config; |
| 38 | import com.hazelcast.config.FileSystemXmlConfig; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 39 | import com.hazelcast.core.EntryEvent; |
| 40 | import com.hazelcast.core.EntryListener; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 41 | import com.hazelcast.core.Hazelcast; |
| 42 | import com.hazelcast.core.HazelcastInstance; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 43 | import com.hazelcast.core.IMap; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 44 | import com.hazelcast.instance.GroupProperties; |
| 45 | |
| 46 | /** |
| 47 | * A datagrid service that uses Hazelcast as a datagrid. |
| 48 | * The relevant data is stored in the Hazelcast datagrid and shared as |
| 49 | * appropriate in a multi-node cluster. |
| 50 | */ |
| 51 | public class HazelcastDatagrid implements IFloodlightModule, IDatagridService { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 52 | private final static int MAX_BUFFER_SIZE = 64*1024; |
| 53 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 54 | protected final static Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 55 | protected IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 56 | protected IRestApiService restApi; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 57 | |
| 58 | protected static final String HazelcastConfigFile = "datagridConfig"; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 59 | private HazelcastInstance hazelcastInstance = null; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 60 | private Config hazelcastConfig = null; |
| 61 | |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 62 | private KryoFactory kryoFactory = new KryoFactory(); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 63 | private IFlowEventHandlerService flowEventHandlerService = null; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 64 | |
| 65 | // State related to the Flow map |
| 66 | protected static final String mapFlowName = "mapFlow"; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 67 | private IMap<Long, byte[]> mapFlow = null; |
| 68 | private MapFlowListener mapFlowListener = null; |
| 69 | private String mapFlowListenerId = null; |
| 70 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 71 | // State related to the Flow Entry map |
| 72 | protected static final String mapFlowEntryName = "mapFlowEntry"; |
| 73 | private IMap<Long, byte[]> mapFlowEntry = null; |
| 74 | private MapFlowEntryListener mapFlowEntryListener = null; |
| 75 | private String mapFlowEntryListenerId = null; |
| 76 | |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 77 | // State related to the Flow ID map |
| 78 | protected static final String mapFlowIdName = "mapFlowId"; |
| 79 | private IMap<Long, byte[]> mapFlowId = null; |
| 80 | private MapFlowIdListener mapFlowIdListener = null; |
| 81 | private String mapFlowIdListenerId = null; |
| 82 | |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 83 | // State related to the Flow Entry ID map |
| 84 | protected static final String mapFlowEntryIdName = "mapFlowEntryId"; |
| 85 | private IMap<Long, byte[]> mapFlowEntryId = null; |
| 86 | private MapFlowEntryIdListener mapFlowEntryIdListener = null; |
| 87 | private String mapFlowEntryIdListenerId = null; |
| 88 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 89 | // State related to the Network Topology map |
| 90 | protected static final String mapTopologyName = "mapTopology"; |
| 91 | private IMap<String, byte[]> mapTopology = null; |
| 92 | private MapTopologyListener mapTopologyListener = null; |
| 93 | private String mapTopologyListenerId = null; |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 94 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 95 | // State related to the ARP map |
| 96 | protected static final String arpMapName = "arpMap"; |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 97 | private IMap<ArpMessage, byte[]> arpMap = null; |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 98 | private List<IArpEventHandler> arpEventHandlers = new ArrayList<IArpEventHandler>(); |
| 99 | private final byte[] dummyByte = {0}; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 100 | |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Class for receiving notifications for Flow state. |
| 103 | * |
| 104 | * The datagrid map is: |
| 105 | * - Key : Flow ID (Long) |
Pavlin Radoslavov | 5367d21 | 2013-11-07 11:18:51 -0800 | [diff] [blame] | 106 | * - Value : Serialized FlowPath (byte[]) |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 107 | */ |
| 108 | class MapFlowListener implements EntryListener<Long, byte[]> { |
| 109 | /** |
| 110 | * Receive a notification that an entry is added. |
| 111 | * |
| 112 | * @param event the notification event for the entry. |
| 113 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 114 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 115 | public void entryAdded(EntryEvent<Long, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 116 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 117 | |
| 118 | // |
| 119 | // Decode the value and deliver the notification |
| 120 | // |
| 121 | Kryo kryo = kryoFactory.newKryo(); |
| 122 | Input input = new Input(valueBytes); |
| 123 | FlowPath flowPath = kryo.readObject(input, FlowPath.class); |
| 124 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 125 | flowEventHandlerService.notificationRecvFlowAdded(flowPath); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Receive a notification that an entry is removed. |
| 130 | * |
| 131 | * @param event the notification event for the entry. |
| 132 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 133 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 134 | public void entryRemoved(EntryEvent<Long, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 135 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 136 | |
| 137 | // |
| 138 | // Decode the value and deliver the notification |
| 139 | // |
| 140 | Kryo kryo = kryoFactory.newKryo(); |
| 141 | Input input = new Input(valueBytes); |
| 142 | FlowPath flowPath = kryo.readObject(input, FlowPath.class); |
| 143 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 144 | flowEventHandlerService.notificationRecvFlowRemoved(flowPath); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Receive a notification that an entry is updated. |
| 149 | * |
| 150 | * @param event the notification event for the entry. |
| 151 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 152 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 153 | public void entryUpdated(EntryEvent<Long, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 154 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 155 | |
| 156 | // |
| 157 | // Decode the value and deliver the notification |
| 158 | // |
| 159 | Kryo kryo = kryoFactory.newKryo(); |
| 160 | Input input = new Input(valueBytes); |
| 161 | FlowPath flowPath = kryo.readObject(input, FlowPath.class); |
| 162 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 163 | flowEventHandlerService.notificationRecvFlowUpdated(flowPath); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Receive a notification that an entry is evicted. |
| 168 | * |
| 169 | * @param event the notification event for the entry. |
| 170 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 171 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 172 | public void entryEvicted(EntryEvent<Long, byte[]> event) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 173 | // NOTE: We don't use eviction for this map |
| 174 | } |
| 175 | } |
| 176 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 177 | /** |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 178 | * Class for receiving notifications for FlowEntry state. |
| 179 | * |
| 180 | * The datagrid map is: |
| 181 | * - Key : FlowEntry ID (Long) |
| 182 | * - Value : Serialized FlowEntry (byte[]) |
| 183 | */ |
| 184 | class MapFlowEntryListener implements EntryListener<Long, byte[]> { |
| 185 | /** |
| 186 | * Receive a notification that an entry is added. |
| 187 | * |
| 188 | * @param event the notification event for the entry. |
| 189 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 190 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 191 | public void entryAdded(EntryEvent<Long, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 192 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 193 | |
| 194 | // |
| 195 | // Decode the value and deliver the notification |
| 196 | // |
| 197 | Kryo kryo = kryoFactory.newKryo(); |
| 198 | Input input = new Input(valueBytes); |
| 199 | FlowEntry flowEntry = kryo.readObject(input, FlowEntry.class); |
| 200 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 201 | flowEventHandlerService.notificationRecvFlowEntryAdded(flowEntry); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Receive a notification that an entry is removed. |
| 206 | * |
| 207 | * @param event the notification event for the entry. |
| 208 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 209 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 210 | public void entryRemoved(EntryEvent<Long, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 211 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 212 | |
| 213 | // |
| 214 | // Decode the value and deliver the notification |
| 215 | // |
| 216 | Kryo kryo = kryoFactory.newKryo(); |
| 217 | Input input = new Input(valueBytes); |
| 218 | FlowEntry flowEntry = kryo.readObject(input, FlowEntry.class); |
| 219 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 220 | flowEventHandlerService.notificationRecvFlowEntryRemoved(flowEntry); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Receive a notification that an entry is updated. |
| 225 | * |
| 226 | * @param event the notification event for the entry. |
| 227 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 228 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 229 | public void entryUpdated(EntryEvent<Long, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 230 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 231 | |
| 232 | // |
| 233 | // Decode the value and deliver the notification |
| 234 | // |
| 235 | Kryo kryo = kryoFactory.newKryo(); |
| 236 | Input input = new Input(valueBytes); |
| 237 | FlowEntry flowEntry = kryo.readObject(input, FlowEntry.class); |
| 238 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 239 | flowEventHandlerService.notificationRecvFlowEntryUpdated(flowEntry); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Receive a notification that an entry is evicted. |
| 244 | * |
| 245 | * @param event the notification event for the entry. |
| 246 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 247 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 248 | public void entryEvicted(EntryEvent<Long, byte[]> event) { |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 249 | // NOTE: We don't use eviction for this map |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 254 | * Class for receiving notifications for FlowId state. |
| 255 | * |
| 256 | * The datagrid map is: |
| 257 | * - Key : FlowId (Long) |
| 258 | * - Value : Serialized FlowId (byte[]) |
| 259 | */ |
| 260 | class MapFlowIdListener implements EntryListener<Long, byte[]> { |
| 261 | /** |
| 262 | * Receive a notification that an entry is added. |
| 263 | * |
| 264 | * @param event the notification event for the entry. |
| 265 | */ |
| 266 | public void entryAdded(EntryEvent<Long, byte[]> event) { |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 267 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 268 | |
| 269 | // |
| 270 | // Decode the value and deliver the notification |
| 271 | // |
| 272 | Kryo kryo = kryoFactory.newKryo(); |
| 273 | Input input = new Input(valueBytes); |
| 274 | FlowId flowId = kryo.readObject(input, FlowId.class); |
| 275 | kryoFactory.deleteKryo(kryo); |
| 276 | flowEventHandlerService.notificationRecvFlowIdAdded(flowId); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Receive a notification that an entry is removed. |
| 281 | * |
| 282 | * @param event the notification event for the entry. |
| 283 | */ |
| 284 | public void entryRemoved(EntryEvent<Long, byte[]> event) { |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 285 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 286 | |
| 287 | // |
| 288 | // Decode the value and deliver the notification |
| 289 | // |
| 290 | Kryo kryo = kryoFactory.newKryo(); |
| 291 | Input input = new Input(valueBytes); |
| 292 | FlowId flowId = kryo.readObject(input, FlowId.class); |
| 293 | kryoFactory.deleteKryo(kryo); |
| 294 | flowEventHandlerService.notificationRecvFlowIdRemoved(flowId); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Receive a notification that an entry is updated. |
| 299 | * |
| 300 | * @param event the notification event for the entry. |
| 301 | */ |
| 302 | public void entryUpdated(EntryEvent<Long, byte[]> event) { |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 303 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 304 | |
| 305 | // |
| 306 | // Decode the value and deliver the notification |
| 307 | // |
| 308 | Kryo kryo = kryoFactory.newKryo(); |
| 309 | Input input = new Input(valueBytes); |
| 310 | FlowId flowId = kryo.readObject(input, FlowId.class); |
| 311 | kryoFactory.deleteKryo(kryo); |
| 312 | flowEventHandlerService.notificationRecvFlowIdUpdated(flowId); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Receive a notification that an entry is evicted. |
| 317 | * |
| 318 | * @param event the notification event for the entry. |
| 319 | */ |
| 320 | public void entryEvicted(EntryEvent<Long, byte[]> event) { |
| 321 | // NOTE: We don't use eviction for this map |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 326 | * Class for receiving notifications for FlowEntryId state. |
| 327 | * |
| 328 | * The datagrid map is: |
| 329 | * - Key : FlowEntryId (Long) |
| 330 | * - Value : Serialized Switch Dpid (byte[]) |
| 331 | */ |
| 332 | class MapFlowEntryIdListener implements EntryListener<Long, byte[]> { |
| 333 | /** |
| 334 | * Receive a notification that an entry is added. |
| 335 | * |
| 336 | * @param event the notification event for the entry. |
| 337 | */ |
| 338 | public void entryAdded(EntryEvent<Long, byte[]> event) { |
| 339 | Long keyLong = event.getKey(); |
| 340 | FlowEntryId flowEntryId = new FlowEntryId(keyLong); |
| 341 | |
| 342 | byte[] valueBytes = event.getValue(); |
| 343 | |
| 344 | // |
| 345 | // Decode the value and deliver the notification |
| 346 | // |
| 347 | Kryo kryo = kryoFactory.newKryo(); |
| 348 | Input input = new Input(valueBytes); |
| 349 | Dpid dpid = kryo.readObject(input, Dpid.class); |
| 350 | kryoFactory.deleteKryo(kryo); |
| 351 | flowEventHandlerService.notificationRecvFlowEntryIdAdded(flowEntryId, dpid); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Receive a notification that an entry is removed. |
| 356 | * |
| 357 | * @param event the notification event for the entry. |
| 358 | */ |
| 359 | public void entryRemoved(EntryEvent<Long, byte[]> event) { |
| 360 | Long keyLong = event.getKey(); |
| 361 | FlowEntryId flowEntryId = new FlowEntryId(keyLong); |
| 362 | |
| 363 | byte[] valueBytes = event.getValue(); |
| 364 | |
| 365 | // |
| 366 | // Decode the value and deliver the notification |
| 367 | // |
| 368 | Kryo kryo = kryoFactory.newKryo(); |
| 369 | Input input = new Input(valueBytes); |
| 370 | Dpid dpid = kryo.readObject(input, Dpid.class); |
| 371 | kryoFactory.deleteKryo(kryo); |
| 372 | flowEventHandlerService.notificationRecvFlowEntryIdRemoved(flowEntryId, dpid); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Receive a notification that an entry is updated. |
| 377 | * |
| 378 | * @param event the notification event for the entry. |
| 379 | */ |
| 380 | public void entryUpdated(EntryEvent<Long, byte[]> event) { |
| 381 | Long keyLong = event.getKey(); |
| 382 | FlowEntryId flowEntryId = new FlowEntryId(keyLong); |
| 383 | |
| 384 | byte[] valueBytes = event.getValue(); |
| 385 | |
| 386 | // |
| 387 | // Decode the value and deliver the notification |
| 388 | // |
| 389 | Kryo kryo = kryoFactory.newKryo(); |
| 390 | Input input = new Input(valueBytes); |
| 391 | Dpid dpid = kryo.readObject(input, Dpid.class); |
| 392 | kryoFactory.deleteKryo(kryo); |
| 393 | flowEventHandlerService.notificationRecvFlowEntryIdUpdated(flowEntryId, dpid); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Receive a notification that an entry is evicted. |
| 398 | * |
| 399 | * @param event the notification event for the entry. |
| 400 | */ |
| 401 | public void entryEvicted(EntryEvent<Long, byte[]> event) { |
| 402 | // NOTE: We don't use eviction for this map |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 407 | * Class for receiving notifications for Network Topology state. |
| 408 | * |
| 409 | * The datagrid map is: |
| 410 | * - Key: TopologyElement ID (String) |
| 411 | * - Value: Serialized TopologyElement (byte[]) |
| 412 | */ |
| 413 | class MapTopologyListener implements EntryListener<String, byte[]> { |
| 414 | /** |
| 415 | * Receive a notification that an entry is added. |
| 416 | * |
| 417 | * @param event the notification event for the entry. |
| 418 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 419 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 420 | public void entryAdded(EntryEvent<String, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 421 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 422 | |
| 423 | // |
| 424 | // Decode the value and deliver the notification |
| 425 | // |
| 426 | Kryo kryo = kryoFactory.newKryo(); |
| 427 | Input input = new Input(valueBytes); |
| 428 | TopologyElement topologyElement = |
| 429 | kryo.readObject(input, TopologyElement.class); |
| 430 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 431 | flowEventHandlerService.notificationRecvTopologyElementAdded(topologyElement); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Receive a notification that an entry is removed. |
| 436 | * |
| 437 | * @param event the notification event for the entry. |
| 438 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 439 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 440 | public void entryRemoved(EntryEvent<String, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 441 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 442 | |
| 443 | // |
| 444 | // Decode the value and deliver the notification |
| 445 | // |
| 446 | Kryo kryo = kryoFactory.newKryo(); |
| 447 | Input input = new Input(valueBytes); |
| 448 | TopologyElement topologyElement = |
| 449 | kryo.readObject(input, TopologyElement.class); |
| 450 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 451 | flowEventHandlerService.notificationRecvTopologyElementRemoved(topologyElement); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Receive a notification that an entry is updated. |
| 456 | * |
| 457 | * @param event the notification event for the entry. |
| 458 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 459 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 460 | public void entryUpdated(EntryEvent<String, byte[]> event) { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 461 | byte[] valueBytes = event.getValue(); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 462 | |
| 463 | // |
| 464 | // Decode the value and deliver the notification |
| 465 | // |
| 466 | Kryo kryo = kryoFactory.newKryo(); |
| 467 | Input input = new Input(valueBytes); |
| 468 | TopologyElement topologyElement = |
| 469 | kryo.readObject(input, TopologyElement.class); |
| 470 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 471 | flowEventHandlerService.notificationRecvTopologyElementUpdated(topologyElement); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Receive a notification that an entry is evicted. |
| 476 | * |
| 477 | * @param event the notification event for the entry. |
| 478 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 479 | @Override |
Pavlin Radoslavov | 95e6e90 | 2013-12-11 12:03:29 -0800 | [diff] [blame] | 480 | public void entryEvicted(EntryEvent<String, byte[]> event) { |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 481 | // NOTE: We don't use eviction for this map |
| 482 | } |
| 483 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 484 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 485 | /** |
| 486 | * Class for receiving notifications for ARP requests. |
| 487 | * |
| 488 | * The datagrid map is: |
| 489 | * - Key: Request ID (String) |
| 490 | * - Value: ARP request packet (byte[]) |
| 491 | */ |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 492 | class ArpMapListener implements EntryListener<ArpMessage, byte[]> { |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 493 | /** |
| 494 | * Receive a notification that an entry is added. |
| 495 | * |
| 496 | * @param event the notification event for the entry. |
| 497 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 498 | @Override |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 499 | public void entryAdded(EntryEvent<ArpMessage, byte[]> event) { |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 500 | for (IArpEventHandler arpEventHandler : arpEventHandlers) { |
| 501 | arpEventHandler.arpRequestNotification(event.getKey()); |
| 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 | // |
| 505 | // Decode the value and deliver the notification |
| 506 | // |
| 507 | /* |
| 508 | Kryo kryo = kryoFactory.newKryo(); |
| 509 | Input input = new Input(valueBytes); |
| 510 | TopologyElement topologyElement = |
| 511 | kryo.readObject(input, TopologyElement.class); |
| 512 | kryoFactory.deleteKryo(kryo); |
| 513 | flowEventHandlerService.notificationRecvTopologyElementAdded(topologyElement); |
| 514 | */ |
| 515 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 516 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 517 | /** |
| 518 | * Receive a notification that an entry is removed. |
| 519 | * |
| 520 | * @param event the notification event for the entry. |
| 521 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 522 | @Override |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 523 | public void entryRemoved(EntryEvent<ArpMessage, byte[]> event) { |
Jonathan Hart | 799242b | 2013-11-12 12:57:29 -0800 | [diff] [blame] | 524 | // Not used |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 525 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 526 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 527 | /** |
| 528 | * Receive a notification that an entry is updated. |
| 529 | * |
| 530 | * @param event the notification event for the entry. |
| 531 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 532 | @Override |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 533 | public void entryUpdated(EntryEvent<ArpMessage, byte[]> event) { |
Jonathan Hart | 799242b | 2013-11-12 12:57:29 -0800 | [diff] [blame] | 534 | // Not used |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 535 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 536 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 537 | /** |
| 538 | * Receive a notification that an entry is evicted. |
| 539 | * |
| 540 | * @param event the notification event for the entry. |
| 541 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 542 | @Override |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 543 | public void entryEvicted(EntryEvent<ArpMessage, byte[]> event) { |
Jonathan Hart | 799242b | 2013-11-12 12:57:29 -0800 | [diff] [blame] | 544 | // Not used |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 545 | } |
| 546 | } |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 547 | |
| 548 | /** |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 549 | * Initialize the Hazelcast Datagrid operation. |
| 550 | * |
| 551 | * @param conf the configuration filename. |
| 552 | */ |
| 553 | public void init(String configFilename) { |
| 554 | /* |
| 555 | System.setProperty("hazelcast.socket.receive.buffer.size", "32"); |
| 556 | System.setProperty("hazelcast.socket.send.buffer.size", "32"); |
| 557 | */ |
| 558 | // System.setProperty("hazelcast.heartbeat.interval.seconds", "100"); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 559 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 560 | // Init from configuration file |
| 561 | try { |
| 562 | hazelcastConfig = new FileSystemXmlConfig(configFilename); |
| 563 | } catch (FileNotFoundException e) { |
| 564 | log.error("Error opening Hazelcast XML configuration. File not found: " + configFilename, e); |
| 565 | } |
| 566 | /* |
| 567 | hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1"); |
| 568 | hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1"); |
| 569 | hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_THREAD_COUNT, "1"); |
| 570 | */ |
| 571 | // |
| 572 | hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_QUEUE_CAPACITY, "4000000"); |
| 573 | hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_RECEIVE_BUFFER_SIZE, "4096"); |
| 574 | hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_SEND_BUFFER_SIZE, "4096"); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Shutdown the Hazelcast Datagrid operation. |
| 579 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 580 | @Override |
| 581 | protected void finalize() { |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 582 | close(); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Shutdown the Hazelcast Datagrid operation. |
| 587 | */ |
| 588 | public void close() { |
| 589 | Hazelcast.shutdownAll(); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Get the collection of offered module services. |
| 594 | * |
| 595 | * @return the collection of offered module services. |
| 596 | */ |
| 597 | @Override |
| 598 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 599 | Collection<Class<? extends IFloodlightService>> l = |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 600 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 601 | l.add(IDatagridService.class); |
| 602 | return l; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Get the collection of implemented services. |
| 607 | * |
| 608 | * @return the collection of implemented services. |
| 609 | */ |
| 610 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 611 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 612 | getServiceImpls() { |
| 613 | Map<Class<? extends IFloodlightService>, |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 614 | IFloodlightService> m = |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 615 | new HashMap<Class<? extends IFloodlightService>, |
| 616 | IFloodlightService>(); |
| 617 | m.put(IDatagridService.class, this); |
| 618 | return m; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Get the collection of modules this module depends on. |
| 623 | * |
| 624 | * @return the collection of modules this module depends on. |
| 625 | */ |
| 626 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 627 | public Collection<Class<? extends IFloodlightService>> |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 628 | getModuleDependencies() { |
| 629 | Collection<Class<? extends IFloodlightService>> l = |
| 630 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 631 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 632 | l.add(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 633 | return l; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Initialize the module. |
| 638 | * |
| 639 | * @param context the module context to use for the initialization. |
| 640 | */ |
| 641 | @Override |
| 642 | public void init(FloodlightModuleContext context) |
| 643 | throws FloodlightModuleException { |
| 644 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 645 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 646 | |
| 647 | // Get the configuration file name and configure the Datagrid |
| 648 | Map<String, String> configMap = context.getConfigParams(this); |
| 649 | String configFilename = configMap.get(HazelcastConfigFile); |
| 650 | this.init(configFilename); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Startup module operation. |
| 655 | * |
| 656 | * @param context the module context to use for the startup. |
| 657 | */ |
| 658 | @Override |
| 659 | public void startUp(FloodlightModuleContext context) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 660 | hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig); |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 661 | |
| 662 | restApi.addRestletRoutable(new DatagridWebRoutable()); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 663 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 664 | arpMap = hazelcastInstance.getMap(arpMapName); |
| 665 | arpMap.addEntryListener(new ArpMapListener(), true); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /** |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 669 | * Register Flow Event Handler Service for receiving Flow-related |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 670 | * notifications. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 671 | * |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 672 | * NOTE: Only a single Flow Event Handler Service can be registered. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 673 | * |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 674 | * @param flowEventHandlerService the Flow Event Handler Service to register. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 675 | */ |
| 676 | @Override |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 677 | public void registerFlowEventHandlerService(IFlowEventHandlerService flowEventHandlerService) { |
| 678 | this.flowEventHandlerService = flowEventHandlerService; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 679 | |
| 680 | // Initialize the Flow-related map state |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 681 | mapFlowListener = new MapFlowListener(); |
| 682 | mapFlow = hazelcastInstance.getMap(mapFlowName); |
| 683 | mapFlowListenerId = mapFlow.addEntryListener(mapFlowListener, true); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 684 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 685 | // Initialize the FlowEntry-related map state |
| 686 | mapFlowEntryListener = new MapFlowEntryListener(); |
| 687 | mapFlowEntry = hazelcastInstance.getMap(mapFlowEntryName); |
| 688 | mapFlowEntryListenerId = mapFlowEntry.addEntryListener(mapFlowEntryListener, true); |
| 689 | |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 690 | // Initialize the FlowId-related map state |
| 691 | mapFlowIdListener = new MapFlowIdListener(); |
| 692 | mapFlowId = hazelcastInstance.getMap(mapFlowIdName); |
| 693 | mapFlowIdListenerId = mapFlowId.addEntryListener(mapFlowIdListener, true); |
| 694 | |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 695 | // Initialize the FlowEntryId-related map state |
| 696 | mapFlowEntryIdListener = new MapFlowEntryIdListener(); |
| 697 | mapFlowEntryId = hazelcastInstance.getMap(mapFlowEntryIdName); |
| 698 | mapFlowEntryIdListenerId = mapFlowEntryId.addEntryListener(mapFlowEntryIdListener, true); |
| 699 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 700 | // Initialize the Topology-related map state |
| 701 | mapTopologyListener = new MapTopologyListener(); |
| 702 | mapTopology = hazelcastInstance.getMap(mapTopologyName); |
| 703 | mapTopologyListenerId = mapTopology.addEntryListener(mapTopologyListener, true); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /** |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 707 | * De-register Flow Event Handler Service for receiving Flow-related |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 708 | * notifications. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 709 | * |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 710 | * NOTE: Only a single Flow Event Handler Service can be registered. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 711 | * |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 712 | * @param flowEventHandlerService the Flow Event Handler Service to |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 713 | * de-register. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 714 | */ |
| 715 | @Override |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 716 | public void deregisterFlowEventHandlerService(IFlowEventHandlerService flowEventHandlerService) { |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 717 | // Clear the Flow-related map state |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 718 | mapFlow.removeEntryListener(mapFlowListenerId); |
| 719 | mapFlow = null; |
| 720 | mapFlowListener = null; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 721 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 722 | // Clear the FlowEntry-related map state |
| 723 | mapFlowEntry.removeEntryListener(mapFlowEntryListenerId); |
| 724 | mapFlowEntry = null; |
| 725 | mapFlowEntryListener = null; |
| 726 | |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 727 | // Clear the FlowId-related map state |
| 728 | mapFlowId.removeEntryListener(mapFlowIdListenerId); |
| 729 | mapFlowId = null; |
| 730 | mapFlowIdListener = null; |
| 731 | |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 732 | // Clear the FlowEntryId-related map state |
| 733 | mapFlowEntryId.removeEntryListener(mapFlowEntryIdListenerId); |
| 734 | mapFlowEntryId = null; |
| 735 | mapFlowEntryIdListener = null; |
| 736 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 737 | // Clear the Topology-related map state |
| 738 | mapTopology.removeEntryListener(mapTopologyListenerId); |
| 739 | mapTopology = null; |
| 740 | mapTopologyListener = null; |
| 741 | |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 742 | this.flowEventHandlerService = null; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 743 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 744 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 745 | @Override |
| 746 | public void registerArpEventHandler(IArpEventHandler arpEventHandler) { |
| 747 | if (arpEventHandler != null) { |
| 748 | arpEventHandlers.add(arpEventHandler); |
| 749 | } |
| 750 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 751 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 752 | @Override |
| 753 | public void deregisterArpEventHandler(IArpEventHandler arpEventHandler) { |
| 754 | arpEventHandlers.remove(arpEventHandler); |
| 755 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 756 | |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 757 | /** |
| 758 | * Get all Flows that are currently in the datagrid. |
| 759 | * |
| 760 | * @return all Flows that are currently in the datagrid. |
| 761 | */ |
| 762 | @Override |
| 763 | public Collection<FlowPath> getAllFlows() { |
| 764 | Collection<FlowPath> allFlows = new LinkedList<FlowPath>(); |
| 765 | |
| 766 | // |
| 767 | // Get all current entries |
| 768 | // |
| 769 | Collection<byte[]> values = mapFlow.values(); |
| 770 | Kryo kryo = kryoFactory.newKryo(); |
| 771 | for (byte[] valueBytes : values) { |
| 772 | // |
| 773 | // Decode the value |
| 774 | // |
| 775 | Input input = new Input(valueBytes); |
| 776 | FlowPath flowPath = kryo.readObject(input, FlowPath.class); |
| 777 | allFlows.add(flowPath); |
| 778 | } |
| 779 | kryoFactory.deleteKryo(kryo); |
| 780 | |
| 781 | return allFlows; |
| 782 | } |
| 783 | |
| 784 | /** |
Pavlin Radoslavov | 379c904 | 2013-11-26 15:40:49 -0800 | [diff] [blame] | 785 | * Get a Flow for a given Flow ID. |
| 786 | * |
| 787 | * @param flowId the Flow ID of the Flow to get. |
| 788 | * @return the Flow if found, otherwise null. |
| 789 | */ |
| 790 | @Override |
| 791 | public FlowPath getFlow(FlowId flowId) { |
| 792 | byte[] valueBytes = mapFlow.get(flowId.value()); |
| 793 | if (valueBytes == null) |
| 794 | return null; |
| 795 | |
| 796 | Kryo kryo = kryoFactory.newKryo(); |
| 797 | // |
| 798 | // Decode the value |
| 799 | // |
| 800 | Input input = new Input(valueBytes); |
| 801 | FlowPath flowPath = kryo.readObject(input, FlowPath.class); |
| 802 | kryoFactory.deleteKryo(kryo); |
| 803 | |
| 804 | return flowPath; |
| 805 | } |
| 806 | |
| 807 | /** |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 808 | * Send a notification that a Flow is added. |
| 809 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 810 | * @param flowPath the Flow that is added. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 811 | */ |
| 812 | @Override |
| 813 | public void notificationSendFlowAdded(FlowPath flowPath) { |
| 814 | // |
| 815 | // Encode the value |
| 816 | // |
| 817 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 818 | Kryo kryo = kryoFactory.newKryo(); |
| 819 | Output output = new Output(buffer, -1); |
| 820 | kryo.writeObject(output, flowPath); |
| 821 | byte[] valueBytes = output.toBytes(); |
| 822 | kryoFactory.deleteKryo(kryo); |
| 823 | |
| 824 | // |
| 825 | // Put the entry: |
| 826 | // - Key : Flow ID (Long) |
| 827 | // - Value : Serialized Flow (byte[]) |
| 828 | // |
| 829 | mapFlow.putAsync(flowPath.flowId().value(), valueBytes); |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Send a notification that a Flow is removed. |
| 834 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 835 | * @param flowId the Flow ID of the Flow that is removed. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 836 | */ |
| 837 | @Override |
| 838 | public void notificationSendFlowRemoved(FlowId flowId) { |
| 839 | // |
| 840 | // Remove the entry: |
| 841 | // - Key : Flow ID (Long) |
| 842 | // - Value : Serialized Flow (byte[]) |
| 843 | // |
| 844 | mapFlow.removeAsync(flowId.value()); |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Send a notification that a Flow is updated. |
| 849 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 850 | * @param flowPath the Flow that is updated. |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 851 | */ |
| 852 | @Override |
| 853 | public void notificationSendFlowUpdated(FlowPath flowPath) { |
| 854 | // NOTE: Adding an entry with an existing key automatically updates it |
| 855 | notificationSendFlowAdded(flowPath); |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * Send a notification that all Flows are removed. |
| 860 | */ |
| 861 | @Override |
| 862 | public void notificationSendAllFlowsRemoved() { |
| 863 | // |
| 864 | // Remove all entries |
| 865 | // NOTE: We remove the entries one-by-one so the per-entry |
| 866 | // notifications will be delivered. |
| 867 | // |
| 868 | // mapFlow.clear(); |
| 869 | Set<Long> keySet = mapFlow.keySet(); |
| 870 | for (Long key : keySet) { |
| 871 | mapFlow.removeAsync(key); |
| 872 | } |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 873 | } |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 874 | |
| 875 | /** |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 876 | * Get all Flow Entries that are currently in the datagrid. |
| 877 | * |
| 878 | * @return all Flow Entries that are currently in the datagrid. |
| 879 | */ |
| 880 | @Override |
| 881 | public Collection<FlowEntry> getAllFlowEntries() { |
| 882 | Collection<FlowEntry> allFlowEntries = new LinkedList<FlowEntry>(); |
| 883 | |
| 884 | // |
| 885 | // Get all current entries |
| 886 | // |
| 887 | Collection<byte[]> values = mapFlowEntry.values(); |
| 888 | Kryo kryo = kryoFactory.newKryo(); |
| 889 | for (byte[] valueBytes : values) { |
| 890 | // |
| 891 | // Decode the value |
| 892 | // |
| 893 | Input input = new Input(valueBytes); |
| 894 | FlowEntry flowEntry = kryo.readObject(input, FlowEntry.class); |
| 895 | allFlowEntries.add(flowEntry); |
| 896 | } |
| 897 | kryoFactory.deleteKryo(kryo); |
| 898 | |
| 899 | return allFlowEntries; |
| 900 | } |
| 901 | |
| 902 | /** |
Pavlin Radoslavov | 379c904 | 2013-11-26 15:40:49 -0800 | [diff] [blame] | 903 | * Get a Flow Entry for a given Flow Entry ID. |
| 904 | * |
| 905 | * @param flowEntryId the Flow Entry ID of the Flow Entry to get. |
| 906 | * @return the Flow Entry if found, otherwise null. |
| 907 | */ |
| 908 | @Override |
| 909 | public FlowEntry getFlowEntry(FlowEntryId flowEntryId) { |
| 910 | byte[] valueBytes = mapFlowEntry.get(flowEntryId.value()); |
| 911 | if (valueBytes == null) |
| 912 | return null; |
| 913 | |
| 914 | Kryo kryo = kryoFactory.newKryo(); |
| 915 | // |
| 916 | // Decode the value |
| 917 | // |
| 918 | Input input = new Input(valueBytes); |
| 919 | FlowEntry flowEntry = kryo.readObject(input, FlowEntry.class); |
| 920 | kryoFactory.deleteKryo(kryo); |
| 921 | |
| 922 | return flowEntry; |
| 923 | } |
| 924 | |
| 925 | /** |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 926 | * Send a notification that a FlowEntry is added. |
| 927 | * |
| 928 | * @param flowEntry the FlowEntry that is added. |
| 929 | */ |
| 930 | @Override |
| 931 | public void notificationSendFlowEntryAdded(FlowEntry flowEntry) { |
| 932 | // |
| 933 | // Encode the value |
| 934 | // |
| 935 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 936 | Kryo kryo = kryoFactory.newKryo(); |
| 937 | Output output = new Output(buffer, -1); |
| 938 | kryo.writeObject(output, flowEntry); |
| 939 | byte[] valueBytes = output.toBytes(); |
| 940 | kryoFactory.deleteKryo(kryo); |
| 941 | |
| 942 | // |
| 943 | // Put the entry: |
| 944 | // - Key : FlowEntry ID (Long) |
| 945 | // - Value : Serialized FlowEntry (byte[]) |
| 946 | // |
| 947 | mapFlowEntry.putAsync(flowEntry.flowEntryId().value(), valueBytes); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Send a notification that a FlowEntry is removed. |
| 952 | * |
| 953 | * @param flowEntryId the FlowEntry ID of the FlowEntry that is removed. |
| 954 | */ |
| 955 | @Override |
| 956 | public void notificationSendFlowEntryRemoved(FlowEntryId flowEntryId) { |
| 957 | // |
| 958 | // Remove the entry: |
| 959 | // - Key : FlowEntry ID (Long) |
| 960 | // - Value : Serialized FlowEntry (byte[]) |
| 961 | // |
| 962 | mapFlowEntry.removeAsync(flowEntryId.value()); |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Send a notification that a FlowEntry is updated. |
| 967 | * |
| 968 | * @param flowEntry the FlowEntry that is updated. |
| 969 | */ |
| 970 | @Override |
| 971 | public void notificationSendFlowEntryUpdated(FlowEntry flowEntry) { |
| 972 | // NOTE: Adding an entry with an existing key automatically updates it |
| 973 | notificationSendFlowEntryAdded(flowEntry); |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * Send a notification that all Flow Entries are removed. |
| 978 | */ |
| 979 | @Override |
| 980 | public void notificationSendAllFlowEntriesRemoved() { |
| 981 | // |
| 982 | // Remove all entries |
| 983 | // NOTE: We remove the entries one-by-one so the per-entry |
| 984 | // notifications will be delivered. |
| 985 | // |
| 986 | // mapFlowEntry.clear(); |
| 987 | Set<Long> keySet = mapFlowEntry.keySet(); |
| 988 | for (Long key : keySet) { |
| 989 | mapFlowEntry.removeAsync(key); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | /** |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 994 | * Get all Flow IDs that are currently in the datagrid. |
| 995 | * |
| 996 | * @return all Flow IDs that are currently in the datagrid. |
| 997 | */ |
| 998 | @Override |
| 999 | public Collection<FlowId> getAllFlowIds() { |
| 1000 | Collection<FlowId> allFlowIds = new LinkedList<FlowId>(); |
| 1001 | |
| 1002 | // |
| 1003 | // Get all current entries |
| 1004 | // |
| 1005 | Collection<byte[]> values = mapFlowId.values(); |
| 1006 | Kryo kryo = kryoFactory.newKryo(); |
| 1007 | for (byte[] valueBytes : values) { |
| 1008 | // |
| 1009 | // Decode the value |
| 1010 | // |
| 1011 | Input input = new Input(valueBytes); |
| 1012 | FlowId flowId = kryo.readObject(input, FlowId.class); |
| 1013 | allFlowIds.add(flowId); |
| 1014 | } |
| 1015 | kryoFactory.deleteKryo(kryo); |
| 1016 | |
| 1017 | return allFlowIds; |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * Send a notification that a FlowId is added. |
| 1022 | * |
| 1023 | * @param flowId the FlowId that is added. |
| 1024 | */ |
| 1025 | @Override |
| 1026 | public void notificationSendFlowIdAdded(FlowId flowId) { |
| 1027 | // |
| 1028 | // Encode the value |
| 1029 | // |
| 1030 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 1031 | Kryo kryo = kryoFactory.newKryo(); |
| 1032 | Output output = new Output(buffer, -1); |
| 1033 | kryo.writeObject(output, flowId); |
| 1034 | byte[] valueBytes = output.toBytes(); |
| 1035 | kryoFactory.deleteKryo(kryo); |
| 1036 | |
| 1037 | // |
| 1038 | // Put the entry: |
| 1039 | // - Key : FlowId (Long) |
| 1040 | // - Value : Serialized FlowId (byte[]) |
| 1041 | // |
| 1042 | mapFlowId.putAsync(flowId.value(), valueBytes); |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * Send a notification that a FlowId is removed. |
| 1047 | * |
| 1048 | * @param flowId the FlowId that is removed. |
| 1049 | */ |
| 1050 | @Override |
| 1051 | public void notificationSendFlowIdRemoved(FlowId flowId) { |
| 1052 | // |
| 1053 | // Remove the entry: |
| 1054 | // - Key : FlowId (Long) |
| 1055 | // - Value : Serialized FlowId (byte[]) |
| 1056 | // |
| 1057 | mapFlowId.removeAsync(flowId.value()); |
| 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * Send a notification that a FlowId is updated. |
| 1062 | * |
| 1063 | * @param flowId the FlowId that is updated. |
| 1064 | */ |
| 1065 | @Override |
| 1066 | public void notificationSendFlowIdUpdated(FlowId flowId) { |
| 1067 | // NOTE: Adding an entry with an existing key automatically updates it |
| 1068 | notificationSendFlowIdAdded(flowId); |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * Send a notification that all Flow IDs are removed. |
| 1073 | */ |
| 1074 | @Override |
| 1075 | public void notificationSendAllFlowIdsRemoved() { |
| 1076 | // |
| 1077 | // Remove all entries |
| 1078 | // NOTE: We remove the entries one-by-one so the per-entry |
| 1079 | // notifications will be delivered. |
| 1080 | // |
| 1081 | // mapFlowId.clear(); |
| 1082 | Set<Long> keySet = mapFlowId.keySet(); |
| 1083 | for (Long key : keySet) { |
| 1084 | mapFlowId.removeAsync(key); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /** |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame^] | 1089 | * Send a notification that a FlowEntryId is added. |
| 1090 | * |
| 1091 | * @param flowEntryId the FlowEntryId that is added. |
| 1092 | * @param dpid the Switch Dpid. |
| 1093 | */ |
| 1094 | @Override |
| 1095 | public void notificationSendFlowEntryIdAdded(FlowEntryId flowEntryId, |
| 1096 | Dpid dpid) { |
| 1097 | // |
| 1098 | // Encode the value |
| 1099 | // |
| 1100 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 1101 | Kryo kryo = kryoFactory.newKryo(); |
| 1102 | Output output = new Output(buffer, -1); |
| 1103 | kryo.writeObject(output, dpid); |
| 1104 | byte[] valueBytes = output.toBytes(); |
| 1105 | kryoFactory.deleteKryo(kryo); |
| 1106 | |
| 1107 | // |
| 1108 | // Put the entry: |
| 1109 | // - Key : FlowEntryId (Long) |
| 1110 | // - Value : Serialized Switch Dpid (byte[]) |
| 1111 | // |
| 1112 | mapFlowEntryId.putAsync(flowEntryId.value(), valueBytes); |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * Send a notification that a FlowEntryId is removed. |
| 1117 | * |
| 1118 | * @param flowEntryId the FlowEntryId that is removed. |
| 1119 | */ |
| 1120 | @Override |
| 1121 | public void notificationSendFlowEntryIdRemoved(FlowEntryId flowEntryId) { |
| 1122 | // |
| 1123 | // Remove the entry: |
| 1124 | // - Key : FlowEntryId (Long) |
| 1125 | // - Value : Serialized Dpid (byte[]) |
| 1126 | // |
| 1127 | mapFlowEntryId.removeAsync(flowEntryId.value()); |
| 1128 | } |
| 1129 | |
| 1130 | /** |
| 1131 | * Send a notification that a FlowEntryId is updated. |
| 1132 | * |
| 1133 | * @param flowEntryId the FlowEntryId that is updated. |
| 1134 | * @param dpid the Switch Dpid. |
| 1135 | */ |
| 1136 | @Override |
| 1137 | public void notificationSendFlowEntryIdUpdated(FlowEntryId flowEntryId, |
| 1138 | Dpid dpid) { |
| 1139 | // NOTE: Adding an entry with an existing key automatically updates it |
| 1140 | notificationSendFlowEntryIdAdded(flowEntryId, dpid); |
| 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * Send a notification that all Flow Entry IDs are removed. |
| 1145 | */ |
| 1146 | @Override |
| 1147 | public void notificationSendAllFlowEntryIdsRemoved() { |
| 1148 | // |
| 1149 | // Remove all entries |
| 1150 | // NOTE: We remove the entries one-by-one so the per-entry |
| 1151 | // notifications will be delivered. |
| 1152 | // |
| 1153 | // mapFlowEntryId.clear(); |
| 1154 | Set<Long> keySet = mapFlowEntryId.keySet(); |
| 1155 | for (Long key : keySet) { |
| 1156 | mapFlowEntryId.removeAsync(key); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | /** |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 1161 | * Get all Topology Elements that are currently in the datagrid. |
| 1162 | * |
| 1163 | * @return all Topology Elements that are currently in the datagrid. |
| 1164 | */ |
| 1165 | @Override |
| 1166 | public Collection<TopologyElement> getAllTopologyElements() { |
| 1167 | Collection<TopologyElement> allTopologyElements = |
| 1168 | new LinkedList<TopologyElement>(); |
| 1169 | |
| 1170 | // |
| 1171 | // Get all current entries |
| 1172 | // |
| 1173 | Collection<byte[]> values = mapTopology.values(); |
| 1174 | Kryo kryo = kryoFactory.newKryo(); |
| 1175 | for (byte[] valueBytes : values) { |
| 1176 | // |
| 1177 | // Decode the value |
| 1178 | // |
| 1179 | Input input = new Input(valueBytes); |
| 1180 | TopologyElement topologyElement = |
| 1181 | kryo.readObject(input, TopologyElement.class); |
| 1182 | allTopologyElements.add(topologyElement); |
| 1183 | } |
| 1184 | kryoFactory.deleteKryo(kryo); |
| 1185 | |
| 1186 | return allTopologyElements; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * Send a notification that a Topology Element is added. |
| 1191 | * |
| 1192 | * @param topologyElement the Topology Element that is added. |
| 1193 | */ |
| 1194 | @Override |
| 1195 | public void notificationSendTopologyElementAdded(TopologyElement topologyElement) { |
| 1196 | // |
| 1197 | // Encode the value |
| 1198 | // |
| 1199 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 1200 | Kryo kryo = kryoFactory.newKryo(); |
| 1201 | Output output = new Output(buffer, -1); |
| 1202 | kryo.writeObject(output, topologyElement); |
| 1203 | byte[] valueBytes = output.toBytes(); |
| 1204 | kryoFactory.deleteKryo(kryo); |
| 1205 | |
| 1206 | // |
| 1207 | // Put the entry: |
| 1208 | // - Key : TopologyElement ID (String) |
| 1209 | // - Value : Serialized TopologyElement (byte[]) |
| 1210 | // |
| 1211 | mapTopology.putAsync(topologyElement.elementId(), valueBytes); |
| 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * Send a notification that a Topology Element is removed. |
| 1216 | * |
| 1217 | * @param topologyElement the Topology Element that is removed. |
| 1218 | */ |
| 1219 | @Override |
| 1220 | public void notificationSendTopologyElementRemoved(TopologyElement topologyElement) { |
| 1221 | // |
| 1222 | // Remove the entry: |
| 1223 | // - Key : TopologyElement ID (String) |
| 1224 | // - Value : Serialized TopologyElement (byte[]) |
| 1225 | // |
| 1226 | mapTopology.removeAsync(topologyElement.elementId()); |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Send a notification that a Topology Element is updated. |
| 1231 | * |
| 1232 | * @param topologyElement the Topology Element that is updated. |
| 1233 | */ |
| 1234 | @Override |
| 1235 | public void notificationSendTopologyElementUpdated(TopologyElement topologyElement) { |
| 1236 | // NOTE: Adding an entry with an existing key automatically updates it |
| 1237 | notificationSendTopologyElementAdded(topologyElement); |
| 1238 | } |
| 1239 | |
| 1240 | /** |
| 1241 | * Send a notification that all Topology Elements are removed. |
| 1242 | */ |
| 1243 | @Override |
| 1244 | public void notificationSendAllTopologyElementsRemoved() { |
| 1245 | // |
| 1246 | // Remove all entries |
| 1247 | // NOTE: We remove the entries one-by-one so the per-entry |
| 1248 | // notifications will be delivered. |
| 1249 | // |
| 1250 | // mapTopology.clear(); |
| 1251 | Set<String> keySet = mapTopology.keySet(); |
| 1252 | for (String key : keySet) { |
| 1253 | mapTopology.removeAsync(key); |
| 1254 | } |
| 1255 | } |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 1256 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 1257 | @Override |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 1258 | public void sendArpRequest(ArpMessage arpMessage) { |
| 1259 | //log.debug("ARP bytes: {}", HexString.toHexString(arpRequest)); |
| 1260 | arpMap.putAsync(arpMessage, dummyByte, 1L, TimeUnit.MILLISECONDS); |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 1261 | } |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 1262 | } |