blob: 60366781c904f9bc3d241c824d5dc389ee192bfc [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 Radoslavov0b22d0e2013-04-02 01:12:46 +00006import java.util.Collections;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08007import java.util.EnumSet;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08008import java.util.HashMap;
Pavlin Radoslavove0575292013-03-28 05:35:25 -07009import java.util.HashSet;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +000010import java.util.LinkedList;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080011import java.util.List;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080012import java.util.Map;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000013import java.util.Random;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070014import java.util.TreeMap;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070015import java.util.concurrent.BlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080016import java.util.concurrent.Executors;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070017import java.util.concurrent.LinkedBlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080018import java.util.concurrent.ScheduledExecutorService;
19import java.util.concurrent.ScheduledFuture;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070020import java.util.concurrent.ThreadPoolExecutor;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080021import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080022
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080023import net.floodlightcontroller.core.IFloodlightProviderService;
24import net.floodlightcontroller.core.INetMapStorage;
25import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
26import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070027import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
Pankaj Berded0079742013-03-27 17:53:25 -070028import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070029import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080030import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080031import net.floodlightcontroller.core.module.FloodlightModuleContext;
32import net.floodlightcontroller.core.module.FloodlightModuleException;
33import net.floodlightcontroller.core.module.IFloodlightModule;
34import net.floodlightcontroller.core.module.IFloodlightService;
35import net.floodlightcontroller.flowcache.IFlowService;
36import net.floodlightcontroller.flowcache.web.FlowWebRoutable;
37import net.floodlightcontroller.restserver.IRestApiService;
38import net.floodlightcontroller.util.CallerId;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080039import net.floodlightcontroller.util.DataPath;
40import net.floodlightcontroller.util.Dpid;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080041import net.floodlightcontroller.util.DataPathEndpoints;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080042import net.floodlightcontroller.util.FlowEntry;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070043import net.floodlightcontroller.util.FlowEntryAction;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080044import net.floodlightcontroller.util.FlowEntryId;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070045import net.floodlightcontroller.util.FlowEntryMatch;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080046import net.floodlightcontroller.util.FlowEntrySwitchState;
47import net.floodlightcontroller.util.FlowEntryUserState;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080048import net.floodlightcontroller.util.FlowId;
49import net.floodlightcontroller.util.FlowPath;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070050import net.floodlightcontroller.util.IPv4Net;
51import net.floodlightcontroller.util.MACAddress;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080052import net.floodlightcontroller.util.OFMessageDamper;
53import net.floodlightcontroller.util.Port;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070054import net.floodlightcontroller.util.SwitchPort;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070055import net.onrc.onos.flow.IFlowManager;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080056import net.onrc.onos.util.GraphDBConnection;
57import net.onrc.onos.util.GraphDBConnection.Transaction;
58
59import org.openflow.protocol.OFFlowMod;
60import org.openflow.protocol.OFMatch;
61import org.openflow.protocol.OFPacketOut;
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070062import org.openflow.protocol.OFPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080063import org.openflow.protocol.OFType;
64import org.openflow.protocol.action.OFAction;
65import org.openflow.protocol.action.OFActionOutput;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080066
67import org.slf4j.Logger;
68import org.slf4j.LoggerFactory;
69
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070070public class FlowManager implements IFloodlightModule, IFlowService, IFlowManager, INetMapStorage {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080071
72 public GraphDBConnection conn;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080073
74 protected IRestApiService restApi;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080075 protected IFloodlightProviderService floodlightProvider;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070076 protected ITopoRouteService topoRouteService;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070077 protected FloodlightModuleContext context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080078
79 protected OFMessageDamper messageDamper;
80
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070081 //
82 // TODO: Values copied from elsewhere (class LearningSwitch).
83 // The local copy should go away!
84 //
85 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
86 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
87 public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity
88 public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite
89 public static final short PRIORITY_DEFAULT = 100;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080090
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000091 // Flow Entry ID generation state
92 private static Random randomGenerator = new Random();
93 private static int nextFlowEntryIdPrefix = 0;
94 private static int nextFlowEntryIdSuffix = 0;
95 private static long nextFlowEntryId = 0;
96
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070097 private static long measurementFlowId = 100000;
98 private static String measurementFlowIdStr = "0x186a0"; // 100000
99 private long modifiedMeasurementFlowTime = 0;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700100
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800101 /** The logger. */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800102 private static Logger log = LoggerFactory.getLogger(FlowManager.class);
103
104 // The periodic task(s)
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700105 private final ScheduledExecutorService measureShortestPathScheduler =
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800106 Executors.newScheduledThreadPool(1);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700107 private final ScheduledExecutorService measureMapReaderScheduler =
108 Executors.newScheduledThreadPool(1);
109 private final ScheduledExecutorService mapReaderScheduler =
110 Executors.newScheduledThreadPool(1);
111
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700112 private BlockingQueue<Runnable> shortestPathQueue = new LinkedBlockingQueue<Runnable>();
113 private ThreadPoolExecutor shortestPathExecutor =
114 new ThreadPoolExecutor(10, 10, 5, TimeUnit.SECONDS, shortestPathQueue);
115
116 class ShortestPathTask implements Runnable {
117 private int hint;
118 private ITopoRouteService topoRouteService;
119 private ArrayList<DataPath> dpList;
120
121 public ShortestPathTask(int hint,
122 ITopoRouteService topoRouteService,
123 ArrayList<DataPath> dpList) {
124 this.hint = hint;
125 this.topoRouteService = topoRouteService;
126 this.dpList = dpList;
127 }
128
129 @Override
130 public void run() {
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700131 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700132 String logMsg = "MEASUREMENT: Running Thread hint " + this.hint;
133 log.debug(logMsg);
134 long startTime = System.nanoTime();
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700135 */
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700136 for (DataPath dp : this.dpList) {
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700137 topoRouteService.getTopoShortestPath(dp.srcPort(), dp.dstPort());
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700138 }
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700139 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700140 long estimatedTime = System.nanoTime() - startTime;
141 double rate = (estimatedTime > 0)? ((double)dpList.size() * 1000000000) / estimatedTime: 0.0;
142 logMsg = "MEASUREMENT: Computed Thread hint " + hint + ": " + dpList.size() + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s";
143 log.debug(logMsg);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700144 */
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700145 }
146 }
147
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700148 final Runnable measureShortestPath = new Runnable() {
149 public void run() {
150 log.debug("Recomputing Shortest Paths from the Network Map Flows...");
151 if (floodlightProvider == null) {
152 log.debug("FloodlightProvider service not found!");
153 return;
154 }
155
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700156 if (topoRouteService == null) {
157 log.debug("Topology Route Service not found");
158 return;
159 }
160
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700161 int leftoverQueueSize = shortestPathExecutor.getQueue().size();
162 if (leftoverQueueSize > 0) {
163 String logMsg = "MEASUREMENT: Leftover Shortest Path Queue Size: " + leftoverQueueSize;
164 log.debug(logMsg);
165 return;
166 }
167 log.debug("MEASUREMENT: Beginning Shortest Path Computation");
168
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700169 //
170 // Recompute the Shortest Paths for all Flows
171 //
172 int counter = 0;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700173 int hint = 0;
174 ArrayList<DataPath> dpList = new ArrayList<DataPath>();
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700175 long startTime = System.nanoTime();
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700176
177 topoRouteService.prepareShortestPathTopo();
178
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700179 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
180 for (IFlowPath flowPathObj : allFlowPaths) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700181 FlowId flowId = new FlowId(flowPathObj.getFlowId());
182
183 // log.debug("Found Path {}", flowId.toString());
184 Dpid srcDpid = new Dpid(flowPathObj.getSrcSwitch());
185 Port srcPort = new Port(flowPathObj.getSrcPort());
186 Dpid dstDpid = new Dpid(flowPathObj.getDstSwitch());
187 Port dstPort = new Port(flowPathObj.getDstPort());
188 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
189 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700190
191 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700192 DataPath dp = new DataPath();
193 dp.setSrcPort(srcSwitchPort);
194 dp.setDstPort(dstSwitchPort);
195 dpList.add(dp);
196 if ((dpList.size() % 10) == 0) {
197 shortestPathExecutor.execute(
198 new ShortestPathTask(hint, topoRouteService,
199 dpList));
200 dpList = new ArrayList<DataPath>();
201 hint++;
202 }
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700203 */
204
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700205 DataPath dataPath =
206 topoRouteService.getTopoShortestPath(srcSwitchPort,
207 dstSwitchPort);
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700208 counter++;
209 }
210 if (dpList.size() > 0) {
211 shortestPathExecutor.execute(
212 new ShortestPathTask(hint, topoRouteService,
213 dpList));
214 }
215
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700216 /*
Pavlin Radoslavov4da61282013-03-20 20:31:36 -0700217 // Wait for all tasks to finish
218 try {
219 while (shortestPathExecutor.getQueue().size() > 0) {
220 Thread.sleep(100);
221 }
222 } catch (InterruptedException ex) {
223 log.debug("MEASUREMENT: Shortest Path Computation interrupted");
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700224 }
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700225 */
226
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700227 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700228 topoRouteService.dropShortestPathTopo();
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700229
230 long estimatedTime = System.nanoTime() - startTime;
231 double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0;
232 String logMsg = "MEASUREMENT: Computed " + counter + " shortest paths in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " flows/s";
233 log.debug(logMsg);
234 }
235 };
236
237 final Runnable measureMapReader = new Runnable() {
238 public void run() {
239 if (floodlightProvider == null) {
240 log.debug("FloodlightProvider service not found!");
241 return;
242 }
243
244 //
245 // Fetch all Flow Entries
246 //
247 int counter = 0;
248 long startTime = System.nanoTime();
249 Iterable<IFlowEntry> allFlowEntries = conn.utils().getAllFlowEntries(conn);
250 for (IFlowEntry flowEntryObj : allFlowEntries) {
251 counter++;
252 FlowEntryId flowEntryId =
253 new FlowEntryId(flowEntryObj.getFlowEntryId());
254 String userState = flowEntryObj.getUserState();
255 String switchState = flowEntryObj.getSwitchState();
256 }
257 conn.endTx(Transaction.COMMIT);
258
259 long estimatedTime = System.nanoTime() - startTime;
260 double rate = (estimatedTime > 0)? ((double)counter * 1000000000) / estimatedTime: 0.0;
261 String logMsg = "MEASUREMENT: Fetched " + counter + " flow entries in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " entries/s";
262 log.debug(logMsg);
263 }
264 };
265
266 final Runnable mapReader = new Runnable() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800267 public void run() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800268 if (floodlightProvider == null) {
269 log.debug("FloodlightProvider service not found!");
270 return;
271 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000272 Map<Long, IOFSwitch> mySwitches =
273 floodlightProvider.getSwitches();
274 Map<Long, IFlowEntry> myFlowEntries =
275 new TreeMap<Long, IFlowEntry>();
276 LinkedList<IFlowEntry> deleteFlowEntries =
277 new LinkedList<IFlowEntry>();
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700278
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700279
280 //
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700281 // Fetch all Flow Entries and select only my Flow Entries
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000282 // that need to be undated into the switches.
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700283 //
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000284 Iterable<IFlowEntry> allFlowEntries =
285 conn.utils().getAllFlowEntries(conn);
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700286 for (IFlowEntry flowEntryObj : allFlowEntries) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000287 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700288 String userState = flowEntryObj.getUserState();
289 String switchState = flowEntryObj.getSwitchState();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000290 String dpidStr = flowEntryObj.getSwitchDpid();
291 if ((flowEntryIdStr == null) ||
292 (userState == null) ||
293 (switchState == null) ||
294 (dpidStr == null)) {
295 log.debug("IGNORING Flow Entry entry with null fields");
296 continue;
297 }
298 FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr);
299 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800300
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000301 /*
302 log.debug("Found Flow Entry Id = {} {}",
Pavlin Radoslavov79a67c12013-03-15 21:05:53 -0700303 flowEntryId.toString(),
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000304 "DPID = " + dpid.toString() +
305 " User State: " + userState +
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700306 " Switch State: " + switchState);
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000307 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800308
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000309 if (! switchState.equals("FE_SWITCH_NOT_UPDATED"))
310 continue; // Ignore the entry: nothing to do
311
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800312 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000313 if (mySwitch == null)
314 continue; // Ignore the entry: not my switch
315
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700316 myFlowEntries.put(flowEntryId.value(), flowEntryObj);
317 }
318
319 //
320 // Process my Flow Entries
321 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700322 boolean processed_measurement_flow = false;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700323 for (Map.Entry<Long, IFlowEntry> entry : myFlowEntries.entrySet()) {
324 IFlowEntry flowEntryObj = entry.getValue();
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700325 // Code for measurement purpose
326 {
327 IFlowPath flowObj =
328 conn.utils().getFlowPathByFlowEntry(conn,
329 flowEntryObj);
330 if ((flowObj != null) &&
331 flowObj.getFlowId().equals(measurementFlowIdStr)) {
332 processed_measurement_flow = true;
333 }
334 }
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700335
336 //
337 // TODO: Eliminate the re-fetching of flowEntryId,
338 // userState, switchState, and dpid from the flowEntryObj.
339 //
340 FlowEntryId flowEntryId =
341 new FlowEntryId(flowEntryObj.getFlowEntryId());
342 Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid());
343 String userState = flowEntryObj.getUserState();
344 String switchState = flowEntryObj.getSwitchState();
345 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000346 if (mySwitch == null)
347 continue; // Shouldn't happen
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800348
349 //
350 // Create the Open Flow Flow Modification Entry to push
351 //
352 OFFlowMod fm =
353 (OFFlowMod) floodlightProvider.getOFMessageFactory()
354 .getMessage(OFType.FLOW_MOD);
355 long cookie = flowEntryId.value();
356
357 short flowModCommand = OFFlowMod.OFPFC_ADD;
358 if (userState.equals("FE_USER_ADD")) {
359 flowModCommand = OFFlowMod.OFPFC_ADD;
360 } else if (userState.equals("FE_USER_MODIFY")) {
361 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
362 } else if (userState.equals("FE_USER_DELETE")) {
363 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
364 } else {
365 // Unknown user state. Ignore the entry
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700366 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
367 flowEntryId.toString(), userState);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800368 continue;
369 }
370
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700371 //
372 // Fetch the match conditions
373 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800374 OFMatch match = new OFMatch();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700375 match.setWildcards(OFMatch.OFPFW_ALL);
376 Short matchInPort = flowEntryObj.getMatchInPort();
377 if (matchInPort != null) {
378 match.setInputPort(matchInPort);
379 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
380 }
381 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
382 if (matchEthernetFrameType != null) {
383 match.setDataLayerType(matchEthernetFrameType);
384 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
385 }
386 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
387 if (matchSrcIPv4Net != null) {
388 match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC);
389 }
390 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
391 if (matchDstIPv4Net != null) {
392 match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST);
393 }
394 String matchSrcMac = flowEntryObj.getMatchSrcMac();
395 if (matchSrcMac != null) {
396 match.setDataLayerSource(matchSrcMac);
397 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
398 }
399 String matchDstMac = flowEntryObj.getMatchDstMac();
400 if (matchDstMac != null) {
401 match.setDataLayerDestination(matchDstMac);
402 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
403 }
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700404
405 //
406 // Fetch the actions
407 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800408 List<OFAction> actions = new ArrayList<OFAction>();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700409 Short actionOutputPort = flowEntryObj.getActionOutput();
410 if (actionOutputPort != null) {
411 OFActionOutput action = new OFActionOutput();
412 // XXX: The max length is hard-coded for now
413 action.setMaxLength((short)0xffff);
414 action.setPort(actionOutputPort);
415 actions.add(action);
416 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800417
418 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
419 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -0700420 .setPriority(PRIORITY_DEFAULT)
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800421 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
422 .setCookie(cookie)
423 .setCommand(flowModCommand)
424 .setMatch(match)
425 .setActions(actions)
426 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -0700427 fm.setOutPort(OFPort.OFPP_NONE.getValue());
428 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
429 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
430 if (actionOutputPort != null)
431 fm.setOutPort(actionOutputPort);
432 }
433
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800434 //
435 // TODO: Set the following flag
436 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
437 // See method ForwardingBase::pushRoute()
438 //
439 try {
440 messageDamper.write(mySwitch, fm, null);
441 mySwitch.flush();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000442 //
443 // TODO: We should use the OpenFlow Barrier mechanism
444 // to check for errors, and update the SwitchState
445 // for a flow entry after the Barrier message is
446 // is received.
447 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800448 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
449 if (userState.equals("FE_USER_DELETE")) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000450 // An entry that needs to be deleted.
451 deleteFlowEntries.add(flowEntryObj);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800452 }
453 } catch (IOException e) {
454 log.error("Failure writing flow mod from network map", e);
455 }
456 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000457
458 //
459 // Delete all entries marked for deletion
460 //
461 // TODO: We should use the OpenFlow Barrier mechanism
462 // to check for errors, and delete the Flow Entries after the
463 // Barrier message is received.
464 //
465 while (! deleteFlowEntries.isEmpty()) {
466 IFlowEntry flowEntryObj = deleteFlowEntries.poll();
467 IFlowPath flowObj =
468 conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj);
469 if (flowObj == null) {
470 log.debug("Did not find FlowPath to be deleted");
471 continue;
472 }
473 flowObj.removeFlowEntry(flowEntryObj);
474 conn.utils().removeFlowEntry(conn, flowEntryObj);
475
476 // Test whether the last flow entry
477 Iterable<IFlowEntry> tmpflowEntries =
478 flowObj.getFlowEntries();
479 boolean found = false;
480 for (IFlowEntry tmpflowEntryObj : tmpflowEntries) {
481 found = true;
482 break;
483 }
484 if (! found) {
485 // Remove the Flow Path as well
486 conn.utils().removeFlowPath(conn, flowObj);
487 }
488 }
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700489
490
491 //
492 // Fetch and recompute the Shortest Path for those
493 // Flow Paths this controller is responsible for.
494 //
495
496 /*
497 * TODO: For now, the computation of the reconciliation is
498 * commented-out.
499 */
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700500 topoRouteService.prepareShortestPathTopo();
501 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
502 HashSet<IFlowPath> flowObjSet = new HashSet<IFlowPath>();
503 for (IFlowPath flowPathObj : allFlowPaths) {
504 if (flowPathObj == null)
505 continue;
506 String dataPathSummaryStr = flowPathObj.getDataPathSummary();
507 if (dataPathSummaryStr == null)
508 continue; // Could be invalid entry?
509 if (dataPathSummaryStr.isEmpty())
510 continue; // No need to maintain this flow
511
512 // Fetch the fields needed to recompute the shortest path
513 String flowIdStr = flowPathObj.getFlowId();
514 String srcDpidStr = flowPathObj.getSrcSwitch();
515 Short srcPortShort = flowPathObj.getSrcPort();
516 String dstDpidStr = flowPathObj.getDstSwitch();
517 Short dstPortShort = flowPathObj.getDstPort();
518 if ((flowIdStr == null) ||
519 (srcDpidStr == null) ||
520 (srcPortShort == null) ||
521 (dstDpidStr == null) ||
522 (dstPortShort == null)) {
523 log.debug("IGNORING Flow Path entry with null fields");
524 continue;
525 }
526
527 FlowId flowId = new FlowId(flowIdStr);
528 Dpid srcDpid = new Dpid(srcDpidStr);
529 Port srcPort = new Port(srcPortShort);
530 Dpid dstDpid = new Dpid(dstDpidStr);
531 Port dstPort = new Port(dstPortShort);
532 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
533 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700534 //
535 // NOTE: Using here the regular getShortestPath() method
536 // won't work here, because that method calls internally
537 // "conn.endTx(Transaction.COMMIT)", and that will
538 // invalidate all handlers to the Titan database.
539 // If we want to experiment with calling here
540 // getShortestPath(), we need to refactor that code
541 // to avoid closing the transaction.
542 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700543 DataPath dataPath =
544 topoRouteService.getTopoShortestPath(srcSwitchPort,
545 dstSwitchPort);
546 String newDataPathSummaryStr = dataPath.dataPathSummary();
547 if (dataPathSummaryStr.equals(newDataPathSummaryStr))
548 continue; // Nothing changed
549
550 //
551 // Use the source DPID as a heuristic to decide
552 // which controller is responsible for maintaining the
553 // shortest path.
554 // NOTE: This heuristic is error-prone: if the switch
555 // goes away and no controller is responsible for that
556 // switch, then the original Flow Path is not cleaned-up
557 //
558 IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
559 if (mySwitch == null)
560 continue; // Ignore: not my responsibility
561
562 log.debug("RECONCILE: Need to Reconcile Shortest Path for FlowID {}",
563 flowId.toString());
564 flowObjSet.add(flowPathObj);
565 }
566 reconcileFlows(flowObjSet);
567 topoRouteService.dropShortestPathTopo();
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700568
569
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800570 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700571
572 if (processed_measurement_flow) {
573 long estimatedTime = System.nanoTime() - modifiedMeasurementFlowTime;
574 String logMsg = "MEASUREMENT: Pushed Flow delay: " +
575 (double)estimatedTime / 1000000000 + " sec";
576 log.debug(logMsg);
577 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800578 }
579 };
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700580
581 /*
582 final ScheduledFuture<?> measureShortestPathHandle =
583 measureShortestPathScheduler.scheduleAtFixedRate(measureShortestPath, 10, 10, TimeUnit.SECONDS);
584 */
585
Pavlin Radoslavove38319c2013-03-21 16:20:00 -0700586 /*
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700587 final ScheduledFuture<?> measureMapReaderHandle =
588 measureMapReaderScheduler.scheduleAtFixedRate(measureMapReader, 10, 10, TimeUnit.SECONDS);
Pavlin Radoslavove38319c2013-03-21 16:20:00 -0700589 */
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700590
591 final ScheduledFuture<?> mapReaderHandle =
592 mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800593
594 @Override
595 public void init(String conf) {
596 conn = GraphDBConnection.getInstance(conf);
597 }
598
599 public void finalize() {
600 close();
601 }
602
603 @Override
604 public void close() {
605 conn.close();
606 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800607
608 @Override
609 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
610 Collection<Class<? extends IFloodlightService>> l =
611 new ArrayList<Class<? extends IFloodlightService>>();
612 l.add(IFlowService.class);
613 return l;
614 }
615
616 @Override
617 public Map<Class<? extends IFloodlightService>, IFloodlightService>
618 getServiceImpls() {
619 Map<Class<? extends IFloodlightService>,
620 IFloodlightService> m =
621 new HashMap<Class<? extends IFloodlightService>,
622 IFloodlightService>();
623 m.put(IFlowService.class, this);
624 return m;
625 }
626
627 @Override
628 public Collection<Class<? extends IFloodlightService>>
629 getModuleDependencies() {
630 Collection<Class<? extends IFloodlightService>> l =
631 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800632 l.add(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700633 l.add(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800634 l.add(IRestApiService.class);
635 return l;
636 }
637
638 @Override
639 public void init(FloodlightModuleContext context)
640 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700641 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800642 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700643 topoRouteService = context.getServiceImpl(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800644 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800645 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
646 EnumSet.of(OFType.FLOW_MOD),
647 OFMESSAGE_DAMPER_TIMEOUT);
648 // TODO: An ugly hack!
649 String conf = "/tmp/cassandra.titan";
650 this.init(conf);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800651 }
652
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000653 private long getNextFlowEntryId() {
654 //
655 // Generate the next Flow Entry ID.
656 // NOTE: For now, the higher 32 bits are random, and
657 // the lower 32 bits are sequential.
658 // In the future, we need a better allocation mechanism.
659 //
660 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
661 nextFlowEntryIdPrefix = randomGenerator.nextInt();
662 nextFlowEntryIdSuffix = 0;
663 } else {
664 nextFlowEntryIdSuffix++;
665 }
666 long result = (long)nextFlowEntryIdPrefix << 32;
667 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
668 return result;
669 }
670
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800671 @Override
672 public void startUp(FloodlightModuleContext context) {
673 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov80ca6302013-03-20 02:08:09 -0700674
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000675 // Initialize the Flow Entry ID generator
676 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800677 }
678
679 /**
680 * Add a flow.
681 *
682 * Internally, ONOS will automatically register the installer for
683 * receiving Flow Path Notifications for that path.
684 *
685 * @param flowPath the Flow Path to install.
686 * @param flowId the return-by-reference Flow ID as assigned internally.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700687 * @param dataPathSummaryStr the data path summary string if the added
688 * flow will be maintained internally, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800689 * @return true on success, otherwise false.
690 */
691 @Override
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700692 public boolean addFlow(FlowPath flowPath, FlowId flowId,
693 String dataPathSummaryStr) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700694 if (flowPath.flowId().value() == measurementFlowId) {
695 modifiedMeasurementFlowTime = System.nanoTime();
696 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800697
698 //
699 // Assign the FlowEntry IDs
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700700 // Right now every new flow entry gets a new flow entry ID
701 // TODO: This needs to be redesigned!
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800702 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800703 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000704 long id = getNextFlowEntryId();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800705 flowEntry.setFlowEntryId(new FlowEntryId(id));
706 }
707
708 IFlowPath flowObj = null;
709 try {
710 if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId()))
711 != null) {
712 log.debug("Adding FlowPath with FlowId {}: found existing FlowPath",
713 flowPath.flowId().toString());
714 } else {
715 flowObj = conn.utils().newFlowPath(conn);
716 log.debug("Adding FlowPath with FlowId {}: creating new FlowPath",
717 flowPath.flowId().toString());
718 }
719 } catch (Exception e) {
720 // TODO: handle exceptions
721 conn.endTx(Transaction.ROLLBACK);
722 log.error(":addFlow FlowId:{} failed",
723 flowPath.flowId().toString());
724 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700725 if (flowObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000726 log.error(":addFlow FlowId:{} failed: Flow object not created",
727 flowPath.flowId().toString());
728 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800729 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700730 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800731
732 //
733 // Set the Flow key:
734 // - flowId
735 //
736 flowObj.setFlowId(flowPath.flowId().toString());
737 flowObj.setType("flow");
738
739 //
740 // Set the Flow attributes:
741 // - flowPath.installerId()
742 // - flowPath.dataPath().srcPort()
743 // - flowPath.dataPath().dstPort()
744 //
745 flowObj.setInstallerId(flowPath.installerId().toString());
746 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
747 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
748 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
749 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
750
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700751 if (dataPathSummaryStr != null) {
752 flowObj.setDataPathSummary(dataPathSummaryStr);
753 } else {
754 flowObj.setDataPathSummary("");
755 }
756
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800757 // Flow edges:
758 // HeadFE
759
760
761 //
762 // Flow Entries:
763 // flowPath.dataPath().flowEntries()
764 //
765 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
766 IFlowEntry flowEntryObj = null;
767 boolean found = false;
768 try {
769 if ((flowEntryObj = conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) {
770 log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry",
771 flowEntry.flowEntryId().toString());
772 found = true;
773 } else {
774 flowEntryObj = conn.utils().newFlowEntry(conn);
775 log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry",
776 flowEntry.flowEntryId().toString());
777 }
778 } catch (Exception e) {
779 // TODO: handle exceptions
780 conn.endTx(Transaction.ROLLBACK);
781 log.error(":addFlow FlowEntryId:{} failed",
782 flowEntry.flowEntryId().toString());
783 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700784 if (flowEntryObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000785 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
786 flowEntry.flowEntryId().toString());
787 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800788 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700789 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800790
791 //
792 // Set the Flow Entry key:
793 // - flowEntry.flowEntryId()
794 //
795 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
796 flowEntryObj.setType("flow_entry");
797
798 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700799 // Set the Flow Entry Edges and attributes:
800 // - Switch edge
801 // - InPort edge
802 // - OutPort edge
803 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800804 // - flowEntry.flowEntryMatch()
805 // - flowEntry.flowEntryActions()
806 // - flowEntry.dpid()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800807 // - flowEntry.flowEntryUserState()
808 // - flowEntry.flowEntrySwitchState()
809 // - flowEntry.flowEntryErrorState()
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700810 // - flowEntry.matchInPort()
811 // - flowEntry.matchEthernetFrameType()
812 // - flowEntry.matchSrcIPv4Net()
813 // - flowEntry.matchDstIPv4Net()
814 // - flowEntry.matchSrcMac()
815 // - flowEntry.matchDstMac()
816 // - flowEntry.actionOutput()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800817 //
Pavlin Radoslavovb6d1e362013-03-28 03:15:41 -0700818 ISwitchObject sw =
819 conn.utils().searchSwitch(conn, flowEntry.dpid().toString());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800820 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
Pankaj Berded0079742013-03-27 17:53:25 -0700821 flowEntryObj.setSwitch(sw);
822 if (flowEntry.flowEntryMatch().matchInPort()) {
Pavlin Radoslavovb6d1e362013-03-28 03:15:41 -0700823 IPortObject inport =
824 conn.utils().searchPort(conn, flowEntry.dpid().toString(),
825 flowEntry.flowEntryMatch().inPort().value());
Pankaj Berded0079742013-03-27 17:53:25 -0700826 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
827 flowEntryObj.setInPort(inport);
828 }
829 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
830 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
831 }
832 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
833 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
834 }
835 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
836 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
837 }
838 if (flowEntry.flowEntryMatch().matchSrcMac()) {
839 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
840 }
841 if (flowEntry.flowEntryMatch().matchDstMac()) {
842 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
843 }
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700844
845 for (FlowEntryAction fa : flowEntry.flowEntryActions()) {
Pankaj Berded0079742013-03-27 17:53:25 -0700846 if (fa.actionOutput() != null) {
Pavlin Radoslavovb6d1e362013-03-28 03:15:41 -0700847 IPortObject outport =
848 conn.utils().searchPort(conn,
849 flowEntry.dpid().toString(),
850 fa.actionOutput().port().value());
851 flowEntryObj.setActionOutput(fa.actionOutput().port().value());
852 flowEntryObj.setOutPort(outport);
Pankaj Berded0079742013-03-27 17:53:25 -0700853 }
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700854 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800855 // TODO: Hacks with hard-coded state names!
856 if (found)
857 flowEntryObj.setUserState("FE_USER_MODIFY");
858 else
859 flowEntryObj.setUserState("FE_USER_ADD");
860 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
861 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700862 // TODO: Take care of the FlowEntryErrorState.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800863 //
864
865 // Flow Entries edges:
866 // Flow
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700867 // NextFE (TODO)
868 if (! found) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800869 flowObj.addFlowEntry(flowEntryObj);
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700870 flowEntryObj.setFlow(flowObj);
871 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800872 }
873 conn.endTx(Transaction.COMMIT);
874
875 //
876 // TODO: We need a proper Flow ID allocation mechanism.
877 //
878 flowId.setValue(flowPath.flowId().value());
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700879
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800880 return true;
881 }
882
883 /**
884 * Delete a previously added flow.
885 *
886 * @param flowId the Flow ID of the flow to delete.
887 * @return true on success, otherwise false.
888 */
889 @Override
890 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700891 if (flowId.value() == measurementFlowId) {
892 modifiedMeasurementFlowTime = System.nanoTime();
893 }
894
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800895 IFlowPath flowObj = null;
896 //
897 // We just mark the entries for deletion,
898 // and let the switches remove each individual entry after
899 // it has been removed from the switches.
900 //
901 try {
902 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
903 != null) {
904 log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath",
905 flowId.toString());
906 } else {
907 log.debug("Deleting FlowPath with FlowId {}: FlowPath not found",
908 flowId.toString());
909 }
910 } catch (Exception e) {
911 // TODO: handle exceptions
912 conn.endTx(Transaction.ROLLBACK);
913 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
914 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700915 if (flowObj == null) {
916 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800917 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700918 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800919
920 //
921 // Find and mark for deletion all Flow Entries
922 //
923 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
924 boolean empty = true; // TODO: an ugly hack
925 for (IFlowEntry flowEntryObj : flowEntries) {
926 empty = false;
927 // flowObj.removeFlowEntry(flowEntryObj);
928 // conn.utils().removeFlowEntry(conn, flowEntryObj);
929 flowEntryObj.setUserState("FE_USER_DELETE");
930 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
931 }
932 // Remove from the database empty flows
933 if (empty)
934 conn.utils().removeFlowPath(conn, flowObj);
935 conn.endTx(Transaction.COMMIT);
936
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800937 return true;
938 }
939
940 /**
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700941 * Clear the state for a previously added flow.
942 *
943 * @param flowId the Flow ID of the flow to clear.
944 * @return true on success, otherwise false.
945 */
946 @Override
947 public boolean clearFlow(FlowId flowId) {
948 IFlowPath flowObj = null;
949 try {
950 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
951 != null) {
952 log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath",
953 flowId.toString());
954 } else {
955 log.debug("Clearing FlowPath with FlowId {}: FlowPath not found",
956 flowId.toString());
957 }
958 } catch (Exception e) {
959 // TODO: handle exceptions
960 conn.endTx(Transaction.ROLLBACK);
961 log.error(":clearFlow FlowId:{} failed", flowId.toString());
962 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700963 if (flowObj == null) {
964 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700965 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700966 }
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700967
968 //
969 // Remove all Flow Entries
970 //
971 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
972 for (IFlowEntry flowEntryObj : flowEntries) {
973 flowObj.removeFlowEntry(flowEntryObj);
974 conn.utils().removeFlowEntry(conn, flowEntryObj);
975 }
976 // Remove the Flow itself
977 conn.utils().removeFlowPath(conn, flowObj);
978 conn.endTx(Transaction.COMMIT);
979
980 return true;
981 }
982
983 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800984 * Get a previously added flow.
985 *
986 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800987 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800988 */
989 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800990 public FlowPath getFlow(FlowId flowId) {
991 IFlowPath flowObj = null;
992 try {
993 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
994 != null) {
995 log.debug("Get FlowPath with FlowId {}: found existing FlowPath",
996 flowId.toString());
997 } else {
998 log.debug("Get FlowPath with FlowId {}: FlowPath not found",
999 flowId.toString());
1000 }
1001 } catch (Exception e) {
1002 // TODO: handle exceptions
1003 conn.endTx(Transaction.ROLLBACK);
1004 log.error(":getFlow FlowId:{} failed", flowId.toString());
1005 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001006 if (flowObj == null) {
1007 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001008 return null; // Flow not found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001009 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001010
1011 //
1012 // Extract the Flow state
1013 //
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001014 FlowPath flowPath = extractFlowPath(flowObj);
1015 conn.endTx(Transaction.COMMIT);
1016
1017 return flowPath;
1018 }
1019
1020 /**
1021 * Get all previously added flows by a specific installer for a given
1022 * data path endpoints.
1023 *
1024 * @param installerId the Caller ID of the installer of the flow to get.
1025 * @param dataPathEndpoints the data path endpoints of the flow to get.
1026 * @return the Flow Paths if found, otherwise null.
1027 */
1028 @Override
1029 public ArrayList<FlowPath> getAllFlows(CallerId installerId,
1030 DataPathEndpoints dataPathEndpoints) {
1031 //
1032 // TODO: The implementation below is not optimal:
1033 // We fetch all flows, and then return only the subset that match
1034 // the query conditions.
1035 // We should use the appropriate Titan/Gremlin query to filter-out
1036 // the flows as appropriate.
1037 //
1038 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001039 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001040
1041 if (allFlows == null) {
1042 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001043 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001044 }
1045
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001046 for (FlowPath flow : allFlows) {
1047 //
1048 // TODO: String-based comparison is sub-optimal.
1049 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -08001050 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001051 //
1052 if (! flow.installerId().toString().equals(installerId.toString()))
1053 continue;
1054 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
1055 continue;
1056 }
1057 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
1058 continue;
1059 }
1060 flowPaths.add(flow);
1061 }
1062
1063 if (flowPaths.isEmpty()) {
1064 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001065 } else {
1066 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints);
1067 }
1068
1069 return flowPaths;
1070 }
1071
1072 /**
1073 * Get all installed flows by all installers for given data path endpoints.
1074 *
1075 * @param dataPathEndpoints the data path endpoints of the flows to get.
1076 * @return the Flow Paths if found, otherwise null.
1077 */
1078 @Override
1079 public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
1080 //
1081 // TODO: The implementation below is not optimal:
1082 // We fetch all flows, and then return only the subset that match
1083 // the query conditions.
1084 // We should use the appropriate Titan/Gremlin query to filter-out
1085 // the flows as appropriate.
1086 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001087 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
1088 ArrayList<FlowPath> allFlows = getAllFlows();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001089
1090 if (allFlows == null) {
1091 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001092 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001093 }
1094
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001095 for (FlowPath flow : allFlows) {
1096 //
1097 // TODO: String-based comparison is sub-optimal.
1098 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -08001099 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001100 //
1101 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
1102 continue;
1103 }
1104 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
1105 continue;
1106 }
1107 flowPaths.add(flow);
1108 }
1109
1110 if (flowPaths.isEmpty()) {
1111 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001112 } else {
1113 log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints);
1114 }
1115
1116 return flowPaths;
1117 }
1118
1119 /**
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001120 * Get summary of all installed flows by all installers in a given range
1121 *
1122 * @param flowId the data path endpoints of the flows to get.
1123 * @param maxFlows: the maximum number of flows to be returned
1124 * @return the Flow Paths if found, otherwise null.
1125 */
1126 @Override
1127 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) {
1128 //
1129 // TODO: The implementation below is not optimal:
1130 // We fetch all flows, and then return only the subset that match
1131 // the query conditions.
1132 // We should use the appropriate Titan/Gremlin query to filter-out
1133 // the flows as appropriate.
1134 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001135 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
1136
1137 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001138
1139 if (allFlows == null) {
1140 log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001141 return flowPaths;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001142 }
1143
Umesh Krishnaswamy244b4ae2013-03-29 12:05:15 -07001144 Collections.sort(allFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001145
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001146 for (FlowPath flow : allFlows) {
1147
1148 // start from desired flowId
Umesh Krishnaswamy2a82c642013-03-29 08:27:17 -07001149 //if (flow.flowId().value() < flowId.value()) {
1150 // continue;
1151 //}
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001152
1153 // Summarize by making null flow entry fields that are not relevant to report
1154 for (FlowEntry flowEntry : flow.dataPath().flowEntries()) {
1155 flowEntry.setFlowEntryActions(null);
1156 flowEntry.setFlowEntryMatch(null);
1157 }
1158
1159 flowPaths.add(flow);
1160 if (maxFlows != 0 && flowPaths.size() >= maxFlows) {
1161 break;
1162 }
1163 }
1164
1165 if (flowPaths.isEmpty()) {
1166 log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001167 } else {
1168 log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows);
1169 }
1170
1171 return flowPaths;
1172 }
1173
1174 /**
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001175 * Get all installed flows by all installers.
1176 *
1177 * @return the Flow Paths if found, otherwise null.
1178 */
1179 @Override
1180 public ArrayList<FlowPath> getAllFlows() {
1181 Iterable<IFlowPath> flowPathsObj = null;
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001182 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001183
1184 try {
1185 if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
1186 log.debug("Get all FlowPaths: found FlowPaths");
1187 } else {
1188 log.debug("Get all FlowPaths: no FlowPaths found");
1189 }
1190 } catch (Exception e) {
1191 // TODO: handle exceptions
1192 conn.endTx(Transaction.ROLLBACK);
1193 log.error(":getAllFlowPaths failed");
1194 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001195 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
1196 conn.endTx(Transaction.COMMIT);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001197 return flowPaths; // No Flows found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001198 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001199
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001200 for (IFlowPath flowObj : flowPathsObj) {
1201 //
1202 // Extract the Flow state
1203 //
1204 FlowPath flowPath = extractFlowPath(flowObj);
Pavlin Radoslavov3f2af732013-03-29 15:29:35 -07001205 if (flowPath != null)
1206 flowPaths.add(flowPath);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001207 }
1208
1209 conn.endTx(Transaction.COMMIT);
1210
1211 return flowPaths;
1212 }
1213
1214 /**
1215 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
1216 *
1217 * @param flowObj the object to extract the Flow Path State from.
1218 * @return the extracted Flow Path State.
1219 */
1220 private FlowPath extractFlowPath(IFlowPath flowObj) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001221 FlowPath flowPath = new FlowPath();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001222
1223 //
1224 // Extract the Flow state
1225 //
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001226 String flowIdStr = flowObj.getFlowId();
1227 String installerIdStr = flowObj.getInstallerId();
1228 String srcSwitchStr = flowObj.getSrcSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001229 Short srcPortShort = flowObj.getSrcPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001230 String dstSwitchStr = flowObj.getDstSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001231 Short dstPortShort = flowObj.getDstPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001232
1233 if ((flowIdStr == null) ||
1234 (installerIdStr == null) ||
1235 (srcSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001236 (srcPortShort == null) ||
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001237 (dstSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001238 (dstPortShort == null)) {
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001239 // TODO: A work-around, becauuse of some bogus database objects
1240 return null;
1241 }
1242
1243 flowPath.setFlowId(new FlowId(flowIdStr));
1244 flowPath.setInstallerId(new CallerId(installerIdStr));
1245 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001246 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001247 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001248 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001249
1250 //
1251 // Extract all Flow Entries
1252 //
1253 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1254 for (IFlowEntry flowEntryObj : flowEntries) {
1255 FlowEntry flowEntry = new FlowEntry();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001256 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1257 String switchDpidStr = flowEntryObj.getSwitchDpid();
1258 String userState = flowEntryObj.getUserState();
1259 String switchState = flowEntryObj.getSwitchState();
1260
1261 if ((flowEntryIdStr == null) ||
1262 (switchDpidStr == null) ||
1263 (userState == null) ||
1264 (switchState == null)) {
1265 // TODO: A work-around, becauuse of some bogus database objects
1266 continue;
1267 }
1268 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
1269 flowEntry.setDpid(new Dpid(switchDpidStr));
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -07001270
1271 //
1272 // Extract the match conditions
1273 //
1274 FlowEntryMatch match = new FlowEntryMatch();
1275 Short matchInPort = flowEntryObj.getMatchInPort();
1276 if (matchInPort != null)
1277 match.enableInPort(new Port(matchInPort));
1278 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1279 if (matchEthernetFrameType != null)
1280 match.enableEthernetFrameType(matchEthernetFrameType);
1281 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1282 if (matchSrcIPv4Net != null)
1283 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1284 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1285 if (matchDstIPv4Net != null)
1286 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1287 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1288 if (matchSrcMac != null)
1289 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1290 String matchDstMac = flowEntryObj.getMatchDstMac();
1291 if (matchDstMac != null)
1292 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1293 flowEntry.setFlowEntryMatch(match);
1294
1295 //
1296 // Extract the actions
1297 //
1298 ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>();
1299 Short actionOutputPort = flowEntryObj.getActionOutput();
1300 if (actionOutputPort != null) {
1301 FlowEntryAction action = new FlowEntryAction();
1302 action.setActionOutput(new Port(actionOutputPort));
1303 actions.add(action);
1304 }
1305 flowEntry.setFlowEntryActions(actions);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001306 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001307 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
1308 //
Pavlin Radoslavovede97582013-03-08 18:57:28 -08001309 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001310 // and FlowEntryErrorState.
1311 //
1312 flowPath.dataPath().flowEntries().add(flowEntry);
1313 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001314
1315 return flowPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001316 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001317
1318 /**
1319 * Add and maintain a shortest-path flow.
1320 *
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001321 * NOTE: The Flow Path argument does NOT contain all flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001322 * Instead, it contains a single dummy flow entry that is used to
1323 * store the matching condition(s).
1324 * That entry is replaced by the appropriate entries from the
1325 * internally performed shortest-path computation.
1326 *
1327 * @param flowPath the Flow Path with the endpoints and the match
1328 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001329 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001330 */
1331 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001332 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001333 String dataPathSummaryStr = null;
1334
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001335 //
1336 // Do the shortest path computation
1337 //
1338 DataPath dataPath =
1339 topoRouteService.getShortestPath(flowPath.dataPath().srcPort(),
1340 flowPath.dataPath().dstPort());
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001341 if (dataPath == null) {
1342 // We need the DataPath to populate the Network MAP
1343 dataPath = new DataPath();
1344 dataPath.setSrcPort(flowPath.dataPath().srcPort());
1345 dataPath.setDstPort(flowPath.dataPath().dstPort());
1346 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001347
1348 FlowEntryMatch userFlowEntryMatch = null;
1349 if (! flowPath.dataPath().flowEntries().isEmpty()) {
1350 userFlowEntryMatch = flowPath.dataPath().flowEntries().get(0).flowEntryMatch();
1351 }
1352
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001353 // Compute the Data Path summary
1354 dataPathSummaryStr = dataPath.dataPathSummary();
1355
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001356 //
1357 // Set the incoming port matching and the outgoing port output
1358 // actions for each flow entry.
1359 //
1360 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1361 // Set the incoming port matching
1362 FlowEntryMatch flowEntryMatch = null;
1363 if (userFlowEntryMatch != null)
1364 flowEntryMatch = new FlowEntryMatch(userFlowEntryMatch);
1365 else
1366 flowEntryMatch = new FlowEntryMatch();
1367 flowEntry.setFlowEntryMatch(flowEntryMatch);
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 computed Flow Path
1383 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001384 FlowPath computedFlowPath = new FlowPath();
1385 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1386 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1387 computedFlowPath.setDataPath(dataPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001388
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001389 FlowId flowId = new FlowId();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001390 if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr))
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001391 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001392
1393 // TODO: Mark the flow for maintenance purpose
1394
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001395 return (computedFlowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001396 }
1397
1398 /**
1399 * Create a Flow from port to port.
1400 *
1401 * TODO: We don't need it for now.
1402 *
1403 * @param src_port the source port.
1404 * @param dest_port the destination port.
1405 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001406 @Override
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001407 public void createFlow(IPortObject src_port, IPortObject dest_port) {
1408 // TODO: We don't need it for now.
1409 }
1410
1411 /**
1412 * Get all Flows matching a source and a destination port.
1413 *
1414 * TODO: Pankaj might be implementing it later.
1415 *
1416 * @param src_port the source port to match.
1417 * @param dest_port the destination port to match.
1418 * @return all flows matching the source and the destination port.
1419 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001420 @Override
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001421 public Iterable<FlowPath> getFlows(IPortObject src_port,
1422 IPortObject dest_port) {
1423 // TODO: Pankaj might be implementing it later.
1424 return null;
1425 }
1426
1427 /**
1428 * Get all Flows going out from a port.
1429 *
1430 * TODO: We need it now: Pankaj
1431 *
1432 * @param port the port to match.
1433 * @return the list of flows that are going out from the port.
1434 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001435 @Override
Pankaj Berde83d83382013-03-28 13:55:34 -07001436 public Iterable<FlowPath> getOutFlows(IPortObject port) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001437 // TODO: We need it now: Pankaj
1438 return null;
1439 }
1440
1441 /**
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001442 * Reconcile all flows on inactive switch port.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001443 *
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001444 * @param portObject the port that has become inactive.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001445 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001446 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001447 public void reconcileFlows(IPortObject portObject) {
1448 Iterable<IFlowEntry> inFlowEntries = portObject.getInFlowEntries();
1449 Iterable<IFlowEntry> outFlowEntries = portObject.getOutFlowEntries();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001450
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001451 //
1452 // Collect all affected Flow IDs from the affected flow entries
1453 //
1454 HashSet<IFlowPath> flowObjSet = new HashSet<IFlowPath>();
1455 for (IFlowEntry flowEntryObj: inFlowEntries) {
1456 IFlowPath flowObj = flowEntryObj.getFlow();
1457 if (flowObj != null)
1458 flowObjSet.add(flowObj);
1459 }
1460 for (IFlowEntry flowEntryObj: outFlowEntries) {
1461 IFlowPath flowObj = flowEntryObj.getFlow();
1462 if (flowObj != null)
1463 flowObjSet.add(flowObj);
1464 }
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001465
1466 // Reconcile the affected flows
1467 reconcileFlows(flowObjSet);
1468 }
1469
1470 /**
1471 * Reconcile all flows in a set.
1472 *
1473 * @param flowObjSet the set of flows that need to be reconciliated.
1474 */
1475 public void reconcileFlows(Iterable<IFlowPath> flowObjSet) {
1476 if (! flowObjSet.iterator().hasNext())
1477 return;
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001478
1479 //
1480 // Remove the old Flow Entries, and add the new Flow Entries
1481 //
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001482
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001483 Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001484 LinkedList<FlowPath> flowPaths = new LinkedList<FlowPath>();
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001485 for (IFlowPath flowObj : flowObjSet) {
1486 FlowPath flowPath = extractFlowPath(flowObj);
1487 if (flowPath == null)
1488 continue;
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001489 flowPaths.add(flowPath);
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001490
1491 //
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001492 // Remove the Flow Entries from the Network MAP
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001493 //
1494 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001495 LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>();
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001496 for (IFlowEntry flowEntryObj : flowEntries) {
1497 String dpidStr = flowEntryObj.getSwitchDpid();
1498 if (dpidStr == null)
1499 continue;
1500 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001501 /*
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001502 IOFSwitch mySwitch = mySwitches.get(dpid.value());
1503 if (mySwitch == null)
1504 continue; // Ignore the entry: not my switch
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001505 */
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001506 deleteFlowEntries.add(flowEntryObj);
1507 }
1508 for (IFlowEntry flowEntryObj : deleteFlowEntries) {
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001509 flowObj.removeFlowEntry(flowEntryObj);
1510 conn.utils().removeFlowEntry(conn, flowEntryObj);
1511 }
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001512 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001513
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001514 for (FlowPath flowPath : flowPaths) {
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001515 //
1516 // Delete the flow entries from the switches
1517 //
1518 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
1519 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001520 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
1521 if (mySwitch == null) {
1522 // Not my switch
1523 installRemoteFlowEntry(flowEntry);
1524 } else {
1525 installFlowEntry(mySwitch, flowEntry);
1526 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001527 }
1528
1529 //
1530 // Calculate the new shortest path and install it in the
1531 // Network MAP.
1532 //
1533 FlowPath addedFlowPath = addAndMaintainShortestPathFlow(flowPath);
1534 if (addedFlowPath == null) {
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001535 log.error("Cannot add Shortest Path Flow from {} to {}: path not found?",
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001536 flowPath.dataPath().srcPort().toString(),
1537 flowPath.dataPath().dstPort().toString());
1538 continue;
1539 }
1540
1541 //
1542 // Add the flow entries to the switches
1543 //
1544 for (FlowEntry flowEntry : addedFlowPath.dataPath().flowEntries()) {
1545 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001546 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
1547 if (mySwitch == null) {
1548 // Not my switch
1549 installRemoteFlowEntry(flowEntry);
1550 continue;
1551 }
1552
1553 IFlowEntry flowEntryObj =
1554 conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId());
1555 if (flowEntryObj == null) {
1556 //
1557 // TODO: Remove the "new Object[] wrapper in the statement
1558 // below after the SLF4J logger is upgraded to
1559 // Version 1.7.5
1560 //
1561 log.error("Cannot add Flow Entry to switch {} for Path Flow from {} to {} : Flow Entry not in the Network MAP",
1562 new Object[] {
1563 flowEntry.dpid(),
1564 flowPath.dataPath().srcPort(),
1565 flowPath.dataPath().dstPort()
1566 });
1567 continue;
1568 }
1569 // Update the Flow Entry Switch State in the Network MAP
1570 if (installFlowEntry(mySwitch, flowEntry)) {
1571 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
1572 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001573 }
1574 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001575 }
1576
1577 /**
1578 * Reconcile all flows between a source and a destination port.
1579 *
1580 * TODO: We don't need it for now.
1581 *
1582 * @param src_port the source port.
1583 * @param dest_port the destination port.
1584 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001585 @Override
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001586 public void reconcileFlow(IPortObject src_port, IPortObject dest_port) {
1587 // TODO: We don't need it for now.
1588 }
1589
1590 /**
1591 * Compute the shortest path between a source and a destination ports.
1592 *
1593 * @param src_port the source port.
1594 * @param dest_port the destination port.
1595 * @return the computed shortest path between the source and the
1596 * destination ports. The flow entries in the path itself would
1597 * contain the incoming port matching and the outgoing port output
1598 * actions set. However, the path itself will NOT have the Flow ID,
1599 * Installer ID, and any additional matching conditions for the
1600 * flow entries (e.g., source or destination MAC address, etc).
1601 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001602 @Override
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001603 public FlowPath computeFlowPath(IPortObject src_port,
1604 IPortObject dest_port) {
1605 //
1606 // Prepare the arguments
1607 //
1608 String dpidStr = src_port.getSwitch().getDPID();
1609 Dpid srcDpid = new Dpid(dpidStr);
1610 Port srcPort = new Port(src_port.getNumber());
1611
1612 dpidStr = dest_port.getSwitch().getDPID();
1613 Dpid dstDpid = new Dpid(dpidStr);
1614 Port dstPort = new Port(dest_port.getNumber());
1615
1616 SwitchPort src = new SwitchPort(srcDpid, srcPort);
1617 SwitchPort dst = new SwitchPort(dstDpid, dstPort);
1618
1619 //
1620 // Do the shortest path computation
1621 //
1622 DataPath dataPath = topoRouteService.getShortestPath(src, dst);
1623 if (dataPath == null)
1624 return null;
1625
1626 //
1627 // Set the incoming port matching and the outgoing port output
1628 // actions for each flow entry.
1629 //
1630 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1631 // Set the incoming port matching
1632 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
1633 if (flowEntryMatch == null) {
1634 flowEntryMatch = new FlowEntryMatch();
1635 flowEntry.setFlowEntryMatch(flowEntryMatch);
1636 }
1637 flowEntryMatch.enableInPort(flowEntry.inPort());
1638
1639 // Set the outgoing port output action
1640 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1641 if (flowEntryActions == null) {
1642 flowEntryActions = new ArrayList<FlowEntryAction>();
1643 flowEntry.setFlowEntryActions(flowEntryActions);
1644 }
1645 FlowEntryAction flowEntryAction = new FlowEntryAction();
1646 flowEntryAction.setActionOutput(flowEntry.outPort());
1647 flowEntryActions.add(flowEntryAction);
1648 }
1649
1650 //
1651 // Prepare the return result
1652 //
1653 FlowPath flowPath = new FlowPath();
1654 flowPath.setDataPath(dataPath);
1655
1656 return flowPath;
1657 }
1658
1659 /**
1660 * Get all Flow Entries of a Flow.
1661 *
1662 * @param flow the flow whose flow entries should be returned.
1663 * @return the flow entries of the flow.
1664 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001665 @Override
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001666 public Iterable<FlowEntry> getFlowEntries(FlowPath flow) {
1667 return flow.dataPath().flowEntries();
1668 }
1669
1670 /**
1671 * Install a Flow Entry on a switch.
1672 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001673 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001674 * @param flowEntry the flow entry to install.
1675 * @return true on success, otherwise false.
1676 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001677 @Override
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001678 public boolean installFlowEntry(IOFSwitch mySwitch, FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001679 //
1680 // Create the OpenFlow Flow Modification Entry to push
1681 //
1682 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1683 .getMessage(OFType.FLOW_MOD);
1684 long cookie = flowEntry.flowEntryId().value();
1685
1686 short flowModCommand = OFFlowMod.OFPFC_ADD;
1687 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
1688 flowModCommand = OFFlowMod.OFPFC_ADD;
1689 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
1690 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1691 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
1692 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1693 } else {
1694 // Unknown user state. Ignore the entry
1695 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1696 flowEntry.flowEntryId().toString(),
1697 flowEntry.flowEntryUserState());
1698 return false;
1699 }
1700
1701 //
1702 // Fetch the match conditions
1703 //
1704 OFMatch match = new OFMatch();
1705 match.setWildcards(OFMatch.OFPFW_ALL);
1706 Port matchInPort = flowEntry.flowEntryMatch().inPort();
1707 if (matchInPort != null) {
1708 match.setInputPort(matchInPort.value());
1709 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1710 }
1711 Short matchEthernetFrameType =
1712 flowEntry.flowEntryMatch().ethernetFrameType();
1713 if (matchEthernetFrameType != null) {
1714 match.setDataLayerType(matchEthernetFrameType);
1715 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1716 }
1717 IPv4Net matchSrcIPv4Net = flowEntry.flowEntryMatch().srcIPv4Net();
1718 if (matchSrcIPv4Net != null) {
1719 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
1720 }
1721 IPv4Net matchDstIPv4Net = flowEntry.flowEntryMatch().dstIPv4Net();
1722 if (matchDstIPv4Net != null) {
1723 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
1724 }
1725 MACAddress matchSrcMac = flowEntry.flowEntryMatch().srcMac();
1726 if (matchSrcMac != null) {
1727 match.setDataLayerSource(matchSrcMac.toString());
1728 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1729 }
1730 MACAddress matchDstMac = flowEntry.flowEntryMatch().dstMac();
1731 if (matchDstMac != null) {
1732 match.setDataLayerDestination(matchDstMac.toString());
1733 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1734 }
1735
1736 //
1737 // Fetch the actions
1738 //
1739 // TODO: For now we support only the "OUTPUT" actions.
1740 //
1741 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1742 List<OFAction> actions = new ArrayList<OFAction>();
1743 ArrayList<FlowEntryAction> flowEntryActions =
1744 flowEntry.flowEntryActions();
1745 for (FlowEntryAction flowEntryAction : flowEntryActions) {
1746 FlowEntryAction.ActionOutput actionOutput =
1747 flowEntryAction.actionOutput();
1748 if (actionOutput != null) {
1749 short actionOutputPort = actionOutput.port().value();
1750 OFActionOutput action = new OFActionOutput();
1751 // XXX: The max length is hard-coded for now
1752 action.setMaxLength((short)0xffff);
1753 action.setPort(actionOutputPort);
1754 actions.add(action);
1755 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1756 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1757 fm.setOutPort(actionOutputPort);
1758 }
1759 }
1760 }
1761
1762 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1763 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1764 .setPriority(PRIORITY_DEFAULT)
1765 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1766 .setCookie(cookie)
1767 .setCommand(flowModCommand)
1768 .setMatch(match)
1769 .setActions(actions)
1770 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1771
1772 //
1773 // TODO: Set the following flag
1774 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1775 // See method ForwardingBase::pushRoute()
1776 //
1777
1778 //
1779 // Write the message to the switch
1780 //
1781 try {
1782 messageDamper.write(mySwitch, fm, null);
1783 mySwitch.flush();
1784 } catch (IOException e) {
1785 log.error("Failure writing flow mod from network map", e);
1786 return false;
1787 }
1788 return true;
1789 }
1790
1791 /**
1792 * Remove a Flow Entry from a switch.
1793 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001794 * @param mySwitch the switch to remove the Flow Entry from.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001795 * @param flowEntry the flow entry to remove.
1796 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001797 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001798 @Override
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001799 public boolean removeFlowEntry(IOFSwitch mySwitch, FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001800 //
1801 // The installFlowEntry() method implements both installation
1802 // and removal of flow entries.
1803 //
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001804 return (installFlowEntry(mySwitch, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001805 }
1806
1807 /**
1808 * Install a Flow Entry on a remote controller.
1809 *
1810 * TODO: We need it now: Jono
1811 * - For now it will make a REST call to the remote controller.
1812 * - Internally, it needs to know the name of the remote controller.
1813 *
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001814 * @param flowEntry the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001815 * @return true on success, otherwise false.
1816 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001817 @Override
1818 public boolean installRemoteFlowEntry(FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001819 // TODO: We need it now: Jono
1820 // - For now it will make a REST call to the remote controller.
1821 // - Internally, it needs to know the name of the remote controller.
1822 return true;
1823 }
1824
1825 /**
1826 * Remove a flow entry on a remote controller.
1827 *
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001828 * @param flowEntry the flow entry to remove.
1829 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001830 */
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001831 @Override
1832 public boolean removeRemoteFlowEntry(FlowEntry flowEntry) {
1833 //
1834 // The installRemoteFlowEntry() method implements both installation
1835 // and removal of flow entries.
1836 //
1837 return (installRemoteFlowEntry(flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001838 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001839}