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