HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 6 | import java.util.LinkedList; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 7 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 9 | import java.util.Random; |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 10 | import java.util.SortedMap; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 11 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 12 | import java.util.concurrent.LinkedBlockingQueue; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 13 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 17 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 18 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 20 | import net.floodlightcontroller.restserver.IRestApiService; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 21 | import net.onrc.onos.datagrid.IDatagridService; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 22 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.core.INetMapStorage; |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 24 | import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService; |
HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService; |
Pavlin Radoslavov | c086266 | 2013-12-10 15:31:49 -0800 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.forwarding.IForwardingService; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.topology.Topology; |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.util.*; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 30 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 34 | /** |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 35 | * Flow Manager class for handling the network flows. |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 36 | */ |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 37 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 38 | protected GraphDBOperation dbHandlerApi; |
| 39 | protected GraphDBOperation dbHandlerInner; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 40 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 41 | protected volatile IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 42 | protected volatile IDatagridService datagridService; |
| 43 | protected IRestApiService restApi; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 44 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 45 | protected FlowEventHandler flowEventHandler; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 46 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 47 | protected IFlowPusherService pusher; |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 48 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 49 | // Flow Entry ID generation state |
| 50 | private static Random randomGenerator = new Random(); |
| 51 | private static int nextFlowEntryIdPrefix = 0; |
| 52 | private static int nextFlowEntryIdSuffix = 0; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 53 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 54 | /** The logger. */ |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 55 | private final static Logger log = LoggerFactory.getLogger(FlowManager.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 56 | |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 57 | // The queue to write Flow Entries to the database |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 58 | private BlockingQueue<FlowPath> flowPathsToDatabaseQueue = |
| 59 | new LinkedBlockingQueue<FlowPath>(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 60 | FlowDatabaseWriter flowDatabaseWriter; |
| 61 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Initialize the Flow Manager. |
| 64 | * |
| 65 | * @param conf the Graph Database configuration string. |
| 66 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 67 | @Override |
| 68 | public void init(String conf) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 69 | dbHandlerApi = new GraphDBOperation(conf); |
| 70 | dbHandlerInner = new GraphDBOperation(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 71 | } |
| 72 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Shutdown the Flow Manager operation. |
| 75 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 76 | public void finalize() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 77 | close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 78 | } |
| 79 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Shutdown the Flow Manager operation. |
| 82 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 83 | @Override |
| 84 | public void close() { |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 85 | datagridService.deregisterFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 86 | dbHandlerApi.close(); |
| 87 | dbHandlerInner.close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 88 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 89 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Get the collection of offered module services. |
| 92 | * |
| 93 | * @return the collection of offered module services. |
| 94 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 97 | Collection<Class<? extends IFloodlightService>> l = |
| 98 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 99 | l.add(IFlowService.class); |
| 100 | return l; |
| 101 | } |
| 102 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Get the collection of implemented services. |
| 105 | * |
| 106 | * @return the collection of implemented services. |
| 107 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 108 | @Override |
| 109 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 110 | getServiceImpls() { |
| 111 | Map<Class<? extends IFloodlightService>, |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 112 | IFloodlightService> m = |
| 113 | new HashMap<Class<? extends IFloodlightService>, |
| 114 | IFloodlightService>(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 115 | m.put(IFlowService.class, this); |
| 116 | return m; |
| 117 | } |
| 118 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Get the collection of modules this module depends on. |
| 121 | * |
| 122 | * @return the collection of modules this module depends on. |
| 123 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 124 | @Override |
| 125 | public Collection<Class<? extends IFloodlightService>> |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 126 | getModuleDependencies() { |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 127 | Collection<Class<? extends IFloodlightService>> l = |
| 128 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 129 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 130 | l.add(INetworkGraphService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 131 | l.add(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 132 | l.add(IRestApiService.class); |
Pavlin Radoslavov | c086266 | 2013-12-10 15:31:49 -0800 | [diff] [blame] | 133 | l.add(IFlowPusherService.class); |
| 134 | l.add(IForwardingService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 135 | return l; |
| 136 | } |
| 137 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Initialize the module. |
| 140 | * |
| 141 | * @param context the module context to use for the initialization. |
| 142 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 143 | @Override |
| 144 | public void init(FloodlightModuleContext context) |
| 145 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 146 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 147 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 148 | datagridService = context.getServiceImpl(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 149 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 939ca6b | 2013-12-03 12:35:37 -0800 | [diff] [blame] | 150 | pusher = context.getServiceImpl(IFlowPusherService.class); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 151 | |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 152 | this.init(""); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 153 | } |
| 154 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Get the next Flow Entry ID to use. |
| 157 | * |
| 158 | * @return the next Flow Entry ID to use. |
| 159 | */ |
Naoki Shiota | 4e77de9 | 2013-11-18 17:29:54 -0800 | [diff] [blame] | 160 | @Override |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 161 | public synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 162 | // |
| 163 | // Generate the next Flow Entry ID. |
| 164 | // NOTE: For now, the higher 32 bits are random, and |
| 165 | // the lower 32 bits are sequential. |
| 166 | // In the future, we need a better allocation mechanism. |
| 167 | // |
| 168 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 169 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 170 | nextFlowEntryIdSuffix = 0; |
| 171 | } else { |
| 172 | nextFlowEntryIdSuffix++; |
| 173 | } |
| 174 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 175 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 176 | return result; |
| 177 | } |
| 178 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Startup module operation. |
| 181 | * |
| 182 | * @param context the module context to use for the startup. |
| 183 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 184 | @Override |
| 185 | public void startUp(FloodlightModuleContext context) { |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 186 | restApi.addRestletRoutable(new FlowWebRoutable()); |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 187 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 188 | // Initialize the Flow Entry ID generator |
| 189 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 190 | |
| 191 | // |
| 192 | // The thread to write to the database |
| 193 | // |
| 194 | flowDatabaseWriter = new FlowDatabaseWriter(this, |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 195 | flowPathsToDatabaseQueue); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 196 | flowDatabaseWriter.start(); |
| 197 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 198 | // |
Pavlin Radoslavov | c9da532 | 2013-11-22 11:59:46 -0800 | [diff] [blame] | 199 | // The Flow Event Handler thread: |
| 200 | // - create |
| 201 | // - register with the Datagrid Service |
| 202 | // - startup |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 203 | // |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 204 | flowEventHandler = new FlowEventHandler(this, datagridService); |
| 205 | datagridService.registerFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 206 | flowEventHandler.start(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Add a flow. |
| 211 | * |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 212 | * @param flowPath the Flow Path to install. |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 213 | * @return the Flow ID on success, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 214 | */ |
| 215 | @Override |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 216 | public FlowId addFlow(FlowPath flowPath) { |
| 217 | |
| 218 | // Allocate the Flow ID if necessary |
Pavlin Radoslavov | 892dd18 | 2013-12-05 23:33:15 -0800 | [diff] [blame] | 219 | if (! flowPath.isValidFlowId()) { |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 220 | long id = getNextFlowEntryId(); |
| 221 | flowPath.setFlowId(new FlowId(id)); |
| 222 | } |
| 223 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 224 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 225 | // NOTE: We need to explicitly initialize some of the state, |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 226 | // in case the application didn't do it. |
| 227 | // |
| 228 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 229 | if (flowEntry.flowEntrySwitchState() == |
| 230 | FlowEntrySwitchState.FE_SWITCH_UNKNOWN) { |
| 231 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 232 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 233 | if (! flowEntry.isValidFlowId()) |
| 234 | flowEntry.setFlowId(new FlowId(flowPath.flowId().value())); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 237 | if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 238 | datagridService.notificationSendFlowAdded(flowPath); |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 239 | return flowPath.flowId(); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 240 | } |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 241 | return null; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 245 | * Delete all previously added flows. |
| 246 | * |
| 247 | * @return true on success, otherwise false. |
| 248 | */ |
| 249 | @Override |
| 250 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 251 | if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 252 | datagridService.notificationSendAllFlowsRemoved(); |
| 253 | return true; |
| 254 | } |
| 255 | return false; |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 259 | * Delete a previously added flow. |
| 260 | * |
| 261 | * @param flowId the Flow ID of the flow to delete. |
| 262 | * @return true on success, otherwise false. |
| 263 | */ |
| 264 | @Override |
| 265 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 266 | if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 267 | datagridService.notificationSendFlowRemoved(flowId); |
| 268 | return true; |
| 269 | } |
| 270 | return false; |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 274 | * Get a previously added flow. |
| 275 | * |
| 276 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 277 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 278 | */ |
| 279 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 280 | public FlowPath getFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 281 | return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 282 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 283 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 284 | /** |
| 285 | * Get all installed flows by all installers. |
| 286 | * |
| 287 | * @return the Flow Paths if found, otherwise null. |
| 288 | */ |
| 289 | @Override |
| 290 | public ArrayList<FlowPath> getAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 291 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 295 | * Get summary of all installed flows by all installers in a given range. |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 296 | * |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 297 | * @param flowId the Flow ID of the first flow in the flow range to get. |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 298 | * @param maxFlows the maximum number of flows to be returned. |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 299 | * @return the Flow Paths if found, otherwise null. |
| 300 | */ |
| 301 | @Override |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 302 | public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId, |
| 303 | int maxFlows) { |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 304 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 305 | SortedMap<Long, FlowPath> sortedFlowPaths = |
| 306 | flowEventHandler.getAllFlowPathsCopy(); |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 307 | |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 308 | // |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 309 | // Truncate each Flow Path and Flow Entry |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 310 | // |
| 311 | for (FlowPath flowPath : sortedFlowPaths.values()) { |
| 312 | // |
| 313 | // TODO: Add only the Flow Paths that have been successfully |
| 314 | // installed. |
| 315 | // |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 316 | flowPath.setFlowEntryMatch(null); |
| 317 | flowPath.setFlowEntryActions(null); |
| 318 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 319 | flowEntry.setFlowEntryMatch(null); |
| 320 | flowEntry.setFlowEntryActions(null); |
| 321 | } |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 322 | flowPaths.add(flowPath); |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 325 | return flowPaths; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 326 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 327 | |
| 328 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 329 | * Get the collection of my switches. |
| 330 | * |
| 331 | * @return the collection of my switches. |
| 332 | */ |
| 333 | public Map<Long, IOFSwitch> getMySwitches() { |
| 334 | return floodlightProvider.getSwitches(); |
| 335 | } |
| 336 | |
| 337 | /** |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 338 | * Get the network topology. |
| 339 | * |
| 340 | * @return the network topology. |
| 341 | */ |
| 342 | public Topology getTopology() { |
| 343 | return flowEventHandler.getTopology(); |
| 344 | } |
| 345 | |
| 346 | /** |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 347 | * Inform the Flow Manager that a Flow Entry on switch expired. |
| 348 | * |
Pavlin Radoslavov | 3bd5ccf | 2013-11-26 15:10:21 -0800 | [diff] [blame] | 349 | * @param sw the switch the Flow Entry expired on. |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 350 | * @param flowEntryId the Flow Entry ID of the expired Flow Entry. |
| 351 | */ |
Pavlin Radoslavov | c5718e7 | 2013-12-10 15:47:10 -0800 | [diff] [blame] | 352 | public void flowEntryOnSwitchExpired(IOFSwitch sw, |
| 353 | FlowEntryId flowEntryId) { |
| 354 | // Find the Flow Entry |
| 355 | FlowEntry flowEntry = datagridService.getFlowEntry(flowEntryId); |
| 356 | if (flowEntryId == null) |
| 357 | return; // Flow Entry not found |
| 358 | |
| 359 | // Find the Flow Path |
| 360 | FlowPath flowPath = datagridService.getFlow(flowEntry.flowId()); |
| 361 | if (flowPath == null) |
| 362 | return; // Flow Path not found |
| 363 | |
| 364 | // |
| 365 | // Remove the Flow if the Flow Entry expired on the first switch |
| 366 | // |
| 367 | Dpid srcDpid = flowPath.dataPath().srcPort().dpid(); |
| 368 | if (srcDpid.value() != sw.getId()) |
| 369 | return; |
| 370 | deleteFlow(flowPath.flowId()); |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 374 | * Inform the Flow Manager that a collection of Flow Entries have been |
| 375 | * pushed to a switch. |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 376 | * |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 377 | * @param entries the collection of <IOFSwitch, FlowEntry> pairs |
| 378 | * that have been pushed. |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 379 | */ |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 380 | public void flowEntriesPushedToSwitch( |
| 381 | Collection<Pair<IOFSwitch, FlowEntry>> entries) { |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 382 | |
| 383 | // |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 384 | // Process all entries |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 385 | // |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 386 | for (Pair<IOFSwitch, FlowEntry> entry : entries) { |
Pavlin Radoslavov | 4535bc1 | 2013-12-05 10:43:49 -0800 | [diff] [blame] | 387 | FlowEntry flowEntry = entry.second; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 388 | |
| 389 | // |
| 390 | // Mark the Flow Entry that it has been pushed to the switch |
| 391 | // |
| 392 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED); |
| 393 | |
| 394 | // |
| 395 | // Write the Flow Entry to the Datagrid |
| 396 | // |
| 397 | switch (flowEntry.flowEntryUserState()) { |
| 398 | case FE_USER_ADD: |
| 399 | datagridService.notificationSendFlowEntryAdded(flowEntry); |
| 400 | break; |
| 401 | case FE_USER_MODIFY: |
| 402 | datagridService.notificationSendFlowEntryUpdated(flowEntry); |
| 403 | break; |
| 404 | case FE_USER_DELETE: |
| 405 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
| 406 | break; |
Naoki Shiota | 991093a | 2013-12-10 14:47:18 -0800 | [diff] [blame] | 407 | default: |
| 408 | break; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 409 | } |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | /** |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 414 | * Push modified Flow-related state as appropriate. |
| 415 | * |
| 416 | * @param modifiedFlowPaths the collection of modified Flow Paths. |
| 417 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 418 | */ |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 419 | void pushModifiedFlowState(Collection<FlowPath> modifiedFlowPaths, |
| 420 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 421 | // |
| 422 | // Push the modified Flow state: |
| 423 | // - Flow Entries to switches and the datagrid |
| 424 | // - Flow Paths to the database |
| 425 | // |
| 426 | pushModifiedFlowEntriesToSwitches(modifiedFlowEntries); |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 427 | pushModifiedFlowPathsToDatabase(modifiedFlowPaths); |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 428 | cleanupDeletedFlowEntriesFromDatagrid(modifiedFlowEntries); |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 432 | * Push modified Flow Entries to switches. |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 433 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 434 | * NOTE: Only the Flow Entries to switches controlled by this instance |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 435 | * are pushed. |
| 436 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 437 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 438 | */ |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 439 | private void pushModifiedFlowEntriesToSwitches( |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 440 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 441 | if (modifiedFlowEntries.isEmpty()) |
| 442 | return; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 443 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 444 | List<Pair<IOFSwitch, FlowEntry>> entries = |
| 445 | new LinkedList<Pair<IOFSwitch, FlowEntry>>(); |
| 446 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 447 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 448 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 449 | // |
| 450 | // Create a collection of my Flow Entries to push |
| 451 | // |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 452 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 453 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 454 | if (mySwitch == null) |
| 455 | continue; |
| 456 | |
Pavlin Radoslavov | aca49d1 | 2013-12-04 19:49:17 -0800 | [diff] [blame] | 457 | // |
| 458 | // Assign Flow Entry IDs if missing. |
| 459 | // |
| 460 | // NOTE: This is an additional safeguard, in case the |
| 461 | // mySwitches set has changed (after the Flow Entry IDs |
| 462 | // assignments by the caller). |
| 463 | // |
| 464 | if (! flowEntry.isValidFlowEntryId()) { |
| 465 | long id = getNextFlowEntryId(); |
| 466 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 467 | } |
| 468 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 469 | log.debug("Pushing Flow Entry To Switch: {}", flowEntry.toString()); |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 470 | entries.add(new Pair<IOFSwitch, FlowEntry>(mySwitch, flowEntry)); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 471 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 472 | |
| 473 | pusher.pushFlowEntries(entries); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 474 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 475 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 476 | /** |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 477 | * Cleanup deleted Flow Entries from the datagrid. |
| 478 | * |
| 479 | * NOTE: We cleanup only the Flow Entries that are not for our switches. |
| 480 | * This is needed to handle the case a switch going down: |
| 481 | * It has no Master controller instance, hence no controller instance |
| 482 | * will cleanup its flow entries. |
| 483 | * This is sub-optimal: we need to elect a controller instance to handle |
| 484 | * the cleanup of such orphaned flow entries. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 485 | * |
| 486 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 487 | */ |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 488 | private void cleanupDeletedFlowEntriesFromDatagrid( |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 489 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 490 | if (modifiedFlowEntries.isEmpty()) |
| 491 | return; |
| 492 | |
| 493 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 494 | |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 495 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 496 | // |
| 497 | // Process only Flow Entries that should be deleted and have |
| 498 | // a valid Flow Entry ID. |
| 499 | // |
Pavlin Radoslavov | 426a853 | 2013-12-02 17:32:21 -0800 | [diff] [blame] | 500 | if (! flowEntry.isValidFlowEntryId()) |
| 501 | continue; |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 502 | if (flowEntry.flowEntryUserState() != |
| 503 | FlowEntryUserState.FE_USER_DELETE) { |
| 504 | continue; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 507 | // |
| 508 | // NOTE: The deletion of Flow Entries for my switches is handled |
| 509 | // elsewhere. |
| 510 | // |
| 511 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 512 | if (mySwitch != null) |
| 513 | continue; |
| 514 | |
| 515 | log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry.toString()); |
| 516 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 517 | // |
| 518 | // Write the Flow Entry to the Datagrid |
| 519 | // |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 520 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 521 | } |
| 522 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 523 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 524 | /** |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 525 | * Class to implement writing to the database in a separate thread. |
| 526 | */ |
| 527 | class FlowDatabaseWriter extends Thread { |
| 528 | private FlowManager flowManager; |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 529 | private BlockingQueue<FlowPath> blockingQueue; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 530 | |
| 531 | /** |
| 532 | * Constructor. |
| 533 | * |
| 534 | * @param flowManager the Flow Manager to use. |
| 535 | * @param blockingQueue the blocking queue to use. |
| 536 | */ |
| 537 | FlowDatabaseWriter(FlowManager flowManager, |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 538 | BlockingQueue<FlowPath> blockingQueue) { |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 539 | this.flowManager = flowManager; |
| 540 | this.blockingQueue = blockingQueue; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Run the thread. |
| 545 | */ |
| 546 | @Override |
| 547 | public void run() { |
| 548 | // |
| 549 | // The main loop |
| 550 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 551 | Collection<FlowPath> collection = new LinkedList<FlowPath>(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 552 | try { |
| 553 | while (true) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 554 | FlowPath flowPath = blockingQueue.take(); |
| 555 | collection.add(flowPath); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 556 | blockingQueue.drainTo(collection); |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 557 | flowManager.writeModifiedFlowPathsToDatabase(collection); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 558 | collection.clear(); |
| 559 | } |
| 560 | } catch (Exception exception) { |
| 561 | log.debug("Exception writing to the Database: ", exception); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 567 | * Push Flow Paths to the Network MAP. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 568 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 569 | * NOTE: The complete Flow Paths are pushed only on the instance |
| 570 | * responsible for the first switch. This is to avoid database errors |
| 571 | * when multiple instances are writing Flow Entries for the same Flow Path. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 572 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 573 | * @param modifiedFlowPaths the collection of Flow Paths to push. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 574 | */ |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 575 | private void pushModifiedFlowPathsToDatabase( |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 576 | Collection<FlowPath> modifiedFlowPaths) { |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 577 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 578 | // We only add the Flow Paths to the Database Queue. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 579 | // The FlowDatabaseWriter thread is responsible for the actual writing. |
| 580 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 581 | flowPathsToDatabaseQueue.addAll(modifiedFlowPaths); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /** |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 585 | * Write Flow Paths to the Network MAP. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 586 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 587 | * NOTE: The complete Flow Paths are pushed only on the instance |
| 588 | * responsible for the first switch. This is to avoid database errors |
| 589 | * when multiple instances are writing Flow Entries for the same Flow Path. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 590 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 591 | * @param modifiedFlowPaths the collection of Flow Paths to write. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 592 | */ |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 593 | private void writeModifiedFlowPathsToDatabase( |
| 594 | Collection<FlowPath> modifiedFlowPaths) { |
| 595 | if (modifiedFlowPaths.isEmpty()) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 596 | return; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 597 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 598 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 599 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 600 | for (FlowPath flowPath : modifiedFlowPaths) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 601 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 602 | // Push the changes only on the instance responsible for the |
| 603 | // first switch. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 604 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 605 | Dpid srcDpid = flowPath.dataPath().srcPort().dpid(); |
| 606 | IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value()); |
| 607 | if (mySrcSwitch == null) |
| 608 | continue; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 609 | |
| 610 | // |
Pavlin Radoslavov | ef54505 | 2013-12-05 15:17:25 -0800 | [diff] [blame] | 611 | // Delete the Flow Path from the Network Map |
| 612 | // |
| 613 | if (flowPath.flowPathUserState() == |
| 614 | FlowPathUserState.FP_USER_DELETE) { |
| 615 | log.debug("Deleting Flow Path From Database: {}", |
| 616 | flowPath.toString()); |
| 617 | |
| 618 | try { |
| 619 | if (! FlowDatabaseOperation.deleteFlow( |
| 620 | dbHandlerInner, |
| 621 | flowPath.flowId())) { |
| 622 | log.error("Cannot delete Flow Path {} from Network Map", |
| 623 | flowPath.flowId()); |
| 624 | } |
| 625 | } catch (Exception e) { |
| 626 | log.error("Exception deleting Flow Path from Network MAP: {}", e); |
| 627 | } |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 632 | // Test whether all Flow Entries are valid |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 633 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 634 | boolean allValid = true; |
| 635 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 636 | if (flowEntry.flowEntryUserState() == |
| 637 | FlowEntryUserState.FE_USER_DELETE) { |
| 638 | continue; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 639 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 640 | if (! flowEntry.isValidFlowEntryId()) { |
| 641 | allValid = false; |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | if (! allValid) |
| 646 | continue; |
| 647 | |
| 648 | log.debug("Pushing Flow Path To Database: {}", flowPath.toString()); |
| 649 | |
| 650 | // |
| 651 | // Write the Flow Path to the Network Map |
| 652 | // |
| 653 | try { |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 654 | if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath)) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 655 | String logMsg = "Cannot write to Network Map Flow Path " + |
| 656 | flowPath.flowId(); |
| 657 | log.error(logMsg); |
| 658 | } |
| 659 | } catch (Exception e) { |
| 660 | log.error("Exception writing Flow Path to Network MAP: ", e); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 663 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 664 | } |