Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.flowcache; |
| 2 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 6 | import java.util.EnumSet; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 7 | import java.util.HashMap; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 8 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 9 | import java.util.Map; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 10 | import java.util.TreeMap; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 11 | import java.util.Collections; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 12 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 13 | import java.util.concurrent.Executors; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 14 | import java.util.concurrent.LinkedBlockingQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 15 | import java.util.concurrent.ScheduledExecutorService; |
| 16 | import java.util.concurrent.ScheduledFuture; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 17 | import java.util.concurrent.ThreadPoolExecutor; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 18 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 19 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 20 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 21 | import net.floodlightcontroller.core.INetMapStorage; |
| 22 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 23 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 26 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 27 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 28 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 29 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 30 | import net.floodlightcontroller.flowcache.IFlowService; |
| 31 | import net.floodlightcontroller.flowcache.web.FlowWebRoutable; |
| 32 | import net.floodlightcontroller.restserver.IRestApiService; |
| 33 | import net.floodlightcontroller.util.CallerId; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 34 | import net.floodlightcontroller.util.DataPath; |
| 35 | import net.floodlightcontroller.util.Dpid; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 36 | import net.floodlightcontroller.util.DataPathEndpoints; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 37 | import net.floodlightcontroller.util.FlowEntry; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 38 | import net.floodlightcontroller.util.FlowEntryAction; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 39 | import net.floodlightcontroller.util.FlowEntryId; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 40 | import net.floodlightcontroller.util.FlowEntryMatch; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 41 | import net.floodlightcontroller.util.FlowEntrySwitchState; |
| 42 | import net.floodlightcontroller.util.FlowEntryUserState; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 43 | import net.floodlightcontroller.util.FlowId; |
| 44 | import net.floodlightcontroller.util.FlowPath; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 45 | import net.floodlightcontroller.util.IPv4Net; |
| 46 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 47 | import net.floodlightcontroller.util.OFMessageDamper; |
| 48 | import net.floodlightcontroller.util.Port; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 49 | import net.floodlightcontroller.util.SwitchPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 50 | import net.onrc.onos.util.GraphDBConnection; |
| 51 | import net.onrc.onos.util.GraphDBConnection.Transaction; |
| 52 | |
| 53 | import org.openflow.protocol.OFFlowMod; |
| 54 | import org.openflow.protocol.OFMatch; |
| 55 | import org.openflow.protocol.OFPacketOut; |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 56 | import org.openflow.protocol.OFPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 57 | import org.openflow.protocol.OFType; |
| 58 | import org.openflow.protocol.action.OFAction; |
| 59 | import org.openflow.protocol.action.OFActionOutput; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 60 | |
| 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 64 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
| 65 | |
| 66 | public GraphDBConnection conn; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 67 | |
| 68 | protected IRestApiService restApi; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 69 | protected IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 70 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 71 | |
| 72 | protected OFMessageDamper messageDamper; |
| 73 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 74 | // |
| 75 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 76 | // The local copy should go away! |
| 77 | // |
| 78 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 79 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
| 80 | public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity |
| 81 | public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite |
| 82 | public static final short PRIORITY_DEFAULT = 100; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 83 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 84 | private static long nextFlowEntryId = 1; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 85 | private static long measurementFlowId = 100000; |
| 86 | private static String measurementFlowIdStr = "0x186a0"; // 100000 |
| 87 | private long modifiedMeasurementFlowTime = 0; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 88 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 89 | /** The logger. */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 90 | private static Logger log = LoggerFactory.getLogger(FlowManager.class); |
| 91 | |
| 92 | // The periodic task(s) |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 93 | private final ScheduledExecutorService measureShortestPathScheduler = |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 94 | Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 95 | private final ScheduledExecutorService measureMapReaderScheduler = |
| 96 | Executors.newScheduledThreadPool(1); |
| 97 | private final ScheduledExecutorService mapReaderScheduler = |
| 98 | Executors.newScheduledThreadPool(1); |
| 99 | |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 100 | private BlockingQueue<Runnable> shortestPathQueue = new LinkedBlockingQueue<Runnable>(); |
| 101 | private ThreadPoolExecutor shortestPathExecutor = |
| 102 | new ThreadPoolExecutor(10, 10, 5, TimeUnit.SECONDS, shortestPathQueue); |
| 103 | |
| 104 | class ShortestPathTask implements Runnable { |
| 105 | private int hint; |
| 106 | private ITopoRouteService topoRouteService; |
| 107 | private ArrayList<DataPath> dpList; |
| 108 | |
| 109 | public ShortestPathTask(int hint, |
| 110 | ITopoRouteService topoRouteService, |
| 111 | ArrayList<DataPath> dpList) { |
| 112 | this.hint = hint; |
| 113 | this.topoRouteService = topoRouteService; |
| 114 | this.dpList = dpList; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void run() { |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 119 | /* |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 120 | String logMsg = "MEASUREMENT: Running Thread hint " + this.hint; |
| 121 | log.debug(logMsg); |
| 122 | long startTime = System.nanoTime(); |
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 | for (DataPath dp : this.dpList) { |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 125 | topoRouteService.getTopoShortestPath(dp.srcPort(), dp.dstPort()); |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 126 | } |
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 | long estimatedTime = System.nanoTime() - startTime; |
| 129 | double rate = (estimatedTime > 0)? ((double)dpList.size() * 1000000000) / estimatedTime: 0.0; |
| 130 | logMsg = "MEASUREMENT: Computed Thread hint " + hint + ": " + dpList.size() + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s"; |
| 131 | log.debug(logMsg); |
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 | } |
| 134 | } |
| 135 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 136 | final Runnable measureShortestPath = new Runnable() { |
| 137 | public void run() { |
| 138 | log.debug("Recomputing Shortest Paths from the Network Map Flows..."); |
| 139 | if (floodlightProvider == null) { |
| 140 | log.debug("FloodlightProvider service not found!"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | ITopoRouteService topoRouteService = |
| 145 | context.getServiceImpl(ITopoRouteService.class); |
| 146 | if (topoRouteService == null) { |
| 147 | log.debug("Topology Route Service not found"); |
| 148 | return; |
| 149 | } |
| 150 | |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 151 | int leftoverQueueSize = shortestPathExecutor.getQueue().size(); |
| 152 | if (leftoverQueueSize > 0) { |
| 153 | String logMsg = "MEASUREMENT: Leftover Shortest Path Queue Size: " + leftoverQueueSize; |
| 154 | log.debug(logMsg); |
| 155 | return; |
| 156 | } |
| 157 | log.debug("MEASUREMENT: Beginning Shortest Path Computation"); |
| 158 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 159 | // |
| 160 | // Recompute the Shortest Paths for all Flows |
| 161 | // |
| 162 | int counter = 0; |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 163 | int hint = 0; |
| 164 | ArrayList<DataPath> dpList = new ArrayList<DataPath>(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 165 | long startTime = System.nanoTime(); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 166 | |
| 167 | topoRouteService.prepareShortestPathTopo(); |
| 168 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 169 | Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn); |
| 170 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 171 | FlowId flowId = new FlowId(flowPathObj.getFlowId()); |
| 172 | |
| 173 | // log.debug("Found Path {}", flowId.toString()); |
| 174 | Dpid srcDpid = new Dpid(flowPathObj.getSrcSwitch()); |
| 175 | Port srcPort = new Port(flowPathObj.getSrcPort()); |
| 176 | Dpid dstDpid = new Dpid(flowPathObj.getDstSwitch()); |
| 177 | Port dstPort = new Port(flowPathObj.getDstPort()); |
| 178 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 179 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 182 | DataPath dp = new DataPath(); |
| 183 | dp.setSrcPort(srcSwitchPort); |
| 184 | dp.setDstPort(dstSwitchPort); |
| 185 | dpList.add(dp); |
| 186 | if ((dpList.size() % 10) == 0) { |
| 187 | shortestPathExecutor.execute( |
| 188 | new ShortestPathTask(hint, topoRouteService, |
| 189 | dpList)); |
| 190 | dpList = new ArrayList<DataPath>(); |
| 191 | hint++; |
| 192 | } |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 193 | */ |
| 194 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 195 | DataPath dataPath = |
| 196 | topoRouteService.getTopoShortestPath(srcSwitchPort, |
| 197 | dstSwitchPort); |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 198 | counter++; |
| 199 | } |
| 200 | if (dpList.size() > 0) { |
| 201 | shortestPathExecutor.execute( |
| 202 | new ShortestPathTask(hint, topoRouteService, |
| 203 | dpList)); |
| 204 | } |
| 205 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 206 | /* |
Pavlin Radoslavov | 4da6128 | 2013-03-20 20:31:36 -0700 | [diff] [blame] | 207 | // Wait for all tasks to finish |
| 208 | try { |
| 209 | while (shortestPathExecutor.getQueue().size() > 0) { |
| 210 | Thread.sleep(100); |
| 211 | } |
| 212 | } catch (InterruptedException ex) { |
| 213 | log.debug("MEASUREMENT: Shortest Path Computation interrupted"); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 214 | } |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 215 | */ |
| 216 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 217 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 218 | topoRouteService.dropShortestPathTopo(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 219 | |
| 220 | long estimatedTime = System.nanoTime() - startTime; |
| 221 | double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0; |
| 222 | String logMsg = "MEASUREMENT: Computed " + counter + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s"; |
| 223 | log.debug(logMsg); |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | final Runnable measureMapReader = new Runnable() { |
| 228 | public void run() { |
| 229 | if (floodlightProvider == null) { |
| 230 | log.debug("FloodlightProvider service not found!"); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | // |
| 235 | // Fetch all Flow Entries |
| 236 | // |
| 237 | int counter = 0; |
| 238 | long startTime = System.nanoTime(); |
| 239 | Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn); |
| 240 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
| 241 | counter++; |
| 242 | FlowEntryId flowEntryId = |
| 243 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
| 244 | String userState = flowEntryObj.getUserState(); |
| 245 | String switchState = flowEntryObj.getSwitchState(); |
| 246 | } |
| 247 | conn.endTx(Transaction.COMMIT); |
| 248 | |
| 249 | long estimatedTime = System.nanoTime() - startTime; |
| 250 | double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0; |
| 251 | String logMsg = "MEASUREMENT: Fetched " + counter + " flow entries in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " entries/s"; |
| 252 | log.debug(logMsg); |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 257 | public void run() { |
| 258 | // log.debug("Reading Flow Entries from the Network Map..."); |
| 259 | if (floodlightProvider == null) { |
| 260 | log.debug("FloodlightProvider service not found!"); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches(); |
| 265 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 266 | Map<Long, IFlowEntry> myFlowEntries = new TreeMap<Long, IFlowEntry>(); |
| 267 | |
| 268 | // |
| 269 | // Fetch all Flow Entries and select only my Flow Entries |
| 270 | // |
| 271 | Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn); |
| 272 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 273 | FlowEntryId flowEntryId = |
| 274 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
Pavlin Radoslavov | 2f9d633 | 2013-03-18 23:05:48 -0700 | [diff] [blame] | 275 | String userState = flowEntryObj.getUserState(); |
| 276 | String switchState = flowEntryObj.getSwitchState(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 277 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 278 | /** |
Pavlin Radoslavov | 79a67c1 | 2013-03-15 21:05:53 -0700 | [diff] [blame] | 279 | log.debug("Found Flow Entry {}: {}", |
| 280 | flowEntryId.toString(), |
Pavlin Radoslavov | 2f9d633 | 2013-03-18 23:05:48 -0700 | [diff] [blame] | 281 | "User State: " + userState + |
| 282 | " Switch State: " + switchState); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 283 | */ |
| 284 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 285 | if (! switchState.equals("FE_SWITCH_NOT_UPDATED")) { |
| 286 | // Ignore the entry: nothing to do |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
| 291 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
| 292 | if (mySwitch == null) { |
Pavlin Radoslavov | 79a67c1 | 2013-03-15 21:05:53 -0700 | [diff] [blame] | 293 | log.debug("Flow Entry ignored: not my switch (FlowEntryId = {} DPID = {})", flowEntryId.toString(), dpid.toString()); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 294 | continue; |
| 295 | } |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 296 | myFlowEntries.put(flowEntryId.value(), flowEntryObj); |
| 297 | } |
| 298 | |
| 299 | // |
| 300 | // Process my Flow Entries |
| 301 | // |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 302 | Boolean processed_measurement_flow = false; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 303 | for (Map.Entry<Long, IFlowEntry> entry : myFlowEntries.entrySet()) { |
| 304 | IFlowEntry flowEntryObj = entry.getValue(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 305 | // Code for measurement purpose |
| 306 | { |
| 307 | IFlowPath flowObj = |
| 308 | conn.utils().getFlowPathByFlowEntry(conn, |
| 309 | flowEntryObj); |
| 310 | if ((flowObj != null) && |
| 311 | flowObj.getFlowId().equals(measurementFlowIdStr)) { |
| 312 | processed_measurement_flow = true; |
| 313 | } |
| 314 | } |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 315 | |
| 316 | // |
| 317 | // TODO: Eliminate the re-fetching of flowEntryId, |
| 318 | // userState, switchState, and dpid from the flowEntryObj. |
| 319 | // |
| 320 | FlowEntryId flowEntryId = |
| 321 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
| 322 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
| 323 | String userState = flowEntryObj.getUserState(); |
| 324 | String switchState = flowEntryObj.getSwitchState(); |
| 325 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
| 326 | if (mySwitch == null) { |
| 327 | log.debug("Flow Entry ignored: not my switch"); |
| 328 | continue; |
| 329 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 330 | |
| 331 | // |
| 332 | // Create the Open Flow Flow Modification Entry to push |
| 333 | // |
| 334 | OFFlowMod fm = |
| 335 | (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 336 | .getMessage(OFType.FLOW_MOD); |
| 337 | long cookie = flowEntryId.value(); |
| 338 | |
| 339 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 340 | if (userState.equals("FE_USER_ADD")) { |
| 341 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 342 | } else if (userState.equals("FE_USER_MODIFY")) { |
| 343 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 344 | } else if (userState.equals("FE_USER_DELETE")) { |
| 345 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 346 | } else { |
| 347 | // Unknown user state. Ignore the entry |
Pavlin Radoslavov | 2f9d633 | 2013-03-18 23:05:48 -0700 | [diff] [blame] | 348 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 349 | flowEntryId.toString(), userState); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 350 | continue; |
| 351 | } |
| 352 | |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 353 | // |
| 354 | // Fetch the match conditions |
| 355 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 356 | OFMatch match = new OFMatch(); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 357 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 358 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 359 | if (matchInPort != null) { |
| 360 | match.setInputPort(matchInPort); |
| 361 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 362 | } |
| 363 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 364 | if (matchEthernetFrameType != null) { |
| 365 | match.setDataLayerType(matchEthernetFrameType); |
| 366 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 367 | } |
| 368 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 369 | if (matchSrcIPv4Net != null) { |
| 370 | match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC); |
| 371 | } |
| 372 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 373 | if (matchDstIPv4Net != null) { |
| 374 | match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST); |
| 375 | } |
| 376 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 377 | if (matchSrcMac != null) { |
| 378 | match.setDataLayerSource(matchSrcMac); |
| 379 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 380 | } |
| 381 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 382 | if (matchDstMac != null) { |
| 383 | match.setDataLayerDestination(matchDstMac); |
| 384 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 385 | } |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 386 | |
| 387 | // |
| 388 | // Fetch the actions |
| 389 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 390 | List<OFAction> actions = new ArrayList<OFAction>(); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 391 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 392 | if (actionOutputPort != null) { |
| 393 | OFActionOutput action = new OFActionOutput(); |
| 394 | // XXX: The max length is hard-coded for now |
| 395 | action.setMaxLength((short)0xffff); |
| 396 | action.setPort(actionOutputPort); |
| 397 | actions.add(action); |
| 398 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 399 | |
| 400 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 401 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 402 | .setPriority(PRIORITY_DEFAULT) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 403 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 404 | .setCookie(cookie) |
| 405 | .setCommand(flowModCommand) |
| 406 | .setMatch(match) |
| 407 | .setActions(actions) |
| 408 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 409 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 410 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 411 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 412 | if (actionOutputPort != null) |
| 413 | fm.setOutPort(actionOutputPort); |
| 414 | } |
| 415 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 416 | // |
| 417 | // TODO: Set the following flag |
| 418 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 419 | // See method ForwardingBase::pushRoute() |
| 420 | // |
| 421 | try { |
| 422 | messageDamper.write(mySwitch, fm, null); |
| 423 | mySwitch.flush(); |
| 424 | flowEntryObj.setSwitchState("FE_SWITCH_UPDATED"); |
| 425 | if (userState.equals("FE_USER_DELETE")) { |
| 426 | // Delete the entry |
| 427 | IFlowPath flowObj = null; |
| 428 | flowObj = conn.utils().getFlowPathByFlowEntry(conn, |
| 429 | flowEntryObj); |
| 430 | if (flowObj != null) |
| 431 | log.debug("Found FlowPath to be deleted"); |
| 432 | else |
| 433 | log.debug("Did not find FlowPath to be deleted"); |
| 434 | flowObj.removeFlowEntry(flowEntryObj); |
| 435 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 436 | |
| 437 | // Test whether the last flow entry |
| 438 | Iterable<IFlowEntry> tmpflowEntries = |
| 439 | flowObj.getFlowEntries(); |
| 440 | boolean found = false; |
| 441 | for (IFlowEntry tmpflowEntryObj : tmpflowEntries) { |
| 442 | found = true; |
| 443 | break; |
| 444 | } |
| 445 | if (! found) { |
| 446 | // Remove the Flow Path as well |
| 447 | conn.utils().removeFlowPath(conn, flowObj); |
| 448 | } |
| 449 | } |
| 450 | } catch (IOException e) { |
| 451 | log.error("Failure writing flow mod from network map", e); |
| 452 | } |
| 453 | } |
| 454 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 455 | |
| 456 | if (processed_measurement_flow) { |
| 457 | long estimatedTime = System.nanoTime() - modifiedMeasurementFlowTime; |
| 458 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 459 | (double)estimatedTime / 1000000000 + " sec"; |
| 460 | log.debug(logMsg); |
| 461 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 462 | } |
| 463 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | final ScheduledFuture<?> measureShortestPathHandle = |
| 467 | measureShortestPathScheduler.scheduleAtFixedRate(measureShortestPath, 10, 10, TimeUnit.SECONDS); |
| 468 | */ |
| 469 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 470 | final ScheduledFuture<?> measureMapReaderHandle = |
| 471 | measureMapReaderScheduler.scheduleAtFixedRate(measureMapReader, 10, 10, TimeUnit.SECONDS); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 472 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 473 | /* |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 474 | final ScheduledFuture<?> mapReaderHandle = |
| 475 | mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 476 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 477 | |
| 478 | @Override |
| 479 | public void init(String conf) { |
| 480 | conn = GraphDBConnection.getInstance(conf); |
| 481 | } |
| 482 | |
| 483 | public void finalize() { |
| 484 | close(); |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public void close() { |
| 489 | conn.close(); |
| 490 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 491 | |
| 492 | @Override |
| 493 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 494 | Collection<Class<? extends IFloodlightService>> l = |
| 495 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 496 | l.add(IFlowService.class); |
| 497 | return l; |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 502 | getServiceImpls() { |
| 503 | Map<Class<? extends IFloodlightService>, |
| 504 | IFloodlightService> m = |
| 505 | new HashMap<Class<? extends IFloodlightService>, |
| 506 | IFloodlightService>(); |
| 507 | m.put(IFlowService.class, this); |
| 508 | return m; |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public Collection<Class<? extends IFloodlightService>> |
| 513 | getModuleDependencies() { |
| 514 | Collection<Class<? extends IFloodlightService>> l = |
| 515 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 516 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 517 | l.add(IRestApiService.class); |
| 518 | return l; |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void init(FloodlightModuleContext context) |
| 523 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 524 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 525 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 526 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 527 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 528 | EnumSet.of(OFType.FLOW_MOD), |
| 529 | OFMESSAGE_DAMPER_TIMEOUT); |
| 530 | // TODO: An ugly hack! |
| 531 | String conf = "/tmp/cassandra.titan"; |
| 532 | this.init(conf); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public void startUp(FloodlightModuleContext context) { |
| 537 | restApi.addRestletRoutable(new FlowWebRoutable()); |
Pavlin Radoslavov | 80ca630 | 2013-03-20 02:08:09 -0700 | [diff] [blame] | 538 | |
| 539 | // |
| 540 | // Extract all flow entries and assign the next Flow Entry ID |
| 541 | // to be larger than the largest Flow Entry ID |
| 542 | // |
| 543 | Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn); |
| 544 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
| 545 | FlowEntryId flowEntryId = |
| 546 | new FlowEntryId(flowEntryObj.getFlowEntryId()); |
| 547 | if (flowEntryId.value() >= nextFlowEntryId) |
| 548 | nextFlowEntryId = flowEntryId.value() + 1; |
| 549 | } |
| 550 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Add a flow. |
| 555 | * |
| 556 | * Internally, ONOS will automatically register the installer for |
| 557 | * receiving Flow Path Notifications for that path. |
| 558 | * |
| 559 | * @param flowPath the Flow Path to install. |
| 560 | * @param flowId the return-by-reference Flow ID as assigned internally. |
| 561 | * @return true on success, otherwise false. |
| 562 | */ |
| 563 | @Override |
| 564 | public boolean addFlow(FlowPath flowPath, FlowId flowId) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 565 | if (flowPath.flowId().value() == measurementFlowId) { |
| 566 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 567 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 568 | |
| 569 | // |
| 570 | // Assign the FlowEntry IDs |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 571 | // Right now every new flow entry gets a new flow entry ID |
| 572 | // TODO: This needs to be redesigned! |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 573 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 574 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 575 | long id = nextFlowEntryId++; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 576 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 577 | } |
| 578 | |
| 579 | IFlowPath flowObj = null; |
| 580 | try { |
| 581 | if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId())) |
| 582 | != null) { |
| 583 | log.debug("Adding FlowPath with FlowId {}: found existing FlowPath", |
| 584 | flowPath.flowId().toString()); |
| 585 | } else { |
| 586 | flowObj = conn.utils().newFlowPath(conn); |
| 587 | log.debug("Adding FlowPath with FlowId {}: creating new FlowPath", |
| 588 | flowPath.flowId().toString()); |
| 589 | } |
| 590 | } catch (Exception e) { |
| 591 | // TODO: handle exceptions |
| 592 | conn.endTx(Transaction.ROLLBACK); |
| 593 | log.error(":addFlow FlowId:{} failed", |
| 594 | flowPath.flowId().toString()); |
| 595 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 596 | if (flowObj == null) { |
| 597 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 598 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 599 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 600 | |
| 601 | // |
| 602 | // Set the Flow key: |
| 603 | // - flowId |
| 604 | // |
| 605 | flowObj.setFlowId(flowPath.flowId().toString()); |
| 606 | flowObj.setType("flow"); |
| 607 | |
| 608 | // |
| 609 | // Set the Flow attributes: |
| 610 | // - flowPath.installerId() |
| 611 | // - flowPath.dataPath().srcPort() |
| 612 | // - flowPath.dataPath().dstPort() |
| 613 | // |
| 614 | flowObj.setInstallerId(flowPath.installerId().toString()); |
| 615 | flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString()); |
| 616 | flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value()); |
| 617 | flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString()); |
| 618 | flowObj.setDstPort(flowPath.dataPath().dstPort().port().value()); |
| 619 | |
| 620 | // Flow edges: |
| 621 | // HeadFE |
| 622 | |
| 623 | |
| 624 | // |
| 625 | // Flow Entries: |
| 626 | // flowPath.dataPath().flowEntries() |
| 627 | // |
| 628 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
| 629 | IFlowEntry flowEntryObj = null; |
| 630 | boolean found = false; |
| 631 | try { |
| 632 | if ((flowEntryObj = conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) { |
| 633 | log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry", |
| 634 | flowEntry.flowEntryId().toString()); |
| 635 | found = true; |
| 636 | } else { |
| 637 | flowEntryObj = conn.utils().newFlowEntry(conn); |
| 638 | log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry", |
| 639 | flowEntry.flowEntryId().toString()); |
| 640 | } |
| 641 | } catch (Exception e) { |
| 642 | // TODO: handle exceptions |
| 643 | conn.endTx(Transaction.ROLLBACK); |
| 644 | log.error(":addFlow FlowEntryId:{} failed", |
| 645 | flowEntry.flowEntryId().toString()); |
| 646 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 647 | if (flowEntryObj == null) { |
| 648 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 649 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 650 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 651 | |
| 652 | // |
| 653 | // Set the Flow Entry key: |
| 654 | // - flowEntry.flowEntryId() |
| 655 | // |
| 656 | flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString()); |
| 657 | flowEntryObj.setType("flow_entry"); |
| 658 | |
| 659 | // |
| 660 | // Set the Flow Entry attributes: |
| 661 | // - flowEntry.flowEntryMatch() |
| 662 | // - flowEntry.flowEntryActions() |
| 663 | // - flowEntry.dpid() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 664 | // - flowEntry.flowEntryUserState() |
| 665 | // - flowEntry.flowEntrySwitchState() |
| 666 | // - flowEntry.flowEntryErrorState() |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 667 | // - flowEntry.matchInPort() |
| 668 | // - flowEntry.matchEthernetFrameType() |
| 669 | // - flowEntry.matchSrcIPv4Net() |
| 670 | // - flowEntry.matchDstIPv4Net() |
| 671 | // - flowEntry.matchSrcMac() |
| 672 | // - flowEntry.matchDstMac() |
| 673 | // - flowEntry.actionOutput() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 674 | // |
| 675 | flowEntryObj.setSwitchDpid(flowEntry.dpid().toString()); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 676 | if (flowEntry.flowEntryMatch().matchInPort()) |
| 677 | flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value()); |
| 678 | if (flowEntry.flowEntryMatch().matchEthernetFrameType()) |
| 679 | flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType()); |
| 680 | if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) |
| 681 | flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString()); |
| 682 | if (flowEntry.flowEntryMatch().matchDstIPv4Net()) |
| 683 | flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString()); |
| 684 | if (flowEntry.flowEntryMatch().matchSrcMac()) |
| 685 | flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString()); |
| 686 | if (flowEntry.flowEntryMatch().matchDstMac()) |
| 687 | flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString()); |
| 688 | |
| 689 | for (FlowEntryAction fa : flowEntry.flowEntryActions()) { |
| 690 | if (fa.actionOutput() != null) |
| 691 | flowEntryObj.setActionOutput(fa.actionOutput().port().value()); |
| 692 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 693 | // TODO: Hacks with hard-coded state names! |
| 694 | if (found) |
| 695 | flowEntryObj.setUserState("FE_USER_MODIFY"); |
| 696 | else |
| 697 | flowEntryObj.setUserState("FE_USER_ADD"); |
| 698 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 699 | // |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 700 | // TODO: Take care of the FlowEntryMatch, FlowEntryAction set, |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 701 | // and FlowEntryErrorState. |
| 702 | // |
| 703 | |
| 704 | // Flow Entries edges: |
| 705 | // Flow |
| 706 | // NextFE |
| 707 | // InPort |
| 708 | // OutPort |
| 709 | // Switch |
| 710 | if (! found) |
| 711 | flowObj.addFlowEntry(flowEntryObj); |
| 712 | } |
| 713 | conn.endTx(Transaction.COMMIT); |
| 714 | |
| 715 | // |
| 716 | // TODO: We need a proper Flow ID allocation mechanism. |
| 717 | // |
| 718 | flowId.setValue(flowPath.flowId().value()); |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 719 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 720 | return true; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Delete a previously added flow. |
| 725 | * |
| 726 | * @param flowId the Flow ID of the flow to delete. |
| 727 | * @return true on success, otherwise false. |
| 728 | */ |
| 729 | @Override |
| 730 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 731 | if (flowId.value() == measurementFlowId) { |
| 732 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 733 | } |
| 734 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 735 | IFlowPath flowObj = null; |
| 736 | // |
| 737 | // We just mark the entries for deletion, |
| 738 | // and let the switches remove each individual entry after |
| 739 | // it has been removed from the switches. |
| 740 | // |
| 741 | try { |
| 742 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 743 | != null) { |
| 744 | log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath", |
| 745 | flowId.toString()); |
| 746 | } else { |
| 747 | log.debug("Deleting FlowPath with FlowId {}: FlowPath not found", |
| 748 | flowId.toString()); |
| 749 | } |
| 750 | } catch (Exception e) { |
| 751 | // TODO: handle exceptions |
| 752 | conn.endTx(Transaction.ROLLBACK); |
| 753 | log.error(":deleteFlow FlowId:{} failed", flowId.toString()); |
| 754 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 755 | if (flowObj == null) { |
| 756 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 757 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 758 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 759 | |
| 760 | // |
| 761 | // Find and mark for deletion all Flow Entries |
| 762 | // |
| 763 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 764 | boolean empty = true; // TODO: an ugly hack |
| 765 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 766 | empty = false; |
| 767 | // flowObj.removeFlowEntry(flowEntryObj); |
| 768 | // conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 769 | flowEntryObj.setUserState("FE_USER_DELETE"); |
| 770 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 771 | } |
| 772 | // Remove from the database empty flows |
| 773 | if (empty) |
| 774 | conn.utils().removeFlowPath(conn, flowObj); |
| 775 | conn.endTx(Transaction.COMMIT); |
| 776 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 777 | return true; |
| 778 | } |
| 779 | |
| 780 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 781 | * Clear the state for a previously added flow. |
| 782 | * |
| 783 | * @param flowId the Flow ID of the flow to clear. |
| 784 | * @return true on success, otherwise false. |
| 785 | */ |
| 786 | @Override |
| 787 | public boolean clearFlow(FlowId flowId) { |
| 788 | IFlowPath flowObj = null; |
| 789 | try { |
| 790 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 791 | != null) { |
| 792 | log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath", |
| 793 | flowId.toString()); |
| 794 | } else { |
| 795 | log.debug("Clearing FlowPath with FlowId {}: FlowPath not found", |
| 796 | flowId.toString()); |
| 797 | } |
| 798 | } catch (Exception e) { |
| 799 | // TODO: handle exceptions |
| 800 | conn.endTx(Transaction.ROLLBACK); |
| 801 | log.error(":clearFlow FlowId:{} failed", flowId.toString()); |
| 802 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 803 | if (flowObj == null) { |
| 804 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 805 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 806 | } |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 807 | |
| 808 | // |
| 809 | // Remove all Flow Entries |
| 810 | // |
| 811 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 812 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 813 | flowObj.removeFlowEntry(flowEntryObj); |
| 814 | conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 815 | } |
| 816 | // Remove the Flow itself |
| 817 | conn.utils().removeFlowPath(conn, flowObj); |
| 818 | conn.endTx(Transaction.COMMIT); |
| 819 | |
| 820 | return true; |
| 821 | } |
| 822 | |
| 823 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 824 | * Get a previously added flow. |
| 825 | * |
| 826 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 827 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 828 | */ |
| 829 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 830 | public FlowPath getFlow(FlowId flowId) { |
| 831 | IFlowPath flowObj = null; |
| 832 | try { |
| 833 | if ((flowObj = conn.utils().searchFlowPath(conn, flowId)) |
| 834 | != null) { |
| 835 | log.debug("Get FlowPath with FlowId {}: found existing FlowPath", |
| 836 | flowId.toString()); |
| 837 | } else { |
| 838 | log.debug("Get FlowPath with FlowId {}: FlowPath not found", |
| 839 | flowId.toString()); |
| 840 | } |
| 841 | } catch (Exception e) { |
| 842 | // TODO: handle exceptions |
| 843 | conn.endTx(Transaction.ROLLBACK); |
| 844 | log.error(":getFlow FlowId:{} failed", flowId.toString()); |
| 845 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 846 | if (flowObj == null) { |
| 847 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 848 | return null; // Flow not found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 849 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 850 | |
| 851 | // |
| 852 | // Extract the Flow state |
| 853 | // |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 854 | FlowPath flowPath = extractFlowPath(flowObj); |
| 855 | conn.endTx(Transaction.COMMIT); |
| 856 | |
| 857 | return flowPath; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Get all previously added flows by a specific installer for a given |
| 862 | * data path endpoints. |
| 863 | * |
| 864 | * @param installerId the Caller ID of the installer of the flow to get. |
| 865 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 866 | * @return the Flow Paths if found, otherwise null. |
| 867 | */ |
| 868 | @Override |
| 869 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 870 | DataPathEndpoints dataPathEndpoints) { |
| 871 | // |
| 872 | // TODO: The implementation below is not optimal: |
| 873 | // We fetch all flows, and then return only the subset that match |
| 874 | // the query conditions. |
| 875 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 876 | // the flows as appropriate. |
| 877 | // |
| 878 | ArrayList<FlowPath> allFlows = getAllFlows(); |
| 879 | |
| 880 | if (allFlows == null) { |
| 881 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
| 882 | return null; |
| 883 | } |
| 884 | |
| 885 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 886 | for (FlowPath flow : allFlows) { |
| 887 | // |
| 888 | // TODO: String-based comparison is sub-optimal. |
| 889 | // 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] | 890 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 891 | // |
| 892 | if (! flow.installerId().toString().equals(installerId.toString())) |
| 893 | continue; |
| 894 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 895 | continue; |
| 896 | } |
| 897 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 898 | continue; |
| 899 | } |
| 900 | flowPaths.add(flow); |
| 901 | } |
| 902 | |
| 903 | if (flowPaths.isEmpty()) { |
| 904 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
| 905 | flowPaths = null; |
| 906 | } else { |
| 907 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints); |
| 908 | } |
| 909 | |
| 910 | return flowPaths; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Get all installed flows by all installers for given data path endpoints. |
| 915 | * |
| 916 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 917 | * @return the Flow Paths if found, otherwise null. |
| 918 | */ |
| 919 | @Override |
| 920 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
| 921 | // |
| 922 | // TODO: The implementation below is not optimal: |
| 923 | // We fetch all flows, and then return only the subset that match |
| 924 | // the query conditions. |
| 925 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 926 | // the flows as appropriate. |
| 927 | // |
| 928 | ArrayList<FlowPath> allFlows = getAllFlows(); |
| 929 | |
| 930 | if (allFlows == null) { |
| 931 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
| 932 | return null; |
| 933 | } |
| 934 | |
| 935 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 936 | for (FlowPath flow : allFlows) { |
| 937 | // |
| 938 | // TODO: String-based comparison is sub-optimal. |
| 939 | // 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] | 940 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 941 | // |
| 942 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 943 | continue; |
| 944 | } |
| 945 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 946 | continue; |
| 947 | } |
| 948 | flowPaths.add(flow); |
| 949 | } |
| 950 | |
| 951 | if (flowPaths.isEmpty()) { |
| 952 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
| 953 | flowPaths = null; |
| 954 | } else { |
| 955 | log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints); |
| 956 | } |
| 957 | |
| 958 | return flowPaths; |
| 959 | } |
| 960 | |
| 961 | /** |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 962 | * Get summary of all installed flows by all installers in a given range |
| 963 | * |
| 964 | * @param flowId the data path endpoints of the flows to get. |
| 965 | * @param maxFlows: the maximum number of flows to be returned |
| 966 | * @return the Flow Paths if found, otherwise null. |
| 967 | */ |
| 968 | @Override |
| 969 | public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) { |
| 970 | // |
| 971 | // TODO: The implementation below is not optimal: |
| 972 | // We fetch all flows, and then return only the subset that match |
| 973 | // the query conditions. |
| 974 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 975 | // the flows as appropriate. |
| 976 | // |
| 977 | ArrayList<FlowPath> allFlows = getAllFlows(); |
| 978 | |
| 979 | if (allFlows == null) { |
| 980 | log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows); |
| 981 | return null; |
| 982 | } |
| 983 | |
| 984 | Collections.sort(allFlows); |
| 985 | |
| 986 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 987 | for (FlowPath flow : allFlows) { |
| 988 | |
| 989 | // start from desired flowId |
| 990 | if (flow.flowId().value() < flowId.value()) { |
| 991 | continue; |
| 992 | } |
| 993 | |
| 994 | // Summarize by making null flow entry fields that are not relevant to report |
| 995 | for (FlowEntry flowEntry : flow.dataPath().flowEntries()) { |
| 996 | flowEntry.setFlowEntryActions(null); |
| 997 | flowEntry.setFlowEntryMatch(null); |
| 998 | } |
| 999 | |
| 1000 | flowPaths.add(flow); |
| 1001 | if (maxFlows != 0 && flowPaths.size() >= maxFlows) { |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (flowPaths.isEmpty()) { |
| 1007 | log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows); |
| 1008 | flowPaths = null; |
| 1009 | } else { |
| 1010 | log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows); |
| 1011 | } |
| 1012 | |
| 1013 | return flowPaths; |
| 1014 | } |
| 1015 | |
| 1016 | /** |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1017 | * Get all installed flows by all installers. |
| 1018 | * |
| 1019 | * @return the Flow Paths if found, otherwise null. |
| 1020 | */ |
| 1021 | @Override |
| 1022 | public ArrayList<FlowPath> getAllFlows() { |
| 1023 | Iterable<IFlowPath> flowPathsObj = null; |
| 1024 | |
| 1025 | try { |
| 1026 | if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) { |
| 1027 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1028 | } else { |
| 1029 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1030 | } |
| 1031 | } catch (Exception e) { |
| 1032 | // TODO: handle exceptions |
| 1033 | conn.endTx(Transaction.ROLLBACK); |
| 1034 | log.error(":getAllFlowPaths failed"); |
| 1035 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1036 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 1037 | conn.endTx(Transaction.COMMIT); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1038 | return null; // No Flows found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1039 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1040 | |
| 1041 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1042 | for (IFlowPath flowObj : flowPathsObj) { |
| 1043 | // |
| 1044 | // Extract the Flow state |
| 1045 | // |
| 1046 | FlowPath flowPath = extractFlowPath(flowObj); |
| 1047 | flowPaths.add(flowPath); |
| 1048 | } |
| 1049 | |
| 1050 | conn.endTx(Transaction.COMMIT); |
| 1051 | |
| 1052 | return flowPaths; |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * Extract Flow Path State from a Titan Database Object @ref IFlowPath. |
| 1057 | * |
| 1058 | * @param flowObj the object to extract the Flow Path State from. |
| 1059 | * @return the extracted Flow Path State. |
| 1060 | */ |
| 1061 | private FlowPath extractFlowPath(IFlowPath flowObj) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1062 | FlowPath flowPath = new FlowPath(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1063 | |
| 1064 | // |
| 1065 | // Extract the Flow state |
| 1066 | // |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1067 | flowPath.setFlowId(new FlowId(flowObj.getFlowId())); |
| 1068 | flowPath.setInstallerId(new CallerId(flowObj.getInstallerId())); |
| 1069 | flowPath.dataPath().srcPort().setDpid(new Dpid(flowObj.getSrcSwitch())); |
| 1070 | flowPath.dataPath().srcPort().setPort(new Port(flowObj.getSrcPort())); |
| 1071 | flowPath.dataPath().dstPort().setDpid(new Dpid(flowObj.getDstSwitch())); |
| 1072 | flowPath.dataPath().dstPort().setPort(new Port(flowObj.getDstPort())); |
| 1073 | |
| 1074 | // |
| 1075 | // Extract all Flow Entries |
| 1076 | // |
| 1077 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1078 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 1079 | FlowEntry flowEntry = new FlowEntry(); |
| 1080 | flowEntry.setFlowEntryId(new FlowEntryId(flowEntryObj.getFlowEntryId())); |
| 1081 | flowEntry.setDpid(new Dpid(flowEntryObj.getSwitchDpid())); |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 1082 | |
| 1083 | // |
| 1084 | // Extract the match conditions |
| 1085 | // |
| 1086 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1087 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1088 | if (matchInPort != null) |
| 1089 | match.enableInPort(new Port(matchInPort)); |
| 1090 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1091 | if (matchEthernetFrameType != null) |
| 1092 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1093 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1094 | if (matchSrcIPv4Net != null) |
| 1095 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1096 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1097 | if (matchDstIPv4Net != null) |
| 1098 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1099 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1100 | if (matchSrcMac != null) |
| 1101 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1102 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1103 | if (matchDstMac != null) |
| 1104 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1105 | flowEntry.setFlowEntryMatch(match); |
| 1106 | |
| 1107 | // |
| 1108 | // Extract the actions |
| 1109 | // |
| 1110 | ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>(); |
| 1111 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1112 | if (actionOutputPort != null) { |
| 1113 | FlowEntryAction action = new FlowEntryAction(); |
| 1114 | action.setActionOutput(new Port(actionOutputPort)); |
| 1115 | actions.add(action); |
| 1116 | } |
| 1117 | flowEntry.setFlowEntryActions(actions); |
| 1118 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1119 | String userState = flowEntryObj.getUserState(); |
| 1120 | flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState)); |
| 1121 | String switchState = flowEntryObj.getSwitchState(); |
| 1122 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState)); |
| 1123 | // |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 1124 | // TODO: Take care of the FlowEntryMatch, FlowEntryAction set, |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1125 | // and FlowEntryErrorState. |
| 1126 | // |
| 1127 | flowPath.dataPath().flowEntries().add(flowEntry); |
| 1128 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1129 | |
| 1130 | return flowPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1131 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1132 | } |