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