blob: 186d4ffb2d1b19cc36fb08b720bb673d10269f99 [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;
5import java.util.HashMap;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +00006import java.util.LinkedList;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08007import java.util.Map;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +00008import java.util.Random;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -08009import java.util.concurrent.BlockingQueue;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080010import java.util.concurrent.LinkedBlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080011import java.util.concurrent.ScheduledExecutorService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080012import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080013
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080014import net.floodlightcontroller.core.IFloodlightProviderService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080015import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080016import net.floodlightcontroller.core.module.FloodlightModuleContext;
17import net.floodlightcontroller.core.module.FloodlightModuleException;
18import net.floodlightcontroller.core.module.IFloodlightModule;
19import net.floodlightcontroller.core.module.IFloodlightService;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080020import net.floodlightcontroller.restserver.IRestApiService;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070021import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070022import net.onrc.onos.graph.GraphDBOperation;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070023import net.onrc.onos.ofcontroller.core.INetMapStorage;
24import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
25import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -070026import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService;
HIGUCHI Yuta60a10142013-06-14 15:50:10 -070027import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
Brian O'Connor8c166a72013-11-14 18:41:48 -080028import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070029import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070030import net.onrc.onos.ofcontroller.util.*;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080031
Naoki Shiota1a37ca12013-11-18 10:55:23 -080032import org.openflow.protocol.OFType;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
admin944ef4f2013-10-08 17:48:37 -070036/**
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -070037 * Flow Manager class for handling the network flows.
admin944ef4f2013-10-08 17:48:37 -070038 */
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070039public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080040 protected GraphDBOperation dbHandlerApi;
41 protected GraphDBOperation dbHandlerInner;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080042
Jonathan Hart50a94982013-04-10 14:49:51 -070043 protected volatile IFloodlightProviderService floodlightProvider;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070044 protected volatile IDatagridService datagridService;
45 protected IRestApiService restApi;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070046 protected FloodlightModuleContext context;
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070047 protected FlowEventHandler flowEventHandler;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080048
Brian O'Connor8c166a72013-11-14 18:41:48 -080049 protected IFlowPusherService pusher;
Naoki Shiota5c8d19f2013-11-05 15:52:38 -080050
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000051 // Flow Entry ID generation state
52 private static Random randomGenerator = new Random();
53 private static int nextFlowEntryIdPrefix = 0;
54 private static int nextFlowEntryIdSuffix = 0;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000055
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080056 /** The logger. */
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070057 private final static Logger log = LoggerFactory.getLogger(FlowManager.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080058
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080059 // The queue to write Flow Entries to the database
60 private BlockingQueue<FlowPathEntryPair> flowEntriesToDatabaseQueue =
61 new LinkedBlockingQueue<FlowPathEntryPair>();
62 FlowDatabaseWriter flowDatabaseWriter;
63
admin944ef4f2013-10-08 17:48:37 -070064 /**
65 * Initialize the Flow Manager.
66 *
67 * @param conf the Graph Database configuration string.
68 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080069 @Override
70 public void init(String conf) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080071 dbHandlerApi = new GraphDBOperation(conf);
72 dbHandlerInner = new GraphDBOperation(conf);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080073 }
74
admin944ef4f2013-10-08 17:48:37 -070075 /**
76 * Shutdown the Flow Manager operation.
77 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080078 public void finalize() {
Toshio Koide9fe1cb22013-06-13 13:51:11 -070079 close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080080 }
81
admin944ef4f2013-10-08 17:48:37 -070082 /**
83 * Shutdown the Flow Manager operation.
84 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080085 @Override
86 public void close() {
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070087 datagridService.deregisterFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080088 dbHandlerApi.close();
89 dbHandlerInner.close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080090 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080091
admin944ef4f2013-10-08 17:48:37 -070092 /**
93 * Get the collection of offered module services.
94 *
95 * @return the collection of offered module services.
96 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080097 @Override
98 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
99 Collection<Class<? extends IFloodlightService>> l =
100 new ArrayList<Class<? extends IFloodlightService>>();
101 l.add(IFlowService.class);
102 return l;
103 }
104
admin944ef4f2013-10-08 17:48:37 -0700105 /**
106 * Get the collection of implemented services.
107 *
108 * @return the collection of implemented services.
109 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800110 @Override
111 public Map<Class<? extends IFloodlightService>, IFloodlightService>
112 getServiceImpls() {
113 Map<Class<? extends IFloodlightService>,
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700114 IFloodlightService> m =
115 new HashMap<Class<? extends IFloodlightService>,
116 IFloodlightService>();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800117 m.put(IFlowService.class, this);
118 return m;
119 }
120
admin944ef4f2013-10-08 17:48:37 -0700121 /**
122 * Get the collection of modules this module depends on.
123 *
124 * @return the collection of modules this module depends on.
125 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800126 @Override
127 public Collection<Class<? extends IFloodlightService>>
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700128 getModuleDependencies() {
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800129 Collection<Class<? extends IFloodlightService>> l =
130 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800131 l.add(IFloodlightProviderService.class);
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700132 l.add(INetworkGraphService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700133 l.add(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800134 l.add(IRestApiService.class);
135 return l;
136 }
137
admin944ef4f2013-10-08 17:48:37 -0700138 /**
139 * Initialize the module.
140 *
141 * @param context the module context to use for the initialization.
142 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800143 @Override
144 public void init(FloodlightModuleContext context)
145 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700146 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800147 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700148 datagridService = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800149 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov939ca6b2013-12-03 12:35:37 -0800150 pusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800151
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700152 this.init("");
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800153 }
154
admin944ef4f2013-10-08 17:48:37 -0700155 /**
156 * Get the next Flow Entry ID to use.
157 *
158 * @return the next Flow Entry ID to use.
159 */
Naoki Shiota4e77de92013-11-18 17:29:54 -0800160 @Override
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700161 public synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000162 //
163 // Generate the next Flow Entry ID.
164 // NOTE: For now, the higher 32 bits are random, and
165 // the lower 32 bits are sequential.
166 // In the future, we need a better allocation mechanism.
167 //
168 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
169 nextFlowEntryIdPrefix = randomGenerator.nextInt();
170 nextFlowEntryIdSuffix = 0;
171 } else {
172 nextFlowEntryIdSuffix++;
173 }
174 long result = (long)nextFlowEntryIdPrefix << 32;
175 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
176 return result;
177 }
178
admin944ef4f2013-10-08 17:48:37 -0700179 /**
180 * Startup module operation.
181 *
182 * @param context the module context to use for the startup.
183 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800184 @Override
185 public void startUp(FloodlightModuleContext context) {
admin944ef4f2013-10-08 17:48:37 -0700186 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700187
admin944ef4f2013-10-08 17:48:37 -0700188 // Initialize the Flow Entry ID generator
189 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800190
191 //
192 // The thread to write to the database
193 //
194 flowDatabaseWriter = new FlowDatabaseWriter(this,
195 flowEntriesToDatabaseQueue);
196 flowDatabaseWriter.start();
197
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700198 //
Pavlin Radoslavovc9da5322013-11-22 11:59:46 -0800199 // The Flow Event Handler thread:
200 // - create
201 // - register with the Datagrid Service
202 // - startup
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700203 //
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700204 flowEventHandler = new FlowEventHandler(this, datagridService);
205 datagridService.registerFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700206 flowEventHandler.start();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800207 }
208
209 /**
210 * Add a flow.
211 *
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800212 * @param flowPath the Flow Path to install.
213 * @param flowId the return-by-reference Flow ID as assigned internally.
214 * @return true on success, otherwise false.
215 */
216 @Override
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700217 public boolean addFlow(FlowPath flowPath, FlowId flowId) {
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700218 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700219 // NOTE: We need to explicitly initialize some of the state,
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700220 // in case the application didn't do it.
221 //
222 for (FlowEntry flowEntry : flowPath.flowEntries()) {
223 if (flowEntry.flowEntrySwitchState() ==
224 FlowEntrySwitchState.FE_SWITCH_UNKNOWN) {
225 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
226 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700227 if (! flowEntry.isValidFlowId())
228 flowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700229 }
230
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800231 if (FlowDatabaseOperation.addFlow(this, dbHandlerApi, flowPath, flowId)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700232 datagridService.notificationSendFlowAdded(flowPath);
233 return true;
234 }
235 return false;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800236 }
237
238 /**
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700239 * Add a flow entry to the Network MAP.
240 *
241 * @param flowObj the corresponding Flow Path object for the Flow Entry.
242 * @param flowEntry the Flow Entry to install.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700243 * @return the added Flow Entry object on success, otherwise null.
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700244 */
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700245 private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800246 return FlowDatabaseOperation.addFlowEntry(this, dbHandlerInner,
247 flowObj, flowEntry);
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700248 }
249
250 /**
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700251 * Delete a flow entry from the Network MAP.
252 *
253 * @param flowObj the corresponding Flow Path object for the Flow Entry.
254 * @param flowEntry the Flow Entry to delete.
255 * @return true on success, otherwise false.
256 */
257 private boolean deleteFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800258 return FlowDatabaseOperation.deleteFlowEntry(dbHandlerInner,
259 flowObj, flowEntry);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800260 }
261
262 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000263 * Delete all previously added flows.
264 *
265 * @return true on success, otherwise false.
266 */
267 @Override
268 public boolean deleteAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800269 if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700270 datagridService.notificationSendAllFlowsRemoved();
271 return true;
272 }
273 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000274 }
275
276 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800277 * Delete a previously added flow.
278 *
279 * @param flowId the Flow ID of the flow to delete.
280 * @return true on success, otherwise false.
281 */
282 @Override
283 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800284 if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700285 datagridService.notificationSendFlowRemoved(flowId);
286 return true;
287 }
288 return false;
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700289 }
290
291 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800292 * Get a previously added flow.
293 *
294 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800295 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800296 */
297 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800298 public FlowPath getFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800299 return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700300 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800301
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700302 /**
303 * Get all installed flows by all installers.
304 *
305 * @return the Flow Paths if found, otherwise null.
306 */
307 @Override
308 public ArrayList<FlowPath> getAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800309 return FlowDatabaseOperation.getAllFlows(dbHandlerApi);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800310 }
311
312 /**
313 * Get all previously added flows by a specific installer for a given
314 * data path endpoints.
315 *
316 * @param installerId the Caller ID of the installer of the flow to get.
317 * @param dataPathEndpoints the data path endpoints of the flow to get.
318 * @return the Flow Paths if found, otherwise null.
319 */
320 @Override
321 public ArrayList<FlowPath> getAllFlows(CallerId installerId,
322 DataPathEndpoints dataPathEndpoints) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800323 return FlowDatabaseOperation.getAllFlows(dbHandlerApi, installerId,
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700324 dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800325 }
326
327 /**
328 * Get all installed flows by all installers for given data path endpoints.
329 *
330 * @param dataPathEndpoints the data path endpoints of the flows to get.
331 * @return the Flow Paths if found, otherwise null.
332 */
333 @Override
334 public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800335 return FlowDatabaseOperation.getAllFlows(dbHandlerApi,
336 dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800337 }
338
339 /**
admin944ef4f2013-10-08 17:48:37 -0700340 * Get summary of all installed flows by all installers in a given range.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700341 *
admin944ef4f2013-10-08 17:48:37 -0700342 * @param flowId the Flow ID of the first flow in the flow range to get.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700343 * @param maxFlows the maximum number of flows to be returned.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700344 * @return the Flow Paths if found, otherwise null.
345 */
346 @Override
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800347 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId,
348 int maxFlows) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800349 return FlowDatabaseOperation.getAllFlowsSummary(dbHandlerApi, flowId,
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700350 maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700351 }
352
353 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700354 * Add and maintain a shortest-path flow.
355 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700356 * NOTE: The Flow Path argument does NOT contain flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700357 *
358 * @param flowPath the Flow Path with the endpoints and the match
359 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700360 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700361 */
362 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700363 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700364 //
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +0000365 // Don't do the shortest path computation here.
366 // Instead, let the Flow reconciliation thread take care of it.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700367 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700368
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700369 FlowId flowId = new FlowId();
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700370 if (! addFlow(flowPath, flowId))
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700371 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700372
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700373 return (flowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700374 }
375
376 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800377 * Get the collection of my switches.
378 *
379 * @return the collection of my switches.
380 */
381 public Map<Long, IOFSwitch> getMySwitches() {
382 return floodlightProvider.getSwitches();
383 }
384
385 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800386 * Get the network topology.
387 *
388 * @return the network topology.
389 */
390 public Topology getTopology() {
391 return flowEventHandler.getTopology();
392 }
393
394 /**
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800395 * Inform the Flow Manager that a Flow Entry on switch expired.
396 *
Pavlin Radoslavov3bd5ccf2013-11-26 15:10:21 -0800397 * @param sw the switch the Flow Entry expired on.
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800398 * @param flowEntryId the Flow Entry ID of the expired Flow Entry.
399 */
Brian O'Connor4f0b60c2013-11-26 15:00:04 -0800400 public void flowEntryOnSwitchExpired(IOFSwitch sw, FlowEntryId flowEntryId) {
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800401 // TODO: Not implemented yet
402 }
403
404 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800405 * Push modified Flow Entries to switches.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700406 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800407 * NOTE: Only the Flow Entries to switches controlled by this instance
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700408 * are pushed.
409 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800410 * @param modifiedFlowEntries the collection of modified Flow Entries.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700411 */
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800412 public void pushModifiedFlowEntriesToSwitches(
413 Collection<FlowPathEntryPair> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800414 if (modifiedFlowEntries.isEmpty())
415 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700416
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800417 Map<Long, IOFSwitch> mySwitches = getMySwitches();
418
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800419 for (FlowPathEntryPair flowPair : modifiedFlowEntries) {
420 FlowPath flowPath = flowPair.flowPath;
421 FlowEntry flowEntry = flowPair.flowEntry;
422
423 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
424 if (mySwitch == null)
425 continue;
426
427 log.debug("Pushing Flow Entry To Switch: {}", flowEntry.toString());
428
429 //
Pavlin Radoslavov939ca6b2013-12-03 12:35:37 -0800430 // Push the Flow Entry into the switch
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800431 //
Pavlin Radoslavov939ca6b2013-12-03 12:35:37 -0800432 if (! pusher.add(mySwitch, flowPath, flowEntry)) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800433 String logMsg = "Cannot install Flow Entry " +
434 flowEntry.flowEntryId() +
435 " from Flow Path " + flowPath.flowId() +
436 " on switch " + flowEntry.dpid();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700437 log.error(logMsg);
438 continue;
439 }
440
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800441 //
442 // NOTE: Here we assume that the switch has been
443 // successfully updated.
444 //
445 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED);
446 }
447 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700448
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800449 /**
450 * Push modified Flow Entries to the datagrid.
451 *
452 * @param modifiedFlowEntries the collection of modified Flow Entries.
453 */
454 public void pushModifiedFlowEntriesToDatagrid(
455 Collection<FlowPathEntryPair> modifiedFlowEntries) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800456 if (modifiedFlowEntries.isEmpty())
457 return;
458
459 Map<Long, IOFSwitch> mySwitches = getMySwitches();
460
461 for (FlowPathEntryPair flowPair : modifiedFlowEntries) {
462 FlowEntry flowEntry = flowPair.flowEntry;
463
Pavlin Radoslavov426a8532013-12-02 17:32:21 -0800464 if (! flowEntry.isValidFlowEntryId())
465 continue;
466
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800467 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
468
469 //
470 // TODO: For now Flow Entries are removed by all instances,
471 // even if this Flow Entry is not for our switches.
472 //
473 // This is needed to handle the case a switch going down:
474 // it has no Master controller instance, hence no
475 // controller instance will cleanup its flow entries.
476 // This is sub-optimal: we need to elect a controller
477 // instance to handle the cleanup of such orphaned flow
478 // entries.
479 //
480 if (mySwitch == null) {
481 if (flowEntry.flowEntryUserState() !=
482 FlowEntryUserState.FE_USER_DELETE) {
483 continue;
484 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800485 }
486
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800487 log.debug("Pushing Flow Entry To Datagrid: {}", flowEntry.toString());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800488 //
489 // Write the Flow Entry to the Datagrid
490 //
491 switch (flowEntry.flowEntryUserState()) {
492 case FE_USER_ADD:
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700493 if (mySwitch == null)
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800494 break; // Install only flow entries for my switches
495 datagridService.notificationSendFlowEntryAdded(flowEntry);
496 break;
497 case FE_USER_MODIFY:
498 if (mySwitch == null)
499 break; // Install only flow entries for my switches
500 datagridService.notificationSendFlowEntryUpdated(flowEntry);
501 break;
502 case FE_USER_DELETE:
503 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
504 break;
505 }
506 }
507 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700508
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800509 /**
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800510 * Class to implement writing to the database in a separate thread.
511 */
512 class FlowDatabaseWriter extends Thread {
513 private FlowManager flowManager;
514 private BlockingQueue<FlowPathEntryPair> blockingQueue;
515
516 /**
517 * Constructor.
518 *
519 * @param flowManager the Flow Manager to use.
520 * @param blockingQueue the blocking queue to use.
521 */
522 FlowDatabaseWriter(FlowManager flowManager,
523 BlockingQueue<FlowPathEntryPair> blockingQueue) {
524 this.flowManager = flowManager;
525 this.blockingQueue = blockingQueue;
526 }
527
528 /**
529 * Run the thread.
530 */
531 @Override
532 public void run() {
533 //
534 // The main loop
535 //
536 Collection<FlowPathEntryPair> collection =
537 new LinkedList<FlowPathEntryPair>();
538 try {
539 while (true) {
540 FlowPathEntryPair entryPair = blockingQueue.take();
541 collection.add(entryPair);
542 blockingQueue.drainTo(collection);
543 flowManager.writeModifiedFlowEntriesToDatabase(collection);
544 collection.clear();
545 }
546 } catch (Exception exception) {
547 log.debug("Exception writing to the Database: ", exception);
548 }
549 }
550 }
551
552 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800553 * Push Flow Entries to the Network MAP.
554 *
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800555 * NOTE: The Flow Entries are pushed only on the instance responsible
556 * for the first switch. This is to avoid database errors when multiple
557 * instances are writing Flow Entries for the same Flow Path.
558 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800559 * @param modifiedFlowEntries the collection of Flow Entries to push.
560 */
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800561 void pushModifiedFlowEntriesToDatabase(
562 Collection<FlowPathEntryPair> modifiedFlowEntries) {
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800563 //
564 // We only add the Flow Entries to the Database Queue.
565 // The FlowDatabaseWriter thread is responsible for the actual writing.
566 //
567 flowEntriesToDatabaseQueue.addAll(modifiedFlowEntries);
568 }
569
570 /**
571 * Write Flow Entries to the Network MAP.
572 *
573 * NOTE: The Flow Entries are written only on the instance responsible
574 * for the first switch. This is to avoid database errors when multiple
575 * instances are writing Flow Entries for the same Flow Path.
576 *
577 * @param modifiedFlowEntries the collection of Flow Entries to write.
578 */
579 private void writeModifiedFlowEntriesToDatabase(
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800580 Collection<FlowPathEntryPair> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800581 if (modifiedFlowEntries.isEmpty())
582 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700583
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800584 Map<Long, IOFSwitch> mySwitches = getMySwitches();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700585
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800586 for (FlowPathEntryPair flowPair : modifiedFlowEntries) {
587 FlowPath flowPath = flowPair.flowPath;
588 FlowEntry flowEntry = flowPair.flowEntry;
589
590 if (! flowEntry.isValidFlowEntryId())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800591 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800592
593 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800594 // Push the changes only on the instance responsible for the
595 // first switch.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800596 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800597 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
598 IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value());
599 if (mySrcSwitch == null)
600 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800601
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800602 log.debug("Pushing Flow Entry To Database: {}", flowEntry.toString());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800603 //
604 // Write the Flow Entry to the Network Map
605 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800606 // NOTE: We try a number of times, in case somehow some other
607 // instances are writing at the same time.
608 // Apparently, if other instances are writing at the same time
609 // this will trigger an error.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800610 //
611 for (int i = 0; i < 6; i++) {
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700612 try {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800613 //
614 // Find the Flow Path in the Network MAP.
615 //
616 // NOTE: The Flow Path might not be found if the Flow was
617 // just removed by some other controller instance.
618 //
619 IFlowPath flowObj =
620 dbHandlerInner.searchFlowPath(flowEntry.flowId());
621 if (flowObj == null) {
622 String logMsg = "Cannot find Network MAP entry for Flow Path " + flowEntry.flowId();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700623 log.error(logMsg);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800624 break;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700625 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800626
627 // Write the Flow Entry
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700628 switch (flowEntry.flowEntryUserState()) {
629 case FE_USER_ADD:
630 // FALLTHROUGH
631 case FE_USER_MODIFY:
632 if (addFlowEntry(flowObj, flowEntry) == null) {
633 String logMsg = "Cannot write to Network MAP Flow Entry " +
634 flowEntry.flowEntryId() +
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800635 " from Flow Path " + flowEntry.flowId() +
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700636 " on switch " + flowEntry.dpid();
637 log.error(logMsg);
638 }
639 break;
640 case FE_USER_DELETE:
641 if (deleteFlowEntry(flowObj, flowEntry) == false) {
642 String logMsg = "Cannot remove from Network MAP Flow Entry " +
643 flowEntry.flowEntryId() +
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800644 " from Flow Path " + flowEntry.flowId() +
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700645 " on switch " + flowEntry.dpid();
646 log.error(logMsg);
647 }
648 break;
649 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800650
651 // Commit to the database
652 dbHandlerInner.commit();
653 break; // Success
654
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700655 } catch (Exception e) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800656 log.debug("Exception writing Flow Entry to Network MAP: ", e);
657 dbHandlerInner.rollback();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800658 // Wait a bit (random value [1ms, 20ms] and try again
659 int delay = 1 + randomGenerator.nextInt() % 20;
660 try {
661 Thread.sleep(delay);
662 } catch (Exception e0) {
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700663 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700664 }
665 }
666 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700667 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800668}