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 | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.util.*; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 33 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 34 | import org.openflow.protocol.OFType; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 38 | /** |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 39 | * Flow Manager class for handling the network flows. |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 40 | */ |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 41 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 42 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 43 | protected GraphDBOperation dbHandler; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 44 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 45 | protected volatile IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | 1278ac7 | 2013-10-16 04:43:49 -0700 | [diff] [blame] | 46 | protected volatile ITopologyNetService topologyNetService; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 47 | protected volatile IDatagridService datagridService; |
| 48 | protected IRestApiService restApi; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 49 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 50 | |
| 51 | protected OFMessageDamper messageDamper; |
| 52 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 53 | // |
| 54 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 55 | // The local copy should go away! |
| 56 | // |
| 57 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 58 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 59 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 60 | // Flow Entry ID generation state |
| 61 | private static Random randomGenerator = new Random(); |
| 62 | private static int nextFlowEntryIdPrefix = 0; |
| 63 | private static int nextFlowEntryIdSuffix = 0; |
| 64 | private static long nextFlowEntryId = 0; |
| 65 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 66 | /** The logger. */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 67 | private static Logger log = LoggerFactory.getLogger(FlowManager.class); |
| 68 | |
| 69 | // The periodic task(s) |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 70 | private ScheduledExecutorService mapReaderScheduler; |
| 71 | private ScheduledExecutorService shortestPathReconcileScheduler; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 72 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Periodic task for reading the Flow Entries and pushing changes |
| 75 | * into the switches. |
| 76 | */ |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 77 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 78 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 79 | try { |
| 80 | runImpl(); |
| 81 | } catch (Exception e) { |
| 82 | log.debug("Exception processing All Flow Entries from the Network MAP: ", e); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 83 | dbHandler.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private void runImpl() { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 89 | long startTime = System.nanoTime(); |
| 90 | int counterAllFlowEntries = 0; |
| 91 | int counterMyNotUpdatedFlowEntries = 0; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 92 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 93 | if (floodlightProvider == null) { |
| 94 | log.debug("FloodlightProvider service not found!"); |
| 95 | return; |
| 96 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 97 | Map<Long, IOFSwitch> mySwitches = |
| 98 | floodlightProvider.getSwitches(); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 99 | if (mySwitches.isEmpty()) { |
Pankaj Berde | 85a877b | 2013-10-03 18:26:35 -0700 | [diff] [blame] | 100 | log.trace("No switches controlled"); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 101 | return; |
| 102 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 103 | LinkedList<IFlowEntry> addFlowEntries = |
| 104 | new LinkedList<IFlowEntry>(); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 105 | LinkedList<IFlowEntry> deleteFlowEntries = |
| 106 | new LinkedList<IFlowEntry>(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 107 | |
| 108 | // |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 109 | // Fetch all Flow Entries which need to be updated and select |
| 110 | // only my Flow Entries that need to be updated into the |
| 111 | // switches. |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 112 | // |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 113 | Iterable<IFlowEntry> allFlowEntries = |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 114 | dbHandler.getAllSwitchNotUpdatedFlowEntries(); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 115 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 116 | counterAllFlowEntries++; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 117 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 118 | String dpidStr = flowEntryObj.getSwitchDpid(); |
| 119 | if (dpidStr == null) |
| 120 | continue; |
| 121 | Dpid dpid = new Dpid(dpidStr); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 122 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 123 | if (mySwitch == null) |
| 124 | continue; // Ignore the entry: not my switch |
| 125 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 126 | IFlowPath flowObj = |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 127 | dbHandler.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 128 | if (flowObj == null) |
| 129 | continue; // Should NOT happen |
| 130 | if (flowObj.getFlowId() == null) |
| 131 | continue; // Invalid entry |
| 132 | |
| 133 | // |
| 134 | // NOTE: For now we process the DELETE before the ADD |
| 135 | // to cover the more common scenario. |
| 136 | // TODO: This is error prone and needs to be fixed! |
| 137 | // |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 138 | String userState = flowEntryObj.getUserState(); |
| 139 | if (userState == null) |
| 140 | continue; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 141 | if (userState.equals("FE_USER_DELETE")) { |
| 142 | // An entry that needs to be deleted. |
| 143 | deleteFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 144 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
| 145 | } else { |
| 146 | addFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 147 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 148 | counterMyNotUpdatedFlowEntries++; |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 151 | // |
| 152 | // Process the Flow Entries that need to be added |
| 153 | // |
| 154 | for (IFlowEntry flowEntryObj : addFlowEntries) { |
| 155 | IFlowPath flowObj = |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 156 | dbHandler.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 157 | if (flowObj == null) |
| 158 | continue; // Should NOT happen |
| 159 | if (flowObj.getFlowId() == null) |
| 160 | continue; // Invalid entry |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 161 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 162 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 163 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 164 | if (mySwitch == null) |
| 165 | continue; // Shouldn't happen |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 166 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 167 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 168 | |
| 169 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 170 | // Delete all Flow Entries marked for deletion from the |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 171 | // Network MAP. |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 172 | // |
| 173 | // TODO: We should use the OpenFlow Barrier mechanism |
| 174 | // to check for errors, and delete the Flow Entries after the |
| 175 | // Barrier message is received. |
| 176 | // |
| 177 | while (! deleteFlowEntries.isEmpty()) { |
| 178 | IFlowEntry flowEntryObj = deleteFlowEntries.poll(); |
| 179 | IFlowPath flowObj = |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 180 | dbHandler.getFlowPathByFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 181 | if (flowObj == null) { |
| 182 | log.debug("Did not find FlowPath to be deleted"); |
| 183 | continue; |
| 184 | } |
| 185 | flowObj.removeFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 186 | dbHandler.removeFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 187 | } |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 188 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 189 | dbHandler.commit(); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 190 | |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 191 | long estimatedTime = System.nanoTime() - startTime; |
| 192 | double rate = 0.0; |
| 193 | if (estimatedTime > 0) |
| 194 | rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime; |
| 195 | String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + |
| 196 | counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + |
| 197 | counterMyNotUpdatedFlowEntries + " in " + |
| 198 | (double)estimatedTime / 1000000000 + " sec: " + |
| 199 | rate + " paths/s"; |
| 200 | log.debug(logMsg); |
| 201 | } |
| 202 | }; |
| 203 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Periodic task for reading the Flow Paths and recomputing the |
| 206 | * shortest paths. |
| 207 | */ |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 208 | final Runnable shortestPathReconcile = new Runnable() { |
| 209 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 210 | try { |
| 211 | runImpl(); |
| 212 | } catch (Exception e) { |
| 213 | log.debug("Exception processing All Flows from the Network MAP: ", e); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 214 | dbHandler.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | private void runImpl() { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 220 | long startTime = System.nanoTime(); |
| 221 | int counterAllFlowPaths = 0; |
| 222 | int counterMyFlowPaths = 0; |
| 223 | |
| 224 | if (floodlightProvider == null) { |
| 225 | log.debug("FloodlightProvider service not found!"); |
| 226 | return; |
| 227 | } |
| 228 | Map<Long, IOFSwitch> mySwitches = |
| 229 | floodlightProvider.getSwitches(); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 230 | if (mySwitches.isEmpty()) { |
Pankaj Berde | 85a877b | 2013-10-03 18:26:35 -0700 | [diff] [blame] | 231 | log.trace("No switches controlled"); |
Pankaj Berde | bf51e4f | 2013-10-03 17:49:23 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 234 | LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>(); |
| 235 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 236 | // |
| 237 | // Fetch and recompute the Shortest Path for those |
| 238 | // Flow Paths this controller is responsible for. |
| 239 | // |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 240 | Topology topology = topologyNetService.newDatabaseTopology(); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 241 | Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths(); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 242 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 243 | counterAllFlowPaths++; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 244 | if (flowPathObj == null) |
| 245 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 246 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 247 | String srcDpidStr = flowPathObj.getSrcSwitch(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 248 | if (srcDpidStr == null) |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 249 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 250 | Dpid srcDpid = new Dpid(srcDpidStr); |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 251 | // |
| 252 | // Use the source DPID as a heuristic to decide |
| 253 | // which controller is responsible for maintaining the |
| 254 | // shortest path. |
| 255 | // NOTE: This heuristic is error-prone: if the switch |
| 256 | // goes away and no controller is responsible for that |
| 257 | // switch, then the original Flow Path is not cleaned-up |
| 258 | // |
| 259 | IOFSwitch mySwitch = mySwitches.get(srcDpid.value()); |
| 260 | if (mySwitch == null) |
| 261 | continue; // Ignore: not my responsibility |
| 262 | |
Pavlin Radoslavov | 99d1b15 | 2013-04-09 22:57:33 -0700 | [diff] [blame] | 263 | // Test the Data Path Summary string |
| 264 | String dataPathSummaryStr = flowPathObj.getDataPathSummary(); |
| 265 | if (dataPathSummaryStr == null) |
| 266 | continue; // Could be invalid entry? |
| 267 | if (dataPathSummaryStr.isEmpty()) |
| 268 | continue; // No need to maintain this flow |
| 269 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 270 | // |
| 271 | // Test whether we need to complete the Flow cleanup, |
| 272 | // if the Flow has been deleted by the user. |
| 273 | // |
| 274 | String flowUserState = flowPathObj.getUserState(); |
| 275 | if ((flowUserState != null) |
| 276 | && flowUserState.equals("FE_USER_DELETE")) { |
| 277 | Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries(); |
Yuta HIGUCHI | 2ded2dd | 2013-10-09 18:06:41 -0700 | [diff] [blame] | 278 | final boolean empty = !flowEntries.iterator().hasNext(); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 279 | if (empty) |
| 280 | deleteFlows.add(flowPathObj); |
| 281 | } |
| 282 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 283 | // Fetch the fields needed to recompute the shortest path |
| 284 | Short srcPortShort = flowPathObj.getSrcPort(); |
| 285 | String dstDpidStr = flowPathObj.getDstSwitch(); |
| 286 | Short dstPortShort = flowPathObj.getDstPort(); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 287 | Long flowPathFlagsLong = flowPathObj.getFlowPathFlags(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 288 | if ((srcPortShort == null) || |
| 289 | (dstDpidStr == null) || |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 290 | (dstPortShort == null) || |
| 291 | (flowPathFlagsLong == null)) { |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 292 | continue; |
| 293 | } |
| 294 | |
| 295 | Port srcPort = new Port(srcPortShort); |
| 296 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 297 | Port dstPort = new Port(dstPortShort); |
| 298 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 299 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 300 | FlowPathFlags flowPathFlags = new FlowPathFlags(flowPathFlagsLong); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 301 | |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 302 | counterMyFlowPaths++; |
| 303 | |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 304 | // |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 305 | // NOTE: Using here the regular getDatabaseShortestPath() |
| 306 | // method won't work here, because that method calls |
| 307 | // internally "conn.endTx(Transaction.COMMIT)", and that |
| 308 | // will invalidate all handlers to the Titan database. |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 309 | // If we want to experiment with calling here |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 310 | // getDatabaseShortestPath(), we need to refactor that code |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 311 | // to avoid closing the transaction. |
| 312 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 313 | DataPath dataPath = |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 314 | topologyNetService.getTopologyShortestPath( |
| 315 | topology, |
| 316 | srcSwitchPort, |
| 317 | dstSwitchPort); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 318 | if (dataPath == null) { |
| 319 | // We need the DataPath to compare the paths |
| 320 | dataPath = new DataPath(); |
| 321 | dataPath.setSrcPort(srcSwitchPort); |
| 322 | dataPath.setDstPort(dstSwitchPort); |
| 323 | } |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 324 | dataPath.applyFlowPathFlags(flowPathFlags); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 325 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 326 | String newDataPathSummaryStr = dataPath.dataPathSummary(); |
| 327 | if (dataPathSummaryStr.equals(newDataPathSummaryStr)) |
| 328 | continue; // Nothing changed |
| 329 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 330 | reconcileFlow(flowPathObj, dataPath); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 331 | } |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 332 | |
| 333 | // |
| 334 | // Delete all leftover Flows marked for deletion from the |
| 335 | // Network MAP. |
| 336 | // |
| 337 | while (! deleteFlows.isEmpty()) { |
| 338 | IFlowPath flowPathObj = deleteFlows.poll(); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 339 | dbHandler.removeFlowPath(flowPathObj); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 342 | topologyNetService.dropTopology(topology); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 343 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 344 | dbHandler.commit(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 345 | |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 346 | long estimatedTime = System.nanoTime() - startTime; |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 347 | double rate = 0.0; |
| 348 | if (estimatedTime > 0) |
| 349 | rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 350 | String logMsg = "MEASUREMENT: Processed AllFlowPaths: " + |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 351 | counterAllFlowPaths + " MyFlowPaths: " + |
| 352 | counterMyFlowPaths + " in " + |
| 353 | (double)estimatedTime / 1000000000 + " sec: " + |
| 354 | rate + " paths/s"; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 355 | log.debug(logMsg); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 356 | } |
| 357 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 358 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 359 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 360 | /** |
| 361 | * Initialize the Flow Manager. |
| 362 | * |
| 363 | * @param conf the Graph Database configuration string. |
| 364 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 365 | @Override |
| 366 | public void init(String conf) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 367 | dbHandler = new GraphDBOperation(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 368 | } |
| 369 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 370 | /** |
| 371 | * Shutdown the Flow Manager operation. |
| 372 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 373 | public void finalize() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 374 | close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 375 | } |
| 376 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 377 | /** |
| 378 | * Shutdown the Flow Manager operation. |
| 379 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 380 | @Override |
| 381 | public void close() { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 382 | dbHandler.close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 383 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 384 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 385 | /** |
| 386 | * Get the collection of offered module services. |
| 387 | * |
| 388 | * @return the collection of offered module services. |
| 389 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 390 | @Override |
| 391 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 392 | Collection<Class<? extends IFloodlightService>> l = |
| 393 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 394 | l.add(IFlowService.class); |
| 395 | return l; |
| 396 | } |
| 397 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 398 | /** |
| 399 | * Get the collection of implemented services. |
| 400 | * |
| 401 | * @return the collection of implemented services. |
| 402 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 403 | @Override |
| 404 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 405 | getServiceImpls() { |
| 406 | Map<Class<? extends IFloodlightService>, |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 407 | IFloodlightService> m = |
| 408 | new HashMap<Class<? extends IFloodlightService>, |
| 409 | IFloodlightService>(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 410 | m.put(IFlowService.class, this); |
| 411 | return m; |
| 412 | } |
| 413 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 414 | /** |
| 415 | * Get the collection of modules this module depends on. |
| 416 | * |
| 417 | * @return the collection of modules this module depends on. |
| 418 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 419 | @Override |
| 420 | public Collection<Class<? extends IFloodlightService>> |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 421 | getModuleDependencies() { |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 422 | Collection<Class<? extends IFloodlightService>> l = |
| 423 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 424 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 425 | l.add(INetworkGraphService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 426 | l.add(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 427 | l.add(IRestApiService.class); |
| 428 | return l; |
| 429 | } |
| 430 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 431 | /** |
| 432 | * Initialize the module. |
| 433 | * |
| 434 | * @param context the module context to use for the initialization. |
| 435 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 436 | @Override |
| 437 | public void init(FloodlightModuleContext context) |
| 438 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 439 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 440 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 441 | topologyNetService = context.getServiceImpl(ITopologyNetService.class); |
| 442 | datagridService = context.getServiceImpl(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 443 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 444 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 445 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 446 | EnumSet.of(OFType.FLOW_MOD), |
| 447 | OFMESSAGE_DAMPER_TIMEOUT); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 448 | |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 449 | this.init(""); |
| 450 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 451 | mapReaderScheduler = Executors.newScheduledThreadPool(1); |
| 452 | shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 453 | } |
| 454 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 455 | /** |
| 456 | * Get the next Flow Entry ID to use. |
| 457 | * |
| 458 | * @return the next Flow Entry ID to use. |
| 459 | */ |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 460 | public synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 461 | // |
| 462 | // Generate the next Flow Entry ID. |
| 463 | // NOTE: For now, the higher 32 bits are random, and |
| 464 | // the lower 32 bits are sequential. |
| 465 | // In the future, we need a better allocation mechanism. |
| 466 | // |
| 467 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 468 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 469 | nextFlowEntryIdSuffix = 0; |
| 470 | } else { |
| 471 | nextFlowEntryIdSuffix++; |
| 472 | } |
| 473 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 474 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 475 | return result; |
| 476 | } |
| 477 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Startup module operation. |
| 480 | * |
| 481 | * @param context the module context to use for the startup. |
| 482 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 483 | @Override |
| 484 | public void startUp(FloodlightModuleContext context) { |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 485 | restApi.addRestletRoutable(new FlowWebRoutable()); |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 486 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 487 | // Initialize the Flow Entry ID generator |
| 488 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 489 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 490 | mapReaderScheduler.scheduleAtFixedRate( |
| 491 | mapReader, 3, 3, TimeUnit.SECONDS); |
| 492 | shortestPathReconcileScheduler.scheduleAtFixedRate( |
| 493 | shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Add a flow. |
| 498 | * |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 499 | * @param flowPath the Flow Path to install. |
| 500 | * @param flowId the return-by-reference Flow ID as assigned internally. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 501 | * @param dataPathSummaryStr the data path summary string if the added |
| 502 | * flow will be maintained internally, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 503 | * @return true on success, otherwise false. |
| 504 | */ |
| 505 | @Override |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 506 | public boolean addFlow(FlowPath flowPath, FlowId flowId, |
| 507 | String dataPathSummaryStr) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 508 | return FlowDatabaseOperation.addFlow(this, dbHandler, flowPath, flowId, |
| 509 | dataPathSummaryStr); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 513 | * Add a flow entry to the Network MAP. |
| 514 | * |
| 515 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 516 | * @param flowEntry the Flow Entry to install. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 517 | * @return the added Flow Entry object on success, otherwise null. |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 518 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 519 | private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 520 | return FlowDatabaseOperation.addFlowEntry(this, dbHandler, flowObj, |
| 521 | flowEntry); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 525 | * Delete all previously added flows. |
| 526 | * |
| 527 | * @return true on success, otherwise false. |
| 528 | */ |
| 529 | @Override |
| 530 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 531 | return FlowDatabaseOperation.deleteAllFlows(dbHandler); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 535 | * Delete a previously added flow. |
| 536 | * |
| 537 | * @param flowId the Flow ID of the flow to delete. |
| 538 | * @return true on success, otherwise false. |
| 539 | */ |
| 540 | @Override |
| 541 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 542 | return FlowDatabaseOperation.deleteFlow(dbHandler, flowId); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 546 | * Clear the state for all previously added flows. |
| 547 | * |
| 548 | * @return true on success, otherwise false. |
| 549 | */ |
| 550 | @Override |
| 551 | public boolean clearAllFlows() { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 552 | return FlowDatabaseOperation.clearAllFlows(dbHandler); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 556 | * Clear the state for a previously added flow. |
| 557 | * |
| 558 | * @param flowId the Flow ID of the flow to clear. |
| 559 | * @return true on success, otherwise false. |
| 560 | */ |
| 561 | @Override |
| 562 | public boolean clearFlow(FlowId flowId) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 563 | return FlowDatabaseOperation.clearFlow(dbHandler, flowId); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 567 | * Get a previously added flow. |
| 568 | * |
| 569 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 570 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 571 | */ |
| 572 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 573 | public FlowPath getFlow(FlowId flowId) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 574 | return FlowDatabaseOperation.getFlow(dbHandler, flowId); |
| 575 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 576 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 577 | /** |
| 578 | * Get all installed flows by all installers. |
| 579 | * |
| 580 | * @return the Flow Paths if found, otherwise null. |
| 581 | */ |
| 582 | @Override |
| 583 | public ArrayList<FlowPath> getAllFlows() { |
| 584 | return FlowDatabaseOperation.getAllFlows(dbHandler); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Get all previously added flows by a specific installer for a given |
| 589 | * data path endpoints. |
| 590 | * |
| 591 | * @param installerId the Caller ID of the installer of the flow to get. |
| 592 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 593 | * @return the Flow Paths if found, otherwise null. |
| 594 | */ |
| 595 | @Override |
| 596 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 597 | DataPathEndpoints dataPathEndpoints) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 598 | return FlowDatabaseOperation.getAllFlows(dbHandler, installerId, |
| 599 | dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Get all installed flows by all installers for given data path endpoints. |
| 604 | * |
| 605 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 606 | * @return the Flow Paths if found, otherwise null. |
| 607 | */ |
| 608 | @Override |
| 609 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 610 | return FlowDatabaseOperation.getAllFlows(dbHandler, dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 614 | * 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] | 615 | * |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 616 | * @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] | 617 | * @param maxFlows the maximum number of flows to be returned. |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 618 | * @return the Flow Paths if found, otherwise null. |
| 619 | */ |
| 620 | @Override |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 621 | public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, |
| 622 | int maxFlows) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 623 | return FlowDatabaseOperation.getAllFlowsSummary(dbHandler, flowId, |
| 624 | maxFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 628 | * Get all Flows information, without the associated Flow Entries. |
| 629 | * |
| 630 | * @return all Flows information, without the associated Flow Entries. |
| 631 | */ |
| 632 | public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries() { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 633 | return FlowDatabaseOperation.getAllFlowsWithoutFlowEntries(dbHandler); |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 637 | * Add and maintain a shortest-path flow. |
| 638 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 639 | * NOTE: The Flow Path argument does NOT contain flow entries. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 640 | * |
| 641 | * @param flowPath the Flow Path with the endpoints and the match |
| 642 | * conditions to install. |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 643 | * @return the added shortest-path flow on success, otherwise null. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 644 | */ |
| 645 | @Override |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 646 | public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 647 | // |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 648 | // Don't do the shortest path computation here. |
| 649 | // Instead, let the Flow reconciliation thread take care of it. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 650 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 651 | |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 652 | // We need the DataPath to populate the Network MAP |
| 653 | DataPath dataPath = new DataPath(); |
| 654 | dataPath.setSrcPort(flowPath.dataPath().srcPort()); |
| 655 | dataPath.setDstPort(flowPath.dataPath().dstPort()); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 656 | |
| 657 | // |
| 658 | // Prepare the computed Flow Path |
| 659 | // |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 660 | FlowPath computedFlowPath = new FlowPath(); |
| 661 | computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value())); |
| 662 | computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value())); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 663 | computedFlowPath.setFlowPathFlags(new FlowPathFlags(flowPath.flowPathFlags().flags())); |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 664 | computedFlowPath.setDataPath(dataPath); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 665 | computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch())); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 666 | computedFlowPath.setFlowEntryActions(new FlowEntryActions(flowPath.flowEntryActions())); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 667 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 668 | FlowId flowId = new FlowId(); |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 669 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 670 | if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr)) |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 671 | return null; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 672 | |
| 673 | // TODO: Mark the flow for maintenance purpose |
| 674 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 675 | return (computedFlowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /** |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 679 | * Reconcile a flow. |
| 680 | * |
| 681 | * @param flowObj the flow that needs to be reconciliated. |
| 682 | * @param newDataPath the new data path to use. |
| 683 | * @return true on success, otherwise false. |
| 684 | */ |
| 685 | public boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 686 | |
| 687 | // |
| 688 | // Set the incoming port matching and the outgoing port output |
| 689 | // actions for each flow entry. |
| 690 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 691 | int idx = 0; |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 692 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
| 693 | // Set the incoming port matching |
| 694 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 695 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 696 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 697 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 698 | // |
| 699 | // Set the actions |
| 700 | // |
| 701 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 702 | // |
| 703 | // If the first Flow Entry, copy the Flow Path actions to it |
| 704 | // |
| 705 | if (idx == 0) { |
| 706 | String actionsStr = flowObj.getActions(); |
| 707 | if (actionsStr != null) { |
| 708 | FlowEntryActions flowActions = new FlowEntryActions(actionsStr); |
| 709 | for (FlowEntryAction action : flowActions.actions()) |
| 710 | flowEntryActions.addAction(action); |
| 711 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 712 | } |
Pavlin Radoslavov | 282f4ff | 2013-07-18 11:21:37 -0700 | [diff] [blame] | 713 | idx++; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 714 | // |
| 715 | // Add the outgoing port output action |
| 716 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 717 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 718 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 719 | flowEntryActions.addAction(flowEntryAction); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | // |
| 723 | // Remove the old Flow Entries, and add the new Flow Entries |
| 724 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 725 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 726 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 727 | flowEntryObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 728 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 729 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 730 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 731 | addFlowEntry(flowObj, flowEntry); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // |
| 735 | // Set the Data Path Summary |
| 736 | // |
| 737 | String dataPathSummaryStr = newDataPath.dataPathSummary(); |
| 738 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 739 | |
| 740 | return true; |
| 741 | } |
| 742 | |
| 743 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 744 | * Reconcile all flows in a set. |
| 745 | * |
| 746 | * @param flowObjSet the set of flows that need to be reconciliated. |
| 747 | */ |
| 748 | public void reconcileFlows(Iterable<IFlowPath> flowObjSet) { |
| 749 | if (! flowObjSet.iterator().hasNext()) |
| 750 | return; |
Pavlin Radoslavov | 0eeb15d | 2013-04-05 10:23:51 -0700 | [diff] [blame] | 751 | // TODO: Not implemented/used yet. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 755 | * Install a Flow Entry on a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 756 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 757 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 758 | * @param flowObj the flow path object for the flow entry to install. |
| 759 | * @param flowEntryObj the flow entry object to install. |
| 760 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 761 | */ |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 762 | public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj, |
| 763 | IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 764 | return FlowSwitchOperation.installFlowEntry( |
| 765 | floodlightProvider.getOFMessageFactory(), |
| 766 | messageDamper, mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Install a Flow Entry on a switch. |
| 771 | * |
| 772 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 773 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 774 | * @param flowEntry the flow entry to install. |
| 775 | * @return true on success, otherwise false. |
| 776 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 777 | public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 778 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 779 | return FlowSwitchOperation.installFlowEntry( |
| 780 | floodlightProvider.getOFMessageFactory(), |
| 781 | messageDamper, mySwitch, flowPath, flowEntry); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Remove a Flow Entry from a switch. |
| 786 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 787 | * @param mySwitch the switch to remove the Flow Entry from. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 788 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 789 | * @param flowEntry the flow entry to remove. |
| 790 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 791 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 792 | public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 793 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 794 | // |
| 795 | // The installFlowEntry() method implements both installation |
| 796 | // and removal of flow entries. |
| 797 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 798 | return (installFlowEntry(mySwitch, flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 799 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 800 | } |