Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.flowcache; |
| 2 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 4 | import java.io.PrintWriter; |
| 5 | import java.io.StringWriter; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 8 | import java.util.Collections; |
| 9 | import java.util.Comparator; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 10 | import java.util.EnumSet; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 11 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 12 | import java.util.LinkedList; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 13 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 14 | import java.util.Map; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 15 | import java.util.Random; |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 16 | import java.util.concurrent.ConcurrentLinkedQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 17 | import java.util.concurrent.Executors; |
| 18 | import java.util.concurrent.ScheduledExecutorService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 19 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 20 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 22 | import net.floodlightcontroller.core.INetMapStorage; |
| 23 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 24 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 25 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
Pankaj Berde | d007974 | 2013-03-27 17:53:25 -0700 | [diff] [blame] | 26 | import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 27 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 28 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 29 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 30 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 31 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 32 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 33 | import net.floodlightcontroller.flowcache.web.FlowWebRoutable; |
| 34 | import net.floodlightcontroller.restserver.IRestApiService; |
| 35 | import net.floodlightcontroller.util.CallerId; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 36 | import net.floodlightcontroller.util.DataPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 37 | import net.floodlightcontroller.util.DataPathEndpoints; |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 38 | import net.floodlightcontroller.util.Dpid; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 39 | import net.floodlightcontroller.util.FlowEntry; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 40 | import net.floodlightcontroller.util.FlowEntryAction; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 41 | import net.floodlightcontroller.util.FlowEntryId; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 42 | import net.floodlightcontroller.util.FlowEntryMatch; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 43 | import net.floodlightcontroller.util.FlowEntrySwitchState; |
| 44 | import net.floodlightcontroller.util.FlowEntryUserState; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 45 | import net.floodlightcontroller.util.FlowId; |
| 46 | import net.floodlightcontroller.util.FlowPath; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 47 | import net.floodlightcontroller.util.IPv4Net; |
| 48 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 49 | import net.floodlightcontroller.util.OFMessageDamper; |
| 50 | import net.floodlightcontroller.util.Port; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 51 | import net.floodlightcontroller.util.SwitchPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 52 | import net.onrc.onos.util.GraphDBConnection; |
| 53 | import net.onrc.onos.util.GraphDBConnection.Transaction; |
| 54 | |
| 55 | import org.openflow.protocol.OFFlowMod; |
| 56 | import org.openflow.protocol.OFMatch; |
| 57 | import org.openflow.protocol.OFPacketOut; |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 58 | import org.openflow.protocol.OFPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 59 | import org.openflow.protocol.OFType; |
| 60 | import org.openflow.protocol.action.OFAction; |
| 61 | import org.openflow.protocol.action.OFActionOutput; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | import org.slf4j.LoggerFactory; |
| 64 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 65 | |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 66 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 67 | |
| 68 | public GraphDBConnection conn; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 69 | |
| 70 | protected IRestApiService restApi; |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 71 | protected volatile IFloodlightProviderService floodlightProvider; |
| 72 | protected volatile ITopoRouteService topoRouteService; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 73 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 74 | |
| 75 | protected OFMessageDamper messageDamper; |
| 76 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 77 | // |
| 78 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 79 | // The local copy should go away! |
| 80 | // |
| 81 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 82 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
| 83 | public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity |
| 84 | public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite |
| 85 | public static final short PRIORITY_DEFAULT = 100; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 86 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 87 | // Flow Entry ID generation state |
| 88 | private static Random randomGenerator = new Random(); |
| 89 | private static int nextFlowEntryIdPrefix = 0; |
| 90 | private static int nextFlowEntryIdSuffix = 0; |
| 91 | private static long nextFlowEntryId = 0; |
| 92 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 93 | // State for measurement purpose |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 94 | private static long measurementFlowId = 100000; |
| 95 | private static String measurementFlowIdStr = "0x186a0"; // 100000 |
| 96 | private long modifiedMeasurementFlowTime = 0; |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 97 | // |
| 98 | private LinkedList<FlowPath> measurementStoredPaths = new LinkedList<FlowPath>(); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 99 | private long measurementStartTimeProcessingPaths = 0; |
| 100 | private long measurementEndTimeProcessingPaths = 0; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 101 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 102 | /** The logger. */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 103 | private static Logger log = LoggerFactory.getLogger(FlowManager.class); |
| 104 | |
| 105 | // The periodic task(s) |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 106 | private ScheduledExecutorService mapReaderScheduler; |
| 107 | private ScheduledExecutorService shortestPathReconcileScheduler; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 108 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 109 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 110 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 111 | try { |
| 112 | runImpl(); |
| 113 | } catch (Exception e) { |
| 114 | log.debug("Exception processing All Flow Entries from the Network MAP: ", e); |
| 115 | conn.endTx(Transaction.ROLLBACK); |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private void runImpl() { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 121 | long startTime = System.nanoTime(); |
| 122 | int counterAllFlowEntries = 0; |
| 123 | int counterMyNotUpdatedFlowEntries = 0; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 124 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 125 | if (floodlightProvider == null) { |
| 126 | log.debug("FloodlightProvider service not found!"); |
| 127 | return; |
| 128 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 129 | Map<Long, IOFSwitch> mySwitches = |
| 130 | floodlightProvider.getSwitches(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 131 | LinkedList<IFlowEntry> addFlowEntries = |
| 132 | new LinkedList<IFlowEntry>(); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 133 | LinkedList<IFlowEntry> deleteFlowEntries = |
| 134 | new LinkedList<IFlowEntry>(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 135 | |
| 136 | // |
Pankaj Berde | a2e14a9 | 2013-04-15 11:59:15 -0700 | [diff] [blame] | 137 | // Fetch all Flow Entries which need to be updated and select only my Flow Entries |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 138 | // that need to be updated into the switches. |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 139 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 140 | boolean processed_measurement_flow = false; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 141 | Iterable<IFlowEntry> allFlowEntries = |
Pankaj Berde | d1c3859 | 2013-04-10 22:46:40 -0700 | [diff] [blame] | 142 | conn.utils().getAllSwitchNotUpdatedFlowEntries(conn); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 143 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 144 | counterAllFlowEntries++; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 145 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 146 | String dpidStr = flowEntryObj.getSwitchDpid(); |
| 147 | if (dpidStr == null) |
| 148 | continue; |
| 149 | Dpid dpid = new Dpid(dpidStr); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 150 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 151 | if (mySwitch == null) |
| 152 | continue; // Ignore the entry: not my switch |
| 153 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 154 | IFlowPath flowObj = |
| 155 | conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj); |
| 156 | if (flowObj == null) |
| 157 | continue; // Should NOT happen |
| 158 | if (flowObj.getFlowId() == null) |
| 159 | continue; // Invalid entry |
| 160 | |
| 161 | // |
| 162 | // NOTE: For now we process the DELETE before the ADD |
| 163 | // to cover the more common scenario. |
| 164 | // TODO: This is error prone and needs to be fixed! |
| 165 | // |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 166 | String userState = flowEntryObj.getUserState(); |
| 167 | if (userState == null) |
| 168 | continue; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 169 | if (userState.equals("FE_USER_DELETE")) { |
| 170 | // An entry that needs to be deleted. |
| 171 | deleteFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 172 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
| 173 | } else { |
| 174 | addFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 175 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 176 | counterMyNotUpdatedFlowEntries++; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 177 | // Code for measurement purpose |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 178 | // TODO: Commented-out for now |
| 179 | /* |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 180 | { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 181 | if (flowObj.getFlowId().equals(measurementFlowIdStr)) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 182 | processed_measurement_flow = true; |
| 183 | } |
| 184 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 185 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 188 | // |
| 189 | // Process the Flow Entries that need to be added |
| 190 | // |
| 191 | for (IFlowEntry flowEntryObj : addFlowEntries) { |
| 192 | IFlowPath flowObj = |
| 193 | conn.utils().getFlowPathByFlowEntry(conn, |
| 194 | flowEntryObj); |
| 195 | if (flowObj == null) |
| 196 | continue; // Should NOT happen |
| 197 | if (flowObj.getFlowId() == null) |
| 198 | continue; // Invalid entry |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 199 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 200 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 201 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 202 | if (mySwitch == null) |
| 203 | continue; // Shouldn't happen |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 204 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 205 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 206 | |
| 207 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 208 | // Delete all Flow Entries marked for deletion from the |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 209 | // Network MAP. |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 210 | // |
| 211 | // TODO: We should use the OpenFlow Barrier mechanism |
| 212 | // to check for errors, and delete the Flow Entries after the |
| 213 | // Barrier message is received. |
| 214 | // |
| 215 | while (! deleteFlowEntries.isEmpty()) { |
| 216 | IFlowEntry flowEntryObj = deleteFlowEntries.poll(); |
| 217 | IFlowPath flowObj = |
| 218 | conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj); |
| 219 | if (flowObj == null) { |
| 220 | log.debug("Did not find FlowPath to be deleted"); |
| 221 | continue; |
| 222 | } |
| 223 | flowObj.removeFlowEntry(flowEntryObj); |
| 224 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 225 | } |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 226 | |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 227 | conn.endTx(Transaction.COMMIT); |
| 228 | |
| 229 | if (processed_measurement_flow) { |
| 230 | long estimatedTime = |
| 231 | System.nanoTime() - modifiedMeasurementFlowTime; |
| 232 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 233 | (double)estimatedTime / 1000000000 + " sec"; |
| 234 | log.debug(logMsg); |
| 235 | } |
| 236 | |
| 237 | long estimatedTime = System.nanoTime() - startTime; |
| 238 | double rate = 0.0; |
| 239 | if (estimatedTime > 0) |
| 240 | rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime; |
| 241 | String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + |
| 242 | counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + |
| 243 | counterMyNotUpdatedFlowEntries + " in " + |
| 244 | (double)estimatedTime / 1000000000 + " sec: " + |
| 245 | rate + " paths/s"; |
| 246 | log.debug(logMsg); |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | final Runnable shortestPathReconcile = new Runnable() { |
| 251 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 252 | try { |
| 253 | runImpl(); |
| 254 | } catch (Exception e) { |
| 255 | log.debug("Exception processing All Flows from the Network MAP: ", e); |
| 256 | conn.endTx(Transaction.ROLLBACK); |
| 257 | return; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private void runImpl() { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 262 | long startTime = System.nanoTime(); |
| 263 | int counterAllFlowPaths = 0; |
| 264 | int counterMyFlowPaths = 0; |
| 265 | |
| 266 | if (floodlightProvider == null) { |
| 267 | log.debug("FloodlightProvider service not found!"); |
| 268 | return; |
| 269 | } |
| 270 | Map<Long, IOFSwitch> mySwitches = |
| 271 | floodlightProvider.getSwitches(); |
| 272 | LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>(); |
| 273 | |
| 274 | boolean processed_measurement_flow = false; |
| 275 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 276 | // |
| 277 | // Fetch and recompute the Shortest Path for those |
| 278 | // Flow Paths this controller is responsible for. |
| 279 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 280 | topoRouteService.prepareShortestPathTopo(); |
| 281 | Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 282 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 283 | counterAllFlowPaths++; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 284 | if (flowPathObj == null) |
| 285 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 286 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 287 | String srcDpidStr = flowPathObj.getSrcSwitch(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 288 | if (srcDpidStr == null) |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 289 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 290 | Dpid srcDpid = new Dpid(srcDpidStr); |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 291 | // |
| 292 | // Use the source DPID as a heuristic to decide |
| 293 | // which controller is responsible for maintaining the |
| 294 | // shortest path. |
| 295 | // NOTE: This heuristic is error-prone: if the switch |
| 296 | // goes away and no controller is responsible for that |
| 297 | // switch, then the original Flow Path is not cleaned-up |
| 298 | // |
| 299 | IOFSwitch mySwitch = mySwitches.get(srcDpid.value()); |
| 300 | if (mySwitch == null) |
| 301 | continue; // Ignore: not my responsibility |
| 302 | |
Pavlin Radoslavov | 99d1b15 | 2013-04-09 22:57:33 -0700 | [diff] [blame] | 303 | // Test the Data Path Summary string |
| 304 | String dataPathSummaryStr = flowPathObj.getDataPathSummary(); |
| 305 | if (dataPathSummaryStr == null) |
| 306 | continue; // Could be invalid entry? |
| 307 | if (dataPathSummaryStr.isEmpty()) |
| 308 | continue; // No need to maintain this flow |
| 309 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 310 | // |
| 311 | // Test whether we need to complete the Flow cleanup, |
| 312 | // if the Flow has been deleted by the user. |
| 313 | // |
| 314 | String flowUserState = flowPathObj.getUserState(); |
| 315 | if ((flowUserState != null) |
| 316 | && flowUserState.equals("FE_USER_DELETE")) { |
| 317 | Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries(); |
| 318 | boolean empty = true; // TODO: an ugly hack |
| 319 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 320 | empty = false; |
| 321 | break; |
| 322 | } |
| 323 | if (empty) |
| 324 | deleteFlows.add(flowPathObj); |
| 325 | } |
| 326 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 327 | // Fetch the fields needed to recompute the shortest path |
| 328 | Short srcPortShort = flowPathObj.getSrcPort(); |
| 329 | String dstDpidStr = flowPathObj.getDstSwitch(); |
| 330 | Short dstPortShort = flowPathObj.getDstPort(); |
| 331 | if ((srcPortShort == null) || |
| 332 | (dstDpidStr == null) || |
| 333 | (dstPortShort == null)) { |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | Port srcPort = new Port(srcPortShort); |
| 338 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 339 | Port dstPort = new Port(dstPortShort); |
| 340 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 341 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 342 | |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 343 | counterMyFlowPaths++; |
| 344 | |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 345 | // |
| 346 | // NOTE: Using here the regular getShortestPath() method |
| 347 | // won't work here, because that method calls internally |
| 348 | // "conn.endTx(Transaction.COMMIT)", and that will |
| 349 | // invalidate all handlers to the Titan database. |
| 350 | // If we want to experiment with calling here |
| 351 | // getShortestPath(), we need to refactor that code |
| 352 | // to avoid closing the transaction. |
| 353 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 354 | DataPath dataPath = |
| 355 | topoRouteService.getTopoShortestPath(srcSwitchPort, |
| 356 | dstSwitchPort); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 357 | if (dataPath == null) { |
| 358 | // We need the DataPath to compare the paths |
| 359 | dataPath = new DataPath(); |
| 360 | dataPath.setSrcPort(srcSwitchPort); |
| 361 | dataPath.setDstPort(dstSwitchPort); |
| 362 | } |
| 363 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 364 | String newDataPathSummaryStr = dataPath.dataPathSummary(); |
| 365 | if (dataPathSummaryStr.equals(newDataPathSummaryStr)) |
| 366 | continue; // Nothing changed |
| 367 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 368 | reconcileFlow(flowPathObj, dataPath); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 369 | } |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 370 | |
| 371 | // |
| 372 | // Delete all leftover Flows marked for deletion from the |
| 373 | // Network MAP. |
| 374 | // |
| 375 | while (! deleteFlows.isEmpty()) { |
| 376 | IFlowPath flowPathObj = deleteFlows.poll(); |
| 377 | conn.utils().removeFlowPath(conn, flowPathObj); |
| 378 | } |
| 379 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 380 | topoRouteService.dropShortestPathTopo(); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 381 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 382 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 383 | |
| 384 | if (processed_measurement_flow) { |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 385 | long estimatedTime = |
| 386 | System.nanoTime() - modifiedMeasurementFlowTime; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 387 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 388 | (double)estimatedTime / 1000000000 + " sec"; |
| 389 | log.debug(logMsg); |
| 390 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 391 | |
| 392 | long estimatedTime = System.nanoTime() - startTime; |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 393 | double rate = 0.0; |
| 394 | if (estimatedTime > 0) |
| 395 | rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 396 | String logMsg = "MEASUREMENT: Processed AllFlowPaths: " + |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 397 | counterAllFlowPaths + " MyFlowPaths: " + |
| 398 | counterMyFlowPaths + " in " + |
| 399 | (double)estimatedTime / 1000000000 + " sec: " + |
| 400 | rate + " paths/s"; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 401 | log.debug(logMsg); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 402 | } |
| 403 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 404 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 405 | //final ScheduledFuture<?> mapReaderHandle = |
| 406 | //mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 407 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 408 | //final ScheduledFuture<?> shortestPathReconcileHandle = |
| 409 | //shortestPathReconcileScheduler.scheduleAtFixedRate(shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 410 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 411 | @Override |
| 412 | public void init(String conf) { |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 413 | conn = GraphDBConnection.getInstance(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | public void finalize() { |
| 417 | close(); |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public void close() { |
| 422 | conn.close(); |
| 423 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 424 | |
| 425 | @Override |
| 426 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 427 | Collection<Class<? extends IFloodlightService>> l = |
| 428 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 429 | l.add(IFlowService.class); |
| 430 | return l; |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 435 | getServiceImpls() { |
| 436 | Map<Class<? extends IFloodlightService>, |
| 437 | IFloodlightService> m = |
| 438 | new HashMap<Class<? extends IFloodlightService>, |
| 439 | IFloodlightService>(); |
| 440 | m.put(IFlowService.class, this); |
| 441 | return m; |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public Collection<Class<? extends IFloodlightService>> |
| 446 | getModuleDependencies() { |
| 447 | Collection<Class<? extends IFloodlightService>> l = |
| 448 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 449 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 450 | l.add(ITopoRouteService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 451 | l.add(IRestApiService.class); |
| 452 | return l; |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public void init(FloodlightModuleContext context) |
| 457 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 458 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 459 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 460 | topoRouteService = context.getServiceImpl(ITopoRouteService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 461 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 462 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 463 | EnumSet.of(OFType.FLOW_MOD), |
| 464 | OFMESSAGE_DAMPER_TIMEOUT); |
| 465 | // TODO: An ugly hack! |
| 466 | String conf = "/tmp/cassandra.titan"; |
| 467 | this.init(conf); |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 468 | |
| 469 | mapReaderScheduler = Executors.newScheduledThreadPool(1); |
| 470 | shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 473 | private synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 474 | // |
| 475 | // Generate the next Flow Entry ID. |
| 476 | // NOTE: For now, the higher 32 bits are random, and |
| 477 | // the lower 32 bits are sequential. |
| 478 | // In the future, we need a better allocation mechanism. |
| 479 | // |
| 480 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 481 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 482 | nextFlowEntryIdSuffix = 0; |
| 483 | } else { |
| 484 | nextFlowEntryIdSuffix++; |
| 485 | } |
| 486 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 487 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 488 | return result; |
| 489 | } |
| 490 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 491 | @Override |
| 492 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 493 | restApi.addRestletRoutable(new FlowWebRoutable()); |
| 494 | |
| 495 | // Initialize the Flow Entry ID generator |
| 496 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 497 | |
| 498 | mapReaderScheduler.scheduleAtFixedRate( |
Pankaj Berde | 55f121a | 2013-04-23 15:42:54 -0700 | [diff] [blame] | 499 | mapReader, 1, 1, TimeUnit.SECONDS); |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 500 | shortestPathReconcileScheduler.scheduleAtFixedRate( |
Pankaj Berde | a2e14a9 | 2013-04-15 11:59:15 -0700 | [diff] [blame] | 501 | shortestPathReconcile, 100, 100, TimeUnit.MILLISECONDS); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Add a flow. |
| 506 | * |
| 507 | * Internally, ONOS will automatically register the installer for |
| 508 | * receiving Flow Path Notifications for that path. |
| 509 | * |
| 510 | * @param flowPath the Flow Path to install. |
| 511 | * @param flowId the return-by-reference Flow ID as assigned internally. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 512 | * @param dataPathSummaryStr the data path summary string if the added |
| 513 | * flow will be maintained internally, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 514 | * @return true on success, otherwise false. |
| 515 | */ |
| 516 | @Override |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 517 | public boolean addFlow(FlowPath flowPath, FlowId flowId, |
| 518 | String dataPathSummaryStr) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 519 | /* |
| 520 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 521 | if (flowPath.flowId().value() == measurementFlowId) { |
| 522 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 523 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 524 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 525 | |
| 526 | IFlowPath flowObj = null; |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 527 | boolean found = false; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 528 | try { |
| 529 | if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId())) |
| 530 | != null) { |
| 531 | log.debug("Adding FlowPath with FlowId {}: found existing FlowPath", |
| 532 | flowPath.flowId().toString()); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 533 | found = true; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 534 | } else { |
| 535 | flowObj = conn.utils().newFlowPath(conn); |
| 536 | log.debug("Adding FlowPath with FlowId {}: creating new FlowPath", |
| 537 | flowPath.flowId().toString()); |
| 538 | } |
| 539 | } catch (Exception e) { |
| 540 | // TODO: handle exceptions |
| 541 | conn.endTx(Transaction.ROLLBACK); |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 542 | |
| 543 | StringWriter sw = new StringWriter(); |
| 544 | e.printStackTrace(new PrintWriter(sw)); |
| 545 | String stacktrace = sw.toString(); |
| 546 | |
| 547 | log.error(":addFlow FlowId:{} failed: {}", |
| 548 | flowPath.flowId().toString(), |
| 549 | stacktrace); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 550 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 551 | if (flowObj == null) { |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 552 | log.error(":addFlow FlowId:{} failed: Flow object not created", |
| 553 | flowPath.flowId().toString()); |
| 554 | conn.endTx(Transaction.ROLLBACK); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 555 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 556 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 557 | |
| 558 | // |
| 559 | // Set the Flow key: |
| 560 | // - flowId |
| 561 | // |
| 562 | flowObj.setFlowId(flowPath.flowId().toString()); |
| 563 | flowObj.setType("flow"); |
| 564 | |
| 565 | // |
| 566 | // Set the Flow attributes: |
| 567 | // - flowPath.installerId() |
| 568 | // - flowPath.dataPath().srcPort() |
| 569 | // - flowPath.dataPath().dstPort() |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 570 | // - flowPath.matchEthernetFrameType() |
| 571 | // - flowPath.matchSrcIPv4Net() |
| 572 | // - flowPath.matchDstIPv4Net() |
| 573 | // - flowPath.matchSrcMac() |
| 574 | // - flowPath.matchDstMac() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 575 | // |
| 576 | flowObj.setInstallerId(flowPath.installerId().toString()); |
| 577 | flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString()); |
| 578 | flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value()); |
| 579 | flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString()); |
| 580 | flowObj.setDstPort(flowPath.dataPath().dstPort().port().value()); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 581 | if (flowPath.flowEntryMatch().matchEthernetFrameType()) { |
| 582 | flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType()); |
| 583 | } |
| 584 | if (flowPath.flowEntryMatch().matchSrcIPv4Net()) { |
| 585 | flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString()); |
| 586 | } |
| 587 | if (flowPath.flowEntryMatch().matchDstIPv4Net()) { |
| 588 | flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString()); |
| 589 | } |
| 590 | if (flowPath.flowEntryMatch().matchSrcMac()) { |
| 591 | flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString()); |
| 592 | } |
| 593 | if (flowPath.flowEntryMatch().matchDstMac()) { |
| 594 | flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString()); |
| 595 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 596 | |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 597 | if (dataPathSummaryStr != null) { |
| 598 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 599 | } else { |
| 600 | flowObj.setDataPathSummary(""); |
| 601 | } |
| 602 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 603 | if (found) |
| 604 | flowObj.setUserState("FE_USER_MODIFY"); |
| 605 | else |
| 606 | flowObj.setUserState("FE_USER_ADD"); |
| 607 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 608 | // Flow edges: |
| 609 | // HeadFE |
| 610 | |
| 611 | |
| 612 | // |
| 613 | // Flow Entries: |
| 614 | // flowPath.dataPath().flowEntries() |
| 615 | // |
| 616 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 617 | if (addFlowEntry(flowObj, flowEntry) == null) { |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 618 | conn.endTx(Transaction.ROLLBACK); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 619 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 620 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 621 | } |
| 622 | conn.endTx(Transaction.COMMIT); |
| 623 | |
| 624 | // |
| 625 | // TODO: We need a proper Flow ID allocation mechanism. |
| 626 | // |
| 627 | flowId.setValue(flowPath.flowId().value()); |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 628 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 629 | return true; |
| 630 | } |
| 631 | |
| 632 | /** |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 633 | * Add a flow entry to the Network MAP. |
| 634 | * |
| 635 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 636 | * @param flowEntry the Flow Entry to install. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 637 | * @return the added Flow Entry object on success, otherwise null. |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 638 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 639 | private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 640 | // Flow edges |
| 641 | // HeadFE (TODO) |
| 642 | |
| 643 | // |
| 644 | // Assign the FlowEntry ID. |
| 645 | // |
| 646 | if ((flowEntry.flowEntryId() == null) || |
| 647 | (flowEntry.flowEntryId().value() == 0)) { |
| 648 | long id = getNextFlowEntryId(); |
| 649 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 650 | } |
| 651 | |
| 652 | IFlowEntry flowEntryObj = null; |
| 653 | boolean found = false; |
| 654 | try { |
| 655 | if ((flowEntryObj = |
| 656 | conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) { |
| 657 | log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry", |
| 658 | flowEntry.flowEntryId().toString()); |
| 659 | found = true; |
| 660 | } else { |
| 661 | flowEntryObj = conn.utils().newFlowEntry(conn); |
| 662 | log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry", |
| 663 | flowEntry.flowEntryId().toString()); |
| 664 | } |
| 665 | } catch (Exception e) { |
| 666 | log.error(":addFlow FlowEntryId:{} failed", |
| 667 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 668 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 669 | } |
| 670 | if (flowEntryObj == null) { |
| 671 | log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created", |
| 672 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 673 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | // |
| 677 | // Set the Flow Entry key: |
| 678 | // - flowEntry.flowEntryId() |
| 679 | // |
| 680 | flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString()); |
| 681 | flowEntryObj.setType("flow_entry"); |
| 682 | |
| 683 | // |
| 684 | // Set the Flow Entry Edges and attributes: |
| 685 | // - Switch edge |
| 686 | // - InPort edge |
| 687 | // - OutPort edge |
| 688 | // |
| 689 | // - flowEntry.flowEntryMatch() |
| 690 | // - flowEntry.flowEntryActions() |
| 691 | // - flowEntry.dpid() |
| 692 | // - flowEntry.flowEntryUserState() |
| 693 | // - flowEntry.flowEntrySwitchState() |
| 694 | // - flowEntry.flowEntryErrorState() |
| 695 | // - flowEntry.matchInPort() |
| 696 | // - flowEntry.matchEthernetFrameType() |
| 697 | // - flowEntry.matchSrcIPv4Net() |
| 698 | // - flowEntry.matchDstIPv4Net() |
| 699 | // - flowEntry.matchSrcMac() |
| 700 | // - flowEntry.matchDstMac() |
| 701 | // - flowEntry.actionOutput() |
| 702 | // |
| 703 | ISwitchObject sw = |
| 704 | conn.utils().searchSwitch(conn, flowEntry.dpid().toString()); |
| 705 | flowEntryObj.setSwitchDpid(flowEntry.dpid().toString()); |
| 706 | flowEntryObj.setSwitch(sw); |
| 707 | if (flowEntry.flowEntryMatch().matchInPort()) { |
| 708 | IPortObject inport = |
| 709 | conn.utils().searchPort(conn, flowEntry.dpid().toString(), |
| 710 | flowEntry.flowEntryMatch().inPort().value()); |
| 711 | flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value()); |
| 712 | flowEntryObj.setInPort(inport); |
| 713 | } |
| 714 | if (flowEntry.flowEntryMatch().matchEthernetFrameType()) { |
| 715 | flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType()); |
| 716 | } |
| 717 | if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) { |
| 718 | flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString()); |
| 719 | } |
| 720 | if (flowEntry.flowEntryMatch().matchDstIPv4Net()) { |
| 721 | flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString()); |
| 722 | } |
| 723 | if (flowEntry.flowEntryMatch().matchSrcMac()) { |
| 724 | flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString()); |
| 725 | } |
| 726 | if (flowEntry.flowEntryMatch().matchDstMac()) { |
| 727 | flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString()); |
| 728 | } |
| 729 | |
| 730 | for (FlowEntryAction fa : flowEntry.flowEntryActions()) { |
| 731 | if (fa.actionOutput() != null) { |
| 732 | IPortObject outport = |
| 733 | conn.utils().searchPort(conn, |
| 734 | flowEntry.dpid().toString(), |
| 735 | fa.actionOutput().port().value()); |
| 736 | flowEntryObj.setActionOutput(fa.actionOutput().port().value()); |
| 737 | flowEntryObj.setOutPort(outport); |
| 738 | } |
| 739 | } |
| 740 | // TODO: Hacks with hard-coded state names! |
| 741 | if (found) |
| 742 | flowEntryObj.setUserState("FE_USER_MODIFY"); |
| 743 | else |
| 744 | flowEntryObj.setUserState("FE_USER_ADD"); |
| 745 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 746 | // |
| 747 | // TODO: Take care of the FlowEntryErrorState. |
| 748 | // |
| 749 | |
| 750 | // Flow Entries edges: |
| 751 | // Flow |
| 752 | // NextFE (TODO) |
| 753 | if (! found) { |
| 754 | flowObj.addFlowEntry(flowEntryObj); |
| 755 | flowEntryObj.setFlow(flowObj); |
| 756 | } |
| 757 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 758 | return flowEntryObj; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 762 | * Delete all previously added flows. |
| 763 | * |
| 764 | * @return true on success, otherwise false. |
| 765 | */ |
| 766 | @Override |
| 767 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 768 | List<Thread> threads = new LinkedList<Thread>(); |
| 769 | final ConcurrentLinkedQueue<FlowId> concurrentAllFlowIds = |
| 770 | new ConcurrentLinkedQueue<FlowId>(); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 771 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 772 | // Get all Flow IDs |
| 773 | Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn); |
| 774 | for (IFlowPath flowPathObj : allFlowPaths) { |
| 775 | if (flowPathObj == null) |
| 776 | continue; |
| 777 | String flowIdStr = flowPathObj.getFlowId(); |
| 778 | if (flowIdStr == null) |
| 779 | continue; |
| 780 | FlowId flowId = new FlowId(flowIdStr); |
| 781 | concurrentAllFlowIds.add(flowId); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 784 | // Delete all flows one-by-one |
| 785 | for (FlowId flowId : concurrentAllFlowIds) |
| 786 | deleteFlow(flowId); |
| 787 | |
| 788 | /* |
| 789 | * TODO: A faster mechanism to delete the Flow Paths by using |
| 790 | * a number of threads. Commented-out for now. |
| 791 | */ |
| 792 | /* |
| 793 | // |
| 794 | // Create the threads to delete the Flow Paths |
| 795 | // |
| 796 | for (int i = 0; i < 10; i++) { |
| 797 | Thread thread = new Thread(new Runnable() { |
| 798 | @Override |
| 799 | public void run() { |
| 800 | while (true) { |
| 801 | FlowId flowId = concurrentAllFlowIds.poll(); |
| 802 | if (flowId == null) |
| 803 | return; |
| 804 | deleteFlow(flowId); |
| 805 | } |
| 806 | }}, "Delete All Flow Paths"); |
| 807 | threads.add(thread); |
| 808 | } |
| 809 | |
| 810 | // Start processing |
| 811 | for (Thread thread : threads) { |
| 812 | thread.start(); |
| 813 | } |
| 814 | |
| 815 | // Want for all threads to complete |
| 816 | for (Thread thread : threads) { |
| 817 | try { |
| 818 | thread.join(); |
| 819 | } catch (InterruptedException e) { |
| 820 | log.debug("Exception waiting for a thread to delete a Flow Path: ", e); |
| 821 | } |
| 822 | } |
| 823 | */ |
| 824 | |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 825 | return true; |
| 826 | } |
| 827 | |
| 828 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 829 | * Delete a previously added flow. |
| 830 | * |
| 831 | * @param flowId the Flow ID of the flow to delete. |
| 832 | * @return true on success, otherwise false. |
| 833 | */ |
| 834 | @Override |
| 835 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 836 | /* |
| 837 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 838 | if (flowId.value() == measurementFlowId) { |
| 839 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 840 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 841 | */ |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 842 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 843 | IFlowPath flowObj = null; |
| 844 | // |
| 845 | // We just mark the entries for deletion, |
| 846 | // and let the switches remove each individual entry after |
| 847 | // it has been removed from the switches. |
| 848 | // |
| 849 | try { |
| 850 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 851 | != null) { |
| 852 | log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath", |
| 853 | flowId.toString()); |
| 854 | } else { |
| 855 | log.debug("Deleting FlowPath with FlowId {}: FlowPath not found", |
| 856 | flowId.toString()); |
| 857 | } |
| 858 | } catch (Exception e) { |
| 859 | // TODO: handle exceptions |
| 860 | conn.endTx(Transaction.ROLLBACK); |
| 861 | log.error(":deleteFlow FlowId:{} failed", flowId.toString()); |
| 862 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 863 | if (flowObj == null) { |
| 864 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 865 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 866 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 867 | |
| 868 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 869 | // Find and mark for deletion all Flow Entries, |
| 870 | // and the Flow itself. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 871 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 872 | flowObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 873 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 874 | boolean empty = true; // TODO: an ugly hack |
| 875 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 876 | empty = false; |
| 877 | // flowObj.removeFlowEntry(flowEntryObj); |
| 878 | // conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 879 | flowEntryObj.setUserState("FE_USER_DELETE"); |
| 880 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 881 | } |
| 882 | // Remove from the database empty flows |
| 883 | if (empty) |
| 884 | conn.utils().removeFlowPath(conn, flowObj); |
| 885 | conn.endTx(Transaction.COMMIT); |
| 886 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 887 | return true; |
| 888 | } |
| 889 | |
| 890 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 891 | * Clear the state for all previously added flows. |
| 892 | * |
| 893 | * @return true on success, otherwise false. |
| 894 | */ |
| 895 | @Override |
| 896 | public boolean clearAllFlows() { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 897 | List<FlowId> allFlowIds = new LinkedList<FlowId>(); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 898 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 899 | // Get all Flow IDs |
| 900 | Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn); |
| 901 | for (IFlowPath flowPathObj : allFlowPaths) { |
| 902 | if (flowPathObj == null) |
| 903 | continue; |
| 904 | String flowIdStr = flowPathObj.getFlowId(); |
| 905 | if (flowIdStr == null) |
| 906 | continue; |
| 907 | FlowId flowId = new FlowId(flowIdStr); |
| 908 | allFlowIds.add(flowId); |
| 909 | } |
| 910 | |
| 911 | // Clear all flows one-by-one |
| 912 | for (FlowId flowId : allFlowIds) { |
| 913 | clearFlow(flowId); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 920 | * Clear the state for a previously added flow. |
| 921 | * |
| 922 | * @param flowId the Flow ID of the flow to clear. |
| 923 | * @return true on success, otherwise false. |
| 924 | */ |
| 925 | @Override |
| 926 | public boolean clearFlow(FlowId flowId) { |
| 927 | IFlowPath flowObj = null; |
| 928 | try { |
| 929 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 930 | != null) { |
| 931 | log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath", |
| 932 | flowId.toString()); |
| 933 | } else { |
| 934 | log.debug("Clearing FlowPath with FlowId {}: FlowPath not found", |
| 935 | flowId.toString()); |
| 936 | } |
| 937 | } catch (Exception e) { |
| 938 | // TODO: handle exceptions |
| 939 | conn.endTx(Transaction.ROLLBACK); |
| 940 | log.error(":clearFlow FlowId:{} failed", flowId.toString()); |
| 941 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 942 | if (flowObj == null) { |
| 943 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 944 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 945 | } |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 946 | |
| 947 | // |
| 948 | // Remove all Flow Entries |
| 949 | // |
| 950 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 951 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 952 | flowObj.removeFlowEntry(flowEntryObj); |
| 953 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 954 | } |
| 955 | // Remove the Flow itself |
| 956 | conn.utils().removeFlowPath(conn, flowObj); |
| 957 | conn.endTx(Transaction.COMMIT); |
| 958 | |
| 959 | return true; |
| 960 | } |
| 961 | |
| 962 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 963 | * Get a previously added flow. |
| 964 | * |
| 965 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 966 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 967 | */ |
| 968 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 969 | public FlowPath getFlow(FlowId flowId) { |
| 970 | IFlowPath flowObj = null; |
| 971 | try { |
| 972 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 973 | != null) { |
| 974 | log.debug("Get FlowPath with FlowId {}: found existing FlowPath", |
| 975 | flowId.toString()); |
| 976 | } else { |
| 977 | log.debug("Get FlowPath with FlowId {}: FlowPath not found", |
| 978 | flowId.toString()); |
| 979 | } |
| 980 | } catch (Exception e) { |
| 981 | // TODO: handle exceptions |
| 982 | conn.endTx(Transaction.ROLLBACK); |
| 983 | log.error(":getFlow FlowId:{} failed", flowId.toString()); |
| 984 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 985 | if (flowObj == null) { |
| 986 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 987 | return null; // Flow not found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 988 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 989 | |
| 990 | // |
| 991 | // Extract the Flow state |
| 992 | // |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 993 | FlowPath flowPath = extractFlowPath(flowObj); |
| 994 | conn.endTx(Transaction.COMMIT); |
| 995 | |
| 996 | return flowPath; |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Get all previously added flows by a specific installer for a given |
| 1001 | * data path endpoints. |
| 1002 | * |
| 1003 | * @param installerId the Caller ID of the installer of the flow to get. |
| 1004 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 1005 | * @return the Flow Paths if found, otherwise null. |
| 1006 | */ |
| 1007 | @Override |
| 1008 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 1009 | DataPathEndpoints dataPathEndpoints) { |
| 1010 | // |
| 1011 | // TODO: The implementation below is not optimal: |
| 1012 | // We fetch all flows, and then return only the subset that match |
| 1013 | // the query conditions. |
| 1014 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1015 | // the flows as appropriate. |
| 1016 | // |
| 1017 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1018 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1019 | |
| 1020 | if (allFlows == null) { |
| 1021 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1022 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1025 | for (FlowPath flow : allFlows) { |
| 1026 | // |
| 1027 | // TODO: String-based comparison is sub-optimal. |
| 1028 | // We are using it for now to save us the extra work of |
Pavlin Radoslavov | c4e76a6 | 2013-03-06 10:52:41 -0800 | [diff] [blame] | 1029 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1030 | // |
| 1031 | if (! flow.installerId().toString().equals(installerId.toString())) |
| 1032 | continue; |
| 1033 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 1034 | continue; |
| 1035 | } |
| 1036 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 1037 | continue; |
| 1038 | } |
| 1039 | flowPaths.add(flow); |
| 1040 | } |
| 1041 | |
| 1042 | if (flowPaths.isEmpty()) { |
| 1043 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1044 | } else { |
| 1045 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints); |
| 1046 | } |
| 1047 | |
| 1048 | return flowPaths; |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Get all installed flows by all installers for given data path endpoints. |
| 1053 | * |
| 1054 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 1055 | * @return the Flow Paths if found, otherwise null. |
| 1056 | */ |
| 1057 | @Override |
| 1058 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
| 1059 | // |
| 1060 | // TODO: The implementation below is not optimal: |
| 1061 | // We fetch all flows, and then return only the subset that match |
| 1062 | // the query conditions. |
| 1063 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1064 | // the flows as appropriate. |
| 1065 | // |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1066 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1067 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1068 | |
| 1069 | if (allFlows == null) { |
| 1070 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1071 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1074 | for (FlowPath flow : allFlows) { |
| 1075 | // |
| 1076 | // TODO: String-based comparison is sub-optimal. |
| 1077 | // We are using it for now to save us the extra work of |
Pavlin Radoslavov | c4e76a6 | 2013-03-06 10:52:41 -0800 | [diff] [blame] | 1078 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1079 | // |
| 1080 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 1081 | continue; |
| 1082 | } |
| 1083 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 1084 | continue; |
| 1085 | } |
| 1086 | flowPaths.add(flow); |
| 1087 | } |
| 1088 | |
| 1089 | if (flowPaths.isEmpty()) { |
| 1090 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1091 | } else { |
| 1092 | log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints); |
| 1093 | } |
| 1094 | |
| 1095 | return flowPaths; |
| 1096 | } |
| 1097 | |
| 1098 | /** |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1099 | * Get summary of all installed flows by all installers in a given range |
| 1100 | * |
| 1101 | * @param flowId the data path endpoints of the flows to get. |
| 1102 | * @param maxFlows: the maximum number of flows to be returned |
| 1103 | * @return the Flow Paths if found, otherwise null. |
| 1104 | */ |
| 1105 | @Override |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1106 | public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) { |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1107 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1108 | // TODO: The implementation below is not optimal: |
| 1109 | // We fetch all flows, and then return only the subset that match |
| 1110 | // the query conditions. |
| 1111 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1112 | // the flows as appropriate. |
| 1113 | // |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1114 | //ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1115 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1116 | ArrayList<IFlowPath> flowPathsWithoutFlowEntries = getAllFlowsWithoutFlowEntries(); |
| 1117 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1118 | Collections.sort(flowPathsWithoutFlowEntries, |
| 1119 | new Comparator<IFlowPath>(){ |
| 1120 | @Override |
| 1121 | public int compare(IFlowPath first, IFlowPath second) { |
| 1122 | // TODO Auto-generated method stub |
| 1123 | long result = new FlowId(first.getFlowId()).value() |
| 1124 | - new FlowId(second.getFlowId()).value(); |
| 1125 | if (result > 0) return 1; |
| 1126 | else if (result < 0) return -1; |
| 1127 | else return 0; |
| 1128 | } |
| 1129 | } |
| 1130 | ); |
| 1131 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1132 | return flowPathsWithoutFlowEntries; |
| 1133 | |
| 1134 | /* |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1135 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1136 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1137 | if (allFlows == null) { |
| 1138 | log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1139 | return flowPaths; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Umesh Krishnaswamy | 244b4ae | 2013-03-29 12:05:15 -0700 | [diff] [blame] | 1142 | Collections.sort(allFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1143 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1144 | for (FlowPath flow : allFlows) { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1145 | flow.setFlowEntryMatch(null); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1146 | |
Pavlin Radoslavov | 96b4342 | 2013-04-04 19:14:56 -0700 | [diff] [blame] | 1147 | // start from desired flowId |
| 1148 | if (flow.flowId().value() < flowId.value()) { |
| 1149 | continue; |
| 1150 | } |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1151 | |
| 1152 | // Summarize by making null flow entry fields that are not relevant to report |
| 1153 | for (FlowEntry flowEntry : flow.dataPath().flowEntries()) { |
| 1154 | flowEntry.setFlowEntryActions(null); |
| 1155 | flowEntry.setFlowEntryMatch(null); |
| 1156 | } |
| 1157 | |
| 1158 | flowPaths.add(flow); |
| 1159 | if (maxFlows != 0 && flowPaths.size() >= maxFlows) { |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (flowPaths.isEmpty()) { |
| 1165 | log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1166 | } else { |
| 1167 | log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows); |
| 1168 | } |
| 1169 | |
| 1170 | return flowPaths; |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1171 | */ |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /** |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1175 | * Get all installed flows by all installers. |
| 1176 | * |
| 1177 | * @return the Flow Paths if found, otherwise null. |
| 1178 | */ |
| 1179 | @Override |
| 1180 | public ArrayList<FlowPath> getAllFlows() { |
| 1181 | Iterable<IFlowPath> flowPathsObj = null; |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1182 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1183 | |
| 1184 | try { |
| 1185 | if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) { |
| 1186 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1187 | } else { |
| 1188 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1189 | } |
| 1190 | } catch (Exception e) { |
| 1191 | // TODO: handle exceptions |
| 1192 | conn.endTx(Transaction.ROLLBACK); |
| 1193 | log.error(":getAllFlowPaths failed"); |
| 1194 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1195 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 1196 | conn.endTx(Transaction.COMMIT); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1197 | return flowPaths; // No Flows found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1198 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1199 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1200 | for (IFlowPath flowObj : flowPathsObj) { |
| 1201 | // |
| 1202 | // Extract the Flow state |
| 1203 | // |
| 1204 | FlowPath flowPath = extractFlowPath(flowObj); |
Pavlin Radoslavov | 3f2af73 | 2013-03-29 15:29:35 -0700 | [diff] [blame] | 1205 | if (flowPath != null) |
| 1206 | flowPaths.add(flowPath); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | conn.endTx(Transaction.COMMIT); |
| 1210 | |
| 1211 | return flowPaths; |
| 1212 | } |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1213 | |
| 1214 | public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(){ |
| 1215 | Iterable<IFlowPath> flowPathsObj = null; |
| 1216 | ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>(); |
| 1217 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1218 | |
Jonathan Hart | e6e9187 | 2013-04-13 11:10:32 -0700 | [diff] [blame] | 1219 | conn.endTx(Transaction.COMMIT); |
| 1220 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1221 | try { |
| 1222 | if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) { |
| 1223 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1224 | } else { |
| 1225 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1226 | } |
| 1227 | } catch (Exception e) { |
| 1228 | // TODO: handle exceptions |
| 1229 | conn.endTx(Transaction.ROLLBACK); |
| 1230 | log.error(":getAllFlowPaths failed"); |
| 1231 | } |
| 1232 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 1233 | return new ArrayList<IFlowPath>(); // No Flows found |
| 1234 | } |
| 1235 | |
| 1236 | for (IFlowPath flowObj : flowPathsObj){ |
| 1237 | flowPathsObjArray.add(flowObj); |
| 1238 | } |
| 1239 | /* |
| 1240 | for (IFlowPath flowObj : flowPathsObj) { |
| 1241 | // |
| 1242 | // Extract the Flow state |
| 1243 | // |
| 1244 | FlowPath flowPath = extractFlowPath(flowObj); |
| 1245 | if (flowPath != null) |
| 1246 | flowPaths.add(flowPath); |
| 1247 | } |
| 1248 | */ |
| 1249 | |
| 1250 | //conn.endTx(Transaction.COMMIT); |
| 1251 | |
| 1252 | return flowPathsObjArray; |
| 1253 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1254 | |
| 1255 | /** |
| 1256 | * Extract Flow Path State from a Titan Database Object @ref IFlowPath. |
| 1257 | * |
| 1258 | * @param flowObj the object to extract the Flow Path State from. |
| 1259 | * @return the extracted Flow Path State. |
| 1260 | */ |
| 1261 | private FlowPath extractFlowPath(IFlowPath flowObj) { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1262 | // |
| 1263 | // Extract the Flow state |
| 1264 | // |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1265 | String flowIdStr = flowObj.getFlowId(); |
| 1266 | String installerIdStr = flowObj.getInstallerId(); |
| 1267 | String srcSwitchStr = flowObj.getSrcSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1268 | Short srcPortShort = flowObj.getSrcPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1269 | String dstSwitchStr = flowObj.getDstSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1270 | Short dstPortShort = flowObj.getDstPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1271 | |
| 1272 | if ((flowIdStr == null) || |
| 1273 | (installerIdStr == null) || |
| 1274 | (srcSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1275 | (srcPortShort == null) || |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1276 | (dstSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1277 | (dstPortShort == null)) { |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1278 | // TODO: A work-around, becauuse of some bogus database objects |
| 1279 | return null; |
| 1280 | } |
| 1281 | |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1282 | FlowPath flowPath = new FlowPath(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1283 | flowPath.setFlowId(new FlowId(flowIdStr)); |
| 1284 | flowPath.setInstallerId(new CallerId(installerIdStr)); |
| 1285 | flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1286 | flowPath.dataPath().srcPort().setPort(new Port(srcPortShort)); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1287 | flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1288 | flowPath.dataPath().dstPort().setPort(new Port(dstPortShort)); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1289 | // |
| 1290 | // Extract the match conditions common for all Flow Entries |
| 1291 | // |
| 1292 | { |
| 1293 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1294 | Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1295 | if (matchEthernetFrameType != null) |
| 1296 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1297 | String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1298 | if (matchSrcIPv4Net != null) |
| 1299 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1300 | String matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1301 | if (matchDstIPv4Net != null) |
| 1302 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1303 | String matchSrcMac = flowObj.getMatchSrcMac(); |
| 1304 | if (matchSrcMac != null) |
| 1305 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1306 | String matchDstMac = flowObj.getMatchDstMac(); |
| 1307 | if (matchDstMac != null) |
| 1308 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1309 | flowPath.setFlowEntryMatch(match); |
| 1310 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1311 | |
| 1312 | // |
| 1313 | // Extract all Flow Entries |
| 1314 | // |
| 1315 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1316 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1317 | FlowEntry flowEntry = extractFlowEntry(flowEntryObj); |
| 1318 | if (flowEntry == null) |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1319 | continue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1320 | flowPath.dataPath().flowEntries().add(flowEntry); |
| 1321 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1322 | |
| 1323 | return flowPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1324 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1325 | |
| 1326 | /** |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1327 | * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry. |
| 1328 | * |
| 1329 | * @param flowEntryObj the object to extract the Flow Entry State from. |
| 1330 | * @return the extracted Flow Entry State. |
| 1331 | */ |
| 1332 | private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) { |
| 1333 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1334 | String switchDpidStr = flowEntryObj.getSwitchDpid(); |
| 1335 | String userState = flowEntryObj.getUserState(); |
| 1336 | String switchState = flowEntryObj.getSwitchState(); |
| 1337 | |
| 1338 | if ((flowEntryIdStr == null) || |
| 1339 | (switchDpidStr == null) || |
| 1340 | (userState == null) || |
| 1341 | (switchState == null)) { |
| 1342 | // TODO: A work-around, becauuse of some bogus database objects |
| 1343 | return null; |
| 1344 | } |
| 1345 | |
| 1346 | FlowEntry flowEntry = new FlowEntry(); |
| 1347 | flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr)); |
| 1348 | flowEntry.setDpid(new Dpid(switchDpidStr)); |
| 1349 | |
| 1350 | // |
| 1351 | // Extract the match conditions |
| 1352 | // |
| 1353 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1354 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1355 | if (matchInPort != null) |
| 1356 | match.enableInPort(new Port(matchInPort)); |
| 1357 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1358 | if (matchEthernetFrameType != null) |
| 1359 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1360 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1361 | if (matchSrcIPv4Net != null) |
| 1362 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1363 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1364 | if (matchDstIPv4Net != null) |
| 1365 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1366 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1367 | if (matchSrcMac != null) |
| 1368 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1369 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1370 | if (matchDstMac != null) |
| 1371 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1372 | flowEntry.setFlowEntryMatch(match); |
| 1373 | |
| 1374 | // |
| 1375 | // Extract the actions |
| 1376 | // |
| 1377 | ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>(); |
| 1378 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1379 | if (actionOutputPort != null) { |
| 1380 | FlowEntryAction action = new FlowEntryAction(); |
| 1381 | action.setActionOutput(new Port(actionOutputPort)); |
| 1382 | actions.add(action); |
| 1383 | } |
| 1384 | flowEntry.setFlowEntryActions(actions); |
| 1385 | flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState)); |
| 1386 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState)); |
| 1387 | // |
| 1388 | // TODO: Take care of the FlowEntryMatch, FlowEntryAction set, |
| 1389 | // and FlowEntryErrorState. |
| 1390 | // |
| 1391 | return flowEntry; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1395 | * Add and maintain a shortest-path flow. |
| 1396 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1397 | * NOTE: The Flow Path argument does NOT contain flow entries. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1398 | * |
| 1399 | * @param flowPath the Flow Path with the endpoints and the match |
| 1400 | * conditions to install. |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1401 | * @return the added shortest-path flow on success, otherwise null. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1402 | */ |
| 1403 | @Override |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1404 | public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1405 | // |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1406 | // Don't do the shortest path computation here. |
| 1407 | // Instead, let the Flow reconciliation thread take care of it. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1408 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1409 | |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1410 | // We need the DataPath to populate the Network MAP |
| 1411 | DataPath dataPath = new DataPath(); |
| 1412 | dataPath.setSrcPort(flowPath.dataPath().srcPort()); |
| 1413 | dataPath.setDstPort(flowPath.dataPath().dstPort()); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1414 | |
| 1415 | // |
| 1416 | // Prepare the computed Flow Path |
| 1417 | // |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1418 | FlowPath computedFlowPath = new FlowPath(); |
| 1419 | computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value())); |
| 1420 | computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value())); |
| 1421 | computedFlowPath.setDataPath(dataPath); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1422 | computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch())); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1423 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1424 | FlowId flowId = new FlowId(); |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1425 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1426 | if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr)) |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1427 | return null; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1428 | |
| 1429 | // TODO: Mark the flow for maintenance purpose |
| 1430 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1431 | return (computedFlowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | /** |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1435 | * Reconcile a flow. |
| 1436 | * |
| 1437 | * @param flowObj the flow that needs to be reconciliated. |
| 1438 | * @param newDataPath the new data path to use. |
| 1439 | * @return true on success, otherwise false. |
| 1440 | */ |
| 1441 | public boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) { |
| 1442 | Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches(); |
| 1443 | |
| 1444 | // |
| 1445 | // Set the incoming port matching and the outgoing port output |
| 1446 | // actions for each flow entry. |
| 1447 | // |
| 1448 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
| 1449 | // Set the incoming port matching |
| 1450 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 1451 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 1452 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 1453 | |
| 1454 | // Set the outgoing port output action |
| 1455 | ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions(); |
| 1456 | if (flowEntryActions == null) { |
| 1457 | flowEntryActions = new ArrayList<FlowEntryAction>(); |
| 1458 | flowEntry.setFlowEntryActions(flowEntryActions); |
| 1459 | } |
| 1460 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 1461 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
| 1462 | flowEntryActions.add(flowEntryAction); |
| 1463 | } |
| 1464 | |
| 1465 | // |
| 1466 | // Remove the old Flow Entries, and add the new Flow Entries |
| 1467 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1468 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1469 | LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>(); |
| 1470 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1471 | flowEntryObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1472 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1473 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1474 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1475 | addFlowEntry(flowObj, flowEntry); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | // |
| 1479 | // Set the Data Path Summary |
| 1480 | // |
| 1481 | String dataPathSummaryStr = newDataPath.dataPathSummary(); |
| 1482 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 1483 | |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1488 | * Reconcile all flows in a set. |
| 1489 | * |
| 1490 | * @param flowObjSet the set of flows that need to be reconciliated. |
| 1491 | */ |
| 1492 | public void reconcileFlows(Iterable<IFlowPath> flowObjSet) { |
| 1493 | if (! flowObjSet.iterator().hasNext()) |
| 1494 | return; |
Pavlin Radoslavov | 0eeb15d | 2013-04-05 10:23:51 -0700 | [diff] [blame] | 1495 | // TODO: Not implemented/used yet. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1499 | * Install a Flow Entry on a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1500 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 1501 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1502 | * @param flowObj the flow path object for the flow entry to install. |
| 1503 | * @param flowEntryObj the flow entry object to install. |
| 1504 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1505 | */ |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1506 | public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj, |
| 1507 | IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1508 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1509 | if (flowEntryIdStr == null) |
| 1510 | return false; |
| 1511 | FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1512 | String userState = flowEntryObj.getUserState(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1513 | if (userState == null) |
| 1514 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1515 | |
| 1516 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1517 | // Create the Open Flow Flow Modification Entry to push |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1518 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1519 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1520 | .getMessage(OFType.FLOW_MOD); |
| 1521 | long cookie = flowEntryId.value(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1522 | |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1523 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1524 | if (userState.equals("FE_USER_ADD")) { |
| 1525 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1526 | } else if (userState.equals("FE_USER_MODIFY")) { |
| 1527 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1528 | } else if (userState.equals("FE_USER_DELETE")) { |
| 1529 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1530 | } else { |
| 1531 | // Unknown user state. Ignore the entry |
| 1532 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1533 | flowEntryId.toString(), userState); |
| 1534 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1538 | // Fetch the match conditions. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1539 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1540 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1541 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1542 | // condition set. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1543 | // |
| 1544 | OFMatch match = new OFMatch(); |
| 1545 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1546 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1547 | // Match the Incoming Port |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1548 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1549 | if (matchInPort != null) { |
| 1550 | match.setInputPort(matchInPort); |
| 1551 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1552 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1553 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1554 | // Match the Ethernet Frame Type |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1555 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1556 | if (matchEthernetFrameType == null) |
| 1557 | matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1558 | if (matchEthernetFrameType != null) { |
| 1559 | match.setDataLayerType(matchEthernetFrameType); |
| 1560 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1561 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1562 | |
| 1563 | // Match the Source IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1564 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1565 | if (matchSrcIPv4Net == null) |
| 1566 | matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1567 | if (matchSrcIPv4Net != null) { |
| 1568 | match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC); |
| 1569 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1570 | |
| 1571 | // Natch the Destination IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1572 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1573 | if (matchDstIPv4Net == null) |
| 1574 | matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1575 | if (matchDstIPv4Net != null) { |
| 1576 | match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST); |
| 1577 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1578 | |
| 1579 | // Match the Source MAC address |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1580 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1581 | if (matchSrcMac == null) |
| 1582 | matchSrcMac = flowObj.getMatchSrcMac(); |
| 1583 | if (matchSrcMac != null) { |
| 1584 | match.setDataLayerSource(matchSrcMac); |
| 1585 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 1586 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1587 | |
| 1588 | // Match the Destination MAC address |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1589 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1590 | if (matchDstMac == null) |
| 1591 | matchDstMac = flowObj.getMatchDstMac(); |
| 1592 | if (matchDstMac != null) { |
| 1593 | match.setDataLayerDestination(matchDstMac); |
| 1594 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 1595 | } |
| 1596 | |
| 1597 | // |
| 1598 | // Fetch the actions |
| 1599 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1600 | // TODO: For now we support only the "OUTPUT" actions. |
| 1601 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1602 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1603 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1604 | if (actionOutputPort != null) { |
| 1605 | OFActionOutput action = new OFActionOutput(); |
| 1606 | // XXX: The max length is hard-coded for now |
| 1607 | action.setMaxLength((short)0xffff); |
| 1608 | action.setPort(actionOutputPort); |
| 1609 | actions.add(action); |
| 1610 | } |
| 1611 | |
| 1612 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 1613 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 1614 | .setPriority(PRIORITY_DEFAULT) |
| 1615 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1616 | .setCookie(cookie) |
| 1617 | .setCommand(flowModCommand) |
| 1618 | .setMatch(match) |
| 1619 | .setActions(actions) |
| 1620 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 1621 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 1622 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 1623 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 1624 | if (actionOutputPort != null) |
| 1625 | fm.setOutPort(actionOutputPort); |
| 1626 | } |
| 1627 | |
| 1628 | // |
| 1629 | // TODO: Set the following flag |
| 1630 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 1631 | // See method ForwardingBase::pushRoute() |
| 1632 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1633 | |
| 1634 | // |
| 1635 | // Write the message to the switch |
| 1636 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1637 | log.debug("MEASUREMENT: Installing flow entry " + userState + |
| 1638 | " into switch DPID: " + |
| 1639 | mySwitch.getStringId() + |
| 1640 | " flowEntryId: " + flowEntryId.toString() + |
| 1641 | " srcMac: " + matchSrcMac + " dstMac: " + matchDstMac + |
| 1642 | " inPort: " + matchInPort + " outPort: " + actionOutputPort |
| 1643 | ); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1644 | try { |
| 1645 | messageDamper.write(mySwitch, fm, null); |
| 1646 | mySwitch.flush(); |
| 1647 | // |
| 1648 | // TODO: We should use the OpenFlow Barrier mechanism |
| 1649 | // to check for errors, and update the SwitchState |
| 1650 | // for a flow entry after the Barrier message is |
| 1651 | // is received. |
| 1652 | // |
| 1653 | flowEntryObj.setSwitchState("FE_SWITCH_UPDATED"); |
| 1654 | } catch (IOException e) { |
| 1655 | log.error("Failure writing flow mod from network map", e); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1656 | return false; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | return true; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /** |
| 1663 | * Install a Flow Entry on a switch. |
| 1664 | * |
| 1665 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1666 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1667 | * @param flowEntry the flow entry to install. |
| 1668 | * @return true on success, otherwise false. |
| 1669 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1670 | public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 1671 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1672 | // |
| 1673 | // Create the OpenFlow Flow Modification Entry to push |
| 1674 | // |
| 1675 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1676 | .getMessage(OFType.FLOW_MOD); |
| 1677 | long cookie = flowEntry.flowEntryId().value(); |
| 1678 | |
| 1679 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1680 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 1681 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1682 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 1683 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1684 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 1685 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1686 | } else { |
| 1687 | // Unknown user state. Ignore the entry |
| 1688 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1689 | flowEntry.flowEntryId().toString(), |
| 1690 | flowEntry.flowEntryUserState()); |
| 1691 | return false; |
| 1692 | } |
| 1693 | |
| 1694 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1695 | // Fetch the match conditions. |
| 1696 | // |
| 1697 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1698 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1699 | // condition set. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1700 | // |
| 1701 | OFMatch match = new OFMatch(); |
| 1702 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1703 | FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch(); |
| 1704 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 1705 | |
| 1706 | // Match the Incoming Port |
| 1707 | Port matchInPort = flowEntryMatch.inPort(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1708 | if (matchInPort != null) { |
| 1709 | match.setInputPort(matchInPort.value()); |
| 1710 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1711 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1712 | |
| 1713 | // Match the Ethernet Frame Type |
| 1714 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
| 1715 | if ((matchEthernetFrameType == null) && (flowPathMatch != null)) { |
| 1716 | matchEthernetFrameType = flowPathMatch.ethernetFrameType(); |
| 1717 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1718 | if (matchEthernetFrameType != null) { |
| 1719 | match.setDataLayerType(matchEthernetFrameType); |
| 1720 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1721 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1722 | |
| 1723 | // Match the Source IPv4 Network prefix |
| 1724 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
| 1725 | if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) { |
| 1726 | matchSrcIPv4Net = flowPathMatch.srcIPv4Net(); |
| 1727 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1728 | if (matchSrcIPv4Net != null) { |
| 1729 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 1730 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1731 | |
| 1732 | // Natch the Destination IPv4 Network prefix |
| 1733 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
| 1734 | if ((matchDstIPv4Net == null) && (flowPathMatch != null)) { |
| 1735 | matchDstIPv4Net = flowPathMatch.dstIPv4Net(); |
| 1736 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1737 | if (matchDstIPv4Net != null) { |
| 1738 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 1739 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1740 | |
| 1741 | // Match the Source MAC address |
| 1742 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
| 1743 | if ((matchSrcMac == null) && (flowPathMatch != null)) { |
| 1744 | matchSrcMac = flowPathMatch.srcMac(); |
| 1745 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1746 | if (matchSrcMac != null) { |
| 1747 | match.setDataLayerSource(matchSrcMac.toString()); |
| 1748 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 1749 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1750 | |
| 1751 | // Match the Destination MAC address |
| 1752 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
| 1753 | if ((matchDstMac == null) && (flowPathMatch != null)) { |
| 1754 | matchDstMac = flowPathMatch.dstMac(); |
| 1755 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1756 | if (matchDstMac != null) { |
| 1757 | match.setDataLayerDestination(matchDstMac.toString()); |
| 1758 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 1759 | } |
| 1760 | |
| 1761 | // |
| 1762 | // Fetch the actions |
| 1763 | // |
| 1764 | // TODO: For now we support only the "OUTPUT" actions. |
| 1765 | // |
| 1766 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 1767 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1768 | ArrayList<FlowEntryAction> flowEntryActions = |
| 1769 | flowEntry.flowEntryActions(); |
| 1770 | for (FlowEntryAction flowEntryAction : flowEntryActions) { |
| 1771 | FlowEntryAction.ActionOutput actionOutput = |
| 1772 | flowEntryAction.actionOutput(); |
| 1773 | if (actionOutput != null) { |
| 1774 | short actionOutputPort = actionOutput.port().value(); |
| 1775 | OFActionOutput action = new OFActionOutput(); |
| 1776 | // XXX: The max length is hard-coded for now |
| 1777 | action.setMaxLength((short)0xffff); |
| 1778 | action.setPort(actionOutputPort); |
| 1779 | actions.add(action); |
| 1780 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 1781 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 1782 | fm.setOutPort(actionOutputPort); |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 1788 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 1789 | .setPriority(PRIORITY_DEFAULT) |
| 1790 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1791 | .setCookie(cookie) |
| 1792 | .setCommand(flowModCommand) |
| 1793 | .setMatch(match) |
| 1794 | .setActions(actions) |
| 1795 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 1796 | |
| 1797 | // |
| 1798 | // TODO: Set the following flag |
| 1799 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 1800 | // See method ForwardingBase::pushRoute() |
| 1801 | // |
| 1802 | |
| 1803 | // |
| 1804 | // Write the message to the switch |
| 1805 | // |
| 1806 | try { |
| 1807 | messageDamper.write(mySwitch, fm, null); |
| 1808 | mySwitch.flush(); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1809 | // |
| 1810 | // TODO: We should use the OpenFlow Barrier mechanism |
| 1811 | // to check for errors, and update the SwitchState |
| 1812 | // for a flow entry after the Barrier message is |
| 1813 | // is received. |
| 1814 | // |
| 1815 | // TODO: The FlowEntry Object in Titan should be set |
| 1816 | // to FE_SWITCH_UPDATED. |
| 1817 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1818 | } catch (IOException e) { |
| 1819 | log.error("Failure writing flow mod from network map", e); |
| 1820 | return false; |
| 1821 | } |
| 1822 | return true; |
| 1823 | } |
| 1824 | |
| 1825 | /** |
| 1826 | * Remove a Flow Entry from a switch. |
| 1827 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 1828 | * @param mySwitch the switch to remove the Flow Entry from. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1829 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1830 | * @param flowEntry the flow entry to remove. |
| 1831 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1832 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1833 | public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 1834 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1835 | // |
| 1836 | // The installFlowEntry() method implements both installation |
| 1837 | // and removal of flow entries. |
| 1838 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1839 | return (installFlowEntry(mySwitch, flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | /** |
| 1843 | * Install a Flow Entry on a remote controller. |
| 1844 | * |
| 1845 | * TODO: We need it now: Jono |
| 1846 | * - For now it will make a REST call to the remote controller. |
| 1847 | * - Internally, it needs to know the name of the remote controller. |
| 1848 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1849 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1850 | * @param flowEntry the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1851 | * @return true on success, otherwise false. |
| 1852 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1853 | public boolean installRemoteFlowEntry(FlowPath flowPath, |
| 1854 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1855 | // TODO: We need it now: Jono |
| 1856 | // - For now it will make a REST call to the remote controller. |
| 1857 | // - Internally, it needs to know the name of the remote controller. |
| 1858 | return true; |
| 1859 | } |
| 1860 | |
| 1861 | /** |
| 1862 | * Remove a flow entry on a remote controller. |
| 1863 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1864 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1865 | * @param flowEntry the flow entry to remove. |
| 1866 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1867 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1868 | public boolean removeRemoteFlowEntry(FlowPath flowPath, |
| 1869 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1870 | // |
| 1871 | // The installRemoteFlowEntry() method implements both installation |
| 1872 | // and removal of flow entries. |
| 1873 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1874 | return (installRemoteFlowEntry(flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1875 | } |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1876 | |
| 1877 | /** |
| 1878 | * Store a path flow for measurement purpose. |
| 1879 | * |
| 1880 | * NOTE: The Flow Path argument does NOT contain flow entries. |
| 1881 | * The Shortest Path is computed, and the corresponding Flow Entries |
| 1882 | * are stored in the Flow Path. |
| 1883 | * |
| 1884 | * @param flowPath the Flow Path with the endpoints and the match |
| 1885 | * conditions to store. |
| 1886 | * @return the stored shortest-path flow on success, otherwise null. |
| 1887 | */ |
| 1888 | @Override |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1889 | public synchronized FlowPath measurementStorePathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1890 | // |
| 1891 | // Prepare the Shortest Path computation if the first Flow Path |
| 1892 | // |
| 1893 | if (measurementStoredPaths.isEmpty()) |
| 1894 | topoRouteService.prepareShortestPathTopo(); |
| 1895 | |
| 1896 | // |
| 1897 | // Compute the Shortest Path |
| 1898 | // |
| 1899 | DataPath dataPath = |
| 1900 | topoRouteService.getTopoShortestPath(flowPath.dataPath().srcPort(), |
| 1901 | flowPath.dataPath().dstPort()); |
| 1902 | if (dataPath == null) { |
| 1903 | // We need the DataPath to populate the Network MAP |
| 1904 | dataPath = new DataPath(); |
| 1905 | dataPath.setSrcPort(flowPath.dataPath().srcPort()); |
| 1906 | dataPath.setDstPort(flowPath.dataPath().dstPort()); |
| 1907 | } |
| 1908 | |
| 1909 | // |
| 1910 | // Set the incoming port matching and the outgoing port output |
| 1911 | // actions for each flow entry. |
| 1912 | // |
| 1913 | for (FlowEntry flowEntry : dataPath.flowEntries()) { |
| 1914 | // Set the incoming port matching |
| 1915 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 1916 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 1917 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 1918 | |
| 1919 | // Set the outgoing port output action |
| 1920 | ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions(); |
| 1921 | if (flowEntryActions == null) { |
| 1922 | flowEntryActions = new ArrayList<FlowEntryAction>(); |
| 1923 | flowEntry.setFlowEntryActions(flowEntryActions); |
| 1924 | } |
| 1925 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 1926 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
| 1927 | flowEntryActions.add(flowEntryAction); |
| 1928 | } |
| 1929 | |
| 1930 | // |
| 1931 | // Prepare the computed Flow Path |
| 1932 | // |
| 1933 | FlowPath computedFlowPath = new FlowPath(); |
| 1934 | computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value())); |
| 1935 | computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value())); |
| 1936 | computedFlowPath.setDataPath(dataPath); |
| 1937 | computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch())); |
| 1938 | |
| 1939 | // |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1940 | // Add the computed Flow Path to the internal storage |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1941 | // |
| 1942 | measurementStoredPaths.add(computedFlowPath); |
| 1943 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1944 | log.debug("Measurement storing path {}", |
| 1945 | computedFlowPath.flowId().toString()); |
| 1946 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1947 | return (computedFlowPath); |
| 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * Install path flows for measurement purpose. |
| 1952 | * |
| 1953 | * @param numThreads the number of threads to use to install the path |
| 1954 | * flows. |
| 1955 | * @return true on success, otherwise false. |
| 1956 | */ |
| 1957 | @Override |
| 1958 | public boolean measurementInstallPaths(Integer numThreads) { |
| 1959 | List<Thread> threads = new LinkedList<Thread>(); |
| 1960 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1961 | // Create a copy of the Flow Paths to install |
| 1962 | final ConcurrentLinkedQueue<FlowPath> measurementProcessingPaths = |
| 1963 | new ConcurrentLinkedQueue<FlowPath>(measurementStoredPaths); |
| 1964 | |
| 1965 | log.debug("Measurement Installing {} flows", |
| 1966 | measurementProcessingPaths.size()); |
Pavlin Radoslavov | e0938f3 | 2013-05-07 23:17:22 +0000 | [diff] [blame] | 1967 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1968 | // |
| 1969 | // Create the threads to install the Flow Paths |
| 1970 | // |
| 1971 | for (int i = 0; i < numThreads; i++) { |
| 1972 | Thread thread = new Thread(new Runnable() { |
| 1973 | @Override |
| 1974 | public void run() { |
| 1975 | while (true) { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1976 | FlowPath flowPath = measurementProcessingPaths.poll(); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1977 | if (flowPath == null) |
| 1978 | return; |
| 1979 | // Install the Flow Path |
| 1980 | FlowId flowId = new FlowId(); |
| 1981 | String dataPathSummaryStr = |
| 1982 | flowPath.dataPath().dataPathSummary(); |
| 1983 | addFlow(flowPath, flowId, dataPathSummaryStr); |
| 1984 | } |
| 1985 | }}, "Measurement Add Flow Path"); |
| 1986 | threads.add(thread); |
| 1987 | } |
| 1988 | |
| 1989 | // |
| 1990 | // Start processing |
| 1991 | // |
| 1992 | measurementEndTimeProcessingPaths = 0; |
| 1993 | measurementStartTimeProcessingPaths = System.nanoTime(); |
| 1994 | for (Thread thread : threads) { |
| 1995 | thread.start(); |
| 1996 | } |
| 1997 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1998 | // Want for all threads to complete |
| 1999 | for (Thread thread : threads) { |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2000 | try { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 2001 | thread.join(); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2002 | } catch (InterruptedException e) { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 2003 | log.debug("Exception waiting for a thread to install a Flow Path: ", e); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 2007 | // Record the end of processing |
| 2008 | measurementEndTimeProcessingPaths = System.nanoTime(); |
| 2009 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2010 | return true; |
| 2011 | } |
| 2012 | |
| 2013 | /** |
| 2014 | * Get the measurement time that took to install the path flows. |
| 2015 | * |
| 2016 | * @return the measurement time (in nanoseconds) it took to install |
| 2017 | * the path flows. |
| 2018 | */ |
| 2019 | @Override |
| 2020 | public Long measurementGetInstallPathsTimeNsec() { |
| 2021 | return new Long(measurementEndTimeProcessingPaths - |
| 2022 | measurementStartTimeProcessingPaths); |
| 2023 | } |
| 2024 | |
| 2025 | /** |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2026 | * Clear the path flows stored for measurement purpose. |
| 2027 | * |
| 2028 | * @return true on success, otherwise false. |
| 2029 | */ |
| 2030 | @Override |
| 2031 | public boolean measurementClearAllPaths() { |
| 2032 | measurementStoredPaths.clear(); |
| 2033 | topoRouteService.dropShortestPathTopo(); |
Pavlin Radoslavov | e0938f3 | 2013-05-07 23:17:22 +0000 | [diff] [blame] | 2034 | measurementStartTimeProcessingPaths = 0; |
| 2035 | measurementEndTimeProcessingPaths = 0; |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2036 | |
| 2037 | return true; |
| 2038 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 2039 | } |