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