blob: 89fe63ee6ece79937e1b3f57f2d3d65afdff3797 [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 Radoslavovab3f8862013-12-04 18:35:53 -08007import java.util.List;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08008import java.util.Map;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +00009import java.util.Random;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080010import java.util.concurrent.BlockingQueue;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080011import java.util.concurrent.LinkedBlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080012import java.util.concurrent.ScheduledExecutorService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080013import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080014
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080015import net.floodlightcontroller.core.IFloodlightProviderService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080016import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080017import net.floodlightcontroller.core.module.FloodlightModuleContext;
18import net.floodlightcontroller.core.module.FloodlightModuleException;
19import net.floodlightcontroller.core.module.IFloodlightModule;
20import net.floodlightcontroller.core.module.IFloodlightService;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080021import net.floodlightcontroller.restserver.IRestApiService;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070022import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070023import net.onrc.onos.graph.GraphDBOperation;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070024import net.onrc.onos.ofcontroller.core.INetMapStorage;
25import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
26import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -070027import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService;
HIGUCHI Yuta60a10142013-06-14 15:50:10 -070028import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
Brian O'Connor8c166a72013-11-14 18:41:48 -080029import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070030import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070031import net.onrc.onos.ofcontroller.util.*;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080032
Naoki Shiota1a37ca12013-11-18 10:55:23 -080033import org.openflow.protocol.OFType;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
admin944ef4f2013-10-08 17:48:37 -070037/**
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -070038 * Flow Manager class for handling the network flows.
admin944ef4f2013-10-08 17:48:37 -070039 */
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070040public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080041 protected GraphDBOperation dbHandlerApi;
42 protected GraphDBOperation dbHandlerInner;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080043
Jonathan Hart50a94982013-04-10 14:49:51 -070044 protected volatile IFloodlightProviderService floodlightProvider;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070045 protected volatile IDatagridService datagridService;
46 protected IRestApiService restApi;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070047 protected FloodlightModuleContext context;
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070048 protected FlowEventHandler flowEventHandler;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080049
Brian O'Connor8c166a72013-11-14 18:41:48 -080050 protected IFlowPusherService pusher;
Naoki Shiota5c8d19f2013-11-05 15:52:38 -080051
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000052 // Flow Entry ID generation state
53 private static Random randomGenerator = new Random();
54 private static int nextFlowEntryIdPrefix = 0;
55 private static int nextFlowEntryIdSuffix = 0;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000056
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080057 /** The logger. */
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070058 private final static Logger log = LoggerFactory.getLogger(FlowManager.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080059
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080060 // The queue to write Flow Entries to the database
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080061 private BlockingQueue<FlowPath> flowPathsToDatabaseQueue =
62 new LinkedBlockingQueue<FlowPath>();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080063 FlowDatabaseWriter flowDatabaseWriter;
64
admin944ef4f2013-10-08 17:48:37 -070065 /**
66 * Initialize the Flow Manager.
67 *
68 * @param conf the Graph Database configuration string.
69 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080070 @Override
71 public void init(String conf) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080072 dbHandlerApi = new GraphDBOperation(conf);
73 dbHandlerInner = new GraphDBOperation(conf);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080074 }
75
admin944ef4f2013-10-08 17:48:37 -070076 /**
77 * Shutdown the Flow Manager operation.
78 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080079 public void finalize() {
Toshio Koide9fe1cb22013-06-13 13:51:11 -070080 close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080081 }
82
admin944ef4f2013-10-08 17:48:37 -070083 /**
84 * Shutdown the Flow Manager operation.
85 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080086 @Override
87 public void close() {
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070088 datagridService.deregisterFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080089 dbHandlerApi.close();
90 dbHandlerInner.close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080091 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080092
admin944ef4f2013-10-08 17:48:37 -070093 /**
94 * Get the collection of offered module services.
95 *
96 * @return the collection of offered module services.
97 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080098 @Override
99 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
100 Collection<Class<? extends IFloodlightService>> l =
101 new ArrayList<Class<? extends IFloodlightService>>();
102 l.add(IFlowService.class);
103 return l;
104 }
105
admin944ef4f2013-10-08 17:48:37 -0700106 /**
107 * Get the collection of implemented services.
108 *
109 * @return the collection of implemented services.
110 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800111 @Override
112 public Map<Class<? extends IFloodlightService>, IFloodlightService>
113 getServiceImpls() {
114 Map<Class<? extends IFloodlightService>,
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700115 IFloodlightService> m =
116 new HashMap<Class<? extends IFloodlightService>,
117 IFloodlightService>();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800118 m.put(IFlowService.class, this);
119 return m;
120 }
121
admin944ef4f2013-10-08 17:48:37 -0700122 /**
123 * Get the collection of modules this module depends on.
124 *
125 * @return the collection of modules this module depends on.
126 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800127 @Override
128 public Collection<Class<? extends IFloodlightService>>
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700129 getModuleDependencies() {
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800130 Collection<Class<? extends IFloodlightService>> l =
131 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800132 l.add(IFloodlightProviderService.class);
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700133 l.add(INetworkGraphService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700134 l.add(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800135 l.add(IRestApiService.class);
136 return l;
137 }
138
admin944ef4f2013-10-08 17:48:37 -0700139 /**
140 * Initialize the module.
141 *
142 * @param context the module context to use for the initialization.
143 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800144 @Override
145 public void init(FloodlightModuleContext context)
146 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700147 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800148 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700149 datagridService = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800150 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov939ca6b2013-12-03 12:35:37 -0800151 pusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800152
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700153 this.init("");
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800154 }
155
admin944ef4f2013-10-08 17:48:37 -0700156 /**
157 * Get the next Flow Entry ID to use.
158 *
159 * @return the next Flow Entry ID to use.
160 */
Naoki Shiota4e77de92013-11-18 17:29:54 -0800161 @Override
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700162 public synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000163 //
164 // Generate the next Flow Entry ID.
165 // NOTE: For now, the higher 32 bits are random, and
166 // the lower 32 bits are sequential.
167 // In the future, we need a better allocation mechanism.
168 //
169 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
170 nextFlowEntryIdPrefix = randomGenerator.nextInt();
171 nextFlowEntryIdSuffix = 0;
172 } else {
173 nextFlowEntryIdSuffix++;
174 }
175 long result = (long)nextFlowEntryIdPrefix << 32;
176 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
177 return result;
178 }
179
admin944ef4f2013-10-08 17:48:37 -0700180 /**
181 * Startup module operation.
182 *
183 * @param context the module context to use for the startup.
184 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800185 @Override
186 public void startUp(FloodlightModuleContext context) {
admin944ef4f2013-10-08 17:48:37 -0700187 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700188
admin944ef4f2013-10-08 17:48:37 -0700189 // Initialize the Flow Entry ID generator
190 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800191
192 //
193 // The thread to write to the database
194 //
195 flowDatabaseWriter = new FlowDatabaseWriter(this,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800196 flowPathsToDatabaseQueue);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800197 flowDatabaseWriter.start();
198
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700199 //
Pavlin Radoslavovc9da5322013-11-22 11:59:46 -0800200 // The Flow Event Handler thread:
201 // - create
202 // - register with the Datagrid Service
203 // - startup
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700204 //
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700205 flowEventHandler = new FlowEventHandler(this, datagridService);
206 datagridService.registerFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700207 flowEventHandler.start();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800208 }
209
210 /**
211 * Add a flow.
212 *
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800213 * @param flowPath the Flow Path to install.
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800214 * @return the Flow ID on success, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800215 */
216 @Override
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800217 public FlowId addFlow(FlowPath flowPath) {
218
219 // Allocate the Flow ID if necessary
Pavlin Radoslavov892dd182013-12-05 23:33:15 -0800220 if (! flowPath.isValidFlowId()) {
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800221 long id = getNextFlowEntryId();
222 flowPath.setFlowId(new FlowId(id));
223 }
224
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700225 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700226 // NOTE: We need to explicitly initialize some of the state,
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700227 // in case the application didn't do it.
228 //
229 for (FlowEntry flowEntry : flowPath.flowEntries()) {
230 if (flowEntry.flowEntrySwitchState() ==
231 FlowEntrySwitchState.FE_SWITCH_UNKNOWN) {
232 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
233 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700234 if (! flowEntry.isValidFlowId())
235 flowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700236 }
237
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800238 if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700239 datagridService.notificationSendFlowAdded(flowPath);
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800240 return flowPath.flowId();
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700241 }
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800242 return null;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800243 }
244
245 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000246 * Delete all previously added flows.
247 *
248 * @return true on success, otherwise false.
249 */
250 @Override
251 public boolean deleteAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800252 if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700253 datagridService.notificationSendAllFlowsRemoved();
254 return true;
255 }
256 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000257 }
258
259 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800260 * Delete a previously added flow.
261 *
262 * @param flowId the Flow ID of the flow to delete.
263 * @return true on success, otherwise false.
264 */
265 @Override
266 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800267 if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700268 datagridService.notificationSendFlowRemoved(flowId);
269 return true;
270 }
271 return false;
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700272 }
273
274 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800275 * Get a previously added flow.
276 *
277 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800278 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800279 */
280 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800281 public FlowPath getFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800282 return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700283 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800284
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700285 /**
286 * Get all installed flows by all installers.
287 *
288 * @return the Flow Paths if found, otherwise null.
289 */
290 @Override
291 public ArrayList<FlowPath> getAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800292 return FlowDatabaseOperation.getAllFlows(dbHandlerApi);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800293 }
294
295 /**
admin944ef4f2013-10-08 17:48:37 -0700296 * Get summary of all installed flows by all installers in a given range.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700297 *
admin944ef4f2013-10-08 17:48:37 -0700298 * @param flowId the Flow ID of the first flow in the flow range to get.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700299 * @param maxFlows the maximum number of flows to be returned.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700300 * @return the Flow Paths if found, otherwise null.
301 */
302 @Override
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800303 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId,
304 int maxFlows) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800305 return FlowDatabaseOperation.getAllFlowsSummary(dbHandlerApi, flowId,
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700306 maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700307 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700308
309 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800310 * Get the collection of my switches.
311 *
312 * @return the collection of my switches.
313 */
314 public Map<Long, IOFSwitch> getMySwitches() {
315 return floodlightProvider.getSwitches();
316 }
317
318 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800319 * Get the network topology.
320 *
321 * @return the network topology.
322 */
323 public Topology getTopology() {
324 return flowEventHandler.getTopology();
325 }
326
327 /**
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800328 * Inform the Flow Manager that a Flow Entry on switch expired.
329 *
Pavlin Radoslavov3bd5ccf2013-11-26 15:10:21 -0800330 * @param sw the switch the Flow Entry expired on.
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800331 * @param flowEntryId the Flow Entry ID of the expired Flow Entry.
332 */
Brian O'Connor4f0b60c2013-11-26 15:00:04 -0800333 public void flowEntryOnSwitchExpired(IOFSwitch sw, FlowEntryId flowEntryId) {
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800334 // TODO: Not implemented yet
335 }
336
337 /**
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800338 * Inform the Flow Manager that a collection of Flow Entries have been
339 * pushed to a switch.
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800340 *
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800341 * @param entries the collection of <IOFSwitch, FlowEntry> pairs
342 * that have been pushed.
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800343 */
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800344 public void flowEntriesPushedToSwitch(
345 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800346
347 //
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800348 // Process all entries
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800349 //
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800350 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
Pavlin Radoslavov4535bc12013-12-05 10:43:49 -0800351 IOFSwitch sw = entry.first;
352 FlowEntry flowEntry = entry.second;
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800353
354 //
355 // Mark the Flow Entry that it has been pushed to the switch
356 //
357 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED);
358
359 //
360 // Write the Flow Entry to the Datagrid
361 //
362 switch (flowEntry.flowEntryUserState()) {
363 case FE_USER_ADD:
364 datagridService.notificationSendFlowEntryAdded(flowEntry);
365 break;
366 case FE_USER_MODIFY:
367 datagridService.notificationSendFlowEntryUpdated(flowEntry);
368 break;
369 case FE_USER_DELETE:
370 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
371 break;
372 }
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800373 }
374 }
375
376 /**
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800377 * Push modified Flow-related state as appropriate.
378 *
379 * @param modifiedFlowPaths the collection of modified Flow Paths.
380 * @param modifiedFlowEntries the collection of modified Flow Entries.
381 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800382 void pushModifiedFlowState(Collection<FlowPath> modifiedFlowPaths,
383 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800384 //
385 // Push the modified Flow state:
386 // - Flow Entries to switches and the datagrid
387 // - Flow Paths to the database
388 //
389 pushModifiedFlowEntriesToSwitches(modifiedFlowEntries);
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800390 pushModifiedFlowPathsToDatabase(modifiedFlowPaths);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800391 cleanupDeletedFlowEntriesFromDatagrid(modifiedFlowEntries);
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800392 }
393
394 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800395 * Push modified Flow Entries to switches.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700396 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800397 * NOTE: Only the Flow Entries to switches controlled by this instance
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700398 * are pushed.
399 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800400 * @param modifiedFlowEntries the collection of modified Flow Entries.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700401 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800402 private void pushModifiedFlowEntriesToSwitches(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800403 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800404 if (modifiedFlowEntries.isEmpty())
405 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700406
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800407 List<Pair<IOFSwitch, FlowEntry>> entries =
408 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
409
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800410 Map<Long, IOFSwitch> mySwitches = getMySwitches();
411
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800412 //
413 // Create a collection of my Flow Entries to push
414 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800415 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800416 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
417 if (mySwitch == null)
418 continue;
419
Pavlin Radoslavovaca49d12013-12-04 19:49:17 -0800420 //
421 // Assign Flow Entry IDs if missing.
422 //
423 // NOTE: This is an additional safeguard, in case the
424 // mySwitches set has changed (after the Flow Entry IDs
425 // assignments by the caller).
426 //
427 if (! flowEntry.isValidFlowEntryId()) {
428 long id = getNextFlowEntryId();
429 flowEntry.setFlowEntryId(new FlowEntryId(id));
430 }
431
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800432 log.debug("Pushing Flow Entry To Switch: {}", flowEntry.toString());
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800433 entries.add(new Pair<IOFSwitch, FlowEntry>(mySwitch, flowEntry));
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800434 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800435
436 pusher.pushFlowEntries(entries);
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800437 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700438
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800439 /**
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800440 * Cleanup deleted Flow Entries from the datagrid.
441 *
442 * NOTE: We cleanup only the Flow Entries that are not for our switches.
443 * This is needed to handle the case a switch going down:
444 * It has no Master controller instance, hence no controller instance
445 * will cleanup its flow entries.
446 * This is sub-optimal: we need to elect a controller instance to handle
447 * the cleanup of such orphaned flow entries.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800448 *
449 * @param modifiedFlowEntries the collection of modified Flow Entries.
450 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800451 private void cleanupDeletedFlowEntriesFromDatagrid(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800452 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800453 if (modifiedFlowEntries.isEmpty())
454 return;
455
456 Map<Long, IOFSwitch> mySwitches = getMySwitches();
457
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800458 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800459 //
460 // Process only Flow Entries that should be deleted and have
461 // a valid Flow Entry ID.
462 //
Pavlin Radoslavov426a8532013-12-02 17:32:21 -0800463 if (! flowEntry.isValidFlowEntryId())
464 continue;
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800465 if (flowEntry.flowEntryUserState() !=
466 FlowEntryUserState.FE_USER_DELETE) {
467 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800468 }
469
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800470 //
471 // NOTE: The deletion of Flow Entries for my switches is handled
472 // elsewhere.
473 //
474 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
475 if (mySwitch != null)
476 continue;
477
478 log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry.toString());
479
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800480 //
481 // Write the Flow Entry to the Datagrid
482 //
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800483 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800484 }
485 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700486
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800487 /**
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800488 * Class to implement writing to the database in a separate thread.
489 */
490 class FlowDatabaseWriter extends Thread {
491 private FlowManager flowManager;
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800492 private BlockingQueue<FlowPath> blockingQueue;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800493
494 /**
495 * Constructor.
496 *
497 * @param flowManager the Flow Manager to use.
498 * @param blockingQueue the blocking queue to use.
499 */
500 FlowDatabaseWriter(FlowManager flowManager,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800501 BlockingQueue<FlowPath> blockingQueue) {
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800502 this.flowManager = flowManager;
503 this.blockingQueue = blockingQueue;
504 }
505
506 /**
507 * Run the thread.
508 */
509 @Override
510 public void run() {
511 //
512 // The main loop
513 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800514 Collection<FlowPath> collection = new LinkedList<FlowPath>();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800515 try {
516 while (true) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800517 FlowPath flowPath = blockingQueue.take();
518 collection.add(flowPath);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800519 blockingQueue.drainTo(collection);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800520 flowManager.writeModifiedFlowPathsToDatabase(collection);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800521 collection.clear();
522 }
523 } catch (Exception exception) {
524 log.debug("Exception writing to the Database: ", exception);
525 }
526 }
527 }
528
529 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800530 * Push Flow Paths to the Network MAP.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800531 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800532 * NOTE: The complete Flow Paths are pushed only on the instance
533 * responsible for the first switch. This is to avoid database errors
534 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800535 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800536 * @param modifiedFlowPaths the collection of Flow Paths to push.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800537 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800538 private void pushModifiedFlowPathsToDatabase(
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800539 Collection<FlowPath> modifiedFlowPaths) {
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800540 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800541 // We only add the Flow Paths to the Database Queue.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800542 // The FlowDatabaseWriter thread is responsible for the actual writing.
543 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800544 flowPathsToDatabaseQueue.addAll(modifiedFlowPaths);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800545 }
546
547 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800548 * Write Flow Paths to the Network MAP.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800549 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800550 * NOTE: The complete Flow Paths are pushed only on the instance
551 * responsible for the first switch. This is to avoid database errors
552 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800553 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800554 * @param modifiedFlowPaths the collection of Flow Paths to write.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800555 */
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800556 private void writeModifiedFlowPathsToDatabase(
557 Collection<FlowPath> modifiedFlowPaths) {
558 if (modifiedFlowPaths.isEmpty())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800559 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700560
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800561 Map<Long, IOFSwitch> mySwitches = getMySwitches();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700562
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800563 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800564 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800565 // Push the changes only on the instance responsible for the
566 // first switch.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800567 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800568 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
569 IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value());
570 if (mySrcSwitch == null)
571 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800572
573 //
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800574 // Delete the Flow Path from the Network Map
575 //
576 if (flowPath.flowPathUserState() ==
577 FlowPathUserState.FP_USER_DELETE) {
578 log.debug("Deleting Flow Path From Database: {}",
579 flowPath.toString());
580
581 try {
582 if (! FlowDatabaseOperation.deleteFlow(
583 dbHandlerInner,
584 flowPath.flowId())) {
585 log.error("Cannot delete Flow Path {} from Network Map",
586 flowPath.flowId());
587 }
588 } catch (Exception e) {
589 log.error("Exception deleting Flow Path from Network MAP: {}", e);
590 }
591 continue;
592 }
593
594 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800595 // Test whether all Flow Entries are valid
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800596 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800597 boolean allValid = true;
598 for (FlowEntry flowEntry : flowPath.flowEntries()) {
599 if (flowEntry.flowEntryUserState() ==
600 FlowEntryUserState.FE_USER_DELETE) {
601 continue;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700602 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800603 if (! flowEntry.isValidFlowEntryId()) {
604 allValid = false;
605 break;
606 }
607 }
608 if (! allValid)
609 continue;
610
611 log.debug("Pushing Flow Path To Database: {}", flowPath.toString());
612
613 //
614 // Write the Flow Path to the Network Map
615 //
616 try {
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800617 if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath)) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800618 String logMsg = "Cannot write to Network Map Flow Path " +
619 flowPath.flowId();
620 log.error(logMsg);
621 }
622 } catch (Exception e) {
623 log.error("Exception writing Flow Path to Network MAP: ", e);
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700624 }
625 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700626 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800627}