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; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 5 | import java.util.EnumSet; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 6 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 7 | import java.util.LinkedList; |
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 | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 10 | import java.util.concurrent.Executors; |
| 11 | import java.util.concurrent.ScheduledExecutorService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 12 | import java.util.concurrent.TimeUnit; |
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 | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.util.OFMessageDamper; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 22 | |
| 23 | import net.onrc.onos.datagrid.IDatagridService; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 24 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.core.INetMapStorage; |
| 26 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 27 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService; |
HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable; |
Pavlin Radoslavov | 1278ac7 | 2013-10-16 04:43:49 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.topology.ITopologyNetService; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.topology.Topology; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 33 | import net.onrc.onos.ofcontroller.util.*; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 34 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 35 | import org.openflow.protocol.OFType; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 39 | /** |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 40 | * Flow Manager class for handling the network flows. |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 41 | */ |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 42 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 43 | |
Pavlin Radoslavov | e0e48f7 | 2013-11-07 11:22:43 -0800 | [diff] [blame^] | 44 | // |
| 45 | // TODO: A temporary variable to switch between the poll-based and |
| 46 | // notification mechanism for the Flow Manager. |
| 47 | // |
| 48 | private final static boolean enableNotifications = false; |
| 49 | |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 50 | protected GraphDBOperation dbHandlerApi; |
| 51 | protected GraphDBOperation dbHandlerInner; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 52 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 53 | protected volatile IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | 1278ac7 | 2013-10-16 04:43:49 -0700 | [diff] [blame] | 54 | protected volatile ITopologyNetService topologyNetService; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 55 | protected volatile IDatagridService datagridService; |
| 56 | protected IRestApiService restApi; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 57 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 58 | protected FlowEventHandler flowEventHandler; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 59 | |
| 60 | protected OFMessageDamper messageDamper; |
| 61 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 62 | // |
| 63 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 64 | // The local copy should go away! |
| 65 | // |
| 66 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 67 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 68 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 69 | // Flow Entry ID generation state |
| 70 | private static Random randomGenerator = new Random(); |
| 71 | private static int nextFlowEntryIdPrefix = 0; |
| 72 | private static int nextFlowEntryIdSuffix = 0; |
| 73 | private static long nextFlowEntryId = 0; |
| 74 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 75 | /** The logger. */ |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 76 | private final static Logger log = LoggerFactory.getLogger(FlowManager.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 77 | |
| 78 | // The periodic task(s) |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 79 | private ScheduledExecutorService mapReaderScheduler; |
| 80 | private ScheduledExecutorService shortestPathReconcileScheduler; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 81 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Periodic task for reading the Flow Entries and pushing changes |
| 84 | * into the switches. |
| 85 | */ |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 86 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 87 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 88 | try { |
| 89 | runImpl(); |
| 90 | } catch (Exception e) { |
| 91 | log.debug("Exception processing All Flow Entries from the Network MAP: ", e); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 92 | dbHandlerInner.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private void runImpl() { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 98 | long startTime = System.nanoTime(); |
| 99 | int counterAllFlowEntries = 0; |
| 100 | int counterMyNotUpdatedFlowEntries = 0; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 101 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 102 | if (floodlightProvider == null) { |
| 103 | log.debug("FloodlightProvider service not found!"); |
| 104 | return; |
| 105 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 106 | Map<Long, IOFSwitch> mySwitches = |
| 107 | floodlightProvider.getSwitches(); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 108 | if (mySwitches.isEmpty()) { |
Pankaj Berde | 85a877b | 2013-10-03 18:26:35 -0700 | [diff] [blame] | 109 | log.trace("No switches controlled"); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 112 | LinkedList<IFlowEntry> addFlowEntries = |
| 113 | new LinkedList<IFlowEntry>(); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 114 | LinkedList<IFlowEntry> deleteFlowEntries = |
| 115 | new LinkedList<IFlowEntry>(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 116 | |
| 117 | // |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 118 | // Fetch all Flow Entries which need to be updated and select |
| 119 | // only my Flow Entries that need to be updated into the |
| 120 | // switches. |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 121 | // |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 122 | Iterable<IFlowEntry> allFlowEntries = |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 123 | dbHandlerInner.getAllSwitchNotUpdatedFlowEntries(); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 124 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 125 | counterAllFlowEntries++; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 126 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 127 | String dpidStr = flowEntryObj.getSwitchDpid(); |
| 128 | if (dpidStr == null) |
| 129 | continue; |
| 130 | Dpid dpid = new Dpid(dpidStr); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 131 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 132 | if (mySwitch == null) |
| 133 | continue; // Ignore the entry: not my switch |
| 134 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 135 | IFlowPath flowObj = |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 136 | dbHandlerInner.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 137 | if (flowObj == null) |
| 138 | continue; // Should NOT happen |
| 139 | if (flowObj.getFlowId() == null) |
| 140 | continue; // Invalid entry |
| 141 | |
| 142 | // |
| 143 | // NOTE: For now we process the DELETE before the ADD |
| 144 | // to cover the more common scenario. |
| 145 | // TODO: This is error prone and needs to be fixed! |
| 146 | // |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 147 | String userState = flowEntryObj.getUserState(); |
| 148 | if (userState == null) |
| 149 | continue; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 150 | if (userState.equals("FE_USER_DELETE")) { |
| 151 | // An entry that needs to be deleted. |
| 152 | deleteFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 153 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
| 154 | } else { |
| 155 | addFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 156 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 157 | counterMyNotUpdatedFlowEntries++; |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 160 | // |
| 161 | // Process the Flow Entries that need to be added |
| 162 | // |
| 163 | for (IFlowEntry flowEntryObj : addFlowEntries) { |
| 164 | IFlowPath flowObj = |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 165 | dbHandlerInner.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 166 | if (flowObj == null) |
| 167 | continue; // Should NOT happen |
| 168 | if (flowObj.getFlowId() == null) |
| 169 | continue; // Invalid entry |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 170 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 171 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 172 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 173 | if (mySwitch == null) |
| 174 | continue; // Shouldn't happen |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 175 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 176 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 177 | |
| 178 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 179 | // Delete all Flow Entries marked for deletion from the |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 180 | // Network MAP. |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 181 | // |
| 182 | // TODO: We should use the OpenFlow Barrier mechanism |
| 183 | // to check for errors, and delete the Flow Entries after the |
| 184 | // Barrier message is received. |
| 185 | // |
| 186 | while (! deleteFlowEntries.isEmpty()) { |
| 187 | IFlowEntry flowEntryObj = deleteFlowEntries.poll(); |
| 188 | IFlowPath flowObj = |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 189 | dbHandlerInner.getFlowPathByFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 190 | if (flowObj == null) { |
| 191 | log.debug("Did not find FlowPath to be deleted"); |
| 192 | continue; |
| 193 | } |
| 194 | flowObj.removeFlowEntry(flowEntryObj); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 195 | dbHandlerInner.removeFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 196 | } |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 197 | |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 198 | dbHandlerInner.commit(); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 199 | |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 200 | long estimatedTime = System.nanoTime() - startTime; |
| 201 | double rate = 0.0; |
| 202 | if (estimatedTime > 0) |
| 203 | rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime; |
| 204 | String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + |
| 205 | counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + |
| 206 | counterMyNotUpdatedFlowEntries + " in " + |
| 207 | (double)estimatedTime / 1000000000 + " sec: " + |
| 208 | rate + " paths/s"; |
| 209 | log.debug(logMsg); |
| 210 | } |
| 211 | }; |
| 212 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Periodic task for reading the Flow Paths and recomputing the |
| 215 | * shortest paths. |
| 216 | */ |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 217 | final Runnable shortestPathReconcile = new Runnable() { |
| 218 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 219 | try { |
| 220 | runImpl(); |
| 221 | } catch (Exception e) { |
| 222 | log.debug("Exception processing All Flows from the Network MAP: ", e); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 223 | dbHandlerInner.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | private void runImpl() { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 229 | long startTime = System.nanoTime(); |
| 230 | int counterAllFlowPaths = 0; |
| 231 | int counterMyFlowPaths = 0; |
| 232 | |
| 233 | if (floodlightProvider == null) { |
| 234 | log.debug("FloodlightProvider service not found!"); |
| 235 | return; |
| 236 | } |
| 237 | Map<Long, IOFSwitch> mySwitches = |
| 238 | floodlightProvider.getSwitches(); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 239 | if (mySwitches.isEmpty()) { |
Pankaj Berde | 85a877b | 2013-10-03 18:26:35 -0700 | [diff] [blame] | 240 | log.trace("No switches controlled"); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 243 | LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>(); |
| 244 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 245 | // |
| 246 | // Fetch and recompute the Shortest Path for those |
| 247 | // Flow Paths this controller is responsible for. |
| 248 | // |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 249 | Topology topology = topologyNetService.newDatabaseTopology(); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 250 | Iterable<IFlowPath> allFlowPaths = dbHandlerInner.getAllFlowPaths(); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 251 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 252 | counterAllFlowPaths++; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 253 | if (flowPathObj == null) |
| 254 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 255 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 256 | String srcDpidStr = flowPathObj.getSrcSwitch(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 257 | if (srcDpidStr == null) |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 258 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 259 | Dpid srcDpid = new Dpid(srcDpidStr); |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 260 | // |
| 261 | // Use the source DPID as a heuristic to decide |
| 262 | // which controller is responsible for maintaining the |
| 263 | // shortest path. |
| 264 | // NOTE: This heuristic is error-prone: if the switch |
| 265 | // goes away and no controller is responsible for that |
| 266 | // switch, then the original Flow Path is not cleaned-up |
| 267 | // |
| 268 | IOFSwitch mySwitch = mySwitches.get(srcDpid.value()); |
| 269 | if (mySwitch == null) |
| 270 | continue; // Ignore: not my responsibility |
| 271 | |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 272 | // Test whether we need to maintain this flow |
| 273 | String flowPathTypeStr = flowPathObj.getFlowPathType(); |
| 274 | if (flowPathTypeStr == null) |
Pavlin Radoslavov | 99d1b15 | 2013-04-09 22:57:33 -0700 | [diff] [blame] | 275 | continue; // Could be invalid entry? |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 276 | if (! flowPathTypeStr.equals("FP_TYPE_SHORTEST_PATH")) |
Pavlin Radoslavov | 99d1b15 | 2013-04-09 22:57:33 -0700 | [diff] [blame] | 277 | continue; // No need to maintain this flow |
| 278 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 279 | // |
| 280 | // Test whether we need to complete the Flow cleanup, |
| 281 | // if the Flow has been deleted by the user. |
| 282 | // |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 283 | String flowPathUserStateStr = flowPathObj.getFlowPathUserState(); |
| 284 | if ((flowPathUserStateStr != null) |
| 285 | && flowPathUserStateStr.equals("FP_USER_DELETE")) { |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 286 | Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries(); |
Yuta HIGUCHI | 2ded2dd | 2013-10-09 18:06:41 -0700 | [diff] [blame] | 287 | final boolean empty = !flowEntries.iterator().hasNext(); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 288 | if (empty) |
| 289 | deleteFlows.add(flowPathObj); |
| 290 | } |
| 291 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 292 | // Fetch the fields needed to recompute the shortest path |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 293 | String dataPathSummaryStr = flowPathObj.getDataPathSummary(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 294 | Short srcPortShort = flowPathObj.getSrcPort(); |
| 295 | String dstDpidStr = flowPathObj.getDstSwitch(); |
| 296 | Short dstPortShort = flowPathObj.getDstPort(); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 297 | Long flowPathFlagsLong = flowPathObj.getFlowPathFlags(); |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 298 | if ((dataPathSummaryStr == null) || |
| 299 | (srcPortShort == null) || |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 300 | (dstDpidStr == null) || |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 301 | (dstPortShort == null) || |
| 302 | (flowPathFlagsLong == null)) { |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 303 | continue; |
| 304 | } |
| 305 | |
| 306 | Port srcPort = new Port(srcPortShort); |
| 307 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 308 | Port dstPort = new Port(dstPortShort); |
| 309 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 310 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 311 | FlowPathType flowPathType = FlowPathType.valueOf(flowPathTypeStr); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 312 | FlowPathUserState flowPathUserState = FlowPathUserState.valueOf(flowPathUserStateStr); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 313 | FlowPathFlags flowPathFlags = new FlowPathFlags(flowPathFlagsLong); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 314 | |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 315 | counterMyFlowPaths++; |
| 316 | |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 317 | // |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 318 | // NOTE: Using here the regular getDatabaseShortestPath() |
| 319 | // method won't work here, because that method calls |
| 320 | // internally "conn.endTx(Transaction.COMMIT)", and that |
| 321 | // will invalidate all handlers to the Titan database. |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 322 | // If we want to experiment with calling here |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 323 | // getDatabaseShortestPath(), we need to refactor that code |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 324 | // to avoid closing the transaction. |
| 325 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 326 | DataPath dataPath = |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 327 | topologyNetService.getTopologyShortestPath( |
| 328 | topology, |
| 329 | srcSwitchPort, |
| 330 | dstSwitchPort); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 331 | if (dataPath == null) { |
| 332 | // We need the DataPath to compare the paths |
| 333 | dataPath = new DataPath(); |
| 334 | dataPath.setSrcPort(srcSwitchPort); |
| 335 | dataPath.setDstPort(dstSwitchPort); |
| 336 | } |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 337 | dataPath.applyFlowPathFlags(flowPathFlags); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 338 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 339 | String newDataPathSummaryStr = dataPath.dataPathSummary(); |
| 340 | if (dataPathSummaryStr.equals(newDataPathSummaryStr)) |
| 341 | continue; // Nothing changed |
| 342 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 343 | reconcileFlow(flowPathObj, dataPath); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 344 | } |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 345 | |
| 346 | // |
| 347 | // Delete all leftover Flows marked for deletion from the |
| 348 | // Network MAP. |
| 349 | // |
| 350 | while (! deleteFlows.isEmpty()) { |
| 351 | IFlowPath flowPathObj = deleteFlows.poll(); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 352 | dbHandlerInner.removeFlowPath(flowPathObj); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 355 | topologyNetService.dropTopology(topology); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 356 | |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 357 | dbHandlerInner.commit(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 358 | |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 359 | long estimatedTime = System.nanoTime() - startTime; |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 360 | double rate = 0.0; |
| 361 | if (estimatedTime > 0) |
| 362 | rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 363 | String logMsg = "MEASUREMENT: Processed AllFlowPaths: " + |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 364 | counterAllFlowPaths + " MyFlowPaths: " + |
| 365 | counterMyFlowPaths + " in " + |
| 366 | (double)estimatedTime / 1000000000 + " sec: " + |
| 367 | rate + " paths/s"; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 368 | log.debug(logMsg); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 369 | } |
| 370 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 371 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 372 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Initialize the Flow Manager. |
| 375 | * |
| 376 | * @param conf the Graph Database configuration string. |
| 377 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 378 | @Override |
| 379 | public void init(String conf) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 380 | dbHandlerApi = new GraphDBOperation(conf); |
| 381 | dbHandlerInner = new GraphDBOperation(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 382 | } |
| 383 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 384 | /** |
| 385 | * Shutdown the Flow Manager operation. |
| 386 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 387 | public void finalize() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 388 | close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 389 | } |
| 390 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 391 | /** |
| 392 | * Shutdown the Flow Manager operation. |
| 393 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 394 | @Override |
| 395 | public void close() { |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 396 | datagridService.deregisterFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 397 | dbHandlerApi.close(); |
| 398 | dbHandlerInner.close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 399 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 400 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 401 | /** |
| 402 | * Get the collection of offered module services. |
| 403 | * |
| 404 | * @return the collection of offered module services. |
| 405 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 406 | @Override |
| 407 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 408 | Collection<Class<? extends IFloodlightService>> l = |
| 409 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 410 | l.add(IFlowService.class); |
| 411 | return l; |
| 412 | } |
| 413 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 414 | /** |
| 415 | * Get the collection of implemented services. |
| 416 | * |
| 417 | * @return the collection of implemented services. |
| 418 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 419 | @Override |
| 420 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 421 | getServiceImpls() { |
| 422 | Map<Class<? extends IFloodlightService>, |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 423 | IFloodlightService> m = |
| 424 | new HashMap<Class<? extends IFloodlightService>, |
| 425 | IFloodlightService>(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 426 | m.put(IFlowService.class, this); |
| 427 | return m; |
| 428 | } |
| 429 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 430 | /** |
| 431 | * Get the collection of modules this module depends on. |
| 432 | * |
| 433 | * @return the collection of modules this module depends on. |
| 434 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 435 | @Override |
| 436 | public Collection<Class<? extends IFloodlightService>> |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 437 | getModuleDependencies() { |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 438 | Collection<Class<? extends IFloodlightService>> l = |
| 439 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 440 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 441 | l.add(INetworkGraphService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 442 | l.add(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 443 | l.add(IRestApiService.class); |
| 444 | return l; |
| 445 | } |
| 446 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 447 | /** |
| 448 | * Initialize the module. |
| 449 | * |
| 450 | * @param context the module context to use for the initialization. |
| 451 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 452 | @Override |
| 453 | public void init(FloodlightModuleContext context) |
| 454 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 455 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 456 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 457 | topologyNetService = context.getServiceImpl(ITopologyNetService.class); |
| 458 | datagridService = context.getServiceImpl(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 459 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 460 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 461 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 462 | EnumSet.of(OFType.FLOW_MOD), |
| 463 | OFMESSAGE_DAMPER_TIMEOUT); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 464 | |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 465 | this.init(""); |
| 466 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 467 | mapReaderScheduler = Executors.newScheduledThreadPool(1); |
| 468 | shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 469 | } |
| 470 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 471 | /** |
| 472 | * Get the next Flow Entry ID to use. |
| 473 | * |
| 474 | * @return the next Flow Entry ID to use. |
| 475 | */ |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 476 | public synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 477 | // |
| 478 | // Generate the next Flow Entry ID. |
| 479 | // NOTE: For now, the higher 32 bits are random, and |
| 480 | // the lower 32 bits are sequential. |
| 481 | // In the future, we need a better allocation mechanism. |
| 482 | // |
| 483 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 484 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 485 | nextFlowEntryIdSuffix = 0; |
| 486 | } else { |
| 487 | nextFlowEntryIdSuffix++; |
| 488 | } |
| 489 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 490 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 491 | return result; |
| 492 | } |
| 493 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 494 | /** |
| 495 | * Startup module operation. |
| 496 | * |
| 497 | * @param context the module context to use for the startup. |
| 498 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 499 | @Override |
| 500 | public void startUp(FloodlightModuleContext context) { |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 501 | restApi.addRestletRoutable(new FlowWebRoutable()); |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 502 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 503 | // Initialize the Flow Entry ID generator |
| 504 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 505 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 506 | // |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 507 | // Create the Flow Event Handler thread and register it with the |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 508 | // Datagrid Service |
| 509 | // |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 510 | flowEventHandler = new FlowEventHandler(this, datagridService); |
| 511 | datagridService.registerFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 512 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 513 | // Schedule the threads and periodic tasks |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 514 | flowEventHandler.start(); |
Pavlin Radoslavov | e0e48f7 | 2013-11-07 11:22:43 -0800 | [diff] [blame^] | 515 | if (! enableNotifications) { |
| 516 | mapReaderScheduler.scheduleAtFixedRate( |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 517 | mapReader, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | e0e48f7 | 2013-11-07 11:22:43 -0800 | [diff] [blame^] | 518 | shortestPathReconcileScheduler.scheduleAtFixedRate( |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 519 | shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | e0e48f7 | 2013-11-07 11:22:43 -0800 | [diff] [blame^] | 520 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Add a flow. |
| 525 | * |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 526 | * @param flowPath the Flow Path to install. |
| 527 | * @param flowId the return-by-reference Flow ID as assigned internally. |
| 528 | * @return true on success, otherwise false. |
| 529 | */ |
| 530 | @Override |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 531 | public boolean addFlow(FlowPath flowPath, FlowId flowId) { |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 532 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 533 | // NOTE: We need to explicitly initialize some of the state, |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 534 | // in case the application didn't do it. |
| 535 | // |
| 536 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 537 | if (flowEntry.flowEntrySwitchState() == |
| 538 | FlowEntrySwitchState.FE_SWITCH_UNKNOWN) { |
| 539 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 540 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 541 | if (! flowEntry.isValidFlowId()) |
| 542 | flowEntry.setFlowId(new FlowId(flowPath.flowId().value())); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 545 | if (FlowDatabaseOperation.addFlow(this, dbHandlerApi, flowPath, flowId)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 546 | datagridService.notificationSendFlowAdded(flowPath); |
| 547 | return true; |
| 548 | } |
| 549 | return false; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 553 | * Add a flow entry to the Network MAP. |
| 554 | * |
| 555 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 556 | * @param flowEntry the Flow Entry to install. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 557 | * @return the added Flow Entry object on success, otherwise null. |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 558 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 559 | private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 560 | return FlowDatabaseOperation.addFlowEntry(this, dbHandlerInner, |
| 561 | flowObj, flowEntry); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /** |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 565 | * Delete a flow entry from the Network MAP. |
| 566 | * |
| 567 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 568 | * @param flowEntry the Flow Entry to delete. |
| 569 | * @return true on success, otherwise false. |
| 570 | */ |
| 571 | private boolean deleteFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 572 | return FlowDatabaseOperation.deleteFlowEntry(dbHandlerInner, |
| 573 | flowObj, flowEntry); |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 577 | * Delete all previously added flows. |
| 578 | * |
| 579 | * @return true on success, otherwise false. |
| 580 | */ |
| 581 | @Override |
| 582 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 583 | if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 584 | datagridService.notificationSendAllFlowsRemoved(); |
| 585 | return true; |
| 586 | } |
| 587 | return false; |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 591 | * Delete a previously added flow. |
| 592 | * |
| 593 | * @param flowId the Flow ID of the flow to delete. |
| 594 | * @return true on success, otherwise false. |
| 595 | */ |
| 596 | @Override |
| 597 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 598 | if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 599 | datagridService.notificationSendFlowRemoved(flowId); |
| 600 | return true; |
| 601 | } |
| 602 | return false; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 606 | * Clear the state for all previously added flows. |
| 607 | * |
| 608 | * @return true on success, otherwise false. |
| 609 | */ |
| 610 | @Override |
| 611 | public boolean clearAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 612 | if (FlowDatabaseOperation.clearAllFlows(dbHandlerApi)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 613 | datagridService.notificationSendAllFlowsRemoved(); |
| 614 | return true; |
| 615 | } |
| 616 | return false; |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 620 | * Clear the state for a previously added flow. |
| 621 | * |
| 622 | * @param flowId the Flow ID of the flow to clear. |
| 623 | * @return true on success, otherwise false. |
| 624 | */ |
| 625 | @Override |
| 626 | public boolean clearFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 627 | if (FlowDatabaseOperation.clearFlow(dbHandlerApi, flowId)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 628 | datagridService.notificationSendFlowRemoved(flowId); |
| 629 | return true; |
| 630 | } |
| 631 | return false; |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 635 | * Get a previously added flow. |
| 636 | * |
| 637 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 638 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 639 | */ |
| 640 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 641 | public FlowPath getFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 642 | return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 643 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 644 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 645 | /** |
| 646 | * Get all installed flows by all installers. |
| 647 | * |
| 648 | * @return the Flow Paths if found, otherwise null. |
| 649 | */ |
| 650 | @Override |
| 651 | public ArrayList<FlowPath> getAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 652 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Get all previously added flows by a specific installer for a given |
| 657 | * data path endpoints. |
| 658 | * |
| 659 | * @param installerId the Caller ID of the installer of the flow to get. |
| 660 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 661 | * @return the Flow Paths if found, otherwise null. |
| 662 | */ |
| 663 | @Override |
| 664 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 665 | DataPathEndpoints dataPathEndpoints) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 666 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi, installerId, |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 667 | dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Get all installed flows by all installers for given data path endpoints. |
| 672 | * |
| 673 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 674 | * @return the Flow Paths if found, otherwise null. |
| 675 | */ |
| 676 | @Override |
| 677 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 678 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi, |
| 679 | dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 683 | * 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] | 684 | * |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 685 | * @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] | 686 | * @param maxFlows the maximum number of flows to be returned. |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 687 | * @return the Flow Paths if found, otherwise null. |
| 688 | */ |
| 689 | @Override |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 690 | public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, |
| 691 | int maxFlows) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 692 | return FlowDatabaseOperation.getAllFlowsSummary(dbHandlerApi, flowId, |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 693 | maxFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /** |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 697 | * Get all Flows information, without the associated Flow Entries. |
| 698 | * |
| 699 | * @return all Flows information, without the associated Flow Entries. |
| 700 | */ |
| 701 | public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 702 | return FlowDatabaseOperation.getAllFlowsWithoutFlowEntries(dbHandlerApi); |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 706 | * Add and maintain a shortest-path flow. |
| 707 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 708 | * NOTE: The Flow Path argument does NOT contain flow entries. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 709 | * |
| 710 | * @param flowPath the Flow Path with the endpoints and the match |
| 711 | * conditions to install. |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 712 | * @return the added shortest-path flow on success, otherwise null. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 713 | */ |
| 714 | @Override |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 715 | public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 716 | // |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 717 | // Don't do the shortest path computation here. |
| 718 | // Instead, let the Flow reconciliation thread take care of it. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 719 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 720 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 721 | FlowId flowId = new FlowId(); |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 722 | if (! addFlow(flowPath, flowId)) |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 723 | return null; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 724 | |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 725 | return (flowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 729 | * Get the collection of my switches. |
| 730 | * |
| 731 | * @return the collection of my switches. |
| 732 | */ |
| 733 | public Map<Long, IOFSwitch> getMySwitches() { |
| 734 | return floodlightProvider.getSwitches(); |
| 735 | } |
| 736 | |
| 737 | /** |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 738 | * Get the network topology. |
| 739 | * |
| 740 | * @return the network topology. |
| 741 | */ |
| 742 | public Topology getTopology() { |
| 743 | return flowEventHandler.getTopology(); |
| 744 | } |
| 745 | |
| 746 | /** |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 747 | * Reconcile a flow. |
| 748 | * |
| 749 | * @param flowObj the flow that needs to be reconciliated. |
| 750 | * @param newDataPath the new data path to use. |
| 751 | * @return true on success, otherwise false. |
| 752 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 753 | private boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 754 | String flowIdStr = flowObj.getFlowId(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 755 | |
| 756 | // |
| 757 | // Set the incoming port matching and the outgoing port output |
| 758 | // actions for each flow entry. |
| 759 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 760 | int idx = 0; |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 761 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 762 | flowEntry.setFlowId(new FlowId(flowIdStr)); |
| 763 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 764 | // Mark the Flow Entry as not updated in the switch |
| 765 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 766 | // Set the incoming port matching |
| 767 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 768 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 769 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 770 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 771 | // |
| 772 | // Set the actions |
| 773 | // |
| 774 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 775 | // |
| 776 | // If the first Flow Entry, copy the Flow Path actions to it |
| 777 | // |
| 778 | if (idx == 0) { |
| 779 | String actionsStr = flowObj.getActions(); |
| 780 | if (actionsStr != null) { |
| 781 | FlowEntryActions flowActions = new FlowEntryActions(actionsStr); |
| 782 | for (FlowEntryAction action : flowActions.actions()) |
| 783 | flowEntryActions.addAction(action); |
| 784 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 785 | } |
Pavlin Radoslavov | 282f4ff | 2013-07-18 11:21:37 -0700 | [diff] [blame] | 786 | idx++; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 787 | // |
| 788 | // Add the outgoing port output action |
| 789 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 790 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 791 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 792 | flowEntryActions.addAction(flowEntryAction); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | // |
| 796 | // Remove the old Flow Entries, and add the new Flow Entries |
| 797 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 798 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 799 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 800 | flowEntryObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 801 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 802 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 803 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 804 | addFlowEntry(flowObj, flowEntry); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // |
| 808 | // Set the Data Path Summary |
| 809 | // |
| 810 | String dataPathSummaryStr = newDataPath.dataPathSummary(); |
| 811 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 812 | |
| 813 | return true; |
| 814 | } |
| 815 | |
| 816 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 817 | * Reconcile all flows in a set. |
| 818 | * |
| 819 | * @param flowObjSet the set of flows that need to be reconciliated. |
| 820 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 821 | private void reconcileFlows(Iterable<IFlowPath> flowObjSet) { |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 822 | if (! flowObjSet.iterator().hasNext()) |
| 823 | return; |
Pavlin Radoslavov | 0eeb15d | 2013-04-05 10:23:51 -0700 | [diff] [blame] | 824 | // TODO: Not implemented/used yet. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 828 | * Install a Flow Entry on a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 829 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 830 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 831 | * @param flowObj the flow path object for the flow entry to install. |
| 832 | * @param flowEntryObj the flow entry object to install. |
| 833 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 834 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 835 | private boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj, |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 836 | IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 837 | return FlowSwitchOperation.installFlowEntry( |
| 838 | floodlightProvider.getOFMessageFactory(), |
| 839 | messageDamper, mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Install a Flow Entry on a switch. |
| 844 | * |
| 845 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 846 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 847 | * @param flowEntry the flow entry to install. |
| 848 | * @return true on success, otherwise false. |
| 849 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 850 | private boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 851 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 852 | return FlowSwitchOperation.installFlowEntry( |
| 853 | floodlightProvider.getOFMessageFactory(), |
| 854 | messageDamper, mySwitch, flowPath, flowEntry); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Remove a Flow Entry from a switch. |
| 859 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 860 | * @param mySwitch the switch to remove the Flow Entry from. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 861 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 862 | * @param flowEntry the flow entry to remove. |
| 863 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 864 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 865 | private boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 866 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 867 | // |
| 868 | // The installFlowEntry() method implements both installation |
| 869 | // and removal of flow entries. |
| 870 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 871 | return (installFlowEntry(mySwitch, flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 872 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 873 | |
| 874 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 875 | * Push modified Flow Entries to switches. |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 876 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 877 | * NOTE: Only the Flow Entries to switches controlled by this instance |
| 878 | * are pushed. |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 879 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 880 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 881 | */ |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 882 | public void pushModifiedFlowEntriesToSwitches( |
| 883 | Collection<FlowPathEntryPair> modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 884 | // TODO: For now, the pushing of Flow Entries is disabled |
Pavlin Radoslavov | e0e48f7 | 2013-11-07 11:22:43 -0800 | [diff] [blame^] | 885 | if (! enableNotifications) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 886 | return; |
| 887 | |
| 888 | if (modifiedFlowEntries.isEmpty()) |
| 889 | return; |
| 890 | |
| 891 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 892 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 893 | for (FlowPathEntryPair flowPair : modifiedFlowEntries) { |
| 894 | FlowPath flowPath = flowPair.flowPath; |
| 895 | FlowEntry flowEntry = flowPair.flowEntry; |
| 896 | |
| 897 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 898 | if (mySwitch == null) |
| 899 | continue; |
| 900 | |
| 901 | log.debug("Pushing Flow Entry To Switch: {}", flowEntry.toString()); |
| 902 | |
| 903 | // |
| 904 | // Install the Flow Entry into the switch |
| 905 | // |
| 906 | if (! installFlowEntry(mySwitch, flowPath, flowEntry)) { |
| 907 | String logMsg = "Cannot install Flow Entry " + |
| 908 | flowEntry.flowEntryId() + |
| 909 | " from Flow Path " + flowPath.flowId() + |
| 910 | " on switch " + flowEntry.dpid(); |
| 911 | log.error(logMsg); |
| 912 | continue; |
| 913 | } |
| 914 | |
| 915 | // |
| 916 | // NOTE: Here we assume that the switch has been |
| 917 | // successfully updated. |
| 918 | // |
| 919 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Push modified Flow Entries to the datagrid. |
| 925 | * |
| 926 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 927 | */ |
| 928 | public void pushModifiedFlowEntriesToDatagrid( |
| 929 | Collection<FlowPathEntryPair> modifiedFlowEntries) { |
| 930 | // TODO: For now, the pushing of Flow Entries is disabled |
Pavlin Radoslavov | e0e48f7 | 2013-11-07 11:22:43 -0800 | [diff] [blame^] | 931 | if (! enableNotifications) |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 932 | return; |
| 933 | |
| 934 | if (modifiedFlowEntries.isEmpty()) |
| 935 | return; |
| 936 | |
| 937 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 938 | |
| 939 | for (FlowPathEntryPair flowPair : modifiedFlowEntries) { |
| 940 | FlowEntry flowEntry = flowPair.flowEntry; |
| 941 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 942 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 943 | |
| 944 | // |
| 945 | // TODO: For now Flow Entries are removed by all instances, |
| 946 | // even if this Flow Entry is not for our switches. |
| 947 | // |
| 948 | // This is needed to handle the case a switch going down: |
| 949 | // it has no Master controller instance, hence no |
| 950 | // controller instance will cleanup its flow entries. |
| 951 | // This is sub-optimal: we need to elect a controller |
| 952 | // instance to handle the cleanup of such orphaned flow |
| 953 | // entries. |
| 954 | // |
| 955 | if (mySwitch == null) { |
| 956 | if (flowEntry.flowEntryUserState() != |
| 957 | FlowEntryUserState.FE_USER_DELETE) { |
| 958 | continue; |
| 959 | } |
| 960 | if (! flowEntry.isValidFlowEntryId()) |
| 961 | continue; |
| 962 | } |
| 963 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 964 | log.debug("Pushing Flow Entry To Datagrid: {}", flowEntry.toString()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 965 | // |
| 966 | // Write the Flow Entry to the Datagrid |
| 967 | // |
| 968 | switch (flowEntry.flowEntryUserState()) { |
| 969 | case FE_USER_ADD: |
| 970 | if (mySwitch == null) |
| 971 | break; // Install only flow entries for my switches |
| 972 | datagridService.notificationSendFlowEntryAdded(flowEntry); |
| 973 | break; |
| 974 | case FE_USER_MODIFY: |
| 975 | if (mySwitch == null) |
| 976 | break; // Install only flow entries for my switches |
| 977 | datagridService.notificationSendFlowEntryUpdated(flowEntry); |
| 978 | break; |
| 979 | case FE_USER_DELETE: |
| 980 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
| 981 | break; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Push Flow Entries to the Network MAP. |
| 988 | * |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 989 | * NOTE: The Flow Entries are pushed only on the instance responsible |
| 990 | * for the first switch. This is to avoid database errors when multiple |
| 991 | * instances are writing Flow Entries for the same Flow Path. |
| 992 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 993 | * @param modifiedFlowEntries the collection of Flow Entries to push. |
| 994 | */ |
| 995 | public void pushModifiedFlowEntriesToDatabase( |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 996 | Collection<FlowPathEntryPair> modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 997 | // TODO: For now, the pushing of Flow Entries is disabled |
Pavlin Radoslavov | e0e48f7 | 2013-11-07 11:22:43 -0800 | [diff] [blame^] | 998 | if (! enableNotifications) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 999 | return; |
| 1000 | |
| 1001 | if (modifiedFlowEntries.isEmpty()) |
| 1002 | return; |
| 1003 | |
| 1004 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 1005 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1006 | for (FlowPathEntryPair flowPair : modifiedFlowEntries) { |
| 1007 | FlowPath flowPath = flowPair.flowPath; |
| 1008 | FlowEntry flowEntry = flowPair.flowEntry; |
| 1009 | |
| 1010 | if (! flowEntry.isValidFlowEntryId()) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1011 | continue; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1012 | |
| 1013 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1014 | // Push the changes only on the instance responsible for the |
| 1015 | // first switch. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1016 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1017 | Dpid srcDpid = flowPath.dataPath().srcPort().dpid(); |
| 1018 | IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value()); |
| 1019 | if (mySrcSwitch == null) |
| 1020 | continue; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1021 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1022 | log.debug("Pushing Flow Entry To Database: {}", flowEntry.toString()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1023 | // |
| 1024 | // Write the Flow Entry to the Network Map |
| 1025 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1026 | // NOTE: We try a number of times, in case somehow some other |
| 1027 | // instances are writing at the same time. |
| 1028 | // Apparently, if other instances are writing at the same time |
| 1029 | // this will trigger an error. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1030 | // |
| 1031 | for (int i = 0; i < 6; i++) { |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 1032 | try { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1033 | // |
| 1034 | // Find the Flow Path in the Network MAP. |
| 1035 | // |
| 1036 | // NOTE: The Flow Path might not be found if the Flow was |
| 1037 | // just removed by some other controller instance. |
| 1038 | // |
| 1039 | IFlowPath flowObj = |
| 1040 | dbHandlerInner.searchFlowPath(flowEntry.flowId()); |
| 1041 | if (flowObj == null) { |
| 1042 | String logMsg = "Cannot find Network MAP entry for Flow Path " + flowEntry.flowId(); |
| 1043 | log.error(logMsg); |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | // Write the Flow Entry |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 1048 | switch (flowEntry.flowEntryUserState()) { |
| 1049 | case FE_USER_ADD: |
| 1050 | // FALLTHROUGH |
| 1051 | case FE_USER_MODIFY: |
| 1052 | if (addFlowEntry(flowObj, flowEntry) == null) { |
| 1053 | String logMsg = "Cannot write to Network MAP Flow Entry " + |
| 1054 | flowEntry.flowEntryId() + |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1055 | " from Flow Path " + flowEntry.flowId() + |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 1056 | " on switch " + flowEntry.dpid(); |
| 1057 | log.error(logMsg); |
| 1058 | } |
| 1059 | break; |
| 1060 | case FE_USER_DELETE: |
| 1061 | if (deleteFlowEntry(flowObj, flowEntry) == false) { |
| 1062 | String logMsg = "Cannot remove from Network MAP Flow Entry " + |
| 1063 | flowEntry.flowEntryId() + |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1064 | " from Flow Path " + flowEntry.flowId() + |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 1065 | " on switch " + flowEntry.dpid(); |
| 1066 | log.error(logMsg); |
| 1067 | } |
| 1068 | break; |
| 1069 | } |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1070 | |
| 1071 | // Commit to the database |
| 1072 | dbHandlerInner.commit(); |
| 1073 | break; // Success |
| 1074 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 1075 | } catch (Exception e) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 1076 | log.debug("Exception writing Flow Entry to Network MAP: ", e); |
| 1077 | dbHandlerInner.rollback(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 1078 | // Wait a bit (random value [1ms, 20ms] and try again |
| 1079 | int delay = 1 + randomGenerator.nextInt() % 20; |
| 1080 | try { |
| 1081 | Thread.sleep(delay); |
| 1082 | } catch (Exception e0) { |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 1083 | } |
| 1084 | } |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 1085 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 1086 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 1087 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1088 | } |