Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowprogrammer; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
Naoki Shiota | 2bdda57 | 2013-12-09 15:05:21 -0800 | [diff] [blame] | 10 | import java.util.concurrent.Callable; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 11 | import java.util.concurrent.ExecutionException; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 12 | import java.util.concurrent.Future; |
Naoki Shiota | 2bdda57 | 2013-12-09 15:05:21 -0800 | [diff] [blame] | 13 | import java.util.concurrent.FutureTask; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 14 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 15 | import net.floodlightcontroller.core.IOFSwitch; |
| 16 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService.MsgPriority; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.util.FlowEntryId; |
| 18 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 19 | import org.openflow.protocol.OFFlowMod; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 20 | import org.openflow.protocol.OFMatch; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 21 | import org.openflow.protocol.OFPort; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 22 | import org.openflow.protocol.OFStatisticsRequest; |
| 23 | import org.openflow.protocol.statistics.OFFlowStatisticsReply; |
| 24 | import org.openflow.protocol.statistics.OFFlowStatisticsRequest; |
| 25 | import org.openflow.protocol.statistics.OFStatistics; |
| 26 | import org.openflow.protocol.statistics.OFStatisticsType; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 30 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 31 | * FlowSynchronizer is an implementation of FlowSyncService. |
Pavlin Radoslavov | 07fb997 | 2013-12-02 16:20:24 -0800 | [diff] [blame] | 32 | * In addition to IFlowSyncService, FlowSynchronizer periodically reads flow |
| 33 | * tables from switches and compare them with GraphDB to drop unnecessary |
| 34 | * flows and/or to install missing flows. |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 35 | */ |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 36 | public class FlowSynchronizer implements IFlowSyncService { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 37 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 38 | private static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 39 | |
Pavlin Radoslavov | 4cf8ee5 | 2014-03-26 18:19:58 -0700 | [diff] [blame] | 40 | // TODO: fix when FlowSynchronizer is refactored |
Pavlin Radoslavov | 697f698 | 2014-03-26 15:55:19 -0700 | [diff] [blame] | 41 | // private DBOperation dbHandler; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 42 | protected IFlowPusherService pusher; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 43 | private Map<IOFSwitch, FutureTask<SyncResult>> switchThreads; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 44 | |
| 45 | public FlowSynchronizer() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 46 | // TODO: fix when FlowSynchronizer is refactored |
| 47 | // dbHandler = GraphDBManager.getDBOperation(); |
| 48 | switchThreads = new HashMap<IOFSwitch, FutureTask<SyncResult>>(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 51 | @Override |
Naoki Shiota | 2bdda57 | 2013-12-09 15:05:21 -0800 | [diff] [blame] | 52 | public Future<SyncResult> synchronize(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 53 | Synchronizer sync = new Synchronizer(sw); |
| 54 | FutureTask<SyncResult> task = new FutureTask<SyncResult>(sync); |
| 55 | switchThreads.put(sw, task); |
| 56 | task.run(); |
| 57 | return task; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 58 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 59 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 60 | @Override |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 61 | public void interrupt(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 62 | FutureTask<SyncResult> t = switchThreads.remove(sw); |
| 63 | if (t != null) { |
| 64 | t.cancel(true); |
| 65 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Initialize Synchronizer. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 70 | * |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 71 | * @param pusherService FlowPusherService used for sending messages. |
| 72 | */ |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 73 | public void init(IFlowPusherService pusherService) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 74 | pusher = pusherService; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Synchronizer represents main thread of synchronization. |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 79 | */ |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 80 | protected class Synchronizer implements Callable<SyncResult> { |
| 81 | IOFSwitch sw; |
| 82 | // TODO: fix when FlowSynchronizer is refactored |
| 83 | // ISwitchObject swObj; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 85 | public Synchronizer(IOFSwitch sw) { |
| 86 | this.sw = sw; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 87 | // TODO: fix when FlowSynchronizer is refactored |
Pavlin Radoslavov | 3255ae5 | 2014-04-09 13:01:54 -0700 | [diff] [blame] | 88 | // Dpid dpid = new Dpid(sw.getId()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 89 | // this.swObj = dbHandler.searchSwitch(dpid.toString()); |
| 90 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 91 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 92 | double graphIDTime, switchTime, compareTime, graphEntryTime, extractTime, pushTime, totalTime; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 93 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public SyncResult call() { |
| 96 | pusher.suspend(sw); |
| 97 | try { |
| 98 | long start = System.nanoTime(); |
| 99 | Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph(); |
| 100 | long step1 = System.nanoTime(); |
| 101 | Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch(); |
| 102 | if (switchEntries == null) { |
| 103 | log.debug("getFlowEntriesFromSwitch() failed"); |
| 104 | return null; |
| 105 | } |
| 106 | long step2 = System.nanoTime(); |
| 107 | SyncResult result = compare(graphEntries, switchEntries); |
| 108 | long step3 = System.nanoTime(); |
| 109 | graphIDTime = (step1 - start); |
| 110 | switchTime = (step2 - step1); |
| 111 | compareTime = (step3 - step2); |
| 112 | totalTime = (step3 - start); |
| 113 | outputTime(); |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 114 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 115 | return result; |
| 116 | } finally { |
| 117 | pusher.resume(sw); |
| 118 | } |
| 119 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 120 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 121 | private void outputTime() { |
| 122 | double div = Math.pow(10, 6); //convert nanoseconds to ms |
| 123 | graphIDTime /= div; |
| 124 | switchTime /= div; |
| 125 | compareTime = (compareTime - graphEntryTime - extractTime - pushTime) / div; |
| 126 | graphEntryTime /= div; |
| 127 | extractTime /= div; |
| 128 | pushTime /= div; |
| 129 | totalTime /= div; |
| 130 | log.debug("Sync time (ms):{},{},{},{},{},{},{}" |
| 131 | , graphIDTime |
| 132 | , switchTime |
| 133 | , compareTime |
| 134 | , graphEntryTime |
| 135 | , extractTime |
| 136 | , pushTime |
| 137 | , totalTime); |
| 138 | } |
Pavlin Radoslavov | 4cf8ee5 | 2014-03-26 18:19:58 -0700 | [diff] [blame] | 139 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Compare flows entries in GraphDB and switch to pick up necessary |
| 142 | * messages. |
| 143 | * After picking up, picked messages are added to FlowPusher. |
| 144 | * |
| 145 | * @param graphEntries Flow entries in GraphDB. |
| 146 | * @param switchEntries Flow entries in switch. |
| 147 | */ |
| 148 | private SyncResult compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) { |
| 149 | int added = 0, removed = 0, skipped = 0; |
| 150 | for (FlowEntryWrapper entry : switchEntries) { |
| 151 | if (graphEntries.contains(entry)) { |
| 152 | graphEntries.remove(entry); |
| 153 | skipped++; |
| 154 | } else { |
| 155 | // remove flow entry from the switch |
| 156 | entry.removeFromSwitch(sw); |
| 157 | removed++; |
| 158 | } |
| 159 | } |
| 160 | for (FlowEntryWrapper entry : graphEntries) { |
| 161 | // add flow entry to switch |
| 162 | entry.addToSwitch(sw); |
| 163 | graphEntryTime += entry.dbTime; |
| 164 | extractTime += entry.extractTime; |
| 165 | pushTime += entry.pushTime; |
| 166 | added++; |
| 167 | } |
| 168 | log.debug("Flow entries added {}, " + |
| 169 | "Flow entries removed {}, " + |
| 170 | "Flow entries skipped {}" |
| 171 | , added |
| 172 | , removed |
| 173 | , skipped); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 174 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 175 | return new SyncResult(added, removed, skipped); |
| 176 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 177 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Read GraphDB to get FlowEntries associated with a switch. |
| 180 | * |
| 181 | * @return set of FlowEntries |
| 182 | */ |
| 183 | private Set<FlowEntryWrapper> getFlowEntriesFromGraph() { |
| 184 | Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 185 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 186 | // TODO: fix when FlowSynchronizer is refactored |
| 187 | /* |
| 188 | for(IFlowEntry entry : swObj.getFlowEntries()) { |
| 189 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 190 | entries.add(fe); |
| 191 | } |
| 192 | */ |
| 193 | return entries; |
| 194 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 195 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Read flow table from switch and derive FlowEntries from table. |
| 198 | * |
| 199 | * @return set of FlowEntries |
| 200 | */ |
| 201 | private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 202 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 203 | int lengthU = 0; |
| 204 | OFMatch match = new OFMatch(); |
| 205 | match.setWildcards(OFMatch.OFPFW_ALL); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 206 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 207 | OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest(); |
| 208 | stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE |
| 209 | stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES) |
| 210 | stat.setMatch(match); |
| 211 | List<OFStatistics> stats = new ArrayList<OFStatistics>(); |
| 212 | stats.add(stat); |
| 213 | lengthU += stat.getLength(); |
| 214 | |
| 215 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 216 | req.setStatisticType(OFStatisticsType.FLOW); |
| 217 | req.setStatistics(stats); |
| 218 | lengthU += req.getLengthU(); |
| 219 | req.setLengthU(lengthU); |
| 220 | |
| 221 | List<OFStatistics> entries = null; |
| 222 | try { |
| 223 | Future<List<OFStatistics>> dfuture = sw.getStatistics(req); |
| 224 | entries = dfuture.get(); |
| 225 | } catch (IOException e) { |
| 226 | // TODO Auto-generated catch block |
| 227 | e.printStackTrace(); |
| 228 | return null; |
| 229 | } catch (InterruptedException e) { |
| 230 | // TODO Auto-generated catch block |
| 231 | e.printStackTrace(); |
| 232 | return null; |
| 233 | } catch (ExecutionException e) { |
| 234 | // TODO Auto-generated catch block |
| 235 | e.printStackTrace(); |
| 236 | return null; |
| 237 | } |
| 238 | |
| 239 | Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>(); |
| 240 | for (OFStatistics result : entries) { |
| 241 | OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result; |
| 242 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 243 | results.add(fe); |
| 244 | } |
| 245 | return results; |
| 246 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 247 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 250 | /** |
Pavlin Radoslavov | 07fb997 | 2013-12-02 16:20:24 -0800 | [diff] [blame] | 251 | * FlowEntryWrapper represents abstract FlowEntry which is embodied |
| 252 | * by FlowEntryId (from GraphDB) or OFFlowStatisticsReply (from switch). |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 253 | */ |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 254 | class FlowEntryWrapper { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 255 | FlowEntryId flowEntryId; |
| 256 | // TODO: fix when FlowSynchronizer is refactored |
| 257 | // IFlowEntry iFlowEntry; |
| 258 | OFFlowStatisticsReply statisticsReply; |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 259 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 260 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 261 | // TODO: fix when FlowSynchronizer is refactored |
| 262 | /* |
| 263 | public FlowEntryWrapper(IFlowEntry entry) { |
| 264 | flowEntryId = new FlowEntryId(entry.getFlowEntryId()); |
| 265 | iFlowEntry = entry; |
| 266 | } |
| 267 | */ |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 268 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 269 | public FlowEntryWrapper(OFFlowStatisticsReply entry) { |
| 270 | flowEntryId = new FlowEntryId(entry.getCookie()); |
| 271 | statisticsReply = entry; |
| 272 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 273 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 274 | /** |
| 275 | * Install this FlowEntry to a switch via FlowPusher. |
| 276 | * |
| 277 | * @param sw Switch to which flow will be installed. |
| 278 | */ |
| 279 | double dbTime, extractTime, pushTime; |
Pavlin Radoslavov | 07fb997 | 2013-12-02 16:20:24 -0800 | [diff] [blame] | 280 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 281 | public void addToSwitch(IOFSwitch sw) { |
| 282 | if (statisticsReply != null) { |
| 283 | log.error("Error adding existing flow entry {} to sw {}", |
| 284 | statisticsReply.getCookie(), sw.getId()); |
| 285 | return; |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 286 | } |
Pavlin Radoslavov | 07fb997 | 2013-12-02 16:20:24 -0800 | [diff] [blame] | 287 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 288 | double startDB = System.nanoTime(); |
| 289 | // Get the Flow Entry state from the Network Graph |
| 290 | // TODO: fix when FlowSynchronizer is refactored |
| 291 | /* |
| 292 | if (iFlowEntry == null) { |
| 293 | try { |
| 294 | // TODO: fix when FlowSynchronizer is refactored |
| 295 | iFlowEntry = dbHandler.searchFlowEntry(flowEntryId); |
| 296 | } catch (Exception e) { |
| 297 | log.error("Error finding flow entry {} in Network Graph", |
| 298 | flowEntryId); |
| 299 | return; |
| 300 | } |
| 301 | } |
| 302 | */ |
| 303 | dbTime = System.nanoTime() - startDB; |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 304 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 305 | // |
| 306 | // TODO: The old FlowDatabaseOperation class is gone, so the code |
| 307 | // |
| 308 | /* |
| 309 | double startExtract = System.nanoTime(); |
| 310 | FlowEntry flowEntry = |
| 311 | FlowDatabaseOperation.extractFlowEntry(iFlowEntry); |
| 312 | if (flowEntry == null) { |
| 313 | log.error("Cannot add flow entry {} to sw {} : flow entry cannot be extracted", |
| 314 | flowEntryId, sw.getId()); |
| 315 | return; |
| 316 | } |
| 317 | extractTime = System.nanoTime() - startExtract; |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 318 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 319 | double startPush = System.nanoTime(); |
| 320 | pusher.pushFlowEntry(sw, flowEntry, MsgPriority.HIGH); |
| 321 | pushTime = System.nanoTime() - startPush; |
| 322 | */ |
| 323 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 324 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Remove this FlowEntry from a switch via FlowPusher. |
| 327 | * |
| 328 | * @param sw Switch from which flow will be removed. |
| 329 | */ |
| 330 | public void removeFromSwitch(IOFSwitch sw) { |
| 331 | if (statisticsReply == null) { |
| 332 | log.error("Error removing non-existent flow entry {} from sw {}", |
| 333 | flowEntryId, sw.getId()); |
| 334 | return; |
| 335 | } |
Pavlin Radoslavov | 07fb997 | 2013-12-02 16:20:24 -0800 | [diff] [blame] | 336 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 337 | // Convert Statistics Reply to Flow Mod, then write it |
| 338 | OFFlowMod fm = new OFFlowMod(); |
| 339 | fm.setCookie(statisticsReply.getCookie()); |
| 340 | fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT); |
| 341 | fm.setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 342 | fm.setMatch(statisticsReply.getMatch()); |
| 343 | fm.setPriority(statisticsReply.getPriority()); |
| 344 | fm.setOutPort(OFPort.OFPP_NONE); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 345 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 346 | pusher.add(sw, fm, MsgPriority.HIGH); |
| 347 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 348 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 349 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 350 | * Return the hash code of the Flow Entry ID. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 351 | */ |
| 352 | @Override |
| 353 | public int hashCode() { |
| 354 | return flowEntryId.hashCode(); |
| 355 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 356 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Returns true of the object is another Flow Entry ID with |
| 359 | * the same value; otherwise, returns false. |
| 360 | * |
Sho SHIMIZU | a1199fa | 2014-06-10 18:11:12 -0700 | [diff] [blame] | 361 | * @param obj to compare |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 362 | * @return true if the object has the same Flow Entry ID. |
| 363 | */ |
| 364 | @Override |
| 365 | public boolean equals(Object obj) { |
| 366 | if (obj != null && obj.getClass() == this.getClass()) { |
| 367 | FlowEntryWrapper entry = (FlowEntryWrapper) obj; |
| 368 | // TODO: we need to actually compare the match + actions |
| 369 | return this.flowEntryId.equals(entry.flowEntryId); |
| 370 | } |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public String toString() { |
| 376 | return flowEntryId.toString(); |
| 377 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 378 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 379 | } |