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