blob: 90ee60a42699b942fb11fc055f977c02e9878620 [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.
214 * @param flowId the return-by-reference Flow ID as assigned internally.
215 * @return true on success, otherwise false.
216 */
217 @Override
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700218 public boolean addFlow(FlowPath flowPath, FlowId flowId) {
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700219 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700220 // NOTE: We need to explicitly initialize some of the state,
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700221 // in case the application didn't do it.
222 //
223 for (FlowEntry flowEntry : flowPath.flowEntries()) {
224 if (flowEntry.flowEntrySwitchState() ==
225 FlowEntrySwitchState.FE_SWITCH_UNKNOWN) {
226 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
227 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700228 if (! flowEntry.isValidFlowId())
229 flowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700230 }
231
Pavlin Radoslavov67bf7622013-12-04 12:28:23 -0800232 if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath, flowId)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700233 datagridService.notificationSendFlowAdded(flowPath);
234 return true;
235 }
236 return false;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800237 }
238
239 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000240 * Delete all previously added flows.
241 *
242 * @return true on success, otherwise false.
243 */
244 @Override
245 public boolean deleteAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800246 if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700247 datagridService.notificationSendAllFlowsRemoved();
248 return true;
249 }
250 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000251 }
252
253 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800254 * Delete a previously added flow.
255 *
256 * @param flowId the Flow ID of the flow to delete.
257 * @return true on success, otherwise false.
258 */
259 @Override
260 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800261 if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700262 datagridService.notificationSendFlowRemoved(flowId);
263 return true;
264 }
265 return false;
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700266 }
267
268 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800269 * Get a previously added flow.
270 *
271 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800272 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800273 */
274 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800275 public FlowPath getFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800276 return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700277 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800278
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700279 /**
280 * Get all installed flows by all installers.
281 *
282 * @return the Flow Paths if found, otherwise null.
283 */
284 @Override
285 public ArrayList<FlowPath> getAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800286 return FlowDatabaseOperation.getAllFlows(dbHandlerApi);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800287 }
288
289 /**
admin944ef4f2013-10-08 17:48:37 -0700290 * Get summary of all installed flows by all installers in a given range.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700291 *
admin944ef4f2013-10-08 17:48:37 -0700292 * @param flowId the Flow ID of the first flow in the flow range to get.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700293 * @param maxFlows the maximum number of flows to be returned.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700294 * @return the Flow Paths if found, otherwise null.
295 */
296 @Override
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800297 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId,
298 int maxFlows) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800299 return FlowDatabaseOperation.getAllFlowsSummary(dbHandlerApi, flowId,
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700300 maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700301 }
302
303 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700304 * Add and maintain a shortest-path flow.
305 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700306 * NOTE: The Flow Path argument does NOT contain flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700307 *
308 * @param flowPath the Flow Path with the endpoints and the match
309 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700310 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700311 */
312 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700313 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700314 //
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +0000315 // Don't do the shortest path computation here.
316 // Instead, let the Flow reconciliation thread take care of it.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700317 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700318
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700319 FlowId flowId = new FlowId();
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700320 if (! addFlow(flowPath, flowId))
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700321 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700322
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700323 return (flowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700324 }
325
326 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800327 * Get the collection of my switches.
328 *
329 * @return the collection of my switches.
330 */
331 public Map<Long, IOFSwitch> getMySwitches() {
332 return floodlightProvider.getSwitches();
333 }
334
335 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800336 * Get the network topology.
337 *
338 * @return the network topology.
339 */
340 public Topology getTopology() {
341 return flowEventHandler.getTopology();
342 }
343
344 /**
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800345 * Inform the Flow Manager that a Flow Entry on switch expired.
346 *
Pavlin Radoslavov3bd5ccf2013-11-26 15:10:21 -0800347 * @param sw the switch the Flow Entry expired on.
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800348 * @param flowEntryId the Flow Entry ID of the expired Flow Entry.
349 */
Brian O'Connor4f0b60c2013-11-26 15:00:04 -0800350 public void flowEntryOnSwitchExpired(IOFSwitch sw, FlowEntryId flowEntryId) {
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800351 // TODO: Not implemented yet
352 }
353
354 /**
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800355 * Inform the Flow Manager that a collection of Flow Entries have been
356 * pushed to a switch.
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800357 *
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800358 * @param entries the collection of <IOFSwitch, FlowEntry> pairs
359 * that have been pushed.
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800360 */
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800361 public void flowEntriesPushedToSwitch(
362 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800363
364 //
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800365 // Process all entries
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800366 //
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800367 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
Pavlin Radoslavov4535bc12013-12-05 10:43:49 -0800368 IOFSwitch sw = entry.first;
369 FlowEntry flowEntry = entry.second;
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800370
371 //
372 // Mark the Flow Entry that it has been pushed to the switch
373 //
374 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED);
375
376 //
377 // Write the Flow Entry to the Datagrid
378 //
379 switch (flowEntry.flowEntryUserState()) {
380 case FE_USER_ADD:
381 datagridService.notificationSendFlowEntryAdded(flowEntry);
382 break;
383 case FE_USER_MODIFY:
384 datagridService.notificationSendFlowEntryUpdated(flowEntry);
385 break;
386 case FE_USER_DELETE:
387 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
388 break;
389 }
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800390 }
391 }
392
393 /**
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800394 * Push modified Flow-related state as appropriate.
395 *
396 * @param modifiedFlowPaths the collection of modified Flow Paths.
397 * @param modifiedFlowEntries the collection of modified Flow Entries.
398 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800399 void pushModifiedFlowState(Collection<FlowPath> modifiedFlowPaths,
400 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800401 //
402 // Push the modified Flow state:
403 // - Flow Entries to switches and the datagrid
404 // - Flow Paths to the database
405 //
406 pushModifiedFlowEntriesToSwitches(modifiedFlowEntries);
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800407 pushModifiedFlowPathsToDatabase(modifiedFlowPaths);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800408 cleanupDeletedFlowEntriesFromDatagrid(modifiedFlowEntries);
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800409 }
410
411 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800412 * Push modified Flow Entries to switches.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700413 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800414 * NOTE: Only the Flow Entries to switches controlled by this instance
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700415 * are pushed.
416 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800417 * @param modifiedFlowEntries the collection of modified Flow Entries.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700418 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800419 private void pushModifiedFlowEntriesToSwitches(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800420 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800421 if (modifiedFlowEntries.isEmpty())
422 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700423
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800424 List<Pair<IOFSwitch, FlowEntry>> entries =
425 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
426
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800427 Map<Long, IOFSwitch> mySwitches = getMySwitches();
428
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800429 //
430 // Create a collection of my Flow Entries to push
431 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800432 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800433 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
434 if (mySwitch == null)
435 continue;
436
Pavlin Radoslavovaca49d12013-12-04 19:49:17 -0800437 //
438 // Assign Flow Entry IDs if missing.
439 //
440 // NOTE: This is an additional safeguard, in case the
441 // mySwitches set has changed (after the Flow Entry IDs
442 // assignments by the caller).
443 //
444 if (! flowEntry.isValidFlowEntryId()) {
445 long id = getNextFlowEntryId();
446 flowEntry.setFlowEntryId(new FlowEntryId(id));
447 }
448
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800449 log.debug("Pushing Flow Entry To Switch: {}", flowEntry.toString());
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800450 entries.add(new Pair<IOFSwitch, FlowEntry>(mySwitch, flowEntry));
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800451 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800452
453 pusher.pushFlowEntries(entries);
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800454 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700455
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800456 /**
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800457 * Cleanup deleted Flow Entries from the datagrid.
458 *
459 * NOTE: We cleanup only the Flow Entries that are not for our switches.
460 * This is needed to handle the case a switch going down:
461 * It has no Master controller instance, hence no controller instance
462 * will cleanup its flow entries.
463 * This is sub-optimal: we need to elect a controller instance to handle
464 * the cleanup of such orphaned flow entries.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800465 *
466 * @param modifiedFlowEntries the collection of modified Flow Entries.
467 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800468 private void cleanupDeletedFlowEntriesFromDatagrid(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800469 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800470 if (modifiedFlowEntries.isEmpty())
471 return;
472
473 Map<Long, IOFSwitch> mySwitches = getMySwitches();
474
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800475 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800476 //
477 // Process only Flow Entries that should be deleted and have
478 // a valid Flow Entry ID.
479 //
Pavlin Radoslavov426a8532013-12-02 17:32:21 -0800480 if (! flowEntry.isValidFlowEntryId())
481 continue;
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800482 if (flowEntry.flowEntryUserState() !=
483 FlowEntryUserState.FE_USER_DELETE) {
484 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800485 }
486
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800487 //
488 // NOTE: The deletion of Flow Entries for my switches is handled
489 // elsewhere.
490 //
491 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
492 if (mySwitch != null)
493 continue;
494
495 log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry.toString());
496
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800497 //
498 // Write the Flow Entry to the Datagrid
499 //
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800500 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800501 }
502 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700503
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800504 /**
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800505 * Class to implement writing to the database in a separate thread.
506 */
507 class FlowDatabaseWriter extends Thread {
508 private FlowManager flowManager;
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800509 private BlockingQueue<FlowPath> blockingQueue;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800510
511 /**
512 * Constructor.
513 *
514 * @param flowManager the Flow Manager to use.
515 * @param blockingQueue the blocking queue to use.
516 */
517 FlowDatabaseWriter(FlowManager flowManager,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800518 BlockingQueue<FlowPath> blockingQueue) {
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800519 this.flowManager = flowManager;
520 this.blockingQueue = blockingQueue;
521 }
522
523 /**
524 * Run the thread.
525 */
526 @Override
527 public void run() {
528 //
529 // The main loop
530 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800531 Collection<FlowPath> collection = new LinkedList<FlowPath>();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800532 try {
533 while (true) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800534 FlowPath flowPath = blockingQueue.take();
535 collection.add(flowPath);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800536 blockingQueue.drainTo(collection);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800537 flowManager.writeModifiedFlowPathsToDatabase(collection);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800538 collection.clear();
539 }
540 } catch (Exception exception) {
541 log.debug("Exception writing to the Database: ", exception);
542 }
543 }
544 }
545
546 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800547 * Push Flow Paths to the Network MAP.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800548 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800549 * NOTE: The complete Flow Paths are pushed only on the instance
550 * responsible for the first switch. This is to avoid database errors
551 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800552 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800553 * @param modifiedFlowPaths the collection of Flow Paths to push.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800554 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800555 private void pushModifiedFlowPathsToDatabase(
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800556 Collection<FlowPath> modifiedFlowPaths) {
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800557 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800558 // We only add the Flow Paths to the Database Queue.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800559 // The FlowDatabaseWriter thread is responsible for the actual writing.
560 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800561 flowPathsToDatabaseQueue.addAll(modifiedFlowPaths);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800562 }
563
564 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800565 * Write Flow Paths to the Network MAP.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800566 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800567 * NOTE: The complete Flow Paths are pushed only on the instance
568 * responsible for the first switch. This is to avoid database errors
569 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800570 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800571 * @param modifiedFlowPaths the collection of Flow Paths to write.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800572 */
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800573 private void writeModifiedFlowPathsToDatabase(
574 Collection<FlowPath> modifiedFlowPaths) {
575 if (modifiedFlowPaths.isEmpty())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800576 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700577
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800578 FlowId dummyFlowId = new FlowId();
579
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800580 Map<Long, IOFSwitch> mySwitches = getMySwitches();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700581
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800582 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800583 //
Pavlin Radoslavov409c9fa2013-12-04 19:16:41 -0800584 // Don't push Flow Paths that are deleted by the user.
585 // Those will be deleted at the ONOS instance that received the
586 // API call to delete the flow.
587 //
588 if (flowPath.flowPathUserState() ==
589 FlowPathUserState.FP_USER_DELETE) {
590 continue;
591 }
592
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
602 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800603 // Test whether all Flow Entries are valid
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800604 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800605 boolean allValid = true;
606 for (FlowEntry flowEntry : flowPath.flowEntries()) {
607 if (flowEntry.flowEntryUserState() ==
608 FlowEntryUserState.FE_USER_DELETE) {
609 continue;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700610 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800611 if (! flowEntry.isValidFlowEntryId()) {
612 allValid = false;
613 break;
614 }
615 }
616 if (! allValid)
617 continue;
618
619 log.debug("Pushing Flow Path To Database: {}", flowPath.toString());
620
621 //
622 // Write the Flow Path to the Network Map
623 //
624 try {
Pavlin Radoslavov67bf7622013-12-04 12:28:23 -0800625 if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath,
626 dummyFlowId)) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800627 String logMsg = "Cannot write to Network Map Flow Path " +
628 flowPath.flowId();
629 log.error(logMsg);
630 }
631 } catch (Exception e) {
632 log.error("Exception writing Flow Path to Network MAP: ", e);
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700633 }
634 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700635 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800636}