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 | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 721 | * Get the network topology. |
| 722 | * |
| 723 | * @return the network topology. |
| 724 | */ |
| 725 | public Topology getTopology() { |
| 726 | return flowEventHandler.getTopology(); |
| 727 | } |
| 728 | |
| 729 | /** |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 730 | * Reconcile a flow. |
| 731 | * |
| 732 | * @param flowObj the flow that needs to be reconciliated. |
| 733 | * @param newDataPath the new data path to use. |
| 734 | * @return true on success, otherwise false. |
| 735 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 736 | private boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 737 | String flowIdStr = flowObj.getFlowId(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 738 | |
| 739 | // |
| 740 | // Set the incoming port matching and the outgoing port output |
| 741 | // actions for each flow entry. |
| 742 | // |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 743 | int idx = 0; |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 744 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 745 | flowEntry.setFlowId(new FlowId(flowIdStr)); |
| 746 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 747 | // Mark the Flow Entry as not updated in the switch |
| 748 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 749 | // Set the incoming port matching |
| 750 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 751 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 752 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 753 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 754 | // |
| 755 | // Set the actions |
| 756 | // |
| 757 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 758 | // |
| 759 | // If the first Flow Entry, copy the Flow Path actions to it |
| 760 | // |
| 761 | if (idx == 0) { |
| 762 | String actionsStr = flowObj.getActions(); |
| 763 | if (actionsStr != null) { |
| 764 | FlowEntryActions flowActions = new FlowEntryActions(actionsStr); |
| 765 | for (FlowEntryAction action : flowActions.actions()) |
| 766 | flowEntryActions.addAction(action); |
| 767 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 768 | } |
Pavlin Radoslavov | 282f4ff | 2013-07-18 11:21:37 -0700 | [diff] [blame] | 769 | idx++; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 770 | // |
| 771 | // Add the outgoing port output action |
| 772 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 773 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 774 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 775 | flowEntryActions.addAction(flowEntryAction); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | // |
| 779 | // Remove the old Flow Entries, and add the new Flow Entries |
| 780 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 781 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 782 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 783 | flowEntryObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 784 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 785 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 786 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 787 | addFlowEntry(flowObj, flowEntry); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // |
| 791 | // Set the Data Path Summary |
| 792 | // |
| 793 | String dataPathSummaryStr = newDataPath.dataPathSummary(); |
| 794 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 795 | |
| 796 | return true; |
| 797 | } |
| 798 | |
| 799 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 800 | * Reconcile all flows in a set. |
| 801 | * |
| 802 | * @param flowObjSet the set of flows that need to be reconciliated. |
| 803 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 804 | private void reconcileFlows(Iterable<IFlowPath> flowObjSet) { |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 805 | if (! flowObjSet.iterator().hasNext()) |
| 806 | return; |
Pavlin Radoslavov | 0eeb15d | 2013-04-05 10:23:51 -0700 | [diff] [blame] | 807 | // TODO: Not implemented/used yet. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 811 | * Install a Flow Entry on a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 812 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 813 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 814 | * @param flowObj the flow path object for the flow entry to install. |
| 815 | * @param flowEntryObj the flow entry object to install. |
| 816 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 817 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 818 | private boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj, |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 819 | IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 820 | return FlowSwitchOperation.installFlowEntry( |
| 821 | floodlightProvider.getOFMessageFactory(), |
| 822 | messageDamper, mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Install a Flow Entry on a switch. |
| 827 | * |
| 828 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 829 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 830 | * @param flowEntry the flow entry to install. |
| 831 | * @return true on success, otherwise false. |
| 832 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 833 | private boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 834 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 835 | return FlowSwitchOperation.installFlowEntry( |
| 836 | floodlightProvider.getOFMessageFactory(), |
| 837 | messageDamper, mySwitch, flowPath, flowEntry); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Remove a Flow Entry from a switch. |
| 842 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 843 | * @param mySwitch the switch to remove the Flow Entry from. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 844 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 845 | * @param flowEntry the flow entry to remove. |
| 846 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 847 | */ |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 848 | private boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 849 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 850 | // |
| 851 | // The installFlowEntry() method implements both installation |
| 852 | // and removal of flow entries. |
| 853 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 854 | return (installFlowEntry(mySwitch, flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 855 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 856 | |
| 857 | /** |
| 858 | * Push the modified Flow Entries of a collection of Flow Paths. |
| 859 | * Only the Flow Entries to switches controlled by this instance |
| 860 | * are pushed. |
| 861 | * |
| 862 | * NOTE: Currently, we write to both the Network MAP and the switches. |
| 863 | * |
| 864 | * @param modifiedFlowPaths the collection of Flow Paths with the modified |
| 865 | * Flow Entries. |
| 866 | */ |
| 867 | public void pushModifiedFlowEntries(Collection<FlowPath> modifiedFlowPaths) { |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 868 | // TODO: For now, the pushing of Flow Entries is disabled |
| 869 | if (true) |
| 870 | return; |
| 871 | |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 872 | if (modifiedFlowPaths.isEmpty()) |
| 873 | return; |
| 874 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 875 | Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches(); |
| 876 | |
| 877 | for (FlowPath flowPath : modifiedFlowPaths) { |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 878 | // |
| 879 | // Find the Flow Path in the Network MAP. |
| 880 | // NOTE: The Flow Path might not be found if the Flow was just |
| 881 | // removed by some other controller instance. |
| 882 | // |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 883 | IFlowPath flowObj = dbHandlerInner.searchFlowPath(flowPath.flowId()); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 884 | |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 885 | boolean isFlowEntryDeleted = false; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 886 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 887 | log.debug("Updating Flow Entry: {}", flowEntry.toString()); |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 888 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 889 | if (flowEntry.flowEntrySwitchState() != |
| 890 | FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) { |
| 891 | continue; // No need to update the entry |
| 892 | } |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 893 | if (flowEntry.flowEntryUserState() == |
| 894 | FlowEntryUserState.FE_USER_DELETE) { |
| 895 | isFlowEntryDeleted = true; |
| 896 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 897 | |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 898 | // |
| 899 | // Install the Flow Entries into my switches |
| 900 | // |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 901 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 902 | if (mySwitch != null) { |
| 903 | // |
| 904 | // Assign the FlowEntry ID if needed |
| 905 | // |
| 906 | if (! flowEntry.isValidFlowEntryId()) { |
| 907 | long id = getNextFlowEntryId(); |
| 908 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 909 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 910 | |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 911 | // |
| 912 | // Install the Flow Entry into the switch |
| 913 | // |
| 914 | if (! installFlowEntry(mySwitch, flowPath, flowEntry)) { |
| 915 | String logMsg = "Cannot install Flow Entry " + |
| 916 | flowEntry.flowEntryId() + |
| 917 | " from Flow Path " + flowPath.flowId() + |
| 918 | " on switch " + flowEntry.dpid(); |
| 919 | log.error(logMsg); |
| 920 | continue; |
| 921 | } |
| 922 | |
| 923 | // |
| 924 | // NOTE: Here we assume that the switch has been |
| 925 | // successfully updated. |
| 926 | // |
| 927 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | // |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 931 | // TODO: For now Flow Entries are removed from the Datagrid |
| 932 | // and from the Network Map by all instances, even if this |
| 933 | // Flow Entry is not for our switches. |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 934 | // |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 935 | // This is needed to handle the case a switch going down: |
| 936 | // it has no Master controller instance, hence no |
| 937 | // controller instance will cleanup its flow entries. |
| 938 | // This is sub-optimal: we need to elect a controller |
| 939 | // instance to handle the cleanup of such orphaned flow |
| 940 | // entries. |
| 941 | // |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 942 | if (mySwitch == null) { |
| 943 | if (flowEntry.flowEntryUserState() != |
| 944 | FlowEntryUserState.FE_USER_DELETE) { |
| 945 | continue; |
| 946 | } |
| 947 | if (! flowEntry.isValidFlowEntryId()) |
| 948 | continue; |
| 949 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 950 | |
| 951 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 952 | // Write the Flow Entry to the Datagrid |
| 953 | // |
| 954 | switch (flowEntry.flowEntryUserState()) { |
| 955 | case FE_USER_ADD: |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 956 | if (mySwitch == null) |
| 957 | break; // Install only flow entries for my switches |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 958 | datagridService.notificationSendFlowEntryAdded(flowEntry); |
| 959 | break; |
| 960 | case FE_USER_MODIFY: |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 961 | if (mySwitch == null) |
| 962 | break; // Install only flow entries for my switches |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 963 | datagridService.notificationSendFlowEntryUpdated(flowEntry); |
| 964 | break; |
| 965 | case FE_USER_DELETE: |
| 966 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
| 967 | break; |
| 968 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 969 | |
| 970 | // |
| 971 | // Write the Flow Entry to the Network Map |
| 972 | // |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 973 | if (flowObj == null) { |
| 974 | String logMsg = "Cannot find Network MAP entry for Flow Path " + flowPath.flowId(); |
| 975 | continue; |
| 976 | } |
| 977 | try { |
| 978 | switch (flowEntry.flowEntryUserState()) { |
| 979 | case FE_USER_ADD: |
| 980 | // FALLTHROUGH |
| 981 | case FE_USER_MODIFY: |
| 982 | if (addFlowEntry(flowObj, flowEntry) == null) { |
| 983 | String logMsg = "Cannot write to Network MAP Flow Entry " + |
| 984 | flowEntry.flowEntryId() + |
| 985 | " from Flow Path " + flowPath.flowId() + |
| 986 | " on switch " + flowEntry.dpid(); |
| 987 | log.error(logMsg); |
| 988 | } |
| 989 | break; |
| 990 | case FE_USER_DELETE: |
| 991 | if (deleteFlowEntry(flowObj, flowEntry) == false) { |
| 992 | String logMsg = "Cannot remove from Network MAP Flow Entry " + |
| 993 | flowEntry.flowEntryId() + |
| 994 | " from Flow Path " + flowPath.flowId() + |
| 995 | " on switch " + flowEntry.dpid(); |
| 996 | log.error(logMsg); |
| 997 | } |
| 998 | break; |
| 999 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 1000 | } catch (Exception e) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 1001 | log.debug("Exception writing Flow Entry to Network MAP: ", e); |
| 1002 | dbHandlerInner.rollback(); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 1003 | continue; |
| 1004 | } |
| 1005 | } |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 1006 | |
| 1007 | // |
| 1008 | // Remove Flow Entries that were deleted |
| 1009 | // |
| 1010 | // NOTE: We create a new ArrayList, and add only the Flow Entries |
| 1011 | // that are NOT FE_USER_DELETE. |
| 1012 | // This is sub-optimal: if it adds notable processing cost, |
| 1013 | // the Flow Entries container should be changed to LinkedList |
| 1014 | // or some other container that has O(1) cost of removing an entry. |
| 1015 | // |
| 1016 | if (isFlowEntryDeleted) { |
| 1017 | ArrayList<FlowEntry> newFlowEntries = new ArrayList<FlowEntry>(); |
| 1018 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 1019 | if (flowEntry.flowEntryUserState() != |
| 1020 | FlowEntryUserState.FE_USER_DELETE) { |
| 1021 | newFlowEntries.add(flowEntry); |
| 1022 | } |
| 1023 | } |
| 1024 | flowPath.dataPath().setFlowEntries(newFlowEntries); |
| 1025 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 1026 | } |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 1027 | // Try to commit to the database |
Pavlin Radoslavov | ab18925 | 2013-11-05 08:50:23 -0800 | [diff] [blame] | 1028 | try { |
| 1029 | dbHandlerInner.commit(); |
| 1030 | } catch (Exception e) { |
| 1031 | log.debug("Exception during commit of Flow Entries to Network MAP", e); |
| 1032 | dbHandlerInner.rollback(); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 1033 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 1034 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1035 | } |