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