blob: 9782a085be1d310cd19ff39a56502698b8ad1f0b [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) {
yoshid38cd312013-12-02 19:54:44 -080089 dbHandlerApi = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
90 dbHandlerInner = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
91
92 //dbHandlerApi = GraphDBManager.getDBOperation(dbStore, conf);
93 //dbHandlerInner = GraphDBManager.getDBOperation(dbStore, conf);
yoshitomob292c622013-11-23 14:35:58 -080094
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080095 }
96
admin944ef4f2013-10-08 17:48:37 -070097 /**
98 * Shutdown the Flow Manager operation.
99 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800100 public void finalize() {
Toshio Koide9fe1cb22013-06-13 13:51:11 -0700101 close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800102 }
103
admin944ef4f2013-10-08 17:48:37 -0700104 /**
105 * Shutdown the Flow Manager operation.
106 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800107 @Override
108 public void close() {
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700109 datagridService.deregisterFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800110 dbHandlerApi.close();
111 dbHandlerInner.close();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800112 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800113
admin944ef4f2013-10-08 17:48:37 -0700114 /**
115 * Get the collection of offered module services.
116 *
117 * @return the collection of offered module services.
118 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800119 @Override
120 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
121 Collection<Class<? extends IFloodlightService>> l =
122 new ArrayList<Class<? extends IFloodlightService>>();
123 l.add(IFlowService.class);
124 return l;
125 }
126
admin944ef4f2013-10-08 17:48:37 -0700127 /**
128 * Get the collection of implemented services.
129 *
130 * @return the collection of implemented services.
131 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800132 @Override
133 public Map<Class<? extends IFloodlightService>, IFloodlightService>
134 getServiceImpls() {
135 Map<Class<? extends IFloodlightService>,
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700136 IFloodlightService> m =
137 new HashMap<Class<? extends IFloodlightService>,
138 IFloodlightService>();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800139 m.put(IFlowService.class, this);
140 return m;
141 }
142
admin944ef4f2013-10-08 17:48:37 -0700143 /**
144 * Get the collection of modules this module depends on.
145 *
146 * @return the collection of modules this module depends on.
147 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800148 @Override
149 public Collection<Class<? extends IFloodlightService>>
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700150 getModuleDependencies() {
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800151 Collection<Class<? extends IFloodlightService>> l =
152 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800153 l.add(IFloodlightProviderService.class);
Pavlin Radoslavove9a3ef92013-10-18 18:46:45 -0700154 l.add(INetworkGraphService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700155 l.add(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800156 l.add(IRestApiService.class);
Pavlin Radoslavovc0862662013-12-10 15:31:49 -0800157 l.add(IFlowPusherService.class);
Pavlin Radoslavov63c2d052013-12-18 18:17:55 -0800158 //
159 // TODO: Comment-out the dependency on the IForwardingService,
160 // because it is an optional module. Apparently, adding the dependency
161 // here automatically enables the module.
162 //
163 // l.add(IForwardingService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800164 return l;
165 }
166
admin944ef4f2013-10-08 17:48:37 -0700167 /**
168 * Initialize the module.
169 *
170 * @param context the module context to use for the initialization.
171 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800172 @Override
173 public void init(FloodlightModuleContext context)
174 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700175 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800176 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700177 datagridService = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800178 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov939ca6b2013-12-03 12:35:37 -0800179 pusher = context.getServiceImpl(IFlowPusherService.class);
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800180 forwardingService = context.getServiceImpl(IForwardingService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800181
yoshi0fee3de2013-11-23 09:13:37 -0800182 this.init("","");
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800183 }
184
admin944ef4f2013-10-08 17:48:37 -0700185 /**
186 * Get the next Flow Entry ID to use.
187 *
188 * @return the next Flow Entry ID to use.
189 */
Naoki Shiota4e77de92013-11-18 17:29:54 -0800190 @Override
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700191 public synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000192 //
193 // Generate the next Flow Entry ID.
194 // NOTE: For now, the higher 32 bits are random, and
195 // the lower 32 bits are sequential.
196 // In the future, we need a better allocation mechanism.
197 //
198 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
199 nextFlowEntryIdPrefix = randomGenerator.nextInt();
200 nextFlowEntryIdSuffix = 0;
201 } else {
202 nextFlowEntryIdSuffix++;
203 }
204 long result = (long)nextFlowEntryIdPrefix << 32;
205 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
206 return result;
207 }
208
admin944ef4f2013-10-08 17:48:37 -0700209 /**
210 * Startup module operation.
211 *
212 * @param context the module context to use for the startup.
213 */
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800214 @Override
215 public void startUp(FloodlightModuleContext context) {
admin944ef4f2013-10-08 17:48:37 -0700216 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov50e532e2013-10-20 02:07:51 -0700217
admin944ef4f2013-10-08 17:48:37 -0700218 // Initialize the Flow Entry ID generator
219 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800220
221 //
222 // The thread to write to the database
223 //
224 flowDatabaseWriter = new FlowDatabaseWriter(this,
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800225 flowPathsToDatabaseQueue);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800226 flowDatabaseWriter.start();
227
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700228 //
Pavlin Radoslavovc9da5322013-11-22 11:59:46 -0800229 // The Flow Event Handler thread:
230 // - create
231 // - register with the Datagrid Service
232 // - startup
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700233 //
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700234 flowEventHandler = new FlowEventHandler(this, datagridService);
235 datagridService.registerFlowEventHandlerService(flowEventHandler);
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700236 flowEventHandler.start();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800237 }
238
239 /**
240 * Add a flow.
241 *
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800242 * @param flowPath the Flow Path to install.
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800243 * @return the Flow ID on success, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800244 */
245 @Override
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800246 public FlowId addFlow(FlowPath flowPath) {
247
248 // Allocate the Flow ID if necessary
Pavlin Radoslavov892dd182013-12-05 23:33:15 -0800249 if (! flowPath.isValidFlowId()) {
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800250 long id = getNextFlowEntryId();
251 flowPath.setFlowId(new FlowId(id));
252 }
253
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700254 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700255 // NOTE: We need to explicitly initialize some of the state,
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700256 // in case the application didn't do it.
257 //
258 for (FlowEntry flowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov07d22b42013-12-15 16:33:33 -0800259 // The Flow Entry switch state
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700260 if (flowEntry.flowEntrySwitchState() ==
261 FlowEntrySwitchState.FE_SWITCH_UNKNOWN) {
262 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800263 }
Pavlin Radoslavov07d22b42013-12-15 16:33:33 -0800264 // The Flow Entry ID
265 if (! flowEntry.isValidFlowEntryId()) {
266 long id = getNextFlowEntryId();
267 flowEntry.setFlowEntryId(new FlowEntryId(id));
268 }
269 // The Flow ID
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700270 if (! flowEntry.isValidFlowId())
271 flowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700272 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800273
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800274 if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700275 datagridService.notificationSendFlowAdded(flowPath);
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800276 return flowPath.flowId();
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700277 }
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800278 return null;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700279 }
280
281 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000282 * Delete all previously added flows.
283 *
284 * @return true on success, otherwise false.
285 */
286 @Override
287 public boolean deleteAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800288 if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700289 datagridService.notificationSendAllFlowsRemoved();
290 return true;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000291 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700292 return false;
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000293 }
294
295 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800296 * Delete a previously added flow.
297 *
298 * @param flowId the Flow ID of the flow to delete.
299 * @return true on success, otherwise false.
300 */
301 @Override
302 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800303 if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) {
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700304 datagridService.notificationSendFlowRemoved(flowId);
305 return true;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700306 }
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700307 return false;
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700308 }
309
310 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800311 * Get a previously added flow.
312 *
313 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800314 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800315 */
316 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800317 public FlowPath getFlow(FlowId flowId) {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800318 return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700319 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800320
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700321 /**
322 * Get all installed flows by all installers.
323 *
324 * @return the Flow Paths if found, otherwise null.
325 */
326 @Override
327 public ArrayList<FlowPath> getAllFlows() {
Pavlin Radoslavovbc96ae12013-11-05 08:44:02 -0800328 return FlowDatabaseOperation.getAllFlows(dbHandlerApi);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800329 }
330
331 /**
admin944ef4f2013-10-08 17:48:37 -0700332 * Get summary of all installed flows by all installers in a given range.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700333 *
admin944ef4f2013-10-08 17:48:37 -0700334 * @param flowId the Flow ID of the first flow in the flow range to get.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700335 * @param maxFlows the maximum number of flows to be returned.
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700336 * @return the Flow Paths if found, otherwise null.
337 */
338 @Override
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800339 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId,
340 int maxFlows) {
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800341 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
342 SortedMap<Long, FlowPath> sortedFlowPaths =
343 flowEventHandler.getAllFlowPathsCopy();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700344
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800345 //
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800346 // Truncate each Flow Path and Flow Entry
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800347 //
348 for (FlowPath flowPath : sortedFlowPaths.values()) {
349 //
350 // TODO: Add only the Flow Paths that have been successfully
351 // installed.
352 //
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800353 flowPath.setFlowEntryMatch(null);
354 flowPath.setFlowEntryActions(null);
355 for (FlowEntry flowEntry : flowPath.flowEntries()) {
356 flowEntry.setFlowEntryMatch(null);
357 flowEntry.setFlowEntryActions(null);
358 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800359 flowPaths.add(flowPath);
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800360 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700361
Pavlin Radoslavove79c19a2013-12-06 09:28:15 -0800362 return flowPaths;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700363 }
364
365 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800366 * Get the collection of my switches.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700367 *
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800368 * @return the collection of my switches.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700369 */
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800370 public Map<Long, IOFSwitch> getMySwitches() {
371 return floodlightProvider.getSwitches();
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700372 }
373
374 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800375 * Get the network topology.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700376 *
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800377 * @return the network topology.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700378 */
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800379 public Topology getTopology() {
380 return flowEventHandler.getTopology();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700381 }
382
383 /**
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800384 * Inform the Flow Manager that a Flow Entry on switch expired.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700385 *
Pavlin Radoslavov3bd5ccf2013-11-26 15:10:21 -0800386 * @param sw the switch the Flow Entry expired on.
Pavlin Radoslavov4df85ae2013-11-26 14:48:32 -0800387 * @param flowEntryId the Flow Entry ID of the expired Flow Entry.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700388 */
Pavlin Radoslavovc5718e72013-12-10 15:47:10 -0800389 public void flowEntryOnSwitchExpired(IOFSwitch sw,
390 FlowEntryId flowEntryId) {
391 // Find the Flow Entry
392 FlowEntry flowEntry = datagridService.getFlowEntry(flowEntryId);
yoshia97632b2013-12-17 15:46:08 -0800393 if (flowEntry == null)
Pavlin Radoslavovc5718e72013-12-10 15:47:10 -0800394 return; // Flow Entry not found
395
396 // Find the Flow Path
397 FlowPath flowPath = datagridService.getFlow(flowEntry.flowId());
398 if (flowPath == null)
399 return; // Flow Path not found
400
401 //
402 // Remove the Flow if the Flow Entry expired on the first switch
403 //
404 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
405 if (srcDpid.value() != sw.getId())
406 return;
407 deleteFlow(flowPath.flowId());
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700408 }
409
410 /**
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800411 * Inform the Flow Manager that a collection of Flow Entries have been
412 * pushed to a switch.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700413 *
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800414 * @param entries the collection of <IOFSwitch, FlowEntry> pairs
415 * that have been pushed.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700416 */
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
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700540 log.debug("Pushing Flow Entry To Switch: {}", flowEntry.toString());
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
586 log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry.toString());
587
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>();
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800623 try {
624 while (true) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800625 FlowPath flowPath = blockingQueue.take();
626 collection.add(flowPath);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800627 blockingQueue.drainTo(collection);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800628 flowManager.writeModifiedFlowPathsToDatabase(collection);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800629 collection.clear();
630 }
631 } catch (Exception exception) {
632 log.debug("Exception writing to the Database: ", exception);
633 }
634 }
635 }
636
637 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800638 * Push Flow Paths to the Network MAP.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800639 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800640 * NOTE: The complete Flow Paths are pushed only on the instance
641 * responsible for the first switch. This is to avoid database errors
642 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800643 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800644 * @param modifiedFlowPaths the collection of Flow Paths to push.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800645 */
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800646 private void pushModifiedFlowPathsToDatabase(
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800647 Collection<FlowPath> modifiedFlowPaths) {
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -0800648 List<FlowPath> copiedFlowPaths = new LinkedList<FlowPath>();
649
650 //
651 // Create a copy of the Flow Paths to push, because the pushing
652 // itself will happen on a separate thread.
653 //
654 Kryo kryo = kryoFactory.newKryo();
655 for (FlowPath flowPath : modifiedFlowPaths) {
656 FlowPath copyFlowPath = kryo.copy(flowPath);
657 copiedFlowPaths.add(copyFlowPath);
658 }
659 kryoFactory.deleteKryo(kryo);
660
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800661 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800662 // We only add the Flow Paths to the Database Queue.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800663 // The FlowDatabaseWriter thread is responsible for the actual writing.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800664 //
Pavlin Radoslavovda8d7232013-12-12 12:48:14 -0800665 flowPathsToDatabaseQueue.addAll(copiedFlowPaths);
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800666 }
667
668 /**
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800669 * Write Flow Paths to the Network MAP.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800670 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800671 * NOTE: The complete Flow Paths are pushed only on the instance
672 * responsible for the first switch. This is to avoid database errors
673 * when multiple instances are writing Flow Entries for the same Flow Path.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800674 *
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800675 * @param modifiedFlowPaths the collection of Flow Paths to write.
Pavlin Radoslavov584bd112013-11-21 20:59:33 -0800676 */
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800677 private void writeModifiedFlowPathsToDatabase(
678 Collection<FlowPath> modifiedFlowPaths) {
679 if (modifiedFlowPaths.isEmpty())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800680 return;
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700681
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800682 Map<Long, IOFSwitch> mySwitches = getMySwitches();
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700683
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800684 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800685 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800686 // Push the changes only on the instance responsible for the
687 // first switch.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800688 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800689 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
690 IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value());
691 if (mySrcSwitch == null)
692 continue;
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800693
694 //
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800695 // Delete the Flow Path from the Network Map
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800696 //
Pavlin Radoslavovef545052013-12-05 15:17:25 -0800697 if (flowPath.flowPathUserState() ==
698 FlowPathUserState.FP_USER_DELETE) {
699 log.debug("Deleting Flow Path From Database: {}",
700 flowPath.toString());
701
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);
718 }
719 } 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
746 log.debug("Pushing Flow Path To Database: {}", flowPath.toString());
747
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 {
755 if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath)) {
756 String logMsg = "Cannot write to Network Map Flow Path " +
757 flowPath.flowId();
758 log.error(logMsg);
759 retry = true;
760 }
761 } catch (TitanException te) {
762 log.error("Titan Exception writing Flow Path to Network MAP: ", te);
763 retry = true;
764 } catch (Exception e) {
765 log.error("Exception writing Flow Path to Network MAP: ", e);
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800766 }
Pavlin Radoslavov262e6832013-12-18 14:37:35 -0800767 } while (retry);
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700768 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800769 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800770}