blob: ba46ebd1cf64cb72fa8ae1406cf4ef3c4753f08c [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 Radoslavov53219802013-12-06 11:02:04 -080010import java.util.SortedMap;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080011import java.util.concurrent.BlockingQueue;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080012import java.util.concurrent.LinkedBlockingQueue;
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;
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -070024import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService;
HIGUCHI Yuta60a10142013-06-14 15:50:10 -070025import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
Brian O'Connor8c166a72013-11-14 18:41:48 -080026import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Pavlin Radoslavovc0862662013-12-10 15:31:49 -080027import net.onrc.onos.ofcontroller.forwarding.IForwardingService;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070028import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavov63e42602013-12-12 12:54:05 -080029import net.onrc.onos.ofcontroller.util.Dpid;
30import net.onrc.onos.ofcontroller.util.FlowEntry;
31import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
32import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
33import net.onrc.onos.ofcontroller.util.FlowEntryId;
34import net.onrc.onos.ofcontroller.util.FlowId;
35import net.onrc.onos.ofcontroller.util.FlowPath;
36import net.onrc.onos.ofcontroller.util.FlowPathUserState;
37import net.onrc.onos.ofcontroller.util.Pair;
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -080038import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
39
Pavlin Radoslavov262e6832013-12-18 14:37:35 -080040import com.thinkaurelius.titan.core.TitanException;
41
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -080042import com.esotericsoftware.kryo2.Kryo;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080043
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080044import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
admin944ef4f2013-10-08 17:48:37 -070047/**
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -070048 * Flow Manager class for handling the network flows.
admin944ef4f2013-10-08 17:48:37 -070049 */
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070050public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -080051
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -080052 private boolean enableOnrc2014MeasurementsFlows = true;
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -080053
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080054 protected GraphDBOperation dbHandlerApi;
55 protected GraphDBOperation dbHandlerInner;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080056
Jonathan Hart50a94982013-04-10 14:49:51 -070057 protected volatile IFloodlightProviderService floodlightProvider;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070058 protected volatile IDatagridService datagridService;
59 protected IRestApiService restApi;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070060 protected FloodlightModuleContext context;
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070061 protected FlowEventHandler flowEventHandler;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080062
Brian O'Connor8c166a72013-11-14 18:41:48 -080063 protected IFlowPusherService pusher;
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -080064 protected IForwardingService forwardingService;
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -080065
66 private KryoFactory kryoFactory = new KryoFactory();
67
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000068 // Flow Entry ID generation state
69 private static Random randomGenerator = new Random();
70 private static int nextFlowEntryIdPrefix = 0;
71 private static int nextFlowEntryIdSuffix = 0;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000072
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080073 /** The logger. */
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070074 private final static Logger log = LoggerFactory.getLogger(FlowManager.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080075
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080076 // The queue to write Flow Entries to the database
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080077 private BlockingQueue<FlowPath> flowPathsToDatabaseQueue =
78 new LinkedBlockingQueue<FlowPath>();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080079 FlowDatabaseWriter flowDatabaseWriter;
80
admin944ef4f2013-10-08 17:48:37 -070081 /**
82 * Initialize the Flow Manager.
83 *
84 * @param conf the Graph Database configuration string.
85 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080086 @Override
87 public void init(String conf) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -080088 dbHandlerApi = new GraphDBOperation(conf);
89 dbHandlerInner = new GraphDBOperation(conf);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080090 }
91
admin944ef4f2013-10-08 17:48:37 -070092 /**
93 * Shutdown the Flow Manager operation.
94 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080095 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080096 protected void finalize() {
Toshio Koide9fe1cb22013-06-13 13:51:11 -070097 close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080098 }
99
admin944ef4f2013-10-08 17:48:37 -0700100 /**
101 * Shutdown the Flow Manager operation.
102 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800103 @Override
104 public void close() {
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700105 datagridService.deregisterFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800106 dbHandlerApi.close();
107 dbHandlerInner.close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800108 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800109
admin944ef4f2013-10-08 17:48:37 -0700110 /**
111 * Get the collection of offered module services.
112 *
113 * @return the collection of offered module services.
114 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800115 @Override
116 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800117 Collection<Class<? extends IFloodlightService>> l =
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800118 new ArrayList<Class<? extends IFloodlightService>>();
119 l.add(IFlowService.class);
120 return l;
121 }
122
admin944ef4f2013-10-08 17:48:37 -0700123 /**
124 * Get the collection of implemented services.
125 *
126 * @return the collection of implemented services.
127 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800128 @Override
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800129 public Map<Class<? extends IFloodlightService>, IFloodlightService>
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800130 getServiceImpls() {
131 Map<Class<? extends IFloodlightService>,
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700132 IFloodlightService> m =
133 new HashMap<Class<? extends IFloodlightService>,
134 IFloodlightService>();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800135 m.put(IFlowService.class, this);
136 return m;
137 }
138
admin944ef4f2013-10-08 17:48:37 -0700139 /**
140 * Get the collection of modules this module depends on.
141 *
142 * @return the collection of modules this module depends on.
143 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800144 @Override
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800145 public Collection<Class<? extends IFloodlightService>>
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700146 getModuleDependencies() {
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800147 Collection<Class<? extends IFloodlightService>> l =
148 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800149 l.add(IFloodlightProviderService.class);
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700150 l.add(INetworkGraphService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700151 l.add(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800152 l.add(IRestApiService.class);
Pavlin Radoslavovc0862662013-12-10 15:31:49 -0800153 l.add(IFlowPusherService.class);
Pavlin Radoslavov63c2d052013-12-18 18:17:55 -0800154 //
155 // TODO: Comment-out the dependency on the IForwardingService,
156 // because it is an optional module. Apparently, adding the dependency
157 // here automatically enables the module.
158 //
159 // l.add(IForwardingService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800160 return l;
161 }
162
admin944ef4f2013-10-08 17:48:37 -0700163 /**
164 * Initialize the module.
165 *
166 * @param context the module context to use for the initialization.
167 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800168 @Override
169 public void init(FloodlightModuleContext context)
170 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700171 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800172 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700173 datagridService = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800174 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov939ca6b2013-12-03 12:35:37 -0800175 pusher = context.getServiceImpl(IFlowPusherService.class);
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800176 forwardingService = context.getServiceImpl(IForwardingService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800177
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700178 this.init("");
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800179 }
180
admin944ef4f2013-10-08 17:48:37 -0700181 /**
182 * Get the next Flow Entry ID to use.
183 *
184 * @return the next Flow Entry ID to use.
185 */
Naoki Shiota4e77de92013-11-18 17:29:54 -0800186 @Override
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700187 public synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000188 //
189 // Generate the next Flow Entry ID.
190 // NOTE: For now, the higher 32 bits are random, and
191 // the lower 32 bits are sequential.
192 // In the future, we need a better allocation mechanism.
193 //
194 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
195 nextFlowEntryIdPrefix = randomGenerator.nextInt();
196 nextFlowEntryIdSuffix = 0;
197 } else {
198 nextFlowEntryIdSuffix++;
199 }
200 long result = (long)nextFlowEntryIdPrefix << 32;
201 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
202 return result;
203 }
204
admin944ef4f2013-10-08 17:48:37 -0700205 /**
206 * Startup module operation.
207 *
208 * @param context the module context to use for the startup.
209 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800210 @Override
211 public void startUp(FloodlightModuleContext context) {
admin944ef4f2013-10-08 17:48:37 -0700212 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700213
admin944ef4f2013-10-08 17:48:37 -0700214 // Initialize the Flow Entry ID generator
215 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800216
217 //
218 // The thread to write to the database
219 //
220 flowDatabaseWriter = new FlowDatabaseWriter(this,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800221 flowPathsToDatabaseQueue);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800222 flowDatabaseWriter.start();
223
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700224 //
Pavlin Radoslavovc9da5322013-11-22 11:59:46 -0800225 // The Flow Event Handler thread:
226 // - create
227 // - register with the Datagrid Service
228 // - startup
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700229 //
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800230 flowEventHandler = new FlowEventHandler(this, datagridService);
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700231 datagridService.registerFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700232 flowEventHandler.start();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800233 }
234
235 /**
236 * Add a flow.
237 *
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800238 * @param flowPath the Flow Path to install.
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800239 * @return the Flow ID on success, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800240 */
241 @Override
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800242 public FlowId addFlow(FlowPath flowPath) {
243
244 // Allocate the Flow ID if necessary
Pavlin Radoslavov892dd182013-12-05 23:33:15 -0800245 if (! flowPath.isValidFlowId()) {
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800246 long id = getNextFlowEntryId();
247 flowPath.setFlowId(new FlowId(id));
248 }
249
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700250 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700251 // NOTE: We need to explicitly initialize some of the state,
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700252 // in case the application didn't do it.
253 //
254 for (FlowEntry flowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov07d22b42013-12-15 16:33:33 -0800255 // The Flow Entry switch state
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700256 if (flowEntry.flowEntrySwitchState() ==
257 FlowEntrySwitchState.FE_SWITCH_UNKNOWN) {
258 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
259 }
Pavlin Radoslavov07d22b42013-12-15 16:33:33 -0800260 // The Flow Entry ID
261 if (! flowEntry.isValidFlowEntryId()) {
262 long id = getNextFlowEntryId();
263 flowEntry.setFlowEntryId(new FlowEntryId(id));
264 }
265 // The Flow ID
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700266 if (! flowEntry.isValidFlowId())
267 flowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700268 }
269
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800270 if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath)) {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800271 if (enableOnrc2014MeasurementsFlows) {
272 datagridService.notificationSendFlowIdAdded(flowPath.flowId());
273 } else {
274 datagridService.notificationSendFlowAdded(flowPath);
275 }
276
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800277 return flowPath.flowId();
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700278 }
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800279 return null;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800280 }
281
282 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000283 * Delete all previously added flows.
284 *
285 * @return true on success, otherwise false.
286 */
287 @Override
288 public boolean deleteAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800289 if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800290 if (enableOnrc2014MeasurementsFlows) {
291 datagridService.notificationSendAllFlowIdsRemoved();
292 } else {
293 datagridService.notificationSendAllFlowsRemoved();
294 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700295 return true;
296 }
297 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000298 }
299
300 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800301 * Delete a previously added flow.
302 *
303 * @param flowId the Flow ID of the flow to delete.
304 * @return true on success, otherwise false.
305 */
306 @Override
307 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800308 if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800309 if (enableOnrc2014MeasurementsFlows) {
310 datagridService.notificationSendFlowIdRemoved(flowId);
311 } else {
312 datagridService.notificationSendFlowRemoved(flowId);
313 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700314 return true;
315 }
316 return false;
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700317 }
318
319 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800320 * Get a previously added flow.
321 *
322 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800323 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800324 */
325 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800326 public FlowPath getFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800327 return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700328 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800329
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700330 /**
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800331 * Get a previously added flow entry.
332 *
333 * @param flowEntryId the Flow Entry ID of the flow entry to get.
334 * @return the Flow Entry if found, otherwise null.
335 */
336 public FlowEntry getFlowEntry(FlowEntryId flowEntryId) {
337 return FlowDatabaseOperation.getFlowEntry(dbHandlerApi, flowEntryId);
338 }
339
340 /**
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800341 * Get the source switch DPID of a previously added flow.
342 *
343 * @param flowId the Flow ID of the flow to get.
344 * @return the source switch DPID if found, otherwise null.
345 */
346 public Dpid getFlowSourceDpid(FlowId flowId) {
347 return FlowDatabaseOperation.getFlowSourceDpid(dbHandlerApi, flowId);
348 }
349
350 /**
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700351 * Get all installed flows by all installers.
352 *
353 * @return the Flow Paths if found, otherwise null.
354 */
355 @Override
356 public ArrayList<FlowPath> getAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800357 return FlowDatabaseOperation.getAllFlows(dbHandlerApi);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800358 }
359
360 /**
Pavlin Radoslavov16b761d2014-01-08 09:47:14 -0800361 * Get all installed flows whose Source Switch is controlled by this
362 * instance.
363 *
364 * @param mySwitches the collection of the switches controlled by this
365 * instance.
366 * @return the Flow Paths if found, otherwise null.
367 */
368 public ArrayList<FlowPath> getAllMyFlows(Map<Long, IOFSwitch> mySwitches) {
369 return FlowDatabaseOperation.getAllMyFlows(dbHandlerApi, mySwitches);
370 }
371
372 /**
admin944ef4f2013-10-08 17:48:37 -0700373 * Get summary of all installed flows by all installers in a given range.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700374 *
admin944ef4f2013-10-08 17:48:37 -0700375 * @param flowId the Flow ID of the first flow in the flow range to get.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700376 * @param maxFlows the maximum number of flows to be returned.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700377 * @return the Flow Paths if found, otherwise null.
378 */
379 @Override
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800380 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId,
381 int maxFlows) {
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800382 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
383 SortedMap<Long, FlowPath> sortedFlowPaths =
384 flowEventHandler.getAllFlowPathsCopy();
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800385
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800386 //
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800387 // Truncate each Flow Path and Flow Entry
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800388 //
389 for (FlowPath flowPath : sortedFlowPaths.values()) {
390 //
391 // TODO: Add only the Flow Paths that have been successfully
392 // installed.
393 //
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800394 flowPath.setFlowEntryMatch(null);
395 flowPath.setFlowEntryActions(null);
396 for (FlowEntry flowEntry : flowPath.flowEntries()) {
397 flowEntry.setFlowEntryMatch(null);
398 flowEntry.setFlowEntryActions(null);
399 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800400 flowPaths.add(flowPath);
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800401 }
402
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800403 return flowPaths;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700404 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700405
406 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800407 * Get the collection of my switches.
408 *
409 * @return the collection of my switches.
410 */
411 public Map<Long, IOFSwitch> getMySwitches() {
412 return floodlightProvider.getSwitches();
413 }
414
415 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800416 * Get the network topology.
417 *
418 * @return the network topology.
419 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800420 @Override
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800421 public Topology getTopology() {
422 return flowEventHandler.getTopology();
423 }
424
425 /**
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800426 * Inform the Flow Manager that a Flow Entry on switch expired.
427 *
Pavlin Radoslavov3bd5ccf2013-11-26 15:10:21 -0800428 * @param sw the switch the Flow Entry expired on.
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800429 * @param flowEntryId the Flow Entry ID of the expired Flow Entry.
430 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800431 @Override
Pavlin Radoslavovc5718e72013-12-10 15:47:10 -0800432 public void flowEntryOnSwitchExpired(IOFSwitch sw,
433 FlowEntryId flowEntryId) {
434 // Find the Flow Entry
435 FlowEntry flowEntry = datagridService.getFlowEntry(flowEntryId);
Pavlin Radoslavovac0fabc2013-12-13 10:47:56 -0800436 if (flowEntry == null)
Pavlin Radoslavovc5718e72013-12-10 15:47:10 -0800437 return; // Flow Entry not found
438
439 // Find the Flow Path
440 FlowPath flowPath = datagridService.getFlow(flowEntry.flowId());
441 if (flowPath == null)
442 return; // Flow Path not found
443
444 //
445 // Remove the Flow if the Flow Entry expired on the first switch
446 //
447 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
448 if (srcDpid.value() != sw.getId())
449 return;
450 deleteFlow(flowPath.flowId());
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800451 }
452
453 /**
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800454 * Inform the Flow Manager that a collection of Flow Entries have been
455 * pushed to a switch.
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800456 *
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800457 * @param entries the collection of <IOFSwitch, FlowEntry> pairs
458 * that have been pushed.
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800459 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800460 @Override
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800461 public void flowEntriesPushedToSwitch(
462 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800463
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800464 if (enableOnrc2014MeasurementsFlows)
465 return;
466
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800467 //
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800468 // Process all entries
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800469 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800470 // TODO: For now we have to create an explicit FlowEntry copy so
471 // we don't modify the original FlowEntry.
472 // This should go away after we start using the OpenFlow Barrier
473 // mechnanism in the FlowPusher.
474 //
475 Kryo kryo = kryoFactory.newKryo();
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800476 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
Pavlin Radoslavov4535bc12013-12-05 10:43:49 -0800477 FlowEntry flowEntry = entry.second;
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800478
479 //
480 // Mark the Flow Entry that it has been pushed to the switch
481 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800482 FlowEntry copyFlowEntry = kryo.copy(flowEntry);
483 copyFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED);
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800484
485 //
486 // Write the Flow Entry to the Datagrid
487 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800488 switch (copyFlowEntry.flowEntryUserState()) {
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800489 case FE_USER_ADD:
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800490 datagridService.notificationSendFlowEntryAdded(copyFlowEntry);
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800491 break;
492 case FE_USER_MODIFY:
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800493 datagridService.notificationSendFlowEntryUpdated(copyFlowEntry);
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800494 break;
495 case FE_USER_DELETE:
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800496 datagridService.notificationSendFlowEntryRemoved(copyFlowEntry.flowEntryId());
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800497 break;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800498 case FE_USER_UNKNOWN:
499 assert(false);
500 break;
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800501 }
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800502 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800503 kryoFactory.deleteKryo(kryo);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800504 }
505
506 /**
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800507 * Generate a notification that a collection of Flow Paths has been
508 * installed in the network.
509 *
510 * @param flowPaths the collection of installed Flow Paths.
511 */
512 void notificationFlowPathsInstalled(Collection<FlowPath> flowPaths) {
Pavlin Radoslavov63c2d052013-12-18 18:17:55 -0800513 //
514 // TODO: Add an explicit check for null pointer, because
515 // the IForwardingService is optional. Remove the "if" statement
516 // after hte Forwarding Module becomes mandatory.
517 //
518 if (forwardingService != null)
519 forwardingService.flowsInstalled(flowPaths);
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800520 }
521
522 /**
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800523 * Push modified Flow-related state as appropriate.
524 *
525 * @param modifiedFlowPaths the collection of modified Flow Paths.
526 * @param modifiedFlowEntries the collection of modified Flow Entries.
527 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800528 void pushModifiedFlowState(Collection<FlowPath> modifiedFlowPaths,
529 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800530 //
531 // Push the modified Flow state:
532 // - Flow Entries to switches and the datagrid
533 // - Flow Paths to the database
534 //
535 pushModifiedFlowEntriesToSwitches(modifiedFlowEntries);
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -0800536 if (enableOnrc2014MeasurementsFlows) {
537 writeModifiedFlowPathsToDatabase(modifiedFlowPaths);
538 } else {
539 pushModifiedFlowPathsToDatabase(modifiedFlowPaths);
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800540 cleanupDeletedFlowEntriesFromDatagrid(modifiedFlowEntries);
541 }
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800542 }
543
544 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800545 * Push modified Flow Entries to switches.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700546 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800547 * NOTE: Only the Flow Entries to switches controlled by this instance
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700548 * are pushed.
549 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800550 * @param modifiedFlowEntries the collection of modified Flow Entries.
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700551 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800552 private void pushModifiedFlowEntriesToSwitches(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800553 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800554 if (modifiedFlowEntries.isEmpty())
555 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700556
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800557 List<Pair<IOFSwitch, FlowEntry>> entries =
558 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
559
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800560 Map<Long, IOFSwitch> mySwitches = getMySwitches();
561
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800562 //
563 // Create a collection of my Flow Entries to push
564 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800565 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800566 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
567 if (mySwitch == null)
568 continue;
569
Pavlin Radoslavovd1b728c2013-12-18 21:39:58 -0800570 if (flowEntry.flowEntrySwitchState() ==
571 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
572 //
573 // Don't push again Flow Entries that were already already
574 // installed into the switches.
575 //
576 continue;
577 }
578
Pavlin Radoslavovaca49d12013-12-04 19:49:17 -0800579 //
580 // Assign Flow Entry IDs if missing.
581 //
582 // NOTE: This is an additional safeguard, in case the
583 // mySwitches set has changed (after the Flow Entry IDs
584 // assignments by the caller).
585 //
586 if (! flowEntry.isValidFlowEntryId()) {
587 long id = getNextFlowEntryId();
588 flowEntry.setFlowEntryId(new FlowEntryId(id));
589 }
590
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800591 log.debug("Pushing Flow Entry To Switch: {}", flowEntry);
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800592 entries.add(new Pair<IOFSwitch, FlowEntry>(mySwitch, flowEntry));
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800593 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800594
595 pusher.pushFlowEntries(entries);
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800596 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700597
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800598 /**
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800599 * Cleanup deleted Flow Entries from the datagrid.
600 *
601 * NOTE: We cleanup only the Flow Entries that are not for our switches.
602 * This is needed to handle the case a switch going down:
603 * It has no Master controller instance, hence no controller instance
604 * will cleanup its flow entries.
605 * This is sub-optimal: we need to elect a controller instance to handle
606 * the cleanup of such orphaned flow entries.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800607 *
608 * @param modifiedFlowEntries the collection of modified Flow Entries.
609 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800610 private void cleanupDeletedFlowEntriesFromDatagrid(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800611 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800612 if (modifiedFlowEntries.isEmpty())
613 return;
614
615 Map<Long, IOFSwitch> mySwitches = getMySwitches();
616
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800617 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800618 //
619 // Process only Flow Entries that should be deleted and have
620 // a valid Flow Entry ID.
621 //
Pavlin Radoslavov426a8532013-12-02 17:32:21 -0800622 if (! flowEntry.isValidFlowEntryId())
623 continue;
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800624 if (flowEntry.flowEntryUserState() !=
625 FlowEntryUserState.FE_USER_DELETE) {
626 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800627 }
628
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800629 //
630 // NOTE: The deletion of Flow Entries for my switches is handled
631 // elsewhere.
632 //
633 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
634 if (mySwitch != null)
635 continue;
636
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800637 log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800638
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800639 //
640 // Write the Flow Entry to the Datagrid
641 //
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800642 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800643 }
644 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700645
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800646 /**
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800647 * Class to implement writing to the database in a separate thread.
648 */
649 class FlowDatabaseWriter extends Thread {
650 private FlowManager flowManager;
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800651 private BlockingQueue<FlowPath> blockingQueue;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800652
653 /**
654 * Constructor.
655 *
656 * @param flowManager the Flow Manager to use.
657 * @param blockingQueue the blocking queue to use.
658 */
659 FlowDatabaseWriter(FlowManager flowManager,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800660 BlockingQueue<FlowPath> blockingQueue) {
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800661 this.flowManager = flowManager;
662 this.blockingQueue = blockingQueue;
663 }
664
665 /**
666 * Run the thread.
667 */
668 @Override
669 public void run() {
670 //
671 // The main loop
672 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800673 Collection<FlowPath> collection = new LinkedList<FlowPath>();
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800674 this.setName("FlowDatabaseWriter " + this.getId() );
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800675 try {
676 while (true) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800677 FlowPath flowPath = blockingQueue.take();
678 collection.add(flowPath);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800679 blockingQueue.drainTo(collection);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800680 flowManager.writeModifiedFlowPathsToDatabase(collection);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800681 collection.clear();
682 }
683 } catch (Exception exception) {
684 log.debug("Exception writing to the Database: ", exception);
685 }
686 }
687 }
688
689 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800690 * Push Flow Paths to the Network MAP.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800691 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800692 * NOTE: The complete Flow Paths are pushed only on the instance
693 * responsible for the first switch. This is to avoid database errors
694 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800695 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800696 * @param modifiedFlowPaths the collection of Flow Paths to push.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800697 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800698 private void pushModifiedFlowPathsToDatabase(
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800699 Collection<FlowPath> modifiedFlowPaths) {
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -0800700 List<FlowPath> copiedFlowPaths = new LinkedList<FlowPath>();
701
702 //
703 // Create a copy of the Flow Paths to push, because the pushing
704 // itself will happen on a separate thread.
705 //
706 Kryo kryo = kryoFactory.newKryo();
707 for (FlowPath flowPath : modifiedFlowPaths) {
708 FlowPath copyFlowPath = kryo.copy(flowPath);
709 copiedFlowPaths.add(copyFlowPath);
710 }
711 kryoFactory.deleteKryo(kryo);
712
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800713 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800714 // We only add the Flow Paths to the Database Queue.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800715 // The FlowDatabaseWriter thread is responsible for the actual writing.
716 //
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -0800717 flowPathsToDatabaseQueue.addAll(copiedFlowPaths);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800718 }
719
720 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800721 * Write Flow Paths to the Network MAP.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800722 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800723 * NOTE: The complete Flow Paths are pushed only on the instance
724 * responsible for the first switch. This is to avoid database errors
725 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800726 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800727 * @param modifiedFlowPaths the collection of Flow Paths to write.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800728 */
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800729 private void writeModifiedFlowPathsToDatabase(
730 Collection<FlowPath> modifiedFlowPaths) {
731 if (modifiedFlowPaths.isEmpty())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800732 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700733
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800734 Map<Long, IOFSwitch> mySwitches = getMySwitches();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700735
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800736 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800737 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800738 // Push the changes only on the instance responsible for the
739 // first switch.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800740 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800741 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
742 IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value());
743 if (mySrcSwitch == null)
744 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800745
746 //
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800747 // Delete the Flow Path from the Network Map
748 //
749 if (flowPath.flowPathUserState() ==
750 FlowPathUserState.FP_USER_DELETE) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800751 log.debug("Deleting Flow Path From Database: {}", flowPath);
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800752
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800753 boolean retry = false;
754 do {
755 retry = false;
756 try {
757 if (! FlowDatabaseOperation.deleteFlow(
758 dbHandlerInner,
759 flowPath.flowId())) {
760 log.error("Cannot delete Flow Path {} from Network Map",
761 flowPath.flowId());
762 retry = true;
763 }
764 } catch (TitanException te) {
765 log.error("Titan Exception deleting Flow Path from Network MAP: {}", te);
766 retry = true;
767 } catch (Exception e) {
768 log.error("Exception deleting Flow Path from Network MAP: {}", e);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800769 }
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800770 } while (retry);
771
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800772 if (enableOnrc2014MeasurementsFlows) {
773 // Send the notification
774 datagridService.notificationSendFlowIdRemoved(flowPath.flowId());
775 }
776
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800777 continue;
778 }
779
780 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800781 // Test whether all Flow Entries are valid
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800782 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800783 boolean allValid = true;
784 for (FlowEntry flowEntry : flowPath.flowEntries()) {
785 if (flowEntry.flowEntryUserState() ==
786 FlowEntryUserState.FE_USER_DELETE) {
787 continue;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700788 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800789 if (! flowEntry.isValidFlowEntryId()) {
790 allValid = false;
791 break;
792 }
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800793 if (! enableOnrc2014MeasurementsFlows) {
794 if (flowEntry.flowEntrySwitchState() !=
795 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
796 allValid = false;
797 break;
798 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800799 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800800 }
801 if (! allValid)
802 continue;
803
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800804 log.debug("Pushing Flow Path To Database: {}", flowPath);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800805
806 //
807 // Write the Flow Path to the Network Map
808 //
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800809 boolean retry = false;
810 do {
811 retry = false;
812 try {
813 if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath)) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800814 log.error("Cannot write to Network Map Flow Path {}", flowPath.flowId());
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800815 retry = true;
816 }
817 } catch (TitanException te) {
818 log.error("Titan Exception writing Flow Path to Network MAP: ", te);
819 retry = true;
820 } catch (Exception e) {
821 log.error("Exception writing Flow Path to Network MAP: ", e);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800822 }
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800823 } while (retry);
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800824
825 if (enableOnrc2014MeasurementsFlows) {
826 // Send the notification
827 datagridService.notificationSendFlowIdAdded(flowPath.flowId());
828 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700829 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700830 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800831}