blob: 815ed123a591442c23632174f9ed3124b230a843 [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;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070025import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
Pankaj Berded0079742013-03-27 17:53:25 -070026import 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 Radoslavovc2877682013-03-27 16:40:07 -07001119 String flowIdStr = flowObj.getFlowId();
1120 String installerIdStr = flowObj.getInstallerId();
1121 String srcSwitchStr = flowObj.getSrcSwitch();
1122 Short srcPortStr = flowObj.getSrcPort();
1123 String dstSwitchStr = flowObj.getDstSwitch();
1124 Short dstPortStr = flowObj.getDstPort();
1125
1126 if ((flowIdStr == null) ||
1127 (installerIdStr == null) ||
1128 (srcSwitchStr == null) ||
1129 (srcPortStr == null) ||
1130 (dstSwitchStr == null) ||
1131 (dstPortStr == null)) {
1132 // TODO: A work-around, becauuse of some bogus database objects
1133 return null;
1134 }
1135
1136 flowPath.setFlowId(new FlowId(flowIdStr));
1137 flowPath.setInstallerId(new CallerId(installerIdStr));
1138 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
1139 flowPath.dataPath().srcPort().setPort(new Port(srcPortStr));
1140 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
1141 flowPath.dataPath().dstPort().setPort(new Port(dstPortStr));
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001142
1143 //
1144 // Extract all Flow Entries
1145 //
1146 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1147 for (IFlowEntry flowEntryObj : flowEntries) {
1148 FlowEntry flowEntry = new FlowEntry();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001149 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1150 String switchDpidStr = flowEntryObj.getSwitchDpid();
1151 String userState = flowEntryObj.getUserState();
1152 String switchState = flowEntryObj.getSwitchState();
1153
1154 if ((flowEntryIdStr == null) ||
1155 (switchDpidStr == null) ||
1156 (userState == null) ||
1157 (switchState == null)) {
1158 // TODO: A work-around, becauuse of some bogus database objects
1159 continue;
1160 }
1161 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
1162 flowEntry.setDpid(new Dpid(switchDpidStr));
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -07001163
1164 //
1165 // Extract the match conditions
1166 //
1167 FlowEntryMatch match = new FlowEntryMatch();
1168 Short matchInPort = flowEntryObj.getMatchInPort();
1169 if (matchInPort != null)
1170 match.enableInPort(new Port(matchInPort));
1171 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1172 if (matchEthernetFrameType != null)
1173 match.enableEthernetFrameType(matchEthernetFrameType);
1174 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1175 if (matchSrcIPv4Net != null)
1176 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1177 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1178 if (matchDstIPv4Net != null)
1179 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1180 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1181 if (matchSrcMac != null)
1182 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1183 String matchDstMac = flowEntryObj.getMatchDstMac();
1184 if (matchDstMac != null)
1185 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1186 flowEntry.setFlowEntryMatch(match);
1187
1188 //
1189 // Extract the actions
1190 //
1191 ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>();
1192 Short actionOutputPort = flowEntryObj.getActionOutput();
1193 if (actionOutputPort != null) {
1194 FlowEntryAction action = new FlowEntryAction();
1195 action.setActionOutput(new Port(actionOutputPort));
1196 actions.add(action);
1197 }
1198 flowEntry.setFlowEntryActions(actions);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001199 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001200 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
1201 //
Pavlin Radoslavovede97582013-03-08 18:57:28 -08001202 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001203 // and FlowEntryErrorState.
1204 //
1205 flowPath.dataPath().flowEntries().add(flowEntry);
1206 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001207
1208 return flowPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001209 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001210
1211 /**
1212 * Add and maintain a shortest-path flow.
1213 *
1214 * NOTE: The Flow Path does NOT contain all flow entries.
1215 * Instead, it contains a single dummy flow entry that is used to
1216 * store the matching condition(s).
1217 * That entry is replaced by the appropriate entries from the
1218 * internally performed shortest-path computation.
1219 *
1220 * @param flowPath the Flow Path with the endpoints and the match
1221 * conditions to install.
1222 * @param flowId the return-by-reference Flow ID as assigned internally.
1223 * @return true on success, otherwise false.
1224 */
1225 @Override
1226 public boolean addAndMaintainShortestPathFlow(FlowPath flowPath,
1227 FlowId flowId) {
1228 //
1229 // Do the shortest path computation
1230 //
1231 DataPath dataPath =
1232 topoRouteService.getShortestPath(flowPath.dataPath().srcPort(),
1233 flowPath.dataPath().dstPort());
1234 if (dataPath == null)
1235 return false;
1236
1237 FlowEntryMatch userFlowEntryMatch = null;
1238 if (! flowPath.dataPath().flowEntries().isEmpty()) {
1239 userFlowEntryMatch = flowPath.dataPath().flowEntries().get(0).flowEntryMatch();
1240 }
1241
1242 //
1243 // Set the incoming port matching and the outgoing port output
1244 // actions for each flow entry.
1245 //
1246 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1247 // Set the incoming port matching
1248 FlowEntryMatch flowEntryMatch = null;
1249 if (userFlowEntryMatch != null)
1250 flowEntryMatch = new FlowEntryMatch(userFlowEntryMatch);
1251 else
1252 flowEntryMatch = new FlowEntryMatch();
1253 flowEntry.setFlowEntryMatch(flowEntryMatch);
1254 flowEntryMatch.enableInPort(flowEntry.inPort());
1255
1256 // Set the outgoing port output action
1257 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1258 if (flowEntryActions == null) {
1259 flowEntryActions = new ArrayList<FlowEntryAction>();
1260 flowEntry.setFlowEntryActions(flowEntryActions);
1261 }
1262 FlowEntryAction flowEntryAction = new FlowEntryAction();
1263 flowEntryAction.setActionOutput(flowEntry.outPort());
1264 flowEntryActions.add(flowEntryAction);
1265 }
1266
1267 //
1268 // Prepare the computed Flow Path
1269 //
1270 FlowPath resultFlowPath = new FlowPath();
1271 resultFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1272 resultFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1273 resultFlowPath.setDataPath(dataPath);
1274
1275 boolean returnValue = addFlow(resultFlowPath, flowId);
1276
1277 // TODO: Mark the flow for maintenance purpose
1278
1279 return (returnValue);
1280 }
1281
1282 /**
1283 * Create a Flow from port to port.
1284 *
1285 * TODO: We don't need it for now.
1286 *
1287 * @param src_port the source port.
1288 * @param dest_port the destination port.
1289 */
1290 public void createFlow(IPortObject src_port, IPortObject dest_port) {
1291 // TODO: We don't need it for now.
1292 }
1293
1294 /**
1295 * Get all Flows matching a source and a destination port.
1296 *
1297 * TODO: Pankaj might be implementing it later.
1298 *
1299 * @param src_port the source port to match.
1300 * @param dest_port the destination port to match.
1301 * @return all flows matching the source and the destination port.
1302 */
1303 public Iterable<FlowPath> getFlows(IPortObject src_port,
1304 IPortObject dest_port) {
1305 // TODO: Pankaj might be implementing it later.
1306 return null;
1307 }
1308
1309 /**
1310 * Get all Flows going out from a port.
1311 *
1312 * TODO: We need it now: Pankaj
1313 *
1314 * @param port the port to match.
1315 * @return the list of flows that are going out from the port.
1316 */
1317 public Iterable<FlowPath> getFlows(IPortObject port) {
1318 // TODO: We need it now: Pankaj
1319 return null;
1320 }
1321
1322 /**
1323 * Reconcile all flows on inactive port (src port of link which might be
1324 * broken).
1325 *
1326 * TODO: We need it now: Pavlin
1327 *
1328 * @param src_port the port that has become inactive.
1329 */
1330 public void reconcileFlows(IPortObject src_port) {
1331 // TODO: We need it now: Pavlin
1332
1333 // TODO: It should call installFlowEntry() as appropriate.
1334 }
1335
1336 /**
1337 * Reconcile all flows between a source and a destination port.
1338 *
1339 * TODO: We don't need it for now.
1340 *
1341 * @param src_port the source port.
1342 * @param dest_port the destination port.
1343 */
1344 public void reconcileFlow(IPortObject src_port, IPortObject dest_port) {
1345 // TODO: We don't need it for now.
1346 }
1347
1348 /**
1349 * Compute the shortest path between a source and a destination ports.
1350 *
1351 * @param src_port the source port.
1352 * @param dest_port the destination port.
1353 * @return the computed shortest path between the source and the
1354 * destination ports. The flow entries in the path itself would
1355 * contain the incoming port matching and the outgoing port output
1356 * actions set. However, the path itself will NOT have the Flow ID,
1357 * Installer ID, and any additional matching conditions for the
1358 * flow entries (e.g., source or destination MAC address, etc).
1359 */
1360 public FlowPath computeFlowPath(IPortObject src_port,
1361 IPortObject dest_port) {
1362 //
1363 // Prepare the arguments
1364 //
1365 String dpidStr = src_port.getSwitch().getDPID();
1366 Dpid srcDpid = new Dpid(dpidStr);
1367 Port srcPort = new Port(src_port.getNumber());
1368
1369 dpidStr = dest_port.getSwitch().getDPID();
1370 Dpid dstDpid = new Dpid(dpidStr);
1371 Port dstPort = new Port(dest_port.getNumber());
1372
1373 SwitchPort src = new SwitchPort(srcDpid, srcPort);
1374 SwitchPort dst = new SwitchPort(dstDpid, dstPort);
1375
1376 //
1377 // Do the shortest path computation
1378 //
1379 DataPath dataPath = topoRouteService.getShortestPath(src, dst);
1380 if (dataPath == null)
1381 return null;
1382
1383 //
1384 // Set the incoming port matching and the outgoing port output
1385 // actions for each flow entry.
1386 //
1387 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1388 // Set the incoming port matching
1389 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
1390 if (flowEntryMatch == null) {
1391 flowEntryMatch = new FlowEntryMatch();
1392 flowEntry.setFlowEntryMatch(flowEntryMatch);
1393 }
1394 flowEntryMatch.enableInPort(flowEntry.inPort());
1395
1396 // Set the outgoing port output action
1397 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1398 if (flowEntryActions == null) {
1399 flowEntryActions = new ArrayList<FlowEntryAction>();
1400 flowEntry.setFlowEntryActions(flowEntryActions);
1401 }
1402 FlowEntryAction flowEntryAction = new FlowEntryAction();
1403 flowEntryAction.setActionOutput(flowEntry.outPort());
1404 flowEntryActions.add(flowEntryAction);
1405 }
1406
1407 //
1408 // Prepare the return result
1409 //
1410 FlowPath flowPath = new FlowPath();
1411 flowPath.setDataPath(dataPath);
1412
1413 return flowPath;
1414 }
1415
1416 /**
1417 * Get all Flow Entries of a Flow.
1418 *
1419 * @param flow the flow whose flow entries should be returned.
1420 * @return the flow entries of the flow.
1421 */
1422 public Iterable<FlowEntry> getFlowEntries(FlowPath flow) {
1423 return flow.dataPath().flowEntries();
1424 }
1425
1426 /**
1427 * Install a Flow Entry on a switch.
1428 *
1429 * @param mySwitches the DPID-to-Switch mapping for the switches
1430 * controlled by this controller.
1431 * @param flowEntry the flow entry to install.
1432 * @return true on success, otherwise false.
1433 */
1434 public boolean installFlowEntry(Map<Long, IOFSwitch> mySwitches,
1435 FlowEntry flowEntry) {
1436 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
1437 if (mySwitch == null) {
1438 // Not my switch
1439 return (installRemoteFlowEntry(flowEntry));
1440 }
1441
1442 //
1443 // Create the OpenFlow Flow Modification Entry to push
1444 //
1445 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1446 .getMessage(OFType.FLOW_MOD);
1447 long cookie = flowEntry.flowEntryId().value();
1448
1449 short flowModCommand = OFFlowMod.OFPFC_ADD;
1450 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
1451 flowModCommand = OFFlowMod.OFPFC_ADD;
1452 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
1453 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1454 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
1455 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1456 } else {
1457 // Unknown user state. Ignore the entry
1458 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1459 flowEntry.flowEntryId().toString(),
1460 flowEntry.flowEntryUserState());
1461 return false;
1462 }
1463
1464 //
1465 // Fetch the match conditions
1466 //
1467 OFMatch match = new OFMatch();
1468 match.setWildcards(OFMatch.OFPFW_ALL);
1469 Port matchInPort = flowEntry.flowEntryMatch().inPort();
1470 if (matchInPort != null) {
1471 match.setInputPort(matchInPort.value());
1472 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1473 }
1474 Short matchEthernetFrameType =
1475 flowEntry.flowEntryMatch().ethernetFrameType();
1476 if (matchEthernetFrameType != null) {
1477 match.setDataLayerType(matchEthernetFrameType);
1478 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1479 }
1480 IPv4Net matchSrcIPv4Net = flowEntry.flowEntryMatch().srcIPv4Net();
1481 if (matchSrcIPv4Net != null) {
1482 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
1483 }
1484 IPv4Net matchDstIPv4Net = flowEntry.flowEntryMatch().dstIPv4Net();
1485 if (matchDstIPv4Net != null) {
1486 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
1487 }
1488 MACAddress matchSrcMac = flowEntry.flowEntryMatch().srcMac();
1489 if (matchSrcMac != null) {
1490 match.setDataLayerSource(matchSrcMac.toString());
1491 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1492 }
1493 MACAddress matchDstMac = flowEntry.flowEntryMatch().dstMac();
1494 if (matchDstMac != null) {
1495 match.setDataLayerDestination(matchDstMac.toString());
1496 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1497 }
1498
1499 //
1500 // Fetch the actions
1501 //
1502 // TODO: For now we support only the "OUTPUT" actions.
1503 //
1504 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1505 List<OFAction> actions = new ArrayList<OFAction>();
1506 ArrayList<FlowEntryAction> flowEntryActions =
1507 flowEntry.flowEntryActions();
1508 for (FlowEntryAction flowEntryAction : flowEntryActions) {
1509 FlowEntryAction.ActionOutput actionOutput =
1510 flowEntryAction.actionOutput();
1511 if (actionOutput != null) {
1512 short actionOutputPort = actionOutput.port().value();
1513 OFActionOutput action = new OFActionOutput();
1514 // XXX: The max length is hard-coded for now
1515 action.setMaxLength((short)0xffff);
1516 action.setPort(actionOutputPort);
1517 actions.add(action);
1518 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1519 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1520 fm.setOutPort(actionOutputPort);
1521 }
1522 }
1523 }
1524
1525 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1526 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1527 .setPriority(PRIORITY_DEFAULT)
1528 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1529 .setCookie(cookie)
1530 .setCommand(flowModCommand)
1531 .setMatch(match)
1532 .setActions(actions)
1533 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1534
1535 //
1536 // TODO: Set the following flag
1537 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1538 // See method ForwardingBase::pushRoute()
1539 //
1540
1541 //
1542 // Write the message to the switch
1543 //
1544 try {
1545 messageDamper.write(mySwitch, fm, null);
1546 mySwitch.flush();
1547 } catch (IOException e) {
1548 log.error("Failure writing flow mod from network map", e);
1549 return false;
1550 }
1551 return true;
1552 }
1553
1554 /**
1555 * Remove a Flow Entry from a switch.
1556 *
1557 * TODO: We need it now: Pavlin
1558 * - Remove only for local switches
1559 * - It will call the removeRemoteFlowEntry() for remote switches.
1560 * - To be called by reconcileFlow()
1561 *
1562 * @param entry the flow entry to remove.
1563 */
1564 public void removeFlowEntry(FlowEntry entry) {
1565 // TODO: We need it now: Pavlin
1566 // - Remove only for local switches
1567 // - It will call the removeRemoteFlowEntry() for remote switches.
1568 // - To be called by reconcileFlow()
1569 }
1570
1571 /**
1572 * Install a Flow Entry on a remote controller.
1573 *
1574 * TODO: We need it now: Jono
1575 * - For now it will make a REST call to the remote controller.
1576 * - Internally, it needs to know the name of the remote controller.
1577 *
1578 * @param entry the flow entry to install.
1579 * @return true on success, otherwise false.
1580 */
1581 public boolean installRemoteFlowEntry(FlowEntry entry) {
1582 // TODO: We need it now: Jono
1583 // - For now it will make a REST call to the remote controller.
1584 // - Internally, it needs to know the name of the remote controller.
1585 return true;
1586 }
1587
1588 /**
1589 * Remove a flow entry on a remote controller.
1590 *
1591 * TODO: We need it now: Jono
1592 * - For now it will make a REST call to the remote controller.
1593 * - Internally, it needs to know the name of the remote controller.
1594 *
1595 * @param entry the flow entry to remove.
1596 */
1597 public void removeRemoteFlowEntry(FlowEntry entry) {
1598 // TODO: We need it now: Jono
1599 // - For now it will make a REST call to the remote controller.
1600 // - Internally, it needs to know the name of the remote controller.
1601 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001602}