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 | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 6 | import java.util.EnumSet; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 7 | import java.util.HashMap; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 8 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 9 | import java.util.Map; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 10 | import java.util.TreeMap; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 11 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 12 | import java.util.concurrent.Executors; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 13 | import java.util.concurrent.LinkedBlockingQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 14 | import java.util.concurrent.ScheduledExecutorService; |
| 15 | import java.util.concurrent.ScheduledFuture; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 16 | import java.util.concurrent.ThreadPoolExecutor; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 17 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 18 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 19 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 20 | import net.floodlightcontroller.core.INetMapStorage; |
| 21 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 22 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 26 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 27 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 28 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 29 | import net.floodlightcontroller.flowcache.IFlowService; |
| 30 | import net.floodlightcontroller.flowcache.web.FlowWebRoutable; |
| 31 | import net.floodlightcontroller.restserver.IRestApiService; |
| 32 | import net.floodlightcontroller.util.CallerId; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 33 | import net.floodlightcontroller.util.DataPath; |
| 34 | import net.floodlightcontroller.util.Dpid; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 35 | import net.floodlightcontroller.util.DataPathEndpoints; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 36 | import net.floodlightcontroller.util.FlowEntry; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 37 | import net.floodlightcontroller.util.FlowEntryAction; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 38 | import net.floodlightcontroller.util.FlowEntryId; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 39 | import net.floodlightcontroller.util.FlowEntryMatch; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 40 | import net.floodlightcontroller.util.FlowEntrySwitchState; |
| 41 | import net.floodlightcontroller.util.FlowEntryUserState; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 42 | import net.floodlightcontroller.util.FlowId; |
| 43 | import net.floodlightcontroller.util.FlowPath; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 44 | import net.floodlightcontroller.util.IPv4Net; |
| 45 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 46 | import net.floodlightcontroller.util.OFMessageDamper; |
| 47 | import net.floodlightcontroller.util.Port; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 48 | import net.floodlightcontroller.util.SwitchPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 49 | import net.onrc.onos.util.GraphDBConnection; |
| 50 | import net.onrc.onos.util.GraphDBConnection.Transaction; |
| 51 | |
| 52 | import org.openflow.protocol.OFFlowMod; |
| 53 | import org.openflow.protocol.OFMatch; |
| 54 | import org.openflow.protocol.OFPacketOut; |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 55 | import org.openflow.protocol.OFPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 56 | import org.openflow.protocol.OFType; |
| 57 | import org.openflow.protocol.action.OFAction; |
| 58 | import org.openflow.protocol.action.OFActionOutput; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 59 | |
| 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
| 62 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 63 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
| 64 | |
| 65 | public GraphDBConnection conn; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 66 | |
| 67 | protected IRestApiService restApi; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 68 | protected IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 69 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 70 | |
| 71 | protected OFMessageDamper messageDamper; |
| 72 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 73 | // |
| 74 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 75 | // The local copy should go away! |
| 76 | // |
| 77 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 78 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
| 79 | public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity |
| 80 | public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite |
| 81 | public static final short PRIORITY_DEFAULT = 100; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 82 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 83 | private static long nextFlowEntryId = 1; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 84 | private static long measurementFlowId = 100000; |
| 85 | private static String measurementFlowIdStr = "0x186a0"; // 100000 |
| 86 | private long modifiedMeasurementFlowTime = 0; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 87 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 88 | /** The logger. */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 89 | private static Logger log = LoggerFactory.getLogger(FlowManager.class); |
| 90 | |
| 91 | // The periodic task(s) |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 92 | private final ScheduledExecutorService measureShortestPathScheduler = |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 93 | Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 94 | private final ScheduledExecutorService measureMapReaderScheduler = |
| 95 | Executors.newScheduledThreadPool(1); |
| 96 | private final ScheduledExecutorService mapReaderScheduler = |
| 97 | Executors.newScheduledThreadPool(1); |
| 98 | |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 99 | private BlockingQueue<Runnable> shortestPathQueue = new LinkedBlockingQueue<Runnable>(); |
| 100 | private ThreadPoolExecutor shortestPathExecutor = |
| 101 | new ThreadPoolExecutor(10, 10, 5, TimeUnit.SECONDS, shortestPathQueue); |
| 102 | |
| 103 | class ShortestPathTask implements Runnable { |
| 104 | private int hint; |
| 105 | private ITopoRouteService topoRouteService; |
| 106 | private ArrayList<DataPath> dpList; |
| 107 | |
| 108 | public ShortestPathTask(int hint, |
| 109 | ITopoRouteService topoRouteService, |
| 110 | ArrayList<DataPath> dpList) { |
| 111 | this.hint = hint; |
| 112 | this.topoRouteService = topoRouteService; |
| 113 | this.dpList = dpList; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void run() { |
| 118 | String logMsg = "MEASUREMENT: Running Thread hint " + this.hint; |
| 119 | log.debug(logMsg); |
| 120 | long startTime = System.nanoTime(); |
| 121 | for (DataPath dp : this.dpList) { |
| 122 | topoRouteService.getShortestPath(dp.srcPort(), dp.dstPort()); |
| 123 | } |
| 124 | long estimatedTime = System.nanoTime() - startTime; |
| 125 | double rate = (estimatedTime > 0)? ((double)dpList.size() * 1000000000) / estimatedTime: 0.0; |
| 126 | logMsg = "MEASUREMENT: Computed Thread hint " + hint + ": " + dpList.size() + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s"; |
| 127 | log.debug(logMsg); |
| 128 | } |
| 129 | } |
| 130 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 131 | final Runnable measureShortestPath = new Runnable() { |
| 132 | public void run() { |
| 133 | log.debug("Recomputing Shortest Paths from the Network Map Flows..."); |
| 134 | if (floodlightProvider == null) { |
| 135 | log.debug("FloodlightProvider service not found!"); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | ITopoRouteService topoRouteService = |
| 140 | context.getServiceImpl(ITopoRouteService.class); |
| 141 | if (topoRouteService == null) { |
| 142 | log.debug("Topology Route Service not found"); |
| 143 | return; |
| 144 | } |
| 145 | |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 146 | int leftoverQueueSize = shortestPathExecutor.getQueue().size(); |
| 147 | if (leftoverQueueSize > 0) { |
| 148 | String logMsg = "MEASUREMENT: Leftover Shortest Path Queue Size: " + leftoverQueueSize; |
| 149 | log.debug(logMsg); |
| 150 | return; |
| 151 | } |
| 152 | log.debug("MEASUREMENT: Beginning Shortest Path Computation"); |
| 153 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 154 | // |
| 155 | // Recompute the Shortest Paths for all Flows |
| 156 | // |
| 157 | int counter = 0; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 158 | int hint = 0; |
| 159 | ArrayList<DataPath> dpList = new ArrayList<DataPath>(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 160 | long startTime = System.nanoTime(); |
| 161 | Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn); |
| 162 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 163 | FlowId flowId = new FlowId(flowPathObj.getFlowId()); |
| 164 | |
| 165 | // log.debug("Found Path {}", flowId.toString()); |
| 166 | Dpid srcDpid = new Dpid(flowPathObj.getSrcSwitch()); |
| 167 | Port srcPort = new Port(flowPathObj.getSrcPort()); |
| 168 | Dpid dstDpid = new Dpid(flowPathObj.getDstSwitch()); |
| 169 | Port dstPort = new Port(flowPathObj.getDstPort()); |
| 170 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 171 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 172 | DataPath dp = new DataPath(); |
| 173 | dp.setSrcPort(srcSwitchPort); |
| 174 | dp.setDstPort(dstSwitchPort); |
| 175 | dpList.add(dp); |
| 176 | if ((dpList.size() % 10) == 0) { |
| 177 | shortestPathExecutor.execute( |
| 178 | new ShortestPathTask(hint, topoRouteService, |
| 179 | dpList)); |
| 180 | dpList = new ArrayList<DataPath>(); |
| 181 | hint++; |
| 182 | } |
| 183 | /* |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 184 | DataPath dataPath = |
| 185 | topoRouteService.getShortestPath(srcSwitchPort, |
| 186 | dstSwitchPort); |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame^] | 187 | */ |
| 188 | |
| 189 | /* |
| 190 | shortestPathExecutor.execute( |
| 191 | new ShortestPathTask(topoRouteService, |
| 192 | srcSwitchPort, |
| 193 | dstSwitchPort)); |
| 194 | */ |
| 195 | counter++; |
| 196 | } |
| 197 | if (dpList.size() > 0) { |
| 198 | shortestPathExecutor.execute( |
| 199 | new ShortestPathTask(hint, topoRouteService, |
| 200 | dpList)); |
| 201 | } |
| 202 | |
| 203 | // Wait for all tasks to finish |
| 204 | try { |
| 205 | while (shortestPathExecutor.getQueue().size() > 0) { |
| 206 | Thread.sleep(100); |
| 207 | } |
| 208 | } catch (InterruptedException ex) { |
| 209 | log.debug("MEASUREMENT: Shortest Path Computation interrupted"); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 210 | } |
| 211 | conn.endTx(Transaction.COMMIT); |
| 212 | |
| 213 | long estimatedTime = System.nanoTime() - startTime; |
| 214 | double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0; |
| 215 | String logMsg = "MEASUREMENT: Computed " + counter + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s"; |
| 216 | log.debug(logMsg); |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | final Runnable measureMapReader = new Runnable() { |
| 221 | public void run() { |
| 222 | if (floodlightProvider == null) { |
| 223 | log.debug("FloodlightProvider service not found!"); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // |
| 228 | // Fetch all Flow Entries |
| 229 | // |
| 230 | int counter = 0; |
| 231 | long startTime = System.nanoTime(); |
| 232 | Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn); |
| 233 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
| 234 | counter++; |
| 235 | FlowEntryId flowEntryId = |
| 236 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
| 237 | String userState = flowEntryObj.getUserState(); |
| 238 | String switchState = flowEntryObj.getSwitchState(); |
| 239 | } |
| 240 | conn.endTx(Transaction.COMMIT); |
| 241 | |
| 242 | long estimatedTime = System.nanoTime() - startTime; |
| 243 | double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0; |
| 244 | String logMsg = "MEASUREMENT: Fetched " + counter + " flow entries in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " entries/s"; |
| 245 | log.debug(logMsg); |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 250 | public void run() { |
| 251 | // log.debug("Reading Flow Entries from the Network Map..."); |
| 252 | if (floodlightProvider == null) { |
| 253 | log.debug("FloodlightProvider service not found!"); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches(); |
| 258 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 259 | Map<Long, IFlowEntry> myFlowEntries = new TreeMap<Long, IFlowEntry>(); |
| 260 | |
| 261 | // |
| 262 | // Fetch all Flow Entries and select only my Flow Entries |
| 263 | // |
| 264 | Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn); |
| 265 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 266 | FlowEntryId flowEntryId = |
| 267 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
Pavlin Radoslavov | 2f9d633 | 2013-03-18 23:05:48 -0700 | [diff] [blame] | 268 | String userState = flowEntryObj.getUserState(); |
| 269 | String switchState = flowEntryObj.getSwitchState(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 270 | |
Pavlin Radoslavov | 79a67c1 | 2013-03-15 21:05:53 -0700 | [diff] [blame] | 271 | log.debug("Found Flow Entry {}: {}", |
| 272 | flowEntryId.toString(), |
Pavlin Radoslavov | 2f9d633 | 2013-03-18 23:05:48 -0700 | [diff] [blame] | 273 | "User State: " + userState + |
| 274 | " Switch State: " + switchState); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 275 | |
| 276 | if (! switchState.equals("FE_SWITCH_NOT_UPDATED")) { |
| 277 | // Ignore the entry: nothing to do |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
| 282 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
| 283 | if (mySwitch == null) { |
Pavlin Radoslavov | 79a67c1 | 2013-03-15 21:05:53 -0700 | [diff] [blame] | 284 | log.debug("Flow Entry ignored: not my switch (FlowEntryId = {} DPID = {})", flowEntryId.toString(), dpid.toString()); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 285 | continue; |
| 286 | } |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 287 | myFlowEntries.put(flowEntryId.value(), flowEntryObj); |
| 288 | } |
| 289 | |
| 290 | // |
| 291 | // Process my Flow Entries |
| 292 | // |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 293 | Boolean processed_measurement_flow = false; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 294 | for (Map.Entry<Long, IFlowEntry> entry : myFlowEntries.entrySet()) { |
| 295 | IFlowEntry flowEntryObj = entry.getValue(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 296 | // Code for measurement purpose |
| 297 | { |
| 298 | IFlowPath flowObj = |
| 299 | conn.utils().getFlowPathByFlowEntry(conn, |
| 300 | flowEntryObj); |
| 301 | if ((flowObj != null) && |
| 302 | flowObj.getFlowId().equals(measurementFlowIdStr)) { |
| 303 | processed_measurement_flow = true; |
| 304 | } |
| 305 | } |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 306 | |
| 307 | // |
| 308 | // TODO: Eliminate the re-fetching of flowEntryId, |
| 309 | // userState, switchState, and dpid from the flowEntryObj. |
| 310 | // |
| 311 | FlowEntryId flowEntryId = |
| 312 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
| 313 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
| 314 | String userState = flowEntryObj.getUserState(); |
| 315 | String switchState = flowEntryObj.getSwitchState(); |
| 316 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
| 317 | if (mySwitch == null) { |
| 318 | log.debug("Flow Entry ignored: not my switch"); |
| 319 | continue; |
| 320 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 321 | |
| 322 | // |
| 323 | // Create the Open Flow Flow Modification Entry to push |
| 324 | // |
| 325 | OFFlowMod fm = |
| 326 | (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 327 | .getMessage(OFType.FLOW_MOD); |
| 328 | long cookie = flowEntryId.value(); |
| 329 | |
| 330 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 331 | if (userState.equals("FE_USER_ADD")) { |
| 332 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 333 | } else if (userState.equals("FE_USER_MODIFY")) { |
| 334 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 335 | } else if (userState.equals("FE_USER_DELETE")) { |
| 336 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 337 | } else { |
| 338 | // Unknown user state. Ignore the entry |
Pavlin Radoslavov | 2f9d633 | 2013-03-18 23:05:48 -0700 | [diff] [blame] | 339 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 340 | flowEntryId.toString(), userState); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 341 | continue; |
| 342 | } |
| 343 | |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 344 | // |
| 345 | // Fetch the match conditions |
| 346 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 347 | OFMatch match = new OFMatch(); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 348 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 349 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 350 | if (matchInPort != null) { |
| 351 | match.setInputPort(matchInPort); |
| 352 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 353 | } |
| 354 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 355 | if (matchEthernetFrameType != null) { |
| 356 | match.setDataLayerType(matchEthernetFrameType); |
| 357 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 358 | } |
| 359 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 360 | if (matchSrcIPv4Net != null) { |
| 361 | match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC); |
| 362 | } |
| 363 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 364 | if (matchDstIPv4Net != null) { |
| 365 | match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST); |
| 366 | } |
| 367 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 368 | if (matchSrcMac != null) { |
| 369 | match.setDataLayerSource(matchSrcMac); |
| 370 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 371 | } |
| 372 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 373 | if (matchDstMac != null) { |
| 374 | match.setDataLayerDestination(matchDstMac); |
| 375 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 376 | } |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 377 | |
| 378 | // |
| 379 | // Fetch the actions |
| 380 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 381 | List<OFAction> actions = new ArrayList<OFAction>(); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 382 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 383 | if (actionOutputPort != null) { |
| 384 | OFActionOutput action = new OFActionOutput(); |
| 385 | // XXX: The max length is hard-coded for now |
| 386 | action.setMaxLength((short)0xffff); |
| 387 | action.setPort(actionOutputPort); |
| 388 | actions.add(action); |
| 389 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 390 | |
| 391 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 392 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 393 | .setPriority(PRIORITY_DEFAULT) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 394 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 395 | .setCookie(cookie) |
| 396 | .setCommand(flowModCommand) |
| 397 | .setMatch(match) |
| 398 | .setActions(actions) |
| 399 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 400 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 401 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 402 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 403 | if (actionOutputPort != null) |
| 404 | fm.setOutPort(actionOutputPort); |
| 405 | } |
| 406 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 407 | // |
| 408 | // TODO: Set the following flag |
| 409 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 410 | // See method ForwardingBase::pushRoute() |
| 411 | // |
| 412 | try { |
| 413 | messageDamper.write(mySwitch, fm, null); |
| 414 | mySwitch.flush(); |
| 415 | flowEntryObj.setSwitchState("FE_SWITCH_UPDATED"); |
| 416 | if (userState.equals("FE_USER_DELETE")) { |
| 417 | // Delete the entry |
| 418 | IFlowPath flowObj = null; |
| 419 | flowObj = conn.utils().getFlowPathByFlowEntry(conn, |
| 420 | flowEntryObj); |
| 421 | if (flowObj != null) |
| 422 | log.debug("Found FlowPath to be deleted"); |
| 423 | else |
| 424 | log.debug("Did not find FlowPath to be deleted"); |
| 425 | flowObj.removeFlowEntry(flowEntryObj); |
| 426 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 427 | |
| 428 | // Test whether the last flow entry |
| 429 | Iterable<IFlowEntry> tmpflowEntries = |
| 430 | flowObj.getFlowEntries(); |
| 431 | boolean found = false; |
| 432 | for (IFlowEntry tmpflowEntryObj : tmpflowEntries) { |
| 433 | found = true; |
| 434 | break; |
| 435 | } |
| 436 | if (! found) { |
| 437 | // Remove the Flow Path as well |
| 438 | conn.utils().removeFlowPath(conn, flowObj); |
| 439 | } |
| 440 | } |
| 441 | } catch (IOException e) { |
| 442 | log.error("Failure writing flow mod from network map", e); |
| 443 | } |
| 444 | } |
| 445 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 446 | |
| 447 | if (processed_measurement_flow) { |
| 448 | long estimatedTime = System.nanoTime() - modifiedMeasurementFlowTime; |
| 449 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 450 | (double)estimatedTime / 1000000000 + " sec"; |
| 451 | log.debug(logMsg); |
| 452 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 453 | } |
| 454 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | final ScheduledFuture<?> measureShortestPathHandle = |
| 458 | measureShortestPathScheduler.scheduleAtFixedRate(measureShortestPath, 10, 10, TimeUnit.SECONDS); |
| 459 | */ |
| 460 | |
| 461 | /* |
| 462 | final ScheduledFuture<?> measureMapReaderHandle = |
| 463 | measureMapReaderScheduler.scheduleAtFixedRate(measureMapReader, 10, 10, TimeUnit.SECONDS); |
| 464 | */ |
| 465 | |
| 466 | final ScheduledFuture<?> mapReaderHandle = |
| 467 | mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 468 | |
| 469 | @Override |
| 470 | public void init(String conf) { |
| 471 | conn = GraphDBConnection.getInstance(conf); |
| 472 | } |
| 473 | |
| 474 | public void finalize() { |
| 475 | close(); |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public void close() { |
| 480 | conn.close(); |
| 481 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 482 | |
| 483 | @Override |
| 484 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 485 | Collection<Class<? extends IFloodlightService>> l = |
| 486 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 487 | l.add(IFlowService.class); |
| 488 | return l; |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 493 | getServiceImpls() { |
| 494 | Map<Class<? extends IFloodlightService>, |
| 495 | IFloodlightService> m = |
| 496 | new HashMap<Class<? extends IFloodlightService>, |
| 497 | IFloodlightService>(); |
| 498 | m.put(IFlowService.class, this); |
| 499 | return m; |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public Collection<Class<? extends IFloodlightService>> |
| 504 | getModuleDependencies() { |
| 505 | Collection<Class<? extends IFloodlightService>> l = |
| 506 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 507 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 508 | l.add(IRestApiService.class); |
| 509 | return l; |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void init(FloodlightModuleContext context) |
| 514 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 515 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 516 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 517 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 518 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 519 | EnumSet.of(OFType.FLOW_MOD), |
| 520 | OFMESSAGE_DAMPER_TIMEOUT); |
| 521 | // TODO: An ugly hack! |
| 522 | String conf = "/tmp/cassandra.titan"; |
| 523 | this.init(conf); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public void startUp(FloodlightModuleContext context) { |
| 528 | restApi.addRestletRoutable(new FlowWebRoutable()); |
Pavlin Radoslavov | 80ca630 | 2013-03-20 02:08:09 -0700 | [diff] [blame] | 529 | |
| 530 | // |
| 531 | // Extract all flow entries and assign the next Flow Entry ID |
| 532 | // to be larger than the largest Flow Entry ID |
| 533 | // |
| 534 | Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn); |
| 535 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
| 536 | FlowEntryId flowEntryId = |
| 537 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
| 538 | if (flowEntryId.value() >= nextFlowEntryId) |
| 539 | nextFlowEntryId = flowEntryId.value() + 1; |
| 540 | } |
| 541 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Add a flow. |
| 546 | * |
| 547 | * Internally, ONOS will automatically register the installer for |
| 548 | * receiving Flow Path Notifications for that path. |
| 549 | * |
| 550 | * @param flowPath the Flow Path to install. |
| 551 | * @param flowId the return-by-reference Flow ID as assigned internally. |
| 552 | * @return true on success, otherwise false. |
| 553 | */ |
| 554 | @Override |
| 555 | public boolean addFlow(FlowPath flowPath, FlowId flowId) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 556 | if (flowPath.flowId().value() == measurementFlowId) { |
| 557 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 558 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 559 | |
| 560 | // |
| 561 | // Assign the FlowEntry IDs |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 562 | // Right now every new flow entry gets a new flow entry ID |
| 563 | // TODO: This needs to be redesigned! |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 564 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 565 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 566 | long id = nextFlowEntryId++; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 567 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 568 | } |
| 569 | |
| 570 | IFlowPath flowObj = null; |
| 571 | try { |
| 572 | if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId())) |
| 573 | != null) { |
| 574 | log.debug("Adding FlowPath with FlowId {}: found existing FlowPath", |
| 575 | flowPath.flowId().toString()); |
| 576 | } else { |
| 577 | flowObj = conn.utils().newFlowPath(conn); |
| 578 | log.debug("Adding FlowPath with FlowId {}: creating new FlowPath", |
| 579 | flowPath.flowId().toString()); |
| 580 | } |
| 581 | } catch (Exception e) { |
| 582 | // TODO: handle exceptions |
| 583 | conn.endTx(Transaction.ROLLBACK); |
| 584 | log.error(":addFlow FlowId:{} failed", |
| 585 | flowPath.flowId().toString()); |
| 586 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 587 | if (flowObj == null) { |
| 588 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 589 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 590 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 591 | |
| 592 | // |
| 593 | // Set the Flow key: |
| 594 | // - flowId |
| 595 | // |
| 596 | flowObj.setFlowId(flowPath.flowId().toString()); |
| 597 | flowObj.setType("flow"); |
| 598 | |
| 599 | // |
| 600 | // Set the Flow attributes: |
| 601 | // - flowPath.installerId() |
| 602 | // - flowPath.dataPath().srcPort() |
| 603 | // - flowPath.dataPath().dstPort() |
| 604 | // |
| 605 | flowObj.setInstallerId(flowPath.installerId().toString()); |
| 606 | flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString()); |
| 607 | flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value()); |
| 608 | flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString()); |
| 609 | flowObj.setDstPort(flowPath.dataPath().dstPort().port().value()); |
| 610 | |
| 611 | // Flow edges: |
| 612 | // HeadFE |
| 613 | |
| 614 | |
| 615 | // |
| 616 | // Flow Entries: |
| 617 | // flowPath.dataPath().flowEntries() |
| 618 | // |
| 619 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
| 620 | IFlowEntry flowEntryObj = null; |
| 621 | boolean found = false; |
| 622 | try { |
| 623 | if ((flowEntryObj = conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) { |
| 624 | log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry", |
| 625 | flowEntry.flowEntryId().toString()); |
| 626 | found = true; |
| 627 | } else { |
| 628 | flowEntryObj = conn.utils().newFlowEntry(conn); |
| 629 | log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry", |
| 630 | flowEntry.flowEntryId().toString()); |
| 631 | } |
| 632 | } catch (Exception e) { |
| 633 | // TODO: handle exceptions |
| 634 | conn.endTx(Transaction.ROLLBACK); |
| 635 | log.error(":addFlow FlowEntryId:{} failed", |
| 636 | flowEntry.flowEntryId().toString()); |
| 637 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 638 | if (flowEntryObj == null) { |
| 639 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 640 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 641 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 642 | |
| 643 | // |
| 644 | // Set the Flow Entry key: |
| 645 | // - flowEntry.flowEntryId() |
| 646 | // |
| 647 | flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString()); |
| 648 | flowEntryObj.setType("flow_entry"); |
| 649 | |
| 650 | // |
| 651 | // Set the Flow Entry attributes: |
| 652 | // - flowEntry.flowEntryMatch() |
| 653 | // - flowEntry.flowEntryActions() |
| 654 | // - flowEntry.dpid() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 655 | // - flowEntry.flowEntryUserState() |
| 656 | // - flowEntry.flowEntrySwitchState() |
| 657 | // - flowEntry.flowEntryErrorState() |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 658 | // - flowEntry.matchInPort() |
| 659 | // - flowEntry.matchEthernetFrameType() |
| 660 | // - flowEntry.matchSrcIPv4Net() |
| 661 | // - flowEntry.matchDstIPv4Net() |
| 662 | // - flowEntry.matchSrcMac() |
| 663 | // - flowEntry.matchDstMac() |
| 664 | // - flowEntry.actionOutput() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 665 | // |
| 666 | flowEntryObj.setSwitchDpid(flowEntry.dpid().toString()); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 667 | if (flowEntry.flowEntryMatch().matchInPort()) |
| 668 | flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value()); |
| 669 | if (flowEntry.flowEntryMatch().matchEthernetFrameType()) |
| 670 | flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType()); |
| 671 | if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) |
| 672 | flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString()); |
| 673 | if (flowEntry.flowEntryMatch().matchDstIPv4Net()) |
| 674 | flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString()); |
| 675 | if (flowEntry.flowEntryMatch().matchSrcMac()) |
| 676 | flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString()); |
| 677 | if (flowEntry.flowEntryMatch().matchDstMac()) |
| 678 | flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString()); |
| 679 | |
| 680 | for (FlowEntryAction fa : flowEntry.flowEntryActions()) { |
| 681 | if (fa.actionOutput() != null) |
| 682 | flowEntryObj.setActionOutput(fa.actionOutput().port().value()); |
| 683 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 684 | // TODO: Hacks with hard-coded state names! |
| 685 | if (found) |
| 686 | flowEntryObj.setUserState("FE_USER_MODIFY"); |
| 687 | else |
| 688 | flowEntryObj.setUserState("FE_USER_ADD"); |
| 689 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 690 | // |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 691 | // TODO: Take care of the FlowEntryMatch, FlowEntryAction set, |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 692 | // and FlowEntryErrorState. |
| 693 | // |
| 694 | |
| 695 | // Flow Entries edges: |
| 696 | // Flow |
| 697 | // NextFE |
| 698 | // InPort |
| 699 | // OutPort |
| 700 | // Switch |
| 701 | if (! found) |
| 702 | flowObj.addFlowEntry(flowEntryObj); |
| 703 | } |
| 704 | conn.endTx(Transaction.COMMIT); |
| 705 | |
| 706 | // |
| 707 | // TODO: We need a proper Flow ID allocation mechanism. |
| 708 | // |
| 709 | flowId.setValue(flowPath.flowId().value()); |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 710 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 711 | return true; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Delete a previously added flow. |
| 716 | * |
| 717 | * @param flowId the Flow ID of the flow to delete. |
| 718 | * @return true on success, otherwise false. |
| 719 | */ |
| 720 | @Override |
| 721 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 722 | if (flowId.value() == measurementFlowId) { |
| 723 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 724 | } |
| 725 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 726 | IFlowPath flowObj = null; |
| 727 | // |
| 728 | // We just mark the entries for deletion, |
| 729 | // and let the switches remove each individual entry after |
| 730 | // it has been removed from the switches. |
| 731 | // |
| 732 | try { |
| 733 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 734 | != null) { |
| 735 | log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath", |
| 736 | flowId.toString()); |
| 737 | } else { |
| 738 | log.debug("Deleting FlowPath with FlowId {}: FlowPath not found", |
| 739 | flowId.toString()); |
| 740 | } |
| 741 | } catch (Exception e) { |
| 742 | // TODO: handle exceptions |
| 743 | conn.endTx(Transaction.ROLLBACK); |
| 744 | log.error(":deleteFlow FlowId:{} failed", flowId.toString()); |
| 745 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 746 | if (flowObj == null) { |
| 747 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 748 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 749 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 750 | |
| 751 | // |
| 752 | // Find and mark for deletion all Flow Entries |
| 753 | // |
| 754 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 755 | boolean empty = true; // TODO: an ugly hack |
| 756 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 757 | empty = false; |
| 758 | // flowObj.removeFlowEntry(flowEntryObj); |
| 759 | // conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 760 | flowEntryObj.setUserState("FE_USER_DELETE"); |
| 761 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 762 | } |
| 763 | // Remove from the database empty flows |
| 764 | if (empty) |
| 765 | conn.utils().removeFlowPath(conn, flowObj); |
| 766 | conn.endTx(Transaction.COMMIT); |
| 767 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 768 | return true; |
| 769 | } |
| 770 | |
| 771 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 772 | * Clear the state for a previously added flow. |
| 773 | * |
| 774 | * @param flowId the Flow ID of the flow to clear. |
| 775 | * @return true on success, otherwise false. |
| 776 | */ |
| 777 | @Override |
| 778 | public boolean clearFlow(FlowId flowId) { |
| 779 | IFlowPath flowObj = null; |
| 780 | try { |
| 781 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 782 | != null) { |
| 783 | log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath", |
| 784 | flowId.toString()); |
| 785 | } else { |
| 786 | log.debug("Clearing FlowPath with FlowId {}: FlowPath not found", |
| 787 | flowId.toString()); |
| 788 | } |
| 789 | } catch (Exception e) { |
| 790 | // TODO: handle exceptions |
| 791 | conn.endTx(Transaction.ROLLBACK); |
| 792 | log.error(":clearFlow FlowId:{} failed", flowId.toString()); |
| 793 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 794 | if (flowObj == null) { |
| 795 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 796 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 797 | } |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 798 | |
| 799 | // |
| 800 | // Remove all Flow Entries |
| 801 | // |
| 802 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 803 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 804 | flowObj.removeFlowEntry(flowEntryObj); |
| 805 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 806 | } |
| 807 | // Remove the Flow itself |
| 808 | conn.utils().removeFlowPath(conn, flowObj); |
| 809 | conn.endTx(Transaction.COMMIT); |
| 810 | |
| 811 | return true; |
| 812 | } |
| 813 | |
| 814 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 815 | * Get a previously added flow. |
| 816 | * |
| 817 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 818 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 819 | */ |
| 820 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 821 | public FlowPath getFlow(FlowId flowId) { |
| 822 | IFlowPath flowObj = null; |
| 823 | try { |
| 824 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 825 | != null) { |
| 826 | log.debug("Get FlowPath with FlowId {}: found existing FlowPath", |
| 827 | flowId.toString()); |
| 828 | } else { |
| 829 | log.debug("Get FlowPath with FlowId {}: FlowPath not found", |
| 830 | flowId.toString()); |
| 831 | } |
| 832 | } catch (Exception e) { |
| 833 | // TODO: handle exceptions |
| 834 | conn.endTx(Transaction.ROLLBACK); |
| 835 | log.error(":getFlow FlowId:{} failed", flowId.toString()); |
| 836 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 837 | if (flowObj == null) { |
| 838 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 839 | return null; // Flow not found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 840 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 841 | |
| 842 | // |
| 843 | // Extract the Flow state |
| 844 | // |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 845 | FlowPath flowPath = extractFlowPath(flowObj); |
| 846 | conn.endTx(Transaction.COMMIT); |
| 847 | |
| 848 | return flowPath; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Get all previously added flows by a specific installer for a given |
| 853 | * data path endpoints. |
| 854 | * |
| 855 | * @param installerId the Caller ID of the installer of the flow to get. |
| 856 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 857 | * @return the Flow Paths if found, otherwise null. |
| 858 | */ |
| 859 | @Override |
| 860 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 861 | DataPathEndpoints dataPathEndpoints) { |
| 862 | // |
| 863 | // TODO: The implementation below is not optimal: |
| 864 | // We fetch all flows, and then return only the subset that match |
| 865 | // the query conditions. |
| 866 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 867 | // the flows as appropriate. |
| 868 | // |
| 869 | ArrayList<FlowPath> allFlows = getAllFlows(); |
| 870 | |
| 871 | if (allFlows == null) { |
| 872 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
| 873 | return null; |
| 874 | } |
| 875 | |
| 876 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 877 | for (FlowPath flow : allFlows) { |
| 878 | // |
| 879 | // TODO: String-based comparison is sub-optimal. |
| 880 | // 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] | 881 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 882 | // |
| 883 | if (! flow.installerId().toString().equals(installerId.toString())) |
| 884 | continue; |
| 885 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 886 | continue; |
| 887 | } |
| 888 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 889 | continue; |
| 890 | } |
| 891 | flowPaths.add(flow); |
| 892 | } |
| 893 | |
| 894 | if (flowPaths.isEmpty()) { |
| 895 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
| 896 | flowPaths = null; |
| 897 | } else { |
| 898 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints); |
| 899 | } |
| 900 | |
| 901 | return flowPaths; |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * Get all installed flows by all installers for given data path endpoints. |
| 906 | * |
| 907 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 908 | * @return the Flow Paths if found, otherwise null. |
| 909 | */ |
| 910 | @Override |
| 911 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
| 912 | // |
| 913 | // TODO: The implementation below is not optimal: |
| 914 | // We fetch all flows, and then return only the subset that match |
| 915 | // the query conditions. |
| 916 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 917 | // the flows as appropriate. |
| 918 | // |
| 919 | ArrayList<FlowPath> allFlows = getAllFlows(); |
| 920 | |
| 921 | if (allFlows == null) { |
| 922 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
| 923 | return null; |
| 924 | } |
| 925 | |
| 926 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 927 | for (FlowPath flow : allFlows) { |
| 928 | // |
| 929 | // TODO: String-based comparison is sub-optimal. |
| 930 | // 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] | 931 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 932 | // |
| 933 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 934 | continue; |
| 935 | } |
| 936 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 937 | continue; |
| 938 | } |
| 939 | flowPaths.add(flow); |
| 940 | } |
| 941 | |
| 942 | if (flowPaths.isEmpty()) { |
| 943 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
| 944 | flowPaths = null; |
| 945 | } else { |
| 946 | log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints); |
| 947 | } |
| 948 | |
| 949 | return flowPaths; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Get all installed flows by all installers. |
| 954 | * |
| 955 | * @return the Flow Paths if found, otherwise null. |
| 956 | */ |
| 957 | @Override |
| 958 | public ArrayList<FlowPath> getAllFlows() { |
| 959 | Iterable<IFlowPath> flowPathsObj = null; |
| 960 | |
| 961 | try { |
| 962 | if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) { |
| 963 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 964 | } else { |
| 965 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 966 | } |
| 967 | } catch (Exception e) { |
| 968 | // TODO: handle exceptions |
| 969 | conn.endTx(Transaction.ROLLBACK); |
| 970 | log.error(":getAllFlowPaths failed"); |
| 971 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 972 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 973 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 974 | return null; // No Flows found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 975 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 976 | |
| 977 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 978 | for (IFlowPath flowObj : flowPathsObj) { |
| 979 | // |
| 980 | // Extract the Flow state |
| 981 | // |
| 982 | FlowPath flowPath = extractFlowPath(flowObj); |
| 983 | flowPaths.add(flowPath); |
| 984 | } |
| 985 | |
| 986 | conn.endTx(Transaction.COMMIT); |
| 987 | |
| 988 | return flowPaths; |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * Extract Flow Path State from a Titan Database Object @ref IFlowPath. |
| 993 | * |
| 994 | * @param flowObj the object to extract the Flow Path State from. |
| 995 | * @return the extracted Flow Path State. |
| 996 | */ |
| 997 | private FlowPath extractFlowPath(IFlowPath flowObj) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 998 | FlowPath flowPath = new FlowPath(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 999 | |
| 1000 | // |
| 1001 | // Extract the Flow state |
| 1002 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1003 | flowPath.setFlowId(new FlowId(flowObj.getFlowId())); |
| 1004 | flowPath.setInstallerId(new CallerId(flowObj.getInstallerId())); |
| 1005 | flowPath.dataPath().srcPort().setDpid(new Dpid(flowObj.getSrcSwitch())); |
| 1006 | flowPath.dataPath().srcPort().setPort(new Port(flowObj.getSrcPort())); |
| 1007 | flowPath.dataPath().dstPort().setDpid(new Dpid(flowObj.getDstSwitch())); |
| 1008 | flowPath.dataPath().dstPort().setPort(new Port(flowObj.getDstPort())); |
| 1009 | |
| 1010 | // |
| 1011 | // Extract all Flow Entries |
| 1012 | // |
| 1013 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1014 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 1015 | FlowEntry flowEntry = new FlowEntry(); |
| 1016 | flowEntry.setFlowEntryId(new FlowEntryId(flowEntryObj.getFlowEntryId())); |
| 1017 | flowEntry.setDpid(new Dpid(flowEntryObj.getSwitchDpid())); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 1018 | |
| 1019 | // |
| 1020 | // Extract the match conditions |
| 1021 | // |
| 1022 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1023 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1024 | if (matchInPort != null) |
| 1025 | match.enableInPort(new Port(matchInPort)); |
| 1026 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1027 | if (matchEthernetFrameType != null) |
| 1028 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1029 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1030 | if (matchSrcIPv4Net != null) |
| 1031 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1032 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1033 | if (matchDstIPv4Net != null) |
| 1034 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1035 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1036 | if (matchSrcMac != null) |
| 1037 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1038 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1039 | if (matchDstMac != null) |
| 1040 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1041 | flowEntry.setFlowEntryMatch(match); |
| 1042 | |
| 1043 | // |
| 1044 | // Extract the actions |
| 1045 | // |
| 1046 | ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>(); |
| 1047 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1048 | if (actionOutputPort != null) { |
| 1049 | FlowEntryAction action = new FlowEntryAction(); |
| 1050 | action.setActionOutput(new Port(actionOutputPort)); |
| 1051 | actions.add(action); |
| 1052 | } |
| 1053 | flowEntry.setFlowEntryActions(actions); |
| 1054 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1055 | String userState = flowEntryObj.getUserState(); |
| 1056 | flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState)); |
| 1057 | String switchState = flowEntryObj.getSwitchState(); |
| 1058 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState)); |
| 1059 | // |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 1060 | // TODO: Take care of the FlowEntryMatch, FlowEntryAction set, |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1061 | // and FlowEntryErrorState. |
| 1062 | // |
| 1063 | flowPath.dataPath().flowEntries().add(flowEntry); |
| 1064 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1065 | |
| 1066 | return flowPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1067 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1068 | } |