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