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