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