blob: 516d99cec82cb2317f8294e5c6bbdc0cb1afaf7e [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 Radoslavovb6f53542013-03-01 16:02:14 -080055import net.onrc.onos.util.GraphDBConnection;
56import net.onrc.onos.util.GraphDBConnection.Transaction;
57
58import org.openflow.protocol.OFFlowMod;
59import org.openflow.protocol.OFMatch;
60import org.openflow.protocol.OFPacketOut;
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070061import org.openflow.protocol.OFPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080062import org.openflow.protocol.OFType;
63import org.openflow.protocol.action.OFAction;
64import org.openflow.protocol.action.OFActionOutput;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080065
66import org.slf4j.Logger;
67import org.slf4j.LoggerFactory;
68
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070069public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080070
71 public GraphDBConnection conn;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080072
73 protected IRestApiService restApi;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080074 protected IFloodlightProviderService floodlightProvider;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070075 protected ITopoRouteService topoRouteService;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070076 protected FloodlightModuleContext context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080077
78 protected OFMessageDamper messageDamper;
79
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070080 //
81 // TODO: Values copied from elsewhere (class LearningSwitch).
82 // The local copy should go away!
83 //
84 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
85 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
86 public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity
87 public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite
88 public static final short PRIORITY_DEFAULT = 100;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080089
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000090 // Flow Entry ID generation state
91 private static Random randomGenerator = new Random();
92 private static int nextFlowEntryIdPrefix = 0;
93 private static int nextFlowEntryIdSuffix = 0;
94 private static long nextFlowEntryId = 0;
95
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070096 private static long measurementFlowId = 100000;
97 private static String measurementFlowIdStr = "0x186a0"; // 100000
98 private long modifiedMeasurementFlowTime = 0;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070099
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800100 /** The logger. */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800101 private static Logger log = LoggerFactory.getLogger(FlowManager.class);
102
103 // The periodic task(s)
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700104 private final ScheduledExecutorService mapReaderScheduler =
105 Executors.newScheduledThreadPool(1);
106
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700107 final Runnable mapReader = new Runnable() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800108 public void run() {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700109 long startTime = System.nanoTime();
110 int counterAllFlowEntries = 0;
111 int counterMyNotUpdatedFlowEntries = 0;
112 int counterAllFlowPaths = 0;
113 int counterMyFlowPaths = 0;
114
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800115 if (floodlightProvider == null) {
116 log.debug("FloodlightProvider service not found!");
117 return;
118 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000119 Map<Long, IOFSwitch> mySwitches =
120 floodlightProvider.getSwitches();
121 Map<Long, IFlowEntry> myFlowEntries =
122 new TreeMap<Long, IFlowEntry>();
123 LinkedList<IFlowEntry> deleteFlowEntries =
124 new LinkedList<IFlowEntry>();
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700125
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700126
127 //
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700128 // Fetch all Flow Entries and select only my Flow Entries
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700129 // that need to be updated into the switches.
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700130 //
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000131 Iterable<IFlowEntry> allFlowEntries =
132 conn.utils().getAllFlowEntries(conn);
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700133 for (IFlowEntry flowEntryObj : allFlowEntries) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700134 counterAllFlowEntries++;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000135 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700136 String userState = flowEntryObj.getUserState();
137 String switchState = flowEntryObj.getSwitchState();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000138 String dpidStr = flowEntryObj.getSwitchDpid();
139 if ((flowEntryIdStr == null) ||
140 (userState == null) ||
141 (switchState == null) ||
142 (dpidStr == null)) {
143 log.debug("IGNORING Flow Entry entry with null fields");
144 continue;
145 }
146 FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr);
147 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800148
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000149 if (! switchState.equals("FE_SWITCH_NOT_UPDATED"))
150 continue; // Ignore the entry: nothing to do
151
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800152 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000153 if (mySwitch == null)
154 continue; // Ignore the entry: not my switch
155
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700156 myFlowEntries.put(flowEntryId.value(), flowEntryObj);
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700157 if (userState.equals("FE_USER_DELETE")) {
158 // An entry that needs to be deleted.
159 deleteFlowEntries.add(flowEntryObj);
160 }
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700161 }
162
Pavlin Radoslavove87f94e2013-04-04 04:31:09 -0700163 log.debug("MEASUREMENT: Found {} My Flow Entries NOT_UPDATED",
164 myFlowEntries.size());
165
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700166 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -0700167 // Process my Flow Entries in the Flow Entry ID order
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700168 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700169 boolean processed_measurement_flow = false;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700170 for (Map.Entry<Long, IFlowEntry> entry : myFlowEntries.entrySet()) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700171 counterMyNotUpdatedFlowEntries++;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700172 IFlowEntry flowEntryObj = entry.getValue();
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700173 IFlowPath flowObj =
174 conn.utils().getFlowPathByFlowEntry(conn,
175 flowEntryObj);
176 if (flowObj == null)
177 continue; // Should NOT happen
178
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700179 // Code for measurement purpose
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700180 // TODO: Commented-out for now
181 /*
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700182 {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700183 if (flowObj.getFlowId().equals(measurementFlowIdStr)) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700184 processed_measurement_flow = true;
185 }
186 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700187 */
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700188
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700189 Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid());
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700190 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000191 if (mySwitch == null)
192 continue; // Shouldn't happen
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700193 installFlowEntry(mySwitch, flowObj, flowEntryObj);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800194 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000195
Pavlin Radoslavove87f94e2013-04-04 04:31:09 -0700196 log.debug("MEASUREMENT: Found {} Flow Entries to delete",
197 deleteFlowEntries.size());
198
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000199 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -0700200 // Delete all entries marked for deletion from the
201 // Network MAP.
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000202 //
203 // TODO: We should use the OpenFlow Barrier mechanism
204 // to check for errors, and delete the Flow Entries after the
205 // Barrier message is received.
206 //
207 while (! deleteFlowEntries.isEmpty()) {
208 IFlowEntry flowEntryObj = deleteFlowEntries.poll();
209 IFlowPath flowObj =
210 conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj);
211 if (flowObj == null) {
212 log.debug("Did not find FlowPath to be deleted");
213 continue;
214 }
215 flowObj.removeFlowEntry(flowEntryObj);
216 conn.utils().removeFlowEntry(conn, flowEntryObj);
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000217 }
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700218
219
220 //
221 // Fetch and recompute the Shortest Path for those
222 // Flow Paths this controller is responsible for.
223 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700224 topoRouteService.prepareShortestPathTopo();
225 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
226 HashSet<IFlowPath> flowObjSet = new HashSet<IFlowPath>();
227 for (IFlowPath flowPathObj : allFlowPaths) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700228 counterAllFlowPaths++;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700229 if (flowPathObj == null)
230 continue;
231 String dataPathSummaryStr = flowPathObj.getDataPathSummary();
232 if (dataPathSummaryStr == null)
233 continue; // Could be invalid entry?
234 if (dataPathSummaryStr.isEmpty())
235 continue; // No need to maintain this flow
236
237 // Fetch the fields needed to recompute the shortest path
238 String flowIdStr = flowPathObj.getFlowId();
239 String srcDpidStr = flowPathObj.getSrcSwitch();
240 Short srcPortShort = flowPathObj.getSrcPort();
241 String dstDpidStr = flowPathObj.getDstSwitch();
242 Short dstPortShort = flowPathObj.getDstPort();
243 if ((flowIdStr == null) ||
244 (srcDpidStr == null) ||
245 (srcPortShort == null) ||
246 (dstDpidStr == null) ||
247 (dstPortShort == null)) {
248 log.debug("IGNORING Flow Path entry with null fields");
249 continue;
250 }
251
252 FlowId flowId = new FlowId(flowIdStr);
253 Dpid srcDpid = new Dpid(srcDpidStr);
254 Port srcPort = new Port(srcPortShort);
255 Dpid dstDpid = new Dpid(dstDpidStr);
256 Port dstPort = new Port(dstPortShort);
257 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
258 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700259
260 //
261 // Use the source DPID as a heuristic to decide
262 // which controller is responsible for maintaining the
263 // shortest path.
264 // NOTE: This heuristic is error-prone: if the switch
265 // goes away and no controller is responsible for that
266 // switch, then the original Flow Path is not cleaned-up
267 //
268 IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
269 if (mySwitch == null)
270 continue; // Ignore: not my responsibility
271
272 counterMyFlowPaths++;
273
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700274 //
275 // NOTE: Using here the regular getShortestPath() method
276 // won't work here, because that method calls internally
277 // "conn.endTx(Transaction.COMMIT)", and that will
278 // invalidate all handlers to the Titan database.
279 // If we want to experiment with calling here
280 // getShortestPath(), we need to refactor that code
281 // to avoid closing the transaction.
282 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700283 DataPath dataPath =
284 topoRouteService.getTopoShortestPath(srcSwitchPort,
285 dstSwitchPort);
Pavlin Radoslavov4a325822013-04-02 22:16:59 +0000286 if (dataPath == null) {
287 // We need the DataPath to compare the paths
288 dataPath = new DataPath();
289 dataPath.setSrcPort(srcSwitchPort);
290 dataPath.setDstPort(dstSwitchPort);
291 }
292
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700293 String newDataPathSummaryStr = dataPath.dataPathSummary();
294 if (dataPathSummaryStr.equals(newDataPathSummaryStr))
295 continue; // Nothing changed
296
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700297 log.debug("RECONCILE: Need to Reconcile Shortest Path for FlowID {}",
298 flowId.toString());
299 flowObjSet.add(flowPathObj);
300 }
Pavlin Radoslavov53a3a8c2013-04-04 04:34:50 -0700301 log.debug("MEASUREMENT: Found {} Flows to reconcile",
Pavlin Radoslavove87f94e2013-04-04 04:31:09 -0700302 flowObjSet.size());
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700303 reconcileFlows(flowObjSet);
304 topoRouteService.dropShortestPathTopo();
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700305
306
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800307 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700308
309 if (processed_measurement_flow) {
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700310 long estimatedTime =
311 System.nanoTime() - modifiedMeasurementFlowTime;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700312 String logMsg = "MEASUREMENT: Pushed Flow delay: " +
313 (double)estimatedTime / 1000000000 + " sec";
314 log.debug(logMsg);
315 }
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700316
317 long estimatedTime = System.nanoTime() - startTime;
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700318 double rate = 0.0;
319 if (estimatedTime > 0)
320 rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime;
321 String logMsg = "MEASUREMENT: Processed AllFlowEntries: " +
322 counterAllFlowEntries + " MyNotUpdatedFlowEntries: " +
323 counterMyNotUpdatedFlowEntries + " AllFlowPaths: " +
324 counterAllFlowPaths + " MyFlowPaths: " +
325 counterMyFlowPaths + " in " +
326 (double)estimatedTime / 1000000000 + " sec: " +
327 rate + " paths/s";
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700328 log.debug(logMsg);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800329 }
330 };
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700331
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700332 final ScheduledFuture<?> mapReaderHandle =
333 mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800334
335 @Override
336 public void init(String conf) {
337 conn = GraphDBConnection.getInstance(conf);
338 }
339
340 public void finalize() {
341 close();
342 }
343
344 @Override
345 public void close() {
346 conn.close();
347 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800348
349 @Override
350 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
351 Collection<Class<? extends IFloodlightService>> l =
352 new ArrayList<Class<? extends IFloodlightService>>();
353 l.add(IFlowService.class);
354 return l;
355 }
356
357 @Override
358 public Map<Class<? extends IFloodlightService>, IFloodlightService>
359 getServiceImpls() {
360 Map<Class<? extends IFloodlightService>,
361 IFloodlightService> m =
362 new HashMap<Class<? extends IFloodlightService>,
363 IFloodlightService>();
364 m.put(IFlowService.class, this);
365 return m;
366 }
367
368 @Override
369 public Collection<Class<? extends IFloodlightService>>
370 getModuleDependencies() {
371 Collection<Class<? extends IFloodlightService>> l =
372 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800373 l.add(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700374 l.add(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800375 l.add(IRestApiService.class);
376 return l;
377 }
378
379 @Override
380 public void init(FloodlightModuleContext context)
381 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700382 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800383 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700384 topoRouteService = context.getServiceImpl(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800385 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800386 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
387 EnumSet.of(OFType.FLOW_MOD),
388 OFMESSAGE_DAMPER_TIMEOUT);
389 // TODO: An ugly hack!
390 String conf = "/tmp/cassandra.titan";
391 this.init(conf);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800392 }
393
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000394 private long getNextFlowEntryId() {
395 //
396 // Generate the next Flow Entry ID.
397 // NOTE: For now, the higher 32 bits are random, and
398 // the lower 32 bits are sequential.
399 // In the future, we need a better allocation mechanism.
400 //
401 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
402 nextFlowEntryIdPrefix = randomGenerator.nextInt();
403 nextFlowEntryIdSuffix = 0;
404 } else {
405 nextFlowEntryIdSuffix++;
406 }
407 long result = (long)nextFlowEntryIdPrefix << 32;
408 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
409 return result;
410 }
411
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800412 @Override
413 public void startUp(FloodlightModuleContext context) {
414 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov80ca6302013-03-20 02:08:09 -0700415
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000416 // Initialize the Flow Entry ID generator
417 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800418 }
419
420 /**
421 * Add a flow.
422 *
423 * Internally, ONOS will automatically register the installer for
424 * receiving Flow Path Notifications for that path.
425 *
426 * @param flowPath the Flow Path to install.
427 * @param flowId the return-by-reference Flow ID as assigned internally.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700428 * @param dataPathSummaryStr the data path summary string if the added
429 * flow will be maintained internally, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800430 * @return true on success, otherwise false.
431 */
432 @Override
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700433 public boolean addFlow(FlowPath flowPath, FlowId flowId,
434 String dataPathSummaryStr) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700435 /*
436 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700437 if (flowPath.flowId().value() == measurementFlowId) {
438 modifiedMeasurementFlowTime = System.nanoTime();
439 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700440 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800441
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800442 IFlowPath flowObj = null;
443 try {
444 if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId()))
445 != null) {
446 log.debug("Adding FlowPath with FlowId {}: found existing FlowPath",
447 flowPath.flowId().toString());
448 } else {
449 flowObj = conn.utils().newFlowPath(conn);
450 log.debug("Adding FlowPath with FlowId {}: creating new FlowPath",
451 flowPath.flowId().toString());
452 }
453 } catch (Exception e) {
454 // TODO: handle exceptions
455 conn.endTx(Transaction.ROLLBACK);
456 log.error(":addFlow FlowId:{} failed",
457 flowPath.flowId().toString());
458 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700459 if (flowObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000460 log.error(":addFlow FlowId:{} failed: Flow object not created",
461 flowPath.flowId().toString());
462 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800463 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700464 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800465
466 //
467 // Set the Flow key:
468 // - flowId
469 //
470 flowObj.setFlowId(flowPath.flowId().toString());
471 flowObj.setType("flow");
472
473 //
474 // Set the Flow attributes:
475 // - flowPath.installerId()
476 // - flowPath.dataPath().srcPort()
477 // - flowPath.dataPath().dstPort()
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700478 // - flowPath.matchEthernetFrameType()
479 // - flowPath.matchSrcIPv4Net()
480 // - flowPath.matchDstIPv4Net()
481 // - flowPath.matchSrcMac()
482 // - flowPath.matchDstMac()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800483 //
484 flowObj.setInstallerId(flowPath.installerId().toString());
485 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
486 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
487 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
488 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700489 if (flowPath.flowEntryMatch().matchEthernetFrameType()) {
490 flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType());
491 }
492 if (flowPath.flowEntryMatch().matchSrcIPv4Net()) {
493 flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString());
494 }
495 if (flowPath.flowEntryMatch().matchDstIPv4Net()) {
496 flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString());
497 }
498 if (flowPath.flowEntryMatch().matchSrcMac()) {
499 flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString());
500 }
501 if (flowPath.flowEntryMatch().matchDstMac()) {
502 flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString());
503 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800504
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700505 if (dataPathSummaryStr != null) {
506 flowObj.setDataPathSummary(dataPathSummaryStr);
507 } else {
508 flowObj.setDataPathSummary("");
509 }
510
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800511 // Flow edges:
512 // HeadFE
513
514
515 //
516 // Flow Entries:
517 // flowPath.dataPath().flowEntries()
518 //
519 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700520 if (addFlowEntry(flowObj, flowEntry) != true) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000521 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800522 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700523 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800524 }
525 conn.endTx(Transaction.COMMIT);
526
527 //
528 // TODO: We need a proper Flow ID allocation mechanism.
529 //
530 flowId.setValue(flowPath.flowId().value());
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700531
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800532 return true;
533 }
534
535 /**
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700536 * Add a flow entry to the Network MAP.
537 *
538 * @param flowObj the corresponding Flow Path object for the Flow Entry.
539 * @param flowEntry the Flow Entry to install.
540 * @return true on success, otherwise false.
541 */
542 private boolean addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
543 // Flow edges
544 // HeadFE (TODO)
545
546 //
547 // Assign the FlowEntry ID.
548 //
549 if ((flowEntry.flowEntryId() == null) ||
550 (flowEntry.flowEntryId().value() == 0)) {
551 long id = getNextFlowEntryId();
552 flowEntry.setFlowEntryId(new FlowEntryId(id));
553 }
554
555 IFlowEntry flowEntryObj = null;
556 boolean found = false;
557 try {
558 if ((flowEntryObj =
559 conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) {
560 log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry",
561 flowEntry.flowEntryId().toString());
562 found = true;
563 } else {
564 flowEntryObj = conn.utils().newFlowEntry(conn);
565 log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry",
566 flowEntry.flowEntryId().toString());
567 }
568 } catch (Exception e) {
569 log.error(":addFlow FlowEntryId:{} failed",
570 flowEntry.flowEntryId().toString());
571 return false;
572 }
573 if (flowEntryObj == null) {
574 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
575 flowEntry.flowEntryId().toString());
576 return false;
577 }
578
579 //
580 // Set the Flow Entry key:
581 // - flowEntry.flowEntryId()
582 //
583 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
584 flowEntryObj.setType("flow_entry");
585
586 //
587 // Set the Flow Entry Edges and attributes:
588 // - Switch edge
589 // - InPort edge
590 // - OutPort edge
591 //
592 // - flowEntry.flowEntryMatch()
593 // - flowEntry.flowEntryActions()
594 // - flowEntry.dpid()
595 // - flowEntry.flowEntryUserState()
596 // - flowEntry.flowEntrySwitchState()
597 // - flowEntry.flowEntryErrorState()
598 // - flowEntry.matchInPort()
599 // - flowEntry.matchEthernetFrameType()
600 // - flowEntry.matchSrcIPv4Net()
601 // - flowEntry.matchDstIPv4Net()
602 // - flowEntry.matchSrcMac()
603 // - flowEntry.matchDstMac()
604 // - flowEntry.actionOutput()
605 //
606 ISwitchObject sw =
607 conn.utils().searchSwitch(conn, flowEntry.dpid().toString());
608 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
609 flowEntryObj.setSwitch(sw);
610 if (flowEntry.flowEntryMatch().matchInPort()) {
611 IPortObject inport =
612 conn.utils().searchPort(conn, flowEntry.dpid().toString(),
613 flowEntry.flowEntryMatch().inPort().value());
614 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
615 flowEntryObj.setInPort(inport);
616 }
617 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
618 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
619 }
620 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
621 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
622 }
623 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
624 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
625 }
626 if (flowEntry.flowEntryMatch().matchSrcMac()) {
627 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
628 }
629 if (flowEntry.flowEntryMatch().matchDstMac()) {
630 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
631 }
632
633 for (FlowEntryAction fa : flowEntry.flowEntryActions()) {
634 if (fa.actionOutput() != null) {
635 IPortObject outport =
636 conn.utils().searchPort(conn,
637 flowEntry.dpid().toString(),
638 fa.actionOutput().port().value());
639 flowEntryObj.setActionOutput(fa.actionOutput().port().value());
640 flowEntryObj.setOutPort(outport);
641 }
642 }
643 // TODO: Hacks with hard-coded state names!
644 if (found)
645 flowEntryObj.setUserState("FE_USER_MODIFY");
646 else
647 flowEntryObj.setUserState("FE_USER_ADD");
648 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
649 //
650 // TODO: Take care of the FlowEntryErrorState.
651 //
652
653 // Flow Entries edges:
654 // Flow
655 // NextFE (TODO)
656 if (! found) {
657 flowObj.addFlowEntry(flowEntryObj);
658 flowEntryObj.setFlow(flowObj);
659 }
660
661 return true;
662 }
663
664 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800665 * Delete a previously added flow.
666 *
667 * @param flowId the Flow ID of the flow to delete.
668 * @return true on success, otherwise false.
669 */
670 @Override
671 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700672 /*
673 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700674 if (flowId.value() == measurementFlowId) {
675 modifiedMeasurementFlowTime = System.nanoTime();
676 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700677 */
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700678
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800679 IFlowPath flowObj = null;
680 //
681 // We just mark the entries for deletion,
682 // and let the switches remove each individual entry after
683 // it has been removed from the switches.
684 //
685 try {
686 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
687 != null) {
688 log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath",
689 flowId.toString());
690 } else {
691 log.debug("Deleting FlowPath with FlowId {}: FlowPath not found",
692 flowId.toString());
693 }
694 } catch (Exception e) {
695 // TODO: handle exceptions
696 conn.endTx(Transaction.ROLLBACK);
697 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
698 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700699 if (flowObj == null) {
700 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800701 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700702 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800703
704 //
705 // Find and mark for deletion all Flow Entries
706 //
707 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
708 boolean empty = true; // TODO: an ugly hack
709 for (IFlowEntry flowEntryObj : flowEntries) {
710 empty = false;
711 // flowObj.removeFlowEntry(flowEntryObj);
712 // conn.utils().removeFlowEntry(conn, flowEntryObj);
713 flowEntryObj.setUserState("FE_USER_DELETE");
714 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
715 }
716 // Remove from the database empty flows
717 if (empty)
718 conn.utils().removeFlowPath(conn, flowObj);
719 conn.endTx(Transaction.COMMIT);
720
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800721 return true;
722 }
723
724 /**
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700725 * Clear the state for a previously added flow.
726 *
727 * @param flowId the Flow ID of the flow to clear.
728 * @return true on success, otherwise false.
729 */
730 @Override
731 public boolean clearFlow(FlowId flowId) {
732 IFlowPath flowObj = null;
733 try {
734 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
735 != null) {
736 log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath",
737 flowId.toString());
738 } else {
739 log.debug("Clearing FlowPath with FlowId {}: FlowPath not found",
740 flowId.toString());
741 }
742 } catch (Exception e) {
743 // TODO: handle exceptions
744 conn.endTx(Transaction.ROLLBACK);
745 log.error(":clearFlow FlowId:{} failed", flowId.toString());
746 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700747 if (flowObj == null) {
748 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700749 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700750 }
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700751
752 //
753 // Remove all Flow Entries
754 //
755 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
756 for (IFlowEntry flowEntryObj : flowEntries) {
757 flowObj.removeFlowEntry(flowEntryObj);
758 conn.utils().removeFlowEntry(conn, flowEntryObj);
759 }
760 // Remove the Flow itself
761 conn.utils().removeFlowPath(conn, flowObj);
762 conn.endTx(Transaction.COMMIT);
763
764 return true;
765 }
766
767 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800768 * Get a previously added flow.
769 *
770 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800771 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800772 */
773 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800774 public FlowPath getFlow(FlowId flowId) {
775 IFlowPath flowObj = null;
776 try {
777 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
778 != null) {
779 log.debug("Get FlowPath with FlowId {}: found existing FlowPath",
780 flowId.toString());
781 } else {
782 log.debug("Get FlowPath with FlowId {}: FlowPath not found",
783 flowId.toString());
784 }
785 } catch (Exception e) {
786 // TODO: handle exceptions
787 conn.endTx(Transaction.ROLLBACK);
788 log.error(":getFlow FlowId:{} failed", flowId.toString());
789 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700790 if (flowObj == null) {
791 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800792 return null; // Flow not found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700793 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800794
795 //
796 // Extract the Flow state
797 //
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800798 FlowPath flowPath = extractFlowPath(flowObj);
799 conn.endTx(Transaction.COMMIT);
800
801 return flowPath;
802 }
803
804 /**
805 * Get all previously added flows by a specific installer for a given
806 * data path endpoints.
807 *
808 * @param installerId the Caller ID of the installer of the flow to get.
809 * @param dataPathEndpoints the data path endpoints of the flow to get.
810 * @return the Flow Paths if found, otherwise null.
811 */
812 @Override
813 public ArrayList<FlowPath> getAllFlows(CallerId installerId,
814 DataPathEndpoints dataPathEndpoints) {
815 //
816 // TODO: The implementation below is not optimal:
817 // We fetch all flows, and then return only the subset that match
818 // the query conditions.
819 // We should use the appropriate Titan/Gremlin query to filter-out
820 // the flows as appropriate.
821 //
822 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700823 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800824
825 if (allFlows == null) {
826 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700827 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800828 }
829
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800830 for (FlowPath flow : allFlows) {
831 //
832 // TODO: String-based comparison is sub-optimal.
833 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -0800834 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800835 //
836 if (! flow.installerId().toString().equals(installerId.toString()))
837 continue;
838 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
839 continue;
840 }
841 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
842 continue;
843 }
844 flowPaths.add(flow);
845 }
846
847 if (flowPaths.isEmpty()) {
848 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800849 } else {
850 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints);
851 }
852
853 return flowPaths;
854 }
855
856 /**
857 * Get all installed flows by all installers for given data path endpoints.
858 *
859 * @param dataPathEndpoints the data path endpoints of the flows to get.
860 * @return the Flow Paths if found, otherwise null.
861 */
862 @Override
863 public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
864 //
865 // TODO: The implementation below is not optimal:
866 // We fetch all flows, and then return only the subset that match
867 // the query conditions.
868 // We should use the appropriate Titan/Gremlin query to filter-out
869 // the flows as appropriate.
870 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700871 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
872 ArrayList<FlowPath> allFlows = getAllFlows();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800873
874 if (allFlows == null) {
875 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700876 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800877 }
878
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800879 for (FlowPath flow : allFlows) {
880 //
881 // TODO: String-based comparison is sub-optimal.
882 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -0800883 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800884 //
885 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
886 continue;
887 }
888 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
889 continue;
890 }
891 flowPaths.add(flow);
892 }
893
894 if (flowPaths.isEmpty()) {
895 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800896 } else {
897 log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints);
898 }
899
900 return flowPaths;
901 }
902
903 /**
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700904 * Get summary of all installed flows by all installers in a given range
905 *
906 * @param flowId the data path endpoints of the flows to get.
907 * @param maxFlows: the maximum number of flows to be returned
908 * @return the Flow Paths if found, otherwise null.
909 */
910 @Override
911 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) {
912 //
913 // TODO: The implementation below is not optimal:
914 // We fetch all flows, and then return only the subset that match
915 // the query conditions.
916 // We should use the appropriate Titan/Gremlin query to filter-out
917 // the flows as appropriate.
918 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700919 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
920
921 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700922
923 if (allFlows == null) {
924 log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700925 return flowPaths;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700926 }
927
Umesh Krishnaswamy244b4ae2013-03-29 12:05:15 -0700928 Collections.sort(allFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700929
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700930 for (FlowPath flow : allFlows) {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700931 flow.setFlowEntryMatch(null);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700932
Pavlin Radoslavov96b43422013-04-04 19:14:56 -0700933 // start from desired flowId
934 if (flow.flowId().value() < flowId.value()) {
935 continue;
936 }
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700937
938 // Summarize by making null flow entry fields that are not relevant to report
939 for (FlowEntry flowEntry : flow.dataPath().flowEntries()) {
940 flowEntry.setFlowEntryActions(null);
941 flowEntry.setFlowEntryMatch(null);
942 }
943
944 flowPaths.add(flow);
945 if (maxFlows != 0 && flowPaths.size() >= maxFlows) {
946 break;
947 }
948 }
949
950 if (flowPaths.isEmpty()) {
951 log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700952 } else {
953 log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows);
954 }
955
956 return flowPaths;
957 }
958
959 /**
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800960 * Get all installed flows by all installers.
961 *
962 * @return the Flow Paths if found, otherwise null.
963 */
964 @Override
965 public ArrayList<FlowPath> getAllFlows() {
966 Iterable<IFlowPath> flowPathsObj = null;
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700967 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800968
969 try {
970 if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
971 log.debug("Get all FlowPaths: found FlowPaths");
972 } else {
973 log.debug("Get all FlowPaths: no FlowPaths found");
974 }
975 } catch (Exception e) {
976 // TODO: handle exceptions
977 conn.endTx(Transaction.ROLLBACK);
978 log.error(":getAllFlowPaths failed");
979 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700980 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
981 conn.endTx(Transaction.COMMIT);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700982 return flowPaths; // No Flows found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700983 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800984
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800985 for (IFlowPath flowObj : flowPathsObj) {
986 //
987 // Extract the Flow state
988 //
989 FlowPath flowPath = extractFlowPath(flowObj);
Pavlin Radoslavov3f2af732013-03-29 15:29:35 -0700990 if (flowPath != null)
991 flowPaths.add(flowPath);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800992 }
993
994 conn.endTx(Transaction.COMMIT);
995
996 return flowPaths;
997 }
998
999 /**
1000 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
1001 *
1002 * @param flowObj the object to extract the Flow Path State from.
1003 * @return the extracted Flow Path State.
1004 */
1005 private FlowPath extractFlowPath(IFlowPath flowObj) {
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001006 //
1007 // Extract the Flow state
1008 //
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001009 String flowIdStr = flowObj.getFlowId();
1010 String installerIdStr = flowObj.getInstallerId();
1011 String srcSwitchStr = flowObj.getSrcSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001012 Short srcPortShort = flowObj.getSrcPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001013 String dstSwitchStr = flowObj.getDstSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001014 Short dstPortShort = flowObj.getDstPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001015
1016 if ((flowIdStr == null) ||
1017 (installerIdStr == null) ||
1018 (srcSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001019 (srcPortShort == null) ||
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001020 (dstSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001021 (dstPortShort == null)) {
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001022 // TODO: A work-around, becauuse of some bogus database objects
1023 return null;
1024 }
1025
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001026 FlowPath flowPath = new FlowPath();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001027 flowPath.setFlowId(new FlowId(flowIdStr));
1028 flowPath.setInstallerId(new CallerId(installerIdStr));
1029 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001030 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001031 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001032 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001033 //
1034 // Extract the match conditions common for all Flow Entries
1035 //
1036 {
1037 FlowEntryMatch match = new FlowEntryMatch();
1038 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
1039 if (matchEthernetFrameType != null)
1040 match.enableEthernetFrameType(matchEthernetFrameType);
1041 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
1042 if (matchSrcIPv4Net != null)
1043 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1044 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
1045 if (matchDstIPv4Net != null)
1046 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1047 String matchSrcMac = flowObj.getMatchSrcMac();
1048 if (matchSrcMac != null)
1049 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1050 String matchDstMac = flowObj.getMatchDstMac();
1051 if (matchDstMac != null)
1052 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1053 flowPath.setFlowEntryMatch(match);
1054 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001055
1056 //
1057 // Extract all Flow Entries
1058 //
1059 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1060 for (IFlowEntry flowEntryObj : flowEntries) {
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001061 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
1062 if (flowEntry == null)
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001063 continue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001064 flowPath.dataPath().flowEntries().add(flowEntry);
1065 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001066
1067 return flowPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001068 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001069
1070 /**
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001071 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
1072 *
1073 * @param flowEntryObj the object to extract the Flow Entry State from.
1074 * @return the extracted Flow Entry State.
1075 */
1076 private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
1077 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1078 String switchDpidStr = flowEntryObj.getSwitchDpid();
1079 String userState = flowEntryObj.getUserState();
1080 String switchState = flowEntryObj.getSwitchState();
1081
1082 if ((flowEntryIdStr == null) ||
1083 (switchDpidStr == null) ||
1084 (userState == null) ||
1085 (switchState == null)) {
1086 // TODO: A work-around, becauuse of some bogus database objects
1087 return null;
1088 }
1089
1090 FlowEntry flowEntry = new FlowEntry();
1091 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
1092 flowEntry.setDpid(new Dpid(switchDpidStr));
1093
1094 //
1095 // Extract the match conditions
1096 //
1097 FlowEntryMatch match = new FlowEntryMatch();
1098 Short matchInPort = flowEntryObj.getMatchInPort();
1099 if (matchInPort != null)
1100 match.enableInPort(new Port(matchInPort));
1101 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1102 if (matchEthernetFrameType != null)
1103 match.enableEthernetFrameType(matchEthernetFrameType);
1104 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1105 if (matchSrcIPv4Net != null)
1106 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1107 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1108 if (matchDstIPv4Net != null)
1109 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1110 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1111 if (matchSrcMac != null)
1112 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1113 String matchDstMac = flowEntryObj.getMatchDstMac();
1114 if (matchDstMac != null)
1115 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1116 flowEntry.setFlowEntryMatch(match);
1117
1118 //
1119 // Extract the actions
1120 //
1121 ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>();
1122 Short actionOutputPort = flowEntryObj.getActionOutput();
1123 if (actionOutputPort != null) {
1124 FlowEntryAction action = new FlowEntryAction();
1125 action.setActionOutput(new Port(actionOutputPort));
1126 actions.add(action);
1127 }
1128 flowEntry.setFlowEntryActions(actions);
1129 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
1130 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
1131 //
1132 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
1133 // and FlowEntryErrorState.
1134 //
1135 return flowEntry;
1136 }
1137
1138 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001139 * Add and maintain a shortest-path flow.
1140 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001141 * NOTE: The Flow Path argument does NOT contain flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001142 *
1143 * @param flowPath the Flow Path with the endpoints and the match
1144 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001145 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001146 */
1147 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001148 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001149 String dataPathSummaryStr = null;
1150
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001151 //
1152 // Do the shortest path computation
1153 //
1154 DataPath dataPath =
1155 topoRouteService.getShortestPath(flowPath.dataPath().srcPort(),
1156 flowPath.dataPath().dstPort());
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001157 if (dataPath == null) {
1158 // We need the DataPath to populate the Network MAP
1159 dataPath = new DataPath();
1160 dataPath.setSrcPort(flowPath.dataPath().srcPort());
1161 dataPath.setDstPort(flowPath.dataPath().dstPort());
1162 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001163
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001164 // Compute the Data Path summary
1165 dataPathSummaryStr = dataPath.dataPathSummary();
1166
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001167 //
1168 // Set the incoming port matching and the outgoing port output
1169 // actions for each flow entry.
1170 //
1171 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1172 // Set the incoming port matching
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001173 FlowEntryMatch flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001174 flowEntry.setFlowEntryMatch(flowEntryMatch);
1175 flowEntryMatch.enableInPort(flowEntry.inPort());
1176
1177 // Set the outgoing port output action
1178 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1179 if (flowEntryActions == null) {
1180 flowEntryActions = new ArrayList<FlowEntryAction>();
1181 flowEntry.setFlowEntryActions(flowEntryActions);
1182 }
1183 FlowEntryAction flowEntryAction = new FlowEntryAction();
1184 flowEntryAction.setActionOutput(flowEntry.outPort());
1185 flowEntryActions.add(flowEntryAction);
1186 }
1187
1188 //
1189 // Prepare the computed Flow Path
1190 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001191 FlowPath computedFlowPath = new FlowPath();
1192 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1193 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1194 computedFlowPath.setDataPath(dataPath);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001195 computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch()));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001196
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001197 FlowId flowId = new FlowId();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001198 if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr))
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001199 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001200
1201 // TODO: Mark the flow for maintenance purpose
1202
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001203 return (computedFlowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001204 }
1205
1206 /**
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001207 * Reconcile all flows in a set.
1208 *
1209 * @param flowObjSet the set of flows that need to be reconciliated.
1210 */
1211 public void reconcileFlows(Iterable<IFlowPath> flowObjSet) {
1212 if (! flowObjSet.iterator().hasNext())
1213 return;
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001214
1215 //
1216 // Remove the old Flow Entries, and add the new Flow Entries
1217 //
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001218
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001219 Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001220 LinkedList<FlowPath> flowPaths = new LinkedList<FlowPath>();
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001221 for (IFlowPath flowObj : flowObjSet) {
1222 FlowPath flowPath = extractFlowPath(flowObj);
1223 if (flowPath == null)
1224 continue;
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001225 flowPaths.add(flowPath);
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001226
1227 //
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001228 // Remove the Flow Entries from the Network MAP
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001229 //
1230 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001231 LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>();
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001232 for (IFlowEntry flowEntryObj : flowEntries) {
1233 String dpidStr = flowEntryObj.getSwitchDpid();
1234 if (dpidStr == null)
1235 continue;
1236 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001237 /*
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001238 IOFSwitch mySwitch = mySwitches.get(dpid.value());
1239 if (mySwitch == null)
1240 continue; // Ignore the entry: not my switch
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001241 */
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001242 deleteFlowEntries.add(flowEntryObj);
1243 }
1244 for (IFlowEntry flowEntryObj : deleteFlowEntries) {
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001245 flowObj.removeFlowEntry(flowEntryObj);
1246 conn.utils().removeFlowEntry(conn, flowEntryObj);
1247 }
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001248 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001249
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001250 for (FlowPath flowPath : flowPaths) {
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001251 //
1252 // Delete the flow entries from the switches
1253 //
1254 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
1255 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001256 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
1257 if (mySwitch == null) {
1258 // Not my switch
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001259 installRemoteFlowEntry(flowPath, flowEntry);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001260 } else {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001261 installFlowEntry(mySwitch, flowPath, flowEntry);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001262 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001263 }
1264
1265 //
1266 // Calculate the new shortest path and install it in the
1267 // Network MAP.
1268 //
1269 FlowPath addedFlowPath = addAndMaintainShortestPathFlow(flowPath);
1270 if (addedFlowPath == null) {
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001271 log.error("Cannot add Shortest Path Flow from {} to {}: path not found?",
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001272 flowPath.dataPath().srcPort().toString(),
1273 flowPath.dataPath().dstPort().toString());
1274 continue;
1275 }
1276
1277 //
1278 // Add the flow entries to the switches
1279 //
1280 for (FlowEntry flowEntry : addedFlowPath.dataPath().flowEntries()) {
1281 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001282 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
1283 if (mySwitch == null) {
1284 // Not my switch
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001285 installRemoteFlowEntry(addedFlowPath, flowEntry);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001286 continue;
1287 }
1288
1289 IFlowEntry flowEntryObj =
1290 conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId());
1291 if (flowEntryObj == null) {
1292 //
1293 // TODO: Remove the "new Object[] wrapper in the statement
1294 // below after the SLF4J logger is upgraded to
1295 // Version 1.7.5
1296 //
1297 log.error("Cannot add Flow Entry to switch {} for Path Flow from {} to {} : Flow Entry not in the Network MAP",
1298 new Object[] {
1299 flowEntry.dpid(),
1300 flowPath.dataPath().srcPort(),
1301 flowPath.dataPath().dstPort()
1302 });
1303 continue;
1304 }
1305 // Update the Flow Entry Switch State in the Network MAP
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001306 if (installFlowEntry(mySwitch, addedFlowPath, flowEntry)) {
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001307 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
1308 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001309 }
1310 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001311 }
1312
1313 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001314 * Install a Flow Entry on a switch.
1315 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001316 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001317 * @param flowObj the flow path object for the flow entry to install.
1318 * @param flowEntryObj the flow entry object to install.
1319 * @return true on success, otherwise false.
1320 */
1321 public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj,
1322 IFlowEntry flowEntryObj) {
1323 FlowEntryId flowEntryId =
1324 new FlowEntryId(flowEntryObj.getFlowEntryId());
1325 String userState = flowEntryObj.getUserState();
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001326
1327 //
1328 // Create the Open Flow Flow Modification Entry to push
1329 //
1330 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1331 .getMessage(OFType.FLOW_MOD);
1332 long cookie = flowEntryId.value();
1333
1334 short flowModCommand = OFFlowMod.OFPFC_ADD;
1335 if (userState.equals("FE_USER_ADD")) {
1336 flowModCommand = OFFlowMod.OFPFC_ADD;
1337 } else if (userState.equals("FE_USER_MODIFY")) {
1338 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1339 } else if (userState.equals("FE_USER_DELETE")) {
1340 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1341 } else {
1342 // Unknown user state. Ignore the entry
1343 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1344 flowEntryId.toString(), userState);
1345 return false;
1346 }
1347
1348 //
1349 // Fetch the match conditions.
1350 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001351 // NOTE: The Flow matching conditions common for all Flow Entries are
1352 // used ONLY if a Flow Entry does NOT have the corresponding matching
1353 // condition set.
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001354 //
1355 OFMatch match = new OFMatch();
1356 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001357
1358 // Match the Incoming Port
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001359 Short matchInPort = flowEntryObj.getMatchInPort();
1360 if (matchInPort != null) {
1361 match.setInputPort(matchInPort);
1362 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1363 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001364
1365 // Match the Ethernet Frame Type
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001366 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1367 if (matchEthernetFrameType == null)
1368 matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
1369 if (matchEthernetFrameType != null) {
1370 match.setDataLayerType(matchEthernetFrameType);
1371 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1372 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001373
1374 // Match the Source IPv4 Network prefix
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001375 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1376 if (matchSrcIPv4Net == null)
1377 matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
1378 if (matchSrcIPv4Net != null) {
1379 match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC);
1380 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001381
1382 // Natch the Destination IPv4 Network prefix
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001383 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1384 if (matchDstIPv4Net == null)
1385 matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
1386 if (matchDstIPv4Net != null) {
1387 match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST);
1388 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001389
1390 // Match the Source MAC address
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001391 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1392 if (matchSrcMac == null)
1393 matchSrcMac = flowObj.getMatchSrcMac();
1394 if (matchSrcMac != null) {
1395 match.setDataLayerSource(matchSrcMac);
1396 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1397 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001398
1399 // Match the Destination MAC address
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001400 String matchDstMac = flowEntryObj.getMatchDstMac();
1401 if (matchDstMac == null)
1402 matchDstMac = flowObj.getMatchDstMac();
1403 if (matchDstMac != null) {
1404 match.setDataLayerDestination(matchDstMac);
1405 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1406 }
1407
1408 //
1409 // Fetch the actions
1410 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001411 // TODO: For now we support only the "OUTPUT" actions.
1412 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001413 List<OFAction> actions = new ArrayList<OFAction>();
1414 Short actionOutputPort = flowEntryObj.getActionOutput();
1415 if (actionOutputPort != null) {
1416 OFActionOutput action = new OFActionOutput();
1417 // XXX: The max length is hard-coded for now
1418 action.setMaxLength((short)0xffff);
1419 action.setPort(actionOutputPort);
1420 actions.add(action);
1421 }
1422
1423 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1424 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1425 .setPriority(PRIORITY_DEFAULT)
1426 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1427 .setCookie(cookie)
1428 .setCommand(flowModCommand)
1429 .setMatch(match)
1430 .setActions(actions)
1431 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1432 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1433 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1434 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1435 if (actionOutputPort != null)
1436 fm.setOutPort(actionOutputPort);
1437 }
1438
1439 //
1440 // TODO: Set the following flag
1441 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1442 // See method ForwardingBase::pushRoute()
1443 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001444
1445 //
1446 // Write the message to the switch
1447 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001448 try {
1449 messageDamper.write(mySwitch, fm, null);
1450 mySwitch.flush();
1451 //
1452 // TODO: We should use the OpenFlow Barrier mechanism
1453 // to check for errors, and update the SwitchState
1454 // for a flow entry after the Barrier message is
1455 // is received.
1456 //
1457 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
1458 } catch (IOException e) {
1459 log.error("Failure writing flow mod from network map", e);
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001460 return false;
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001461 }
1462
1463 return true;
1464 }
1465
1466 /**
1467 * Install a Flow Entry on a switch.
1468 *
1469 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001470 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001471 * @param flowEntry the flow entry to install.
1472 * @return true on success, otherwise false.
1473 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001474 public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1475 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001476 //
1477 // Create the OpenFlow Flow Modification Entry to push
1478 //
1479 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1480 .getMessage(OFType.FLOW_MOD);
1481 long cookie = flowEntry.flowEntryId().value();
1482
1483 short flowModCommand = OFFlowMod.OFPFC_ADD;
1484 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
1485 flowModCommand = OFFlowMod.OFPFC_ADD;
1486 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
1487 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1488 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
1489 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1490 } else {
1491 // Unknown user state. Ignore the entry
1492 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1493 flowEntry.flowEntryId().toString(),
1494 flowEntry.flowEntryUserState());
1495 return false;
1496 }
1497
1498 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001499 // Fetch the match conditions.
1500 //
1501 // NOTE: The Flow matching conditions common for all Flow Entries are
1502 // used ONLY if a Flow Entry does NOT have the corresponding matching
1503 // condition set.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001504 //
1505 OFMatch match = new OFMatch();
1506 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001507 FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch();
1508 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
1509
1510 // Match the Incoming Port
1511 Port matchInPort = flowEntryMatch.inPort();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001512 if (matchInPort != null) {
1513 match.setInputPort(matchInPort.value());
1514 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1515 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001516
1517 // Match the Ethernet Frame Type
1518 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
1519 if ((matchEthernetFrameType == null) && (flowPathMatch != null)) {
1520 matchEthernetFrameType = flowPathMatch.ethernetFrameType();
1521 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001522 if (matchEthernetFrameType != null) {
1523 match.setDataLayerType(matchEthernetFrameType);
1524 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1525 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001526
1527 // Match the Source IPv4 Network prefix
1528 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
1529 if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) {
1530 matchSrcIPv4Net = flowPathMatch.srcIPv4Net();
1531 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001532 if (matchSrcIPv4Net != null) {
1533 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
1534 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001535
1536 // Natch the Destination IPv4 Network prefix
1537 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
1538 if ((matchDstIPv4Net == null) && (flowPathMatch != null)) {
1539 matchDstIPv4Net = flowPathMatch.dstIPv4Net();
1540 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001541 if (matchDstIPv4Net != null) {
1542 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
1543 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001544
1545 // Match the Source MAC address
1546 MACAddress matchSrcMac = flowEntryMatch.srcMac();
1547 if ((matchSrcMac == null) && (flowPathMatch != null)) {
1548 matchSrcMac = flowPathMatch.srcMac();
1549 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001550 if (matchSrcMac != null) {
1551 match.setDataLayerSource(matchSrcMac.toString());
1552 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1553 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001554
1555 // Match the Destination MAC address
1556 MACAddress matchDstMac = flowEntryMatch.dstMac();
1557 if ((matchDstMac == null) && (flowPathMatch != null)) {
1558 matchDstMac = flowPathMatch.dstMac();
1559 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001560 if (matchDstMac != null) {
1561 match.setDataLayerDestination(matchDstMac.toString());
1562 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1563 }
1564
1565 //
1566 // Fetch the actions
1567 //
1568 // TODO: For now we support only the "OUTPUT" actions.
1569 //
1570 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1571 List<OFAction> actions = new ArrayList<OFAction>();
1572 ArrayList<FlowEntryAction> flowEntryActions =
1573 flowEntry.flowEntryActions();
1574 for (FlowEntryAction flowEntryAction : flowEntryActions) {
1575 FlowEntryAction.ActionOutput actionOutput =
1576 flowEntryAction.actionOutput();
1577 if (actionOutput != null) {
1578 short actionOutputPort = actionOutput.port().value();
1579 OFActionOutput action = new OFActionOutput();
1580 // XXX: The max length is hard-coded for now
1581 action.setMaxLength((short)0xffff);
1582 action.setPort(actionOutputPort);
1583 actions.add(action);
1584 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1585 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1586 fm.setOutPort(actionOutputPort);
1587 }
1588 }
1589 }
1590
1591 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1592 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1593 .setPriority(PRIORITY_DEFAULT)
1594 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1595 .setCookie(cookie)
1596 .setCommand(flowModCommand)
1597 .setMatch(match)
1598 .setActions(actions)
1599 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1600
1601 //
1602 // TODO: Set the following flag
1603 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1604 // See method ForwardingBase::pushRoute()
1605 //
1606
1607 //
1608 // Write the message to the switch
1609 //
1610 try {
1611 messageDamper.write(mySwitch, fm, null);
1612 mySwitch.flush();
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001613 //
1614 // TODO: We should use the OpenFlow Barrier mechanism
1615 // to check for errors, and update the SwitchState
1616 // for a flow entry after the Barrier message is
1617 // is received.
1618 //
1619 // TODO: The FlowEntry Object in Titan should be set
1620 // to FE_SWITCH_UPDATED.
1621 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001622 } catch (IOException e) {
1623 log.error("Failure writing flow mod from network map", e);
1624 return false;
1625 }
1626 return true;
1627 }
1628
1629 /**
1630 * Remove a Flow Entry from a switch.
1631 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001632 * @param mySwitch the switch to remove the Flow Entry from.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001633 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001634 * @param flowEntry the flow entry to remove.
1635 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001636 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001637 public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1638 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001639 //
1640 // The installFlowEntry() method implements both installation
1641 // and removal of flow entries.
1642 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001643 return (installFlowEntry(mySwitch, flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001644 }
1645
1646 /**
1647 * Install a Flow Entry on a remote controller.
1648 *
1649 * TODO: We need it now: Jono
1650 * - For now it will make a REST call to the remote controller.
1651 * - Internally, it needs to know the name of the remote controller.
1652 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001653 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001654 * @param flowEntry the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001655 * @return true on success, otherwise false.
1656 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001657 public boolean installRemoteFlowEntry(FlowPath flowPath,
1658 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001659 // TODO: We need it now: Jono
1660 // - For now it will make a REST call to the remote controller.
1661 // - Internally, it needs to know the name of the remote controller.
1662 return true;
1663 }
1664
1665 /**
1666 * Remove a flow entry on a remote controller.
1667 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001668 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001669 * @param flowEntry the flow entry to remove.
1670 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001671 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001672 public boolean removeRemoteFlowEntry(FlowPath flowPath,
1673 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001674 //
1675 // The installRemoteFlowEntry() method implements both installation
1676 // and removal of flow entries.
1677 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001678 return (installRemoteFlowEntry(flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001679 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001680}