blob: 257add884974265c5ba40d148cbafdcbeb40acdc [file] [log] [blame]
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001package net.floodlightcontroller.flowcache;
2
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08003import java.io.IOException;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08004import java.util.ArrayList;
5import java.util.Collection;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08006import java.util.EnumSet;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08007import java.util.HashMap;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +00008import java.util.LinkedList;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08009import java.util.List;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080010import java.util.Map;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070011import java.util.TreeMap;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -070012import java.util.Collections;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070013import java.util.concurrent.BlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080014import java.util.concurrent.Executors;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070015import java.util.concurrent.LinkedBlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080016import java.util.concurrent.ScheduledExecutorService;
17import java.util.concurrent.ScheduledFuture;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070018import java.util.concurrent.ThreadPoolExecutor;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080019import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080020
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080021import net.floodlightcontroller.core.IFloodlightProviderService;
22import net.floodlightcontroller.core.INetMapStorage;
23import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
24import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pankaj Berded0079742013-03-27 17:53:25 -070025import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
26import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070027import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080028import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080029import net.floodlightcontroller.core.module.FloodlightModuleContext;
30import net.floodlightcontroller.core.module.FloodlightModuleException;
31import net.floodlightcontroller.core.module.IFloodlightModule;
32import net.floodlightcontroller.core.module.IFloodlightService;
33import net.floodlightcontroller.flowcache.IFlowService;
34import net.floodlightcontroller.flowcache.web.FlowWebRoutable;
35import net.floodlightcontroller.restserver.IRestApiService;
36import net.floodlightcontroller.util.CallerId;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080037import net.floodlightcontroller.util.DataPath;
38import net.floodlightcontroller.util.Dpid;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080039import net.floodlightcontroller.util.DataPathEndpoints;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080040import net.floodlightcontroller.util.FlowEntry;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070041import net.floodlightcontroller.util.FlowEntryAction;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080042import net.floodlightcontroller.util.FlowEntryId;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070043import net.floodlightcontroller.util.FlowEntryMatch;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080044import net.floodlightcontroller.util.FlowEntrySwitchState;
45import net.floodlightcontroller.util.FlowEntryUserState;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080046import net.floodlightcontroller.util.FlowId;
47import net.floodlightcontroller.util.FlowPath;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070048import net.floodlightcontroller.util.IPv4Net;
49import net.floodlightcontroller.util.MACAddress;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080050import net.floodlightcontroller.util.OFMessageDamper;
51import net.floodlightcontroller.util.Port;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070052import net.floodlightcontroller.util.SwitchPort;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070053import net.onrc.onos.flow.IFlowManager;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080054import net.onrc.onos.util.GraphDBConnection;
55import net.onrc.onos.util.GraphDBConnection.Transaction;
56
57import org.openflow.protocol.OFFlowMod;
58import org.openflow.protocol.OFMatch;
59import org.openflow.protocol.OFPacketOut;
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070060import org.openflow.protocol.OFPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080061import org.openflow.protocol.OFType;
62import org.openflow.protocol.action.OFAction;
63import org.openflow.protocol.action.OFActionOutput;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080064
65import org.slf4j.Logger;
66import org.slf4j.LoggerFactory;
67
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070068public class FlowManager implements IFloodlightModule, IFlowService, IFlowManager, INetMapStorage {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080069
70 public GraphDBConnection conn;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080071
72 protected IRestApiService restApi;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080073 protected IFloodlightProviderService floodlightProvider;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070074 protected ITopoRouteService topoRouteService;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070075 protected FloodlightModuleContext context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080076
77 protected OFMessageDamper messageDamper;
78
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070079 //
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 Radoslavov9e5344c2013-02-18 09:58:30 -080088
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070089 private static long nextFlowEntryId = 1;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070090 private static long measurementFlowId = 100000;
91 private static String measurementFlowIdStr = "0x186a0"; // 100000
92 private long modifiedMeasurementFlowTime = 0;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070093
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080094 /** The logger. */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080095 private static Logger log = LoggerFactory.getLogger(FlowManager.class);
96
97 // The periodic task(s)
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070098 private final ScheduledExecutorService measureShortestPathScheduler =
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080099 Executors.newScheduledThreadPool(1);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700100 private final ScheduledExecutorService measureMapReaderScheduler =
101 Executors.newScheduledThreadPool(1);
102 private final ScheduledExecutorService mapReaderScheduler =
103 Executors.newScheduledThreadPool(1);
104
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700105 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 Radoslavova5f167b2013-03-21 11:39:27 -0700124 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700125 String logMsg = "MEASUREMENT: Running Thread hint " + this.hint;
126 log.debug(logMsg);
127 long startTime = System.nanoTime();
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700128 */
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700129 for (DataPath dp : this.dpList) {
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700130 topoRouteService.getTopoShortestPath(dp.srcPort(), dp.dstPort());
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700131 }
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700132 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700133 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 Radoslavova5f167b2013-03-21 11:39:27 -0700137 */
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700138 }
139 }
140
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700141 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 Radoslavov571cff92013-03-20 02:01:32 -0700149 if (topoRouteService == null) {
150 log.debug("Topology Route Service not found");
151 return;
152 }
153
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700154 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 Radoslavov571cff92013-03-20 02:01:32 -0700162 //
163 // Recompute the Shortest Paths for all Flows
164 //
165 int counter = 0;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700166 int hint = 0;
167 ArrayList<DataPath> dpList = new ArrayList<DataPath>();
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700168 long startTime = System.nanoTime();
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700169
170 topoRouteService.prepareShortestPathTopo();
171
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700172 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
173 for (IFlowPath flowPathObj : allFlowPaths) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700174 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 Radoslavova5f167b2013-03-21 11:39:27 -0700183
184 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700185 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 Radoslavov4da61282013-03-20 20:31:36 -0700196 */
197
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700198 DataPath dataPath =
199 topoRouteService.getTopoShortestPath(srcSwitchPort,
200 dstSwitchPort);
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700201 counter++;
202 }
203 if (dpList.size() > 0) {
204 shortestPathExecutor.execute(
205 new ShortestPathTask(hint, topoRouteService,
206 dpList));
207 }
208
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700209 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700210 // 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 Radoslavov571cff92013-03-20 02:01:32 -0700217 }
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700218 */
219
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700220 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700221 topoRouteService.dropShortestPathTopo();
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700222
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 Radoslavovb6f53542013-03-01 16:02:14 -0800260 public void run() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800261 if (floodlightProvider == null) {
262 log.debug("FloodlightProvider service not found!");
263 return;
264 }
265
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000266 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 Radoslavov01391c92013-03-14 17:13:21 -0700272
273 //
274 // Fetch all Flow Entries and select only my Flow Entries
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000275 // that need to be undated into the switches.
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700276 //
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000277 Iterable<IFlowEntry> allFlowEntries =
278 conn.utils().getAllFlowEntries(conn);
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700279 for (IFlowEntry flowEntryObj : allFlowEntries) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000280 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700281 String userState = flowEntryObj.getUserState();
282 String switchState = flowEntryObj.getSwitchState();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000283 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 Radoslavovb6f53542013-03-01 16:02:14 -0800293
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000294 /*
295 log.debug("Found Flow Entry Id = {} {}",
Pavlin Radoslavov79a67c12013-03-15 21:05:53 -0700296 flowEntryId.toString(),
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000297 "DPID = " + dpid.toString() +
298 " User State: " + userState +
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700299 " Switch State: " + switchState);
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000300 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800301
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000302 if (! switchState.equals("FE_SWITCH_NOT_UPDATED"))
303 continue; // Ignore the entry: nothing to do
304
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800305 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000306 if (mySwitch == null)
307 continue; // Ignore the entry: not my switch
308
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700309 myFlowEntries.put(flowEntryId.value(), flowEntryObj);
310 }
311
312 //
313 // Process my Flow Entries
314 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700315 boolean processed_measurement_flow = false;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700316 for (Map.Entry<Long, IFlowEntry> entry : myFlowEntries.entrySet()) {
317 IFlowEntry flowEntryObj = entry.getValue();
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700318 // 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 Radoslavov01391c92013-03-14 17:13:21 -0700328
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 Kobayashic9da09e2013-03-26 20:52:02 +0000339 if (mySwitch == null)
340 continue; // Shouldn't happen
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800341
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700342 // TODO: PAVPAVPAV: FROM HERE...
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800343 //
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 Radoslavov2f9d6332013-03-18 23:05:48 -0700360 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
361 flowEntryId.toString(), userState);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800362 continue;
363 }
364
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700365 //
366 // Fetch the match conditions
367 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800368 OFMatch match = new OFMatch();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700369 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 Radoslavove2f0de82013-03-12 01:39:30 -0700398
399 //
400 // Fetch the actions
401 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800402 List<OFAction> actions = new ArrayList<OFAction>();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700403 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 Radoslavovb6f53542013-03-01 16:02:14 -0800411
412 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
413 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -0700414 .setPriority(PRIORITY_DEFAULT)
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800415 .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 Radoslavov78c4e492013-03-12 17:17:48 -0700421 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 Radoslavovb6f53542013-03-01 16:02:14 -0800428 //
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 Kobayashic9da09e2013-03-26 20:52:02 +0000436 //
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 Radoslavovb6f53542013-03-01 16:02:14 -0800442 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
443 if (userState.equals("FE_USER_DELETE")) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000444 // An entry that needs to be deleted.
445 deleteFlowEntries.add(flowEntryObj);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800446 }
447 } catch (IOException e) {
448 log.error("Failure writing flow mod from network map", e);
449 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700450 // TODO: XXX: PAVPAVPAV: TO HERE.
451 // TODO: XXX: PAVPAVPAV: Update the flowEntryObj
452 // to "FE_SWITCH_UPDATED" in the new (refactored) code.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800453 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000454
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 Radoslavovb6f53542013-03-01 16:02:14 -0800486 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700487
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 Radoslavovb6f53542013-03-01 16:02:14 -0800494 }
495 };
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700496
497 /*
498 final ScheduledFuture<?> measureShortestPathHandle =
499 measureShortestPathScheduler.scheduleAtFixedRate(measureShortestPath, 10, 10, TimeUnit.SECONDS);
500 */
501
Pavlin Radoslavove38319c2013-03-21 16:20:00 -0700502 /*
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700503 final ScheduledFuture<?> measureMapReaderHandle =
504 measureMapReaderScheduler.scheduleAtFixedRate(measureMapReader, 10, 10, TimeUnit.SECONDS);
Pavlin Radoslavove38319c2013-03-21 16:20:00 -0700505 */
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700506
507 final ScheduledFuture<?> mapReaderHandle =
508 mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800509
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 Radoslavov9e5344c2013-02-18 09:58:30 -0800523
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 Radoslavovb6f53542013-03-01 16:02:14 -0800548 l.add(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700549 l.add(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800550 l.add(IRestApiService.class);
551 return l;
552 }
553
554 @Override
555 public void init(FloodlightModuleContext context)
556 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700557 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800558 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700559 topoRouteService = context.getServiceImpl(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800560 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800561 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 Radoslavov9e5344c2013-02-18 09:58:30 -0800567 }
568
569 @Override
570 public void startUp(FloodlightModuleContext context) {
571 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov80ca6302013-03-20 02:08:09 -0700572
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 Radoslavov9e5344c2013-02-18 09:58:30 -0800585 }
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 Radoslavov571cff92013-03-20 02:01:32 -0700599 if (flowPath.flowId().value() == measurementFlowId) {
600 modifiedMeasurementFlowTime = System.nanoTime();
601 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800602
603 //
604 // Assign the FlowEntry IDs
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700605 // Right now every new flow entry gets a new flow entry ID
606 // TODO: This needs to be redesigned!
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800607 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800608 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700609 long id = nextFlowEntryId++;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800610 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 Radoslavov89c8f432013-03-15 18:50:46 -0700630 if (flowObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000631 log.error(":addFlow FlowId:{} failed: Flow object not created",
632 flowPath.flowId().toString());
633 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800634 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700635 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800636
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 Radoslavov89c8f432013-03-15 18:50:46 -0700683 if (flowEntryObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000684 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
685 flowEntry.flowEntryId().toString());
686 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800687 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700688 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800689
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 Radoslavovb6f53542013-03-01 16:02:14 -0800702 // - flowEntry.flowEntryUserState()
703 // - flowEntry.flowEntrySwitchState()
704 // - flowEntry.flowEntryErrorState()
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700705 // - flowEntry.matchInPort()
706 // - flowEntry.matchEthernetFrameType()
707 // - flowEntry.matchSrcIPv4Net()
708 // - flowEntry.matchDstIPv4Net()
709 // - flowEntry.matchSrcMac()
710 // - flowEntry.matchDstMac()
711 // - flowEntry.actionOutput()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800712 //
Pankaj Berded0079742013-03-27 17:53:25 -0700713 ISwitchObject sw = conn.utils().searchSwitch(conn,flowEntry.dpid().toString());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800714 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
Pankaj Berded0079742013-03-27 17:53:25 -0700715
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 Radoslavove2f0de82013-03-12 01:39:30 -0700738
739 for (FlowEntryAction fa : flowEntry.flowEntryActions()) {
Pankaj Berded0079742013-03-27 17:53:25 -0700740 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 Radoslavove2f0de82013-03-12 01:39:30 -0700746 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800747 // 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 Radoslavovede97582013-03-08 18:57:28 -0800754 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800755 // 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 Radoslavov89c8f432013-03-15 18:50:46 -0700773
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800774 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 Radoslavov571cff92013-03-20 02:01:32 -0700785 if (flowId.value() == measurementFlowId) {
786 modifiedMeasurementFlowTime = System.nanoTime();
787 }
788
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800789 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 Radoslavov89c8f432013-03-15 18:50:46 -0700809 if (flowObj == null) {
810 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800811 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700812 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800813
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 Radoslavov9e5344c2013-02-18 09:58:30 -0800831 return true;
832 }
833
834 /**
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700835 * 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 Radoslavov89c8f432013-03-15 18:50:46 -0700857 if (flowObj == null) {
858 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700859 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700860 }
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700861
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 Radoslavov9e5344c2013-02-18 09:58:30 -0800878 * Get a previously added flow.
879 *
880 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800881 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800882 */
883 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800884 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 Radoslavov89c8f432013-03-15 18:50:46 -0700900 if (flowObj == null) {
901 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800902 return null; // Flow not found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700903 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800904
905 //
906 // Extract the Flow state
907 //
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800908 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 Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700933 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800934
935 if (allFlows == null) {
936 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700937 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800938 }
939
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800940 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 Radoslavovc4e76a62013-03-06 10:52:41 -0800944 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800945 //
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 Radoslavov706df052013-03-06 10:49:07 -0800959 } 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 Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700981 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
982 ArrayList<FlowPath> allFlows = getAllFlows();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800983
984 if (allFlows == null) {
985 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700986 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800987 }
988
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800989 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 Radoslavovc4e76a62013-03-06 10:52:41 -0800993 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800994 //
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 Radoslavov706df052013-03-06 10:49:07 -08001006 } else {
1007 log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints);
1008 }
1009
1010 return flowPaths;
1011 }
1012
1013 /**
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001014 * 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 Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001029 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
1030
1031 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001032
1033 if (allFlows == null) {
1034 log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001035 return flowPaths;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001036 }
1037
1038 Collections.sort(allFlows);
1039
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001040 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 Krishnaswamy57a32a92013-03-21 14:21:15 -07001061 } else {
1062 log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows);
1063 }
1064
1065 return flowPaths;
1066 }
1067
1068 /**
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001069 * 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 Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001076 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001077
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 Radoslavov89c8f432013-03-15 18:50:46 -07001089 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
1090 conn.endTx(Transaction.COMMIT);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001091 return flowPaths; // No Flows found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001092 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001093
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001094 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 Radoslavovb6f53542013-03-01 16:02:14 -08001114 FlowPath flowPath = new FlowPath();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001115
1116 //
1117 // Extract the Flow state
1118 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001119 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 Radoslavove2f0de82013-03-12 01:39:30 -07001134
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 Radoslavovb6f53542013-03-01 16:02:14 -08001171 String userState = flowEntryObj.getUserState();
1172 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
1173 String switchState = flowEntryObj.getSwitchState();
1174 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
1175 //
Pavlin Radoslavovede97582013-03-08 18:57:28 -08001176 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001177 // and FlowEntryErrorState.
1178 //
1179 flowPath.dataPath().flowEntries().add(flowEntry);
1180 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001181
1182 return flowPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001183 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001184
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 Radoslavov9e5344c2013-02-18 09:58:30 -08001576}