blob: 415b1b19e3665f497e5ab65ff4d2a4b101a05f00 [file] [log] [blame]
Brian O'Connor8c166a72013-11-14 18:41:48 -08001package net.onrc.onos.ofcontroller.flowprogrammer;
Brian O'Connora8e49802013-10-30 20:49:59 -07002
3import java.io.IOException;
4import java.util.ArrayList;
Brian O'Connora8e49802013-10-30 20:49:59 -07005import java.util.HashMap;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Map;
9import java.util.Set;
Naoki Shiota2bdda572013-12-09 15:05:21 -080010import java.util.concurrent.Callable;
Brian O'Connora8e49802013-10-30 20:49:59 -070011import java.util.concurrent.ExecutionException;
Brian O'Connora8e49802013-10-30 20:49:59 -070012import java.util.concurrent.Future;
Naoki Shiota2bdda572013-12-09 15:05:21 -080013import java.util.concurrent.FutureTask;
Brian O'Connora8e49802013-10-30 20:49:59 -070014
Brian O'Connor0d6ba512013-11-05 15:17:44 -080015import org.openflow.protocol.OFFlowMod;
Brian O'Connora8e49802013-10-30 20:49:59 -070016import org.openflow.protocol.OFMatch;
Brian O'Connor0d6ba512013-11-05 15:17:44 -080017import org.openflow.protocol.OFPort;
Brian O'Connora8e49802013-10-30 20:49:59 -070018import org.openflow.protocol.OFStatisticsRequest;
19import org.openflow.protocol.statistics.OFFlowStatisticsReply;
20import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
21import org.openflow.protocol.statistics.OFStatistics;
22import org.openflow.protocol.statistics.OFStatisticsType;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Brian O'Connora8e49802013-10-30 20:49:59 -070026import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connora8e49802013-10-30 20:49:59 -070027import net.onrc.onos.graph.GraphDBOperation;
28import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
29import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -080030import net.onrc.onos.ofcontroller.flowmanager.FlowDatabaseOperation;
Brian O'Connora8e49802013-10-30 20:49:59 -070031import net.onrc.onos.ofcontroller.util.Dpid;
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -080032import net.onrc.onos.ofcontroller.util.FlowEntry;
Brian O'Connora8e49802013-10-30 20:49:59 -070033import net.onrc.onos.ofcontroller.util.FlowEntryId;
34
Naoki Shiotae3199732013-11-25 16:14:43 -080035/**
Naoki Shiotab485d412013-11-26 12:04:19 -080036 * FlowSynchronizer is an implementation of FlowSyncService.
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -080037 * In addition to IFlowSyncService, FlowSynchronizer periodically reads flow
38 * tables from switches and compare them with GraphDB to drop unnecessary
39 * flows and/or to install missing flows.
Naoki Shiotae3199732013-11-25 16:14:43 -080040 * @author Brian
41 *
42 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080043public class FlowSynchronizer implements IFlowSyncService {
Brian O'Connora8e49802013-10-30 20:49:59 -070044
Brian O'Connorea1efbe2013-11-25 22:57:43 -080045 private static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class);
Brian O'Connore46492e2013-11-14 21:11:50 -080046
Brian O'Connor8c166a72013-11-14 18:41:48 -080047 private GraphDBOperation dbHandler;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080048 protected IFlowPusherService pusher;
Naoki Shiota2bdda572013-12-09 15:05:21 -080049 private Map<IOFSwitch, FutureTask<SyncResult>> switchThreads;
Brian O'Connore46492e2013-11-14 21:11:50 -080050
51 public FlowSynchronizer() {
52 dbHandler = new GraphDBOperation("");
Naoki Shiota2bdda572013-12-09 15:05:21 -080053 switchThreads = new HashMap<IOFSwitch, FutureTask<SyncResult>>();
Brian O'Connore46492e2013-11-14 21:11:50 -080054 }
55
Brian O'Connorea1efbe2013-11-25 22:57:43 -080056 @Override
Naoki Shiota2bdda572013-12-09 15:05:21 -080057 public Future<SyncResult> synchronize(IOFSwitch sw) {
Pavlin Radoslavovf8c78552013-11-26 10:56:59 -080058 Synchronizer sync = new Synchronizer(sw);
Naoki Shiota2bdda572013-12-09 15:05:21 -080059 FutureTask<SyncResult> task = new FutureTask<SyncResult>(sync);
60 switchThreads.put(sw, task);
61 task.run();
62 return task;
Brian O'Connore46492e2013-11-14 21:11:50 -080063 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -080064
Brian O'Connore46492e2013-11-14 21:11:50 -080065 @Override
Brian O'Connorea1efbe2013-11-25 22:57:43 -080066 public void interrupt(IOFSwitch sw) {
Naoki Shiota2bdda572013-12-09 15:05:21 -080067 FutureTask<SyncResult> t = switchThreads.remove(sw);
Brian O'Connore46492e2013-11-14 21:11:50 -080068 if(t != null) {
Naoki Shiota2bdda572013-12-09 15:05:21 -080069 t.cancel(true);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080070 }
Brian O'Connore46492e2013-11-14 21:11:50 -080071 }
72
Naoki Shiotab485d412013-11-26 12:04:19 -080073 /**
74 * Initialize Synchronizer.
75 * @param pusherService FlowPusherService used for sending messages.
76 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080077 public void init(IFlowPusherService pusherService) {
78 pusher = pusherService;
Brian O'Connore46492e2013-11-14 21:11:50 -080079 }
80
Naoki Shiotae3199732013-11-25 16:14:43 -080081 /**
82 * Synchronizer represents main thread of synchronization.
83 * @author Brian
84 *
85 */
Naoki Shiota2bdda572013-12-09 15:05:21 -080086 protected class Synchronizer implements Callable<SyncResult> {
Brian O'Connora8e49802013-10-30 20:49:59 -070087 IOFSwitch sw;
88 ISwitchObject swObj;
Brian O'Connore46492e2013-11-14 21:11:50 -080089
Pavlin Radoslavovf8c78552013-11-26 10:56:59 -080090 public Synchronizer(IOFSwitch sw) {
Brian O'Connora8e49802013-10-30 20:49:59 -070091 this.sw = sw;
92 Dpid dpid = new Dpid(sw.getId());
93 this.swObj = dbHandler.searchSwitch(dpid.toString());
94 }
Brian O'Connore46492e2013-11-14 21:11:50 -080095
Brian O'Connora8e49802013-10-30 20:49:59 -070096 @Override
Naoki Shiota2bdda572013-12-09 15:05:21 -080097 public SyncResult call() {
Brian O'Connorea1efbe2013-11-25 22:57:43 -080098 // TODO: stop adding other flow entries while synchronizing
99 //pusher.suspend(sw);
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800100 Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph();
101 Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch();
Naoki Shiota2bdda572013-12-09 15:05:21 -0800102 SyncResult result = compare(graphEntries, switchEntries);
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800103 //pusher.resume(sw);
Naoki Shiota2bdda572013-12-09 15:05:21 -0800104
105 return result;
Brian O'Connora8e49802013-10-30 20:49:59 -0700106 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800107
Naoki Shiotae3199732013-11-25 16:14:43 -0800108 /**
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800109 * Compare flows entries in GraphDB and switch to pick up necessary
110 * messages.
Naoki Shiotae3199732013-11-25 16:14:43 -0800111 * After picking up, picked messages are added to FlowPusher.
112 * @param graphEntries Flow entries in GraphDB.
113 * @param switchEntries Flow entries in switch.
114 */
Naoki Shiota2bdda572013-12-09 15:05:21 -0800115 private SyncResult compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) {
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800116 int added = 0, removed = 0, skipped = 0;
117 for(FlowEntryWrapper entry : switchEntries) {
Brian O'Connore46492e2013-11-14 21:11:50 -0800118 if(graphEntries.contains(entry)) {
119 graphEntries.remove(entry);
120 skipped++;
121 }
122 else {
123 // remove flow entry from the switch
124 entry.removeFromSwitch(sw);
125 removed++;
126 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700127 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800128 for(FlowEntryWrapper entry : graphEntries) {
Brian O'Connore46492e2013-11-14 21:11:50 -0800129 // add flow entry to switch
130 entry.addToSwitch(sw);
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800131 added++;
132 }
133 log.debug("Flow entries added "+ added + ", " +
134 "Flow entries removed "+ removed + ", " +
135 "Flow entries skipped " + skipped);
Naoki Shiota2bdda572013-12-09 15:05:21 -0800136
137 return new SyncResult(added, removed, skipped);
Brian O'Connora8e49802013-10-30 20:49:59 -0700138 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800139
Naoki Shiotae3199732013-11-25 16:14:43 -0800140 /**
141 * Read GraphDB to get FlowEntries associated with a switch.
142 * @return set of FlowEntries
143 */
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800144 private Set<FlowEntryWrapper> getFlowEntriesFromGraph() {
145 Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>();
Brian O'Connora8e49802013-10-30 20:49:59 -0700146 for(IFlowEntry entry : swObj.getFlowEntries()) {
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800147 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
148 entries.add(fe);
Brian O'Connora8e49802013-10-30 20:49:59 -0700149 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800150 return entries;
Brian O'Connora8e49802013-10-30 20:49:59 -0700151 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800152
Naoki Shiotae3199732013-11-25 16:14:43 -0800153 /**
154 * Read flow table from switch and derive FlowEntries from table.
155 * @return set of FlowEntries
156 */
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800157 private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() {
Brian O'Connora8e49802013-10-30 20:49:59 -0700158
159 int lengthU = 0;
160 OFMatch match = new OFMatch();
161 match.setWildcards(OFMatch.OFPFW_ALL);
162
163 OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest();
164 stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE
165 stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES)
166 stat.setMatch(match);
167 List<OFStatistics> stats = new ArrayList<OFStatistics>();
168 stats.add(stat);
169 lengthU += stat.getLength();
170
171 OFStatisticsRequest req = new OFStatisticsRequest();
172 req.setStatisticType(OFStatisticsType.FLOW);
173 req.setStatistics(stats);
174 lengthU += req.getLengthU();
175 req.setLengthU(lengthU);
176
177 List<OFStatistics> entries = null;
178 try {
179 Future<List<OFStatistics>> dfuture = sw.getStatistics(req);
180 entries = dfuture.get();
181 } catch (IOException e) {
182 // TODO Auto-generated catch block
183 e.printStackTrace();
184 } catch (InterruptedException e) {
185 // TODO Auto-generated catch block
186 e.printStackTrace();
187 } catch (ExecutionException e) {
188 // TODO Auto-generated catch block
189 e.printStackTrace();
190 }
Brian O'Connore46492e2013-11-14 21:11:50 -0800191
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800192 Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>();
Brian O'Connora8e49802013-10-30 20:49:59 -0700193 for(OFStatistics result : entries){
Brian O'Connora8e49802013-10-30 20:49:59 -0700194 OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result;
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800195 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
196 results.add(fe);
Brian O'Connora8e49802013-10-30 20:49:59 -0700197 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800198 return results;
Brian O'Connora8e49802013-10-30 20:49:59 -0700199 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800200
Brian O'Connora8e49802013-10-30 20:49:59 -0700201 }
202
Naoki Shiotae3199732013-11-25 16:14:43 -0800203 /**
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800204 * FlowEntryWrapper represents abstract FlowEntry which is embodied
205 * by FlowEntryId (from GraphDB) or OFFlowStatisticsReply (from switch).
Naoki Shiotae3199732013-11-25 16:14:43 -0800206 * @author Brian
207 *
208 */
Brian O'Connore46492e2013-11-14 21:11:50 -0800209 class FlowEntryWrapper {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800210 FlowEntryId flowEntryId;
Brian O'Connore46492e2013-11-14 21:11:50 -0800211 OFFlowStatisticsReply statisticsReply;
212
213 public FlowEntryWrapper(IFlowEntry entry) {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800214 flowEntryId = new FlowEntryId(entry.getFlowEntryId());
Brian O'Connora8e49802013-10-30 20:49:59 -0700215 }
216
Brian O'Connore46492e2013-11-14 21:11:50 -0800217 public FlowEntryWrapper(OFFlowStatisticsReply entry) {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800218 flowEntryId = new FlowEntryId(entry.getCookie());
Brian O'Connore46492e2013-11-14 21:11:50 -0800219 statisticsReply = entry;
Brian O'Connore46492e2013-11-14 21:11:50 -0800220 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700221
Naoki Shiotae3199732013-11-25 16:14:43 -0800222 /**
223 * Install this FlowEntry to a switch via FlowPusher.
Naoki Shiotab485d412013-11-26 12:04:19 -0800224 * @param sw Switch to which flow will be installed.
Naoki Shiotae3199732013-11-25 16:14:43 -0800225 */
Brian O'Connore46492e2013-11-14 21:11:50 -0800226 public void addToSwitch(IOFSwitch sw) {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800227 if (statisticsReply != null) {
228 log.error("Error adding existing flow entry {} to sw {}",
Brian O'Connore46492e2013-11-14 21:11:50 -0800229 statisticsReply.getCookie(), sw.getId());
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800230 return;
Brian O'Connore46492e2013-11-14 21:11:50 -0800231 }
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800232
233 // Get the Flow Entry state from the Network Graph
234 IFlowEntry iFlowEntry = null;
235 try {
236 iFlowEntry = dbHandler.searchFlowEntry(flowEntryId);
237 } catch (Exception e) {
238 log.error("Error finding flow entry {} in Network Graph",
239 flowEntryId);
240 return;
241 }
242 if (iFlowEntry == null) {
243 log.error("Cannot add flow entry {} to sw {} : flow entry not found",
244 flowEntryId, sw.getId());
245 return;
246 }
247
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -0800248 FlowEntry flowEntry =
249 FlowDatabaseOperation.extractFlowEntry(iFlowEntry);
250 if (flowEntry == null) {
251 log.error("Cannot add flow entry {} to sw {} : flow entry cannot be extracted",
252 flowEntryId, sw.getId());
253 return;
254 }
255
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800256 pusher.pushFlowEntry(sw, flowEntry);
Brian O'Connore46492e2013-11-14 21:11:50 -0800257 }
258
Naoki Shiotae3199732013-11-25 16:14:43 -0800259 /**
260 * Remove this FlowEntry from a switch via FlowPusher.
Naoki Shiotab485d412013-11-26 12:04:19 -0800261 * @param sw Switch from which flow will be removed.
Naoki Shiotae3199732013-11-25 16:14:43 -0800262 */
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800263 public void removeFromSwitch(IOFSwitch sw) {
264 if (statisticsReply == null) {
265 log.error("Error removing non-existent flow entry {} from sw {}",
266 flowEntryId, sw.getId());
267 return;
268 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700269
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800270 // Convert Statistics Reply to Flow Mod, then write it
271 OFFlowMod fm = new OFFlowMod();
272 fm.setCookie(statisticsReply.getCookie());
273 fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT);
274 fm.setLengthU(OFFlowMod.MINIMUM_LENGTH);
275 fm.setMatch(statisticsReply.getMatch());
276 fm.setPriority(statisticsReply.getPriority());
277 fm.setOutPort(OFPort.OFPP_NONE);
278
279 pusher.add(sw, fm);
Brian O'Connore46492e2013-11-14 21:11:50 -0800280 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700281
Brian O'Connore46492e2013-11-14 21:11:50 -0800282 /**
283 * Return the hash code of the Flow Entry ID
284 */
285 @Override
286 public int hashCode() {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800287 return flowEntryId.hashCode();
Brian O'Connore46492e2013-11-14 21:11:50 -0800288 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700289
Brian O'Connore46492e2013-11-14 21:11:50 -0800290 /**
291 * Returns true of the object is another Flow Entry ID with
292 * the same value; otherwise, returns false.
293 *
294 * @param Object to compare
Naoki Shiotab485d412013-11-26 12:04:19 -0800295 * @return true if the object has the same Flow Entry ID.
Brian O'Connore46492e2013-11-14 21:11:50 -0800296 */
297 @Override
298 public boolean equals(Object obj){
299 if(obj.getClass() == this.getClass()) {
300 FlowEntryWrapper entry = (FlowEntryWrapper) obj;
301 // TODO: we need to actually compare the match + actions
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800302 return this.flowEntryId.equals(entry.flowEntryId);
Brian O'Connore46492e2013-11-14 21:11:50 -0800303 }
304 return false;
305 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800306
Brian O'Connore46492e2013-11-14 21:11:50 -0800307 @Override
308 public String toString() {
Pavlin Radoslavov07fb9972013-12-02 16:20:24 -0800309 return flowEntryId.toString();
Brian O'Connore46492e2013-11-14 21:11:50 -0800310 }
311 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800312}