blob: 44607a98ce836b19a16c5139d44cd1f67d5b58cc [file] [log] [blame]
HIGUCHI Yuta60a10142013-06-14 15:50:10 -07001package net.onrc.onos.ofcontroller.flowmanager;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08002
3import java.util.ArrayList;
4import java.util.Collection;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08005import java.util.EnumSet;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08006import java.util.HashMap;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +00007import java.util.LinkedList;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08008import java.util.Map;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +00009import java.util.Random;
Pavlin Radoslavov3f9ba652013-10-25 17:19:01 -070010import java.util.concurrent.BlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080011import java.util.concurrent.Executors;
Pavlin Radoslavov3f9ba652013-10-25 17:19:01 -070012import java.util.concurrent.LinkedBlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080013import java.util.concurrent.ScheduledExecutorService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080014import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080015
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080016import net.floodlightcontroller.core.IFloodlightProviderService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080017import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080018import net.floodlightcontroller.core.module.FloodlightModuleContext;
19import net.floodlightcontroller.core.module.FloodlightModuleException;
20import net.floodlightcontroller.core.module.IFloodlightModule;
21import net.floodlightcontroller.core.module.IFloodlightService;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080022import net.floodlightcontroller.restserver.IRestApiService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080023import net.floodlightcontroller.util.OFMessageDamper;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070024
25import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070026import net.onrc.onos.graph.GraphDBOperation;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070027import net.onrc.onos.ofcontroller.core.INetMapStorage;
28import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
29import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -070030import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService;
HIGUCHI Yuta60a10142013-06-14 15:50:10 -070031import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
Pavlin Radoslavov1278ac72013-10-16 04:43:49 -070032import net.onrc.onos.ofcontroller.topology.ITopologyNetService;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070033import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070034import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070035import net.onrc.onos.ofcontroller.util.*;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080036
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080037import org.openflow.protocol.OFType;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
admin944ef4f2013-10-08 17:48:37 -070041/**
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -070042 * Flow Manager class for handling the network flows.
admin944ef4f2013-10-08 17:48:37 -070043 */
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070044public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080045
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070046 protected GraphDBOperation dbHandler;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080047
Jonathan Hart50a94982013-04-10 14:49:51 -070048 protected volatile IFloodlightProviderService floodlightProvider;
Pavlin Radoslavov1278ac72013-10-16 04:43:49 -070049 protected volatile ITopologyNetService topologyNetService;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070050 protected volatile IDatagridService datagridService;
51 protected IRestApiService restApi;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070052 protected FloodlightModuleContext context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080053
54 protected OFMessageDamper messageDamper;
55
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070056 //
57 // TODO: Values copied from elsewhere (class LearningSwitch).
58 // The local copy should go away!
59 //
60 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
61 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080062
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000063 // Flow Entry ID generation state
64 private static Random randomGenerator = new Random();
65 private static int nextFlowEntryIdPrefix = 0;
66 private static int nextFlowEntryIdSuffix = 0;
67 private static long nextFlowEntryId = 0;
68
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080069 /** The logger. */
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070070 private final static Logger log = LoggerFactory.getLogger(FlowManager.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080071
72 // The periodic task(s)
Jonathan Hart50a94982013-04-10 14:49:51 -070073 private ScheduledExecutorService mapReaderScheduler;
74 private ScheduledExecutorService shortestPathReconcileScheduler;
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -070075
Pavlin Radoslavov3f9ba652013-10-25 17:19:01 -070076 // The queue with Flow Path updates
77 protected BlockingQueue<EventEntry<FlowPath>> flowPathEvents =
78 new LinkedBlockingQueue<EventEntry<FlowPath>>();
79
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070080 // The queue with Topology Element updates
81 protected BlockingQueue<EventEntry<TopologyElement>> topologyEvents =
82 new LinkedBlockingQueue<EventEntry<TopologyElement>>();
83
admin944ef4f2013-10-08 17:48:37 -070084 /**
85 * Periodic task for reading the Flow Entries and pushing changes
86 * into the switches.
87 */
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070088 final Runnable mapReader = new Runnable() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080089 public void run() {
Pavlin Radoslavova75caea2013-04-10 19:11:26 -070090 try {
91 runImpl();
92 } catch (Exception e) {
93 log.debug("Exception processing All Flow Entries from the Network MAP: ", e);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070094 dbHandler.rollback();
Pavlin Radoslavova75caea2013-04-10 19:11:26 -070095 return;
96 }
97 }
98
99 private void runImpl() {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700100 long startTime = System.nanoTime();
101 int counterAllFlowEntries = 0;
102 int counterMyNotUpdatedFlowEntries = 0;
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700103
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800104 if (floodlightProvider == null) {
105 log.debug("FloodlightProvider service not found!");
106 return;
107 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000108 Map<Long, IOFSwitch> mySwitches =
109 floodlightProvider.getSwitches();
Pankaj Berdebf51e4f2013-10-03 17:49:23 -0700110 if (mySwitches.isEmpty()) {
Pankaj Berde85a877b2013-10-03 18:26:35 -0700111 log.trace("No switches controlled");
Pankaj Berdebf51e4f2013-10-03 17:49:23 -0700112 return;
113 }
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700114 LinkedList<IFlowEntry> addFlowEntries =
115 new LinkedList<IFlowEntry>();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000116 LinkedList<IFlowEntry> deleteFlowEntries =
117 new LinkedList<IFlowEntry>();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700118
119 //
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700120 // Fetch all Flow Entries which need to be updated and select
121 // only my Flow Entries that need to be updated into the
122 // switches.
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700123 //
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000124 Iterable<IFlowEntry> allFlowEntries =
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700125 dbHandler.getAllSwitchNotUpdatedFlowEntries();
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700126 for (IFlowEntry flowEntryObj : allFlowEntries) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700127 counterAllFlowEntries++;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000128
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000129 String dpidStr = flowEntryObj.getSwitchDpid();
130 if (dpidStr == null)
131 continue;
132 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800133 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000134 if (mySwitch == null)
135 continue; // Ignore the entry: not my switch
136
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700137 IFlowPath flowObj =
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700138 dbHandler.getFlowPathByFlowEntry(flowEntryObj);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700139 if (flowObj == null)
140 continue; // Should NOT happen
141 if (flowObj.getFlowId() == null)
142 continue; // Invalid entry
143
144 //
145 // NOTE: For now we process the DELETE before the ADD
146 // to cover the more common scenario.
147 // TODO: This is error prone and needs to be fixed!
148 //
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000149 String userState = flowEntryObj.getUserState();
150 if (userState == null)
151 continue;
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700152 if (userState.equals("FE_USER_DELETE")) {
153 // An entry that needs to be deleted.
154 deleteFlowEntries.add(flowEntryObj);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700155 installFlowEntry(mySwitch, flowObj, flowEntryObj);
156 } else {
157 addFlowEntries.add(flowEntryObj);
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700158 }
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700159 counterMyNotUpdatedFlowEntries++;
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700160 }
161
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700162 //
163 // Process the Flow Entries that need to be added
164 //
165 for (IFlowEntry flowEntryObj : addFlowEntries) {
166 IFlowPath flowObj =
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700167 dbHandler.getFlowPathByFlowEntry(flowEntryObj);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700168 if (flowObj == null)
169 continue; // Should NOT happen
170 if (flowObj.getFlowId() == null)
171 continue; // Invalid entry
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700172
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700173 Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid());
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700174 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000175 if (mySwitch == null)
176 continue; // Shouldn't happen
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700177 installFlowEntry(mySwitch, flowObj, flowEntryObj);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800178 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000179
180 //
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000181 // Delete all Flow Entries marked for deletion from the
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -0700182 // Network MAP.
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000183 //
184 // TODO: We should use the OpenFlow Barrier mechanism
185 // to check for errors, and delete the Flow Entries after the
186 // Barrier message is received.
187 //
188 while (! deleteFlowEntries.isEmpty()) {
189 IFlowEntry flowEntryObj = deleteFlowEntries.poll();
190 IFlowPath flowObj =
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700191 dbHandler.getFlowPathByFlowEntry(flowEntryObj);
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000192 if (flowObj == null) {
193 log.debug("Did not find FlowPath to be deleted");
194 continue;
195 }
196 flowObj.removeFlowEntry(flowEntryObj);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700197 dbHandler.removeFlowEntry(flowEntryObj);
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000198 }
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700199
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700200 dbHandler.commit();
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700201
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700202 long estimatedTime = System.nanoTime() - startTime;
203 double rate = 0.0;
204 if (estimatedTime > 0)
205 rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime;
206 String logMsg = "MEASUREMENT: Processed AllFlowEntries: " +
207 counterAllFlowEntries + " MyNotUpdatedFlowEntries: " +
208 counterMyNotUpdatedFlowEntries + " in " +
209 (double)estimatedTime / 1000000000 + " sec: " +
210 rate + " paths/s";
211 log.debug(logMsg);
212 }
213 };
214
admin944ef4f2013-10-08 17:48:37 -0700215 /**
216 * Periodic task for reading the Flow Paths and recomputing the
217 * shortest paths.
218 */
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700219 final Runnable shortestPathReconcile = new Runnable() {
220 public void run() {
Pavlin Radoslavova75caea2013-04-10 19:11:26 -0700221 try {
222 runImpl();
223 } catch (Exception e) {
224 log.debug("Exception processing All Flows from the Network MAP: ", e);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700225 dbHandler.rollback();
Pavlin Radoslavova75caea2013-04-10 19:11:26 -0700226 return;
227 }
228 }
229
230 private void runImpl() {
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700231 long startTime = System.nanoTime();
232 int counterAllFlowPaths = 0;
233 int counterMyFlowPaths = 0;
234
235 if (floodlightProvider == null) {
236 log.debug("FloodlightProvider service not found!");
237 return;
238 }
239 Map<Long, IOFSwitch> mySwitches =
240 floodlightProvider.getSwitches();
Pankaj Berdebf51e4f2013-10-03 17:49:23 -0700241 if (mySwitches.isEmpty()) {
Pankaj Berde85a877b2013-10-03 18:26:35 -0700242 log.trace("No switches controlled");
Pankaj Berdebf51e4f2013-10-03 17:49:23 -0700243 return;
244 }
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700245 LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>();
246
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700247 //
248 // Fetch and recompute the Shortest Path for those
249 // Flow Paths this controller is responsible for.
250 //
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700251 Topology topology = topologyNetService.newDatabaseTopology();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700252 Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths();
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700253 for (IFlowPath flowPathObj : allFlowPaths) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700254 counterAllFlowPaths++;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700255 if (flowPathObj == null)
256 continue;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700257
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700258 String srcDpidStr = flowPathObj.getSrcSwitch();
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000259 if (srcDpidStr == null)
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700260 continue;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700261 Dpid srcDpid = new Dpid(srcDpidStr);
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700262 //
263 // Use the source DPID as a heuristic to decide
264 // which controller is responsible for maintaining the
265 // shortest path.
266 // NOTE: This heuristic is error-prone: if the switch
267 // goes away and no controller is responsible for that
268 // switch, then the original Flow Path is not cleaned-up
269 //
270 IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
271 if (mySwitch == null)
272 continue; // Ignore: not my responsibility
273
Pavlin Radoslavov99d1b152013-04-09 22:57:33 -0700274 // Test the Data Path Summary string
275 String dataPathSummaryStr = flowPathObj.getDataPathSummary();
276 if (dataPathSummaryStr == null)
277 continue; // Could be invalid entry?
278 if (dataPathSummaryStr.isEmpty())
279 continue; // No need to maintain this flow
280
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000281 //
282 // Test whether we need to complete the Flow cleanup,
283 // if the Flow has been deleted by the user.
284 //
285 String flowUserState = flowPathObj.getUserState();
286 if ((flowUserState != null)
287 && flowUserState.equals("FE_USER_DELETE")) {
288 Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries();
Yuta HIGUCHI2ded2dd2013-10-09 18:06:41 -0700289 final boolean empty = !flowEntries.iterator().hasNext();
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000290 if (empty)
291 deleteFlows.add(flowPathObj);
292 }
293
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000294 // Fetch the fields needed to recompute the shortest path
295 Short srcPortShort = flowPathObj.getSrcPort();
296 String dstDpidStr = flowPathObj.getDstSwitch();
297 Short dstPortShort = flowPathObj.getDstPort();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700298 Long flowPathFlagsLong = flowPathObj.getFlowPathFlags();
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000299 if ((srcPortShort == null) ||
300 (dstDpidStr == null) ||
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700301 (dstPortShort == null) ||
302 (flowPathFlagsLong == null)) {
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000303 continue;
304 }
305
306 Port srcPort = new Port(srcPortShort);
307 Dpid dstDpid = new Dpid(dstDpidStr);
308 Port dstPort = new Port(dstPortShort);
309 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
310 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700311 FlowPathFlags flowPathFlags = new FlowPathFlags(flowPathFlagsLong);
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000312
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700313 counterMyFlowPaths++;
314
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700315 //
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700316 // NOTE: Using here the regular getDatabaseShortestPath()
317 // method won't work here, because that method calls
318 // internally "conn.endTx(Transaction.COMMIT)", and that
319 // will invalidate all handlers to the Titan database.
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700320 // If we want to experiment with calling here
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700321 // getDatabaseShortestPath(), we need to refactor that code
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700322 // to avoid closing the transaction.
323 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700324 DataPath dataPath =
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700325 topologyNetService.getTopologyShortestPath(
326 topology,
327 srcSwitchPort,
328 dstSwitchPort);
Pavlin Radoslavov4a325822013-04-02 22:16:59 +0000329 if (dataPath == null) {
330 // We need the DataPath to compare the paths
331 dataPath = new DataPath();
332 dataPath.setSrcPort(srcSwitchPort);
333 dataPath.setDstPort(dstSwitchPort);
334 }
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700335 dataPath.applyFlowPathFlags(flowPathFlags);
Pavlin Radoslavov4a325822013-04-02 22:16:59 +0000336
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700337 String newDataPathSummaryStr = dataPath.dataPathSummary();
338 if (dataPathSummaryStr.equals(newDataPathSummaryStr))
339 continue; // Nothing changed
340
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700341 reconcileFlow(flowPathObj, dataPath);
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700342 }
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000343
344 //
345 // Delete all leftover Flows marked for deletion from the
346 // Network MAP.
347 //
348 while (! deleteFlows.isEmpty()) {
349 IFlowPath flowPathObj = deleteFlows.poll();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700350 dbHandler.removeFlowPath(flowPathObj);
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000351 }
352
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700353 topologyNetService.dropTopology(topology);
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700354
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700355 dbHandler.commit();
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700356
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700357 long estimatedTime = System.nanoTime() - startTime;
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700358 double rate = 0.0;
359 if (estimatedTime > 0)
360 rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime;
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700361 String logMsg = "MEASUREMENT: Processed AllFlowPaths: " +
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700362 counterAllFlowPaths + " MyFlowPaths: " +
363 counterMyFlowPaths + " in " +
364 (double)estimatedTime / 1000000000 + " sec: " +
365 rate + " paths/s";
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700366 log.debug(logMsg);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800367 }
368 };
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700369
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800370
admin944ef4f2013-10-08 17:48:37 -0700371 /**
372 * Initialize the Flow Manager.
373 *
374 * @param conf the Graph Database configuration string.
375 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800376 @Override
377 public void init(String conf) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700378 dbHandler = new GraphDBOperation(conf);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800379 }
380
admin944ef4f2013-10-08 17:48:37 -0700381 /**
382 * Shutdown the Flow Manager operation.
383 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800384 public void finalize() {
Toshio Koide9fe1cb22013-06-13 13:51:11 -0700385 close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800386 }
387
admin944ef4f2013-10-08 17:48:37 -0700388 /**
389 * Shutdown the Flow Manager operation.
390 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800391 @Override
392 public void close() {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700393 datagridService.deregisterFlowService(this);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700394 dbHandler.close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800395 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800396
admin944ef4f2013-10-08 17:48:37 -0700397 /**
398 * Get the collection of offered module services.
399 *
400 * @return the collection of offered module services.
401 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800402 @Override
403 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
404 Collection<Class<? extends IFloodlightService>> l =
405 new ArrayList<Class<? extends IFloodlightService>>();
406 l.add(IFlowService.class);
407 return l;
408 }
409
admin944ef4f2013-10-08 17:48:37 -0700410 /**
411 * Get the collection of implemented services.
412 *
413 * @return the collection of implemented services.
414 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800415 @Override
416 public Map<Class<? extends IFloodlightService>, IFloodlightService>
417 getServiceImpls() {
418 Map<Class<? extends IFloodlightService>,
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700419 IFloodlightService> m =
420 new HashMap<Class<? extends IFloodlightService>,
421 IFloodlightService>();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800422 m.put(IFlowService.class, this);
423 return m;
424 }
425
admin944ef4f2013-10-08 17:48:37 -0700426 /**
427 * Get the collection of modules this module depends on.
428 *
429 * @return the collection of modules this module depends on.
430 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800431 @Override
432 public Collection<Class<? extends IFloodlightService>>
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700433 getModuleDependencies() {
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800434 Collection<Class<? extends IFloodlightService>> l =
435 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800436 l.add(IFloodlightProviderService.class);
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700437 l.add(INetworkGraphService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700438 l.add(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800439 l.add(IRestApiService.class);
440 return l;
441 }
442
admin944ef4f2013-10-08 17:48:37 -0700443 /**
444 * Initialize the module.
445 *
446 * @param context the module context to use for the initialization.
447 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800448 @Override
449 public void init(FloodlightModuleContext context)
450 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700451 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800452 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700453 topologyNetService = context.getServiceImpl(ITopologyNetService.class);
454 datagridService = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800455 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700456
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800457 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
458 EnumSet.of(OFType.FLOW_MOD),
459 OFMESSAGE_DAMPER_TIMEOUT);
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -0700460
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700461 this.init("");
462
admin944ef4f2013-10-08 17:48:37 -0700463 mapReaderScheduler = Executors.newScheduledThreadPool(1);
464 shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800465 }
466
admin944ef4f2013-10-08 17:48:37 -0700467 /**
468 * Get the next Flow Entry ID to use.
469 *
470 * @return the next Flow Entry ID to use.
471 */
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700472 public synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000473 //
474 // Generate the next Flow Entry ID.
475 // NOTE: For now, the higher 32 bits are random, and
476 // the lower 32 bits are sequential.
477 // In the future, we need a better allocation mechanism.
478 //
479 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
480 nextFlowEntryIdPrefix = randomGenerator.nextInt();
481 nextFlowEntryIdSuffix = 0;
482 } else {
483 nextFlowEntryIdSuffix++;
484 }
485 long result = (long)nextFlowEntryIdPrefix << 32;
486 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
487 return result;
488 }
489
admin944ef4f2013-10-08 17:48:37 -0700490 /**
491 * Startup module operation.
492 *
493 * @param context the module context to use for the startup.
494 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800495 @Override
496 public void startUp(FloodlightModuleContext context) {
admin944ef4f2013-10-08 17:48:37 -0700497 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700498
admin944ef4f2013-10-08 17:48:37 -0700499 // Initialize the Flow Entry ID generator
500 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700501
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700502 // Register with the Datagrid Service
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700503 datagridService.registerFlowService(this);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700504
505 // Obtain the initial Topology state
506 Collection<TopologyElement> topologyElements =
507 datagridService.getAllTopologyElements();
508 for (TopologyElement topologyElement : topologyElements) {
509 EventEntry<TopologyElement> eventEntry =
510 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
511 topologyEvents.add(eventEntry);
512 }
513
514 // Obtain the initial Flow state
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700515 Collection<FlowPath> flowPaths = datagridService.getAllFlows();
Pavlin Radoslavov3f9ba652013-10-25 17:19:01 -0700516 for (FlowPath flowPath : flowPaths) {
517 EventEntry<FlowPath> eventEntry =
518 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
519 flowPathEvents.add(eventEntry);
520 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700521
Pavlin Radoslavov3f9ba652013-10-25 17:19:01 -0700522 // Schedule the periodic tasks
admin944ef4f2013-10-08 17:48:37 -0700523 mapReaderScheduler.scheduleAtFixedRate(
524 mapReader, 3, 3, TimeUnit.SECONDS);
525 shortestPathReconcileScheduler.scheduleAtFixedRate(
526 shortestPathReconcile, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800527 }
528
529 /**
530 * Add a flow.
531 *
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800532 * @param flowPath the Flow Path to install.
533 * @param flowId the return-by-reference Flow ID as assigned internally.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700534 * @param dataPathSummaryStr the data path summary string if the added
535 * flow will be maintained internally, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800536 * @return true on success, otherwise false.
537 */
538 @Override
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700539 public boolean addFlow(FlowPath flowPath, FlowId flowId,
540 String dataPathSummaryStr) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700541 if (FlowDatabaseOperation.addFlow(this, dbHandler, flowPath, flowId,
542 dataPathSummaryStr)) {
543 datagridService.notificationSendFlowAdded(flowPath);
544 return true;
545 }
546 return false;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800547 }
548
549 /**
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700550 * Add a flow entry to the Network MAP.
551 *
552 * @param flowObj the corresponding Flow Path object for the Flow Entry.
553 * @param flowEntry the Flow Entry to install.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700554 * @return the added Flow Entry object on success, otherwise null.
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700555 */
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700556 private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700557 return FlowDatabaseOperation.addFlowEntry(this, dbHandler, flowObj,
558 flowEntry);
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700559 }
560
561 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000562 * Delete all previously added flows.
563 *
564 * @return true on success, otherwise false.
565 */
566 @Override
567 public boolean deleteAllFlows() {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700568 if (FlowDatabaseOperation.deleteAllFlows(dbHandler)) {
569 datagridService.notificationSendAllFlowsRemoved();
570 return true;
571 }
572 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000573 }
574
575 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800576 * Delete a previously added flow.
577 *
578 * @param flowId the Flow ID of the flow to delete.
579 * @return true on success, otherwise false.
580 */
581 @Override
582 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700583 if (FlowDatabaseOperation.deleteFlow(dbHandler, flowId)) {
584 datagridService.notificationSendFlowRemoved(flowId);
585 return true;
586 }
587 return false;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800588 }
589
590 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000591 * Clear the state for all previously added flows.
592 *
593 * @return true on success, otherwise false.
594 */
595 @Override
596 public boolean clearAllFlows() {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700597 if (FlowDatabaseOperation.clearAllFlows(dbHandler)) {
598 datagridService.notificationSendAllFlowsRemoved();
599 return true;
600 }
601 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000602 }
603
604 /**
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700605 * Clear the state for a previously added flow.
606 *
607 * @param flowId the Flow ID of the flow to clear.
608 * @return true on success, otherwise false.
609 */
610 @Override
611 public boolean clearFlow(FlowId flowId) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700612 if (FlowDatabaseOperation.clearFlow(dbHandler, flowId)) {
613 datagridService.notificationSendFlowRemoved(flowId);
614 return true;
615 }
616 return false;
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700617 }
618
619 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800620 * Get a previously added flow.
621 *
622 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800623 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800624 */
625 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800626 public FlowPath getFlow(FlowId flowId) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700627 return FlowDatabaseOperation.getFlow(dbHandler, flowId);
628 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800629
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700630 /**
631 * Get all installed flows by all installers.
632 *
633 * @return the Flow Paths if found, otherwise null.
634 */
635 @Override
636 public ArrayList<FlowPath> getAllFlows() {
637 return FlowDatabaseOperation.getAllFlows(dbHandler);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800638 }
639
640 /**
641 * Get all previously added flows by a specific installer for a given
642 * data path endpoints.
643 *
644 * @param installerId the Caller ID of the installer of the flow to get.
645 * @param dataPathEndpoints the data path endpoints of the flow to get.
646 * @return the Flow Paths if found, otherwise null.
647 */
648 @Override
649 public ArrayList<FlowPath> getAllFlows(CallerId installerId,
650 DataPathEndpoints dataPathEndpoints) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700651 return FlowDatabaseOperation.getAllFlows(dbHandler, installerId,
652 dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800653 }
654
655 /**
656 * Get all installed flows by all installers for given data path endpoints.
657 *
658 * @param dataPathEndpoints the data path endpoints of the flows to get.
659 * @return the Flow Paths if found, otherwise null.
660 */
661 @Override
662 public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700663 return FlowDatabaseOperation.getAllFlows(dbHandler, dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800664 }
665
666 /**
admin944ef4f2013-10-08 17:48:37 -0700667 * Get summary of all installed flows by all installers in a given range.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700668 *
admin944ef4f2013-10-08 17:48:37 -0700669 * @param flowId the Flow ID of the first flow in the flow range to get.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700670 * @param maxFlows the maximum number of flows to be returned.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700671 * @return the Flow Paths if found, otherwise null.
672 */
673 @Override
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700674 public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId,
675 int maxFlows) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700676 return FlowDatabaseOperation.getAllFlowsSummary(dbHandler, flowId,
677 maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700678 }
679
680 /**
admin944ef4f2013-10-08 17:48:37 -0700681 * Get all Flows information, without the associated Flow Entries.
682 *
683 * @return all Flows information, without the associated Flow Entries.
684 */
685 public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries() {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700686 return FlowDatabaseOperation.getAllFlowsWithoutFlowEntries(dbHandler);
Pavlin Radoslavov99b12752013-04-04 17:28:06 -0700687 }
688
689 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700690 * Add and maintain a shortest-path flow.
691 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700692 * NOTE: The Flow Path argument does NOT contain flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700693 *
694 * @param flowPath the Flow Path with the endpoints and the match
695 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700696 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700697 */
698 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700699 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700700 //
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +0000701 // Don't do the shortest path computation here.
702 // Instead, let the Flow reconciliation thread take care of it.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700703 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700704
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +0000705 // We need the DataPath to populate the Network MAP
706 DataPath dataPath = new DataPath();
707 dataPath.setSrcPort(flowPath.dataPath().srcPort());
708 dataPath.setDstPort(flowPath.dataPath().dstPort());
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700709
710 //
711 // Prepare the computed Flow Path
712 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700713 FlowPath computedFlowPath = new FlowPath();
714 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
715 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700716 computedFlowPath.setFlowPathFlags(new FlowPathFlags(flowPath.flowPathFlags().flags()));
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700717 computedFlowPath.setDataPath(dataPath);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700718 computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch()));
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700719 computedFlowPath.setFlowEntryActions(new FlowEntryActions(flowPath.flowEntryActions()));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700720
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700721 FlowId flowId = new FlowId();
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +0000722 String dataPathSummaryStr = dataPath.dataPathSummary();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700723 if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr))
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700724 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700725
726 // TODO: Mark the flow for maintenance purpose
727
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700728 return (computedFlowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700729 }
730
731 /**
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700732 * Reconcile a flow.
733 *
734 * @param flowObj the flow that needs to be reconciliated.
735 * @param newDataPath the new data path to use.
736 * @return true on success, otherwise false.
737 */
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700738 private boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700739
740 //
741 // Set the incoming port matching and the outgoing port output
742 // actions for each flow entry.
743 //
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700744 int idx = 0;
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700745 for (FlowEntry flowEntry : newDataPath.flowEntries()) {
746 // Set the incoming port matching
747 FlowEntryMatch flowEntryMatch = new FlowEntryMatch();
748 flowEntry.setFlowEntryMatch(flowEntryMatch);
749 flowEntryMatch.enableInPort(flowEntry.inPort());
750
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700751 //
752 // Set the actions
753 //
754 FlowEntryActions flowEntryActions = flowEntry.flowEntryActions();
755 //
756 // If the first Flow Entry, copy the Flow Path actions to it
757 //
758 if (idx == 0) {
759 String actionsStr = flowObj.getActions();
760 if (actionsStr != null) {
761 FlowEntryActions flowActions = new FlowEntryActions(actionsStr);
762 for (FlowEntryAction action : flowActions.actions())
763 flowEntryActions.addAction(action);
764 }
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700765 }
Pavlin Radoslavov282f4ff2013-07-18 11:21:37 -0700766 idx++;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700767 //
768 // Add the outgoing port output action
769 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700770 FlowEntryAction flowEntryAction = new FlowEntryAction();
771 flowEntryAction.setActionOutput(flowEntry.outPort());
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700772 flowEntryActions.addAction(flowEntryAction);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700773 }
774
775 //
776 // Remove the old Flow Entries, and add the new Flow Entries
777 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700778 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700779 for (IFlowEntry flowEntryObj : flowEntries) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700780 flowEntryObj.setUserState("FE_USER_DELETE");
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700781 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700782 }
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700783 for (FlowEntry flowEntry : newDataPath.flowEntries()) {
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700784 addFlowEntry(flowObj, flowEntry);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700785 }
786
787 //
788 // Set the Data Path Summary
789 //
790 String dataPathSummaryStr = newDataPath.dataPathSummary();
791 flowObj.setDataPathSummary(dataPathSummaryStr);
792
793 return true;
794 }
795
796 /**
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700797 * Reconcile all flows in a set.
798 *
799 * @param flowObjSet the set of flows that need to be reconciliated.
800 */
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700801 private void reconcileFlows(Iterable<IFlowPath> flowObjSet) {
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700802 if (! flowObjSet.iterator().hasNext())
803 return;
Pavlin Radoslavov0eeb15d2013-04-05 10:23:51 -0700804 // TODO: Not implemented/used yet.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700805 }
806
807 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700808 * Install a Flow Entry on a switch.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700809 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -0700810 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700811 * @param flowObj the flow path object for the flow entry to install.
812 * @param flowEntryObj the flow entry object to install.
813 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700814 */
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700815 private boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj,
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700816 IFlowEntry flowEntryObj) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700817 return FlowSwitchOperation.installFlowEntry(
818 floodlightProvider.getOFMessageFactory(),
819 messageDamper, mySwitch, flowObj, flowEntryObj);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700820 }
821
822 /**
823 * Install a Flow Entry on a switch.
824 *
825 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700826 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700827 * @param flowEntry the flow entry to install.
828 * @return true on success, otherwise false.
829 */
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700830 private boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700831 FlowEntry flowEntry) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700832 return FlowSwitchOperation.installFlowEntry(
833 floodlightProvider.getOFMessageFactory(),
834 messageDamper, mySwitch, flowPath, flowEntry);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700835 }
836
837 /**
838 * Remove a Flow Entry from a switch.
839 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -0700840 * @param mySwitch the switch to remove the Flow Entry from.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700841 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -0700842 * @param flowEntry the flow entry to remove.
843 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700844 */
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700845 private boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700846 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -0700847 //
848 // The installFlowEntry() method implements both installation
849 // and removal of flow entries.
850 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700851 return (installFlowEntry(mySwitch, flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700852 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700853
854 /**
855 * Receive a notification that a Flow is added.
856 *
857 * @param flowPath the flow that is added.
858 */
859 @Override
860 public void notificationRecvFlowAdded(FlowPath flowPath) {
861 // TODO
862 }
863
864 /**
865 * Receive a notification that a Flow is removed.
866 *
867 * @param flowPath the flow that is removed.
868 */
869 @Override
870 public void notificationRecvFlowRemoved(FlowPath flowPath) {
871 // TODO
872 }
873
874 /**
875 * Receive a notification that a Flow is updated.
876 *
877 * @param flowPath the flow that is updated.
878 */
879 @Override
880 public void notificationRecvFlowUpdated(FlowPath flowPath) {
881 // TODO
882 }
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700883
884 /**
885 * Receive a notification that a Topology Element is added.
886 *
887 * @param topologyElement the Topology Element that is added.
888 */
889 @Override
890 public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) {
891 // TODO
892 }
893
894 /**
895 * Receive a notification that a Topology Element is removed.
896 *
897 * @param topologyElement the Topology Element that is removed.
898 */
899 @Override
900 public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) {
901 // TODO
902 }
903
904 /**
905 * Receive a notification that a Topology Element is updated.
906 *
907 * @param topologyElement the Topology Element that is updated.
908 */
909 @Override
910 public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) {
911 // TODO
912 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800913}