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