blob: 0e4e3da0907541af4a00d4bc3e368848ea5ff9e9 [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 Radoslavovb6f53542013-03-01 16:02:14 -080021import net.floodlightcontroller.util.OFMessageDamper;
yoshi2fd4c7e2013-11-22 15:47:55 -080022import net.onrc.onos.graph.DBOperation;
23import net.onrc.onos.graph.GraphDBManager;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070024import net.onrc.onos.datagrid.IDatagridService;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070025import net.onrc.onos.ofcontroller.core.INetMapStorage;
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -070026import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService;
HIGUCHI Yuta60a10142013-06-14 15:50:10 -070027import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
Brian O'Connor8c166a72013-11-14 18:41:48 -080028import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Pavlin Radoslavovc0862662013-12-10 15:31:49 -080029import net.onrc.onos.ofcontroller.forwarding.IForwardingService;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070030import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavov63e42602013-12-12 12:54:05 -080031import net.onrc.onos.ofcontroller.util.Dpid;
32import net.onrc.onos.ofcontroller.util.FlowEntry;
33import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
34import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
35import net.onrc.onos.ofcontroller.util.FlowEntryId;
36import net.onrc.onos.ofcontroller.util.FlowId;
37import net.onrc.onos.ofcontroller.util.FlowPath;
38import net.onrc.onos.ofcontroller.util.FlowPathUserState;
39import net.onrc.onos.ofcontroller.util.Pair;
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -080040import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080041
Pavlin Radoslavov262e6832013-12-18 14:37:35 -080042import com.thinkaurelius.titan.core.TitanException;
43
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -080044import com.esotericsoftware.kryo2.Kryo;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080045
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
admin944ef4f2013-10-08 17:48:37 -070049/**
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -070050 * Flow Manager class for handling the network flows.
admin944ef4f2013-10-08 17:48:37 -070051 */
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070052public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Naoki Shiota1a37ca12013-11-18 10:55:23 -080053 // flag to use FlowPusher instead of FlowSwitchOperation/MessageDamper
54 private final static boolean enableFlowPusher = false;
yoshitomob292c622013-11-23 14:35:58 -080055 protected DBOperation dbHandlerApi;
56 protected DBOperation dbHandlerInner;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080057
Jonathan Hart50a94982013-04-10 14:49:51 -070058 protected volatile IFloodlightProviderService floodlightProvider;
Pavlin Radoslavov05378272013-10-19 23:23:05 -070059 protected volatile IDatagridService datagridService;
60 protected IRestApiService restApi;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070061 protected FloodlightModuleContext context;
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070062 protected FlowEventHandler flowEventHandler;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080063
Brian O'Connor8c166a72013-11-14 18:41:48 -080064 protected IFlowPusherService pusher;
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -080065 protected IForwardingService forwardingService;
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -080066
67 private KryoFactory kryoFactory = new KryoFactory();
68
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000069 // Flow Entry ID generation state
70 private static Random randomGenerator = new Random();
71 private static int nextFlowEntryIdPrefix = 0;
72 private static int nextFlowEntryIdSuffix = 0;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070073
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080074 /** The logger. */
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070075 private final static Logger log = LoggerFactory.getLogger(FlowManager.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080076
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080077 // The queue to write Flow Entries to the database
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080078 private BlockingQueue<FlowPath> flowPathsToDatabaseQueue =
79 new LinkedBlockingQueue<FlowPath>();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -080080 FlowDatabaseWriter flowDatabaseWriter;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080081
admin944ef4f2013-10-08 17:48:37 -070082 /**
83 * Initialize the Flow Manager.
84 *
85 * @param conf the Graph Database configuration string.
86 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080087 @Override
yoshi2fd4c7e2013-11-22 15:47:55 -080088 public void init(final String dbStore, final String conf) {
yoshib1f40702014-01-22 13:07:52 -080089 dbHandlerApi = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
90 dbHandlerInner = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080091 }
92
admin944ef4f2013-10-08 17:48:37 -070093 /**
94 * Shutdown the Flow Manager operation.
95 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080096 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080097 protected void finalize() {
Toshio Koide9fe1cb22013-06-13 13:51:11 -070098 close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080099 }
100
admin944ef4f2013-10-08 17:48:37 -0700101 /**
102 * Shutdown the Flow Manager operation.
103 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800104 @Override
105 public void close() {
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700106 datagridService.deregisterFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800107 dbHandlerApi.close();
108 dbHandlerInner.close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800109 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800110
admin944ef4f2013-10-08 17:48:37 -0700111 /**
112 * Get the collection of offered module services.
113 *
114 * @return the collection of offered module services.
115 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800116 @Override
117 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800118 Collection<Class<? extends IFloodlightService>> l =
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800119 new ArrayList<Class<? extends IFloodlightService>>();
120 l.add(IFlowService.class);
121 return l;
122 }
123
admin944ef4f2013-10-08 17:48:37 -0700124 /**
125 * Get the collection of implemented services.
126 *
127 * @return the collection of implemented services.
128 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800129 @Override
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800130 public Map<Class<? extends IFloodlightService>, IFloodlightService>
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800131 getServiceImpls() {
132 Map<Class<? extends IFloodlightService>,
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700133 IFloodlightService> m =
134 new HashMap<Class<? extends IFloodlightService>,
135 IFloodlightService>();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800136 m.put(IFlowService.class, this);
137 return m;
138 }
139
admin944ef4f2013-10-08 17:48:37 -0700140 /**
141 * Get the collection of modules this module depends on.
142 *
143 * @return the collection of modules this module depends on.
144 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800145 @Override
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800146 public Collection<Class<? extends IFloodlightService>>
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700147 getModuleDependencies() {
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800148 Collection<Class<? extends IFloodlightService>> l =
149 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800150 l.add(IFloodlightProviderService.class);
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700151 l.add(INetworkGraphService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700152 l.add(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800153 l.add(IRestApiService.class);
Pavlin Radoslavovc0862662013-12-10 15:31:49 -0800154 l.add(IFlowPusherService.class);
Pavlin Radoslavov63c2d052013-12-18 18:17:55 -0800155 //
156 // TODO: Comment-out the dependency on the IForwardingService,
157 // because it is an optional module. Apparently, adding the dependency
158 // here automatically enables the module.
159 //
160 // l.add(IForwardingService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800161 return l;
162 }
163
admin944ef4f2013-10-08 17:48:37 -0700164 /**
165 * Initialize the module.
166 *
167 * @param context the module context to use for the initialization.
168 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800169 @Override
170 public void init(FloodlightModuleContext context)
171 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700172 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800173 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700174 datagridService = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800175 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov939ca6b2013-12-03 12:35:37 -0800176 pusher = context.getServiceImpl(IFlowPusherService.class);
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800177 forwardingService = context.getServiceImpl(IForwardingService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800178
yoshi0fee3de2013-11-23 09:13:37 -0800179 this.init("","");
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800180 }
181
admin944ef4f2013-10-08 17:48:37 -0700182 /**
183 * Get the next Flow Entry ID to use.
184 *
185 * @return the next Flow Entry ID to use.
186 */
Naoki Shiota4e77de92013-11-18 17:29:54 -0800187 @Override
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700188 public synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000189 //
190 // Generate the next Flow Entry ID.
191 // NOTE: For now, the higher 32 bits are random, and
192 // the lower 32 bits are sequential.
193 // In the future, we need a better allocation mechanism.
194 //
195 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
196 nextFlowEntryIdPrefix = randomGenerator.nextInt();
197 nextFlowEntryIdSuffix = 0;
198 } else {
199 nextFlowEntryIdSuffix++;
200 }
201 long result = (long)nextFlowEntryIdPrefix << 32;
202 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
203 return result;
204 }
205
admin944ef4f2013-10-08 17:48:37 -0700206 /**
207 * Startup module operation.
208 *
209 * @param context the module context to use for the startup.
210 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800211 @Override
212 public void startUp(FloodlightModuleContext context) {
admin944ef4f2013-10-08 17:48:37 -0700213 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700214
admin944ef4f2013-10-08 17:48:37 -0700215 // Initialize the Flow Entry ID generator
216 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800217
218 //
219 // The thread to write to the database
220 //
221 flowDatabaseWriter = new FlowDatabaseWriter(this,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800222 flowPathsToDatabaseQueue);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800223 flowDatabaseWriter.start();
224
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700225 //
Pavlin Radoslavovc9da5322013-11-22 11:59:46 -0800226 // The Flow Event Handler thread:
227 // - create
228 // - register with the Datagrid Service
229 // - startup
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700230 //
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700231 flowEventHandler = new FlowEventHandler(this, datagridService);
232 datagridService.registerFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700233 flowEventHandler.start();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800234 }
235
236 /**
237 * Add a flow.
238 *
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800239 * @param flowPath the Flow Path to install.
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800240 * @return the Flow ID on success, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800241 */
242 @Override
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800243 public FlowId addFlow(FlowPath flowPath) {
244
245 // Allocate the Flow ID if necessary
Pavlin Radoslavov892dd182013-12-05 23:33:15 -0800246 if (! flowPath.isValidFlowId()) {
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800247 long id = getNextFlowEntryId();
248 flowPath.setFlowId(new FlowId(id));
249 }
250
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700251 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700252 // NOTE: We need to explicitly initialize some of the state,
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700253 // in case the application didn't do it.
254 //
255 for (FlowEntry flowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov07d22b42013-12-15 16:33:33 -0800256 // The Flow Entry switch state
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700257 if (flowEntry.flowEntrySwitchState() ==
258 FlowEntrySwitchState.FE_SWITCH_UNKNOWN) {
259 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800260 }
Pavlin Radoslavov07d22b42013-12-15 16:33:33 -0800261 // The Flow Entry ID
262 if (! flowEntry.isValidFlowEntryId()) {
263 long id = getNextFlowEntryId();
264 flowEntry.setFlowEntryId(new FlowEntryId(id));
265 }
266 // The Flow ID
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700267 if (! flowEntry.isValidFlowId())
268 flowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700269 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800270
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800271 if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700272 datagridService.notificationSendFlowAdded(flowPath);
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800273 return flowPath.flowId();
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700274 }
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800275 return null;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700276 }
277
278 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000279 * Delete all previously added flows.
280 *
281 * @return true on success, otherwise false.
282 */
283 @Override
284 public boolean deleteAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800285 if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700286 datagridService.notificationSendAllFlowsRemoved();
287 return true;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000288 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700289 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000290 }
291
292 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800293 * Delete a previously added flow.
294 *
295 * @param flowId the Flow ID of the flow to delete.
296 * @return true on success, otherwise false.
297 */
298 @Override
299 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800300 if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700301 datagridService.notificationSendFlowRemoved(flowId);
302 return true;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700303 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700304 return false;
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700305 }
306
307 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800308 * Get a previously added flow.
309 *
310 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800311 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800312 */
313 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800314 public FlowPath getFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800315 return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700316 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800317
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700318 /**
319 * Get all installed flows by all installers.
320 *
321 * @return the Flow Paths if found, otherwise null.
322 */
323 @Override
324 public ArrayList<FlowPath> getAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800325 return FlowDatabaseOperation.getAllFlows(dbHandlerApi);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800326 }
327
328 /**
admin944ef4f2013-10-08 17:48:37 -0700329 * Get summary of all installed flows by all installers in a given range.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700330 *
admin944ef4f2013-10-08 17:48:37 -0700331 * @param flowId the Flow ID of the first flow in the flow range to get.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700332 * @param maxFlows the maximum number of flows to be returned.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700333 * @return the Flow Paths if found, otherwise null.
334 */
335 @Override
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800336 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId,
337 int maxFlows) {
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800338 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
339 SortedMap<Long, FlowPath> sortedFlowPaths =
340 flowEventHandler.getAllFlowPathsCopy();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700341
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800342 //
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800343 // Truncate each Flow Path and Flow Entry
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800344 //
345 for (FlowPath flowPath : sortedFlowPaths.values()) {
346 //
347 // TODO: Add only the Flow Paths that have been successfully
348 // installed.
349 //
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800350 flowPath.setFlowEntryMatch(null);
351 flowPath.setFlowEntryActions(null);
352 for (FlowEntry flowEntry : flowPath.flowEntries()) {
353 flowEntry.setFlowEntryMatch(null);
354 flowEntry.setFlowEntryActions(null);
355 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800356 flowPaths.add(flowPath);
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800357 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700358
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800359 return flowPaths;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700360 }
361
362 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800363 * Get the collection of my switches.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700364 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800365 * @return the collection of my switches.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700366 */
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800367 public Map<Long, IOFSwitch> getMySwitches() {
368 return floodlightProvider.getSwitches();
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700369 }
370
371 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800372 * Get the network topology.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700373 *
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800374 * @return the network topology.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700375 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800376 @Override
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800377 public Topology getTopology() {
378 return flowEventHandler.getTopology();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700379 }
380
381 /**
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800382 * Inform the Flow Manager that a Flow Entry on switch expired.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700383 *
Pavlin Radoslavov3bd5ccf2013-11-26 15:10:21 -0800384 * @param sw the switch the Flow Entry expired on.
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800385 * @param flowEntryId the Flow Entry ID of the expired Flow Entry.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700386 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800387 @Override
Pavlin Radoslavovc5718e72013-12-10 15:47:10 -0800388 public void flowEntryOnSwitchExpired(IOFSwitch sw,
389 FlowEntryId flowEntryId) {
390 // Find the Flow Entry
391 FlowEntry flowEntry = datagridService.getFlowEntry(flowEntryId);
yoshia97632b2013-12-17 15:46:08 -0800392 if (flowEntry == null)
Pavlin Radoslavovc5718e72013-12-10 15:47:10 -0800393 return; // Flow Entry not found
394
395 // Find the Flow Path
396 FlowPath flowPath = datagridService.getFlow(flowEntry.flowId());
397 if (flowPath == null)
398 return; // Flow Path not found
399
400 //
401 // Remove the Flow if the Flow Entry expired on the first switch
402 //
403 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
404 if (srcDpid.value() != sw.getId())
405 return;
406 deleteFlow(flowPath.flowId());
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700407 }
408
409 /**
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800410 * Inform the Flow Manager that a collection of Flow Entries have been
411 * pushed to a switch.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700412 *
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800413 * @param entries the collection of <IOFSwitch, FlowEntry> pairs
414 * that have been pushed.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700415 */
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800416 @Override
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800417 public void flowEntriesPushedToSwitch(
418 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700419
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800420 //
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800421 // Process all entries
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800422 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800423 // TODO: For now we have to create an explicit FlowEntry copy so
424 // we don't modify the original FlowEntry.
425 // This should go away after we start using the OpenFlow Barrier
426 // mechnanism in the FlowPusher.
427 //
428 Kryo kryo = kryoFactory.newKryo();
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800429 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
Pavlin Radoslavov4535bc12013-12-05 10:43:49 -0800430 FlowEntry flowEntry = entry.second;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800431
432 //
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800433 // Mark the Flow Entry that it has been pushed to the switch
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800434 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800435 FlowEntry copyFlowEntry = kryo.copy(flowEntry);
436 copyFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700437
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800438 //
439 // Write the Flow Entry to the Datagrid
440 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800441 switch (copyFlowEntry.flowEntryUserState()) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800442 case FE_USER_ADD:
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800443 datagridService.notificationSendFlowEntryAdded(copyFlowEntry);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800444 break;
445 case FE_USER_MODIFY:
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800446 datagridService.notificationSendFlowEntryUpdated(copyFlowEntry);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800447 break;
448 case FE_USER_DELETE:
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800449 datagridService.notificationSendFlowEntryRemoved(copyFlowEntry.flowEntryId());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800450 break;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800451 case FE_USER_UNKNOWN:
452 assert(false);
453 break;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800454 }
455 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800456 kryoFactory.deleteKryo(kryo);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800457 }
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700458
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800459 /**
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800460 * Generate a notification that a collection of Flow Paths has been
461 * installed in the network.
462 *
463 * @param flowPaths the collection of installed Flow Paths.
464 */
465 void notificationFlowPathsInstalled(Collection<FlowPath> flowPaths) {
Pavlin Radoslavov63c2d052013-12-18 18:17:55 -0800466 //
467 // TODO: Add an explicit check for null pointer, because
468 // the IForwardingService is optional. Remove the "if" statement
469 // after hte Forwarding Module becomes mandatory.
470 //
471 if (forwardingService != null)
472 forwardingService.flowsInstalled(flowPaths);
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800473 }
474
475 /**
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800476 * Push modified Flow-related state as appropriate.
477 *
478 * @param modifiedFlowPaths the collection of modified Flow Paths.
479 * @param modifiedFlowEntries the collection of modified Flow Entries.
480 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800481 void pushModifiedFlowState(Collection<FlowPath> modifiedFlowPaths,
482 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800483 //
484 // Push the modified Flow state:
485 // - Flow Entries to switches and the datagrid
486 // - Flow Paths to the database
487 //
488 pushModifiedFlowEntriesToSwitches(modifiedFlowEntries);
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800489 pushModifiedFlowPathsToDatabase(modifiedFlowPaths);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800490 cleanupDeletedFlowEntriesFromDatagrid(modifiedFlowEntries);
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800491 }
492
493 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700494 * Push modified Flow Entries to switches.
495 *
496 * NOTE: Only the Flow Entries to switches controlled by this instance
497 * are pushed.
498 *
499 * @param modifiedFlowEntries the collection of modified Flow Entries.
500 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800501 private void pushModifiedFlowEntriesToSwitches(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800502 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700503 if (modifiedFlowEntries.isEmpty())
504 return;
505
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800506 List<Pair<IOFSwitch, FlowEntry>> entries =
507 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
508
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700509 Map<Long, IOFSwitch> mySwitches = getMySwitches();
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700510
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800511 //
512 // Create a collection of my Flow Entries to push
513 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800514 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700515 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
516 if (mySwitch == null)
517 continue;
518
Pavlin Radoslavovd1b728c2013-12-18 21:39:58 -0800519 if (flowEntry.flowEntrySwitchState() ==
520 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
521 //
522 // Don't push again Flow Entries that were already already
523 // installed into the switches.
524 //
525 continue;
526 }
527
Pavlin Radoslavovaca49d12013-12-04 19:49:17 -0800528 //
529 // Assign Flow Entry IDs if missing.
530 //
531 // NOTE: This is an additional safeguard, in case the
532 // mySwitches set has changed (after the Flow Entry IDs
533 // assignments by the caller).
534 //
535 if (! flowEntry.isValidFlowEntryId()) {
536 long id = getNextFlowEntryId();
537 flowEntry.setFlowEntryId(new FlowEntryId(id));
538 }
539
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800540 log.debug("Pushing Flow Entry To Switch: {}", flowEntry);
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800541 entries.add(new Pair<IOFSwitch, FlowEntry>(mySwitch, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700542 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800543
544 pusher.pushFlowEntries(entries);
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700545 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700546
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700547 /**
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800548 * Cleanup deleted Flow Entries from the datagrid.
549 *
550 * NOTE: We cleanup only the Flow Entries that are not for our switches.
551 * This is needed to handle the case a switch going down:
552 * It has no Master controller instance, hence no controller instance
553 * will cleanup its flow entries.
554 * This is sub-optimal: we need to elect a controller instance to handle
555 * the cleanup of such orphaned flow entries.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700556 *
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800557 * @param modifiedFlowEntries the collection of modified Flow Entries.
558 */
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800559 private void cleanupDeletedFlowEntriesFromDatagrid(
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800560 Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800561 if (modifiedFlowEntries.isEmpty())
562 return;
563
564 Map<Long, IOFSwitch> mySwitches = getMySwitches();
565
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800566 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800567 //
568 // Process only Flow Entries that should be deleted and have
569 // a valid Flow Entry ID.
570 //
Pavlin Radoslavov426a8532013-12-02 17:32:21 -0800571 if (! flowEntry.isValidFlowEntryId())
572 continue;
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800573 if (flowEntry.flowEntryUserState() !=
574 FlowEntryUserState.FE_USER_DELETE) {
575 continue;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700576 }
577
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800578 //
579 // NOTE: The deletion of Flow Entries for my switches is handled
580 // elsewhere.
581 //
582 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
583 if (mySwitch != null)
584 continue;
585
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800586 log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800587
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700588 //
589 // Write the Flow Entry to the Datagrid
590 //
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800591 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700592 }
593 }
594
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800595 /**
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800596 * Class to implement writing to the database in a separate thread.
597 */
598 class FlowDatabaseWriter extends Thread {
599 private FlowManager flowManager;
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800600 private BlockingQueue<FlowPath> blockingQueue;
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800601
602 /**
603 * Constructor.
604 *
605 * @param flowManager the Flow Manager to use.
606 * @param blockingQueue the blocking queue to use.
607 */
608 FlowDatabaseWriter(FlowManager flowManager,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800609 BlockingQueue<FlowPath> blockingQueue) {
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800610 this.flowManager = flowManager;
611 this.blockingQueue = blockingQueue;
612 }
613
614 /**
615 * Run the thread.
616 */
617 @Override
618 public void run() {
619 //
620 // The main loop
621 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800622 Collection<FlowPath> collection = new LinkedList<FlowPath>();
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800623 this.setName("FlowDatabaseWriter " + this.getId() );
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800624 try {
625 while (true) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800626 FlowPath flowPath = blockingQueue.take();
627 collection.add(flowPath);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800628 blockingQueue.drainTo(collection);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800629 flowManager.writeModifiedFlowPathsToDatabase(collection);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800630 collection.clear();
631 }
632 } catch (Exception exception) {
633 log.debug("Exception writing to the Database: ", exception);
634 }
635 }
636 }
637
638 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800639 * Push Flow Paths to the Network MAP.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800640 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800641 * NOTE: The complete Flow Paths are pushed only on the instance
642 * responsible for the first switch. This is to avoid database errors
643 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800644 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800645 * @param modifiedFlowPaths the collection of Flow Paths to push.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800646 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800647 private void pushModifiedFlowPathsToDatabase(
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800648 Collection<FlowPath> modifiedFlowPaths) {
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -0800649 List<FlowPath> copiedFlowPaths = new LinkedList<FlowPath>();
650
651 //
652 // Create a copy of the Flow Paths to push, because the pushing
653 // itself will happen on a separate thread.
654 //
655 Kryo kryo = kryoFactory.newKryo();
656 for (FlowPath flowPath : modifiedFlowPaths) {
657 FlowPath copyFlowPath = kryo.copy(flowPath);
658 copiedFlowPaths.add(copyFlowPath);
659 }
660 kryoFactory.deleteKryo(kryo);
661
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800662 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800663 // We only add the Flow Paths to the Database Queue.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800664 // The FlowDatabaseWriter thread is responsible for the actual writing.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800665 //
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -0800666 flowPathsToDatabaseQueue.addAll(copiedFlowPaths);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800667 }
668
669 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800670 * Write Flow Paths to the Network MAP.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800671 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800672 * NOTE: The complete Flow Paths are pushed only on the instance
673 * responsible for the first switch. This is to avoid database errors
674 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800675 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800676 * @param modifiedFlowPaths the collection of Flow Paths to write.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800677 */
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800678 private void writeModifiedFlowPathsToDatabase(
679 Collection<FlowPath> modifiedFlowPaths) {
680 if (modifiedFlowPaths.isEmpty())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800681 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700682
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800683 Map<Long, IOFSwitch> mySwitches = getMySwitches();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700684
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800685 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800686 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800687 // Push the changes only on the instance responsible for the
688 // first switch.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800689 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800690 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
691 IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value());
692 if (mySrcSwitch == null)
693 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800694
695 //
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800696 // Delete the Flow Path from the Network Map
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800697 //
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800698 if (flowPath.flowPathUserState() ==
699 FlowPathUserState.FP_USER_DELETE) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800700 log.debug("Deleting Flow Path From Database: {}", flowPath);
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800701
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800702 boolean retry = false;
703 do {
704 retry = false;
705 try {
706 if (! FlowDatabaseOperation.deleteFlow(
707 dbHandlerInner,
708 flowPath.flowId())) {
709 log.error("Cannot delete Flow Path {} from Network Map",
710 flowPath.flowId());
711 retry = true;
712 }
713 } catch (TitanException te) {
714 log.error("Titan Exception deleting Flow Path from Network MAP: {}", te);
715 retry = true;
716 } catch (Exception e) {
717 log.error("Exception deleting Flow Path from Network MAP: {}", e);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800718 }
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800719 } while (retry);
720
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800721 continue;
722 }
723
724 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800725 // Test whether all Flow Entries are valid
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800726 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800727 boolean allValid = true;
728 for (FlowEntry flowEntry : flowPath.flowEntries()) {
729 if (flowEntry.flowEntryUserState() ==
730 FlowEntryUserState.FE_USER_DELETE) {
731 continue;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700732 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800733 if (! flowEntry.isValidFlowEntryId()) {
734 allValid = false;
735 break;
736 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800737 if (flowEntry.flowEntrySwitchState() !=
738 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
739 allValid = false;
740 break;
741 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800742 }
743 if (! allValid)
744 continue;
745
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800746 log.debug("Pushing Flow Path To Database: {}", flowPath);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800747
748 //
749 // Write the Flow Path to the Network Map
750 //
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800751 boolean retry = false;
752 do {
753 retry = false;
754 try {
Masayoshi Kobayashi178ead12013-12-19 20:50:19 -0800755 long startTime = System.nanoTime();
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800756 if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath)) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800757 log.error("Cannot write to Network Map Flow Path {}", flowPath.flowId());
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800758 retry = true;
759 }
Masayoshi Kobayashi178ead12013-12-19 20:50:19 -0800760 // FIXME Flag to turn ON logging
761 //long endTime = System.nanoTime();
762 //log.error("Performance %% Flow path total time {} : {}", endTime - startTime, flowPath.toString());
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800763 } catch (TitanException te) {
764 log.error("Titan Exception writing Flow Path to Network MAP: ", te);
765 retry = true;
Masayoshi Kobayashi178ead12013-12-19 20:50:19 -0800766 // FIXME Flag to turn ON logging
767 //long endTime = System.nanoTime();
768 //log.error("Performance %% Flow path total time {} : {}", endTime - startTime, flowPath.toString());
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800769 } catch (Exception e) {
770 log.error("Exception writing Flow Path to Network MAP: ", e);
Masayoshi Kobayashi178ead12013-12-19 20:50:19 -0800771 // FIXME Flag to turn ON logging
772 //long endTime = System.nanoTime();
773 //log.error("Performance %% Flow path total time {} : {}", endTime - startTime, flowPath.toString());
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800774 }
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800775 } while (retry);
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700776 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800777 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800778}