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