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