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