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; |
| 10 | import java.util.concurrent.ExecutionException; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 11 | import java.util.concurrent.Future; |
| 12 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 13 | import org.openflow.protocol.OFFlowMod; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 14 | import org.openflow.protocol.OFMatch; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 15 | import org.openflow.protocol.OFPort; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 16 | import org.openflow.protocol.OFStatisticsRequest; |
| 17 | import org.openflow.protocol.statistics.OFFlowStatisticsReply; |
| 18 | import org.openflow.protocol.statistics.OFFlowStatisticsRequest; |
| 19 | import org.openflow.protocol.statistics.OFStatistics; |
| 20 | import org.openflow.protocol.statistics.OFStatisticsType; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 25 | import net.onrc.onos.graph.GraphDBOperation; |
| 26 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 27 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.util.Dpid; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 30 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 31 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 32 | * FlowSynchronizer is an implementation of FlowSyncService. |
| 33 | * In addition to IFlowSyncService, FlowSynchronizer periodically reads flow tables from |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 34 | * switches and compare them with GraphDB to drop unnecessary flows and/or to install |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 35 | * missing flows. |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 36 | * @author Brian |
| 37 | * |
| 38 | */ |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 39 | public class FlowSynchronizer implements IFlowSyncService { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 40 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 41 | private static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 42 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 43 | private GraphDBOperation dbHandler; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 44 | protected IFlowPusherService pusher; |
| 45 | private Map<IOFSwitch, Thread> switchThreads; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 46 | |
| 47 | public FlowSynchronizer() { |
| 48 | dbHandler = new GraphDBOperation(""); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 49 | switchThreads = new HashMap<IOFSwitch, Thread>(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 52 | @Override |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 53 | public void synchronize(IOFSwitch sw) { |
Pavlin Radoslavov | f8c7855 | 2013-11-26 10:56:59 -0800 | [diff] [blame] | 54 | Synchronizer sync = new Synchronizer(sw); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 55 | Thread t = new Thread(sync); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 56 | switchThreads.put(sw, t); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 57 | t.start(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 58 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [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) { |
| 62 | Thread t = switchThreads.remove(sw); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 63 | if(t != null) { |
| 64 | t.interrupt(); |
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 | } |
| 67 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Initialize Synchronizer. |
| 70 | * @param pusherService FlowPusherService used for sending messages. |
| 71 | */ |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 72 | public void init(IFlowPusherService pusherService) { |
| 73 | pusher = pusherService; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Synchronizer represents main thread of synchronization. |
| 78 | * @author Brian |
| 79 | * |
| 80 | */ |
| 81 | protected class Synchronizer implements Runnable { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 82 | IOFSwitch sw; |
| 83 | ISwitchObject swObj; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 84 | |
Pavlin Radoslavov | f8c7855 | 2013-11-26 10:56:59 -0800 | [diff] [blame] | 85 | public Synchronizer(IOFSwitch sw) { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 86 | this.sw = sw; |
| 87 | Dpid dpid = new Dpid(sw.getId()); |
| 88 | this.swObj = dbHandler.searchSwitch(dpid.toString()); |
| 89 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 90 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public void run() { |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 93 | // TODO: stop adding other flow entries while synchronizing |
| 94 | //pusher.suspend(sw); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 95 | Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph(); |
| 96 | Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 97 | compare(graphEntries, switchEntries); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 98 | //pusher.resume(sw); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 99 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 100 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Compare flows entries in GraphDB and switch to pick up necessary messages. |
| 103 | * After picking up, picked messages are added to FlowPusher. |
| 104 | * @param graphEntries Flow entries in GraphDB. |
| 105 | * @param switchEntries Flow entries in switch. |
| 106 | */ |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 107 | private void compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 108 | int added = 0, removed = 0, skipped = 0; |
| 109 | for(FlowEntryWrapper entry : switchEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 110 | if(graphEntries.contains(entry)) { |
| 111 | graphEntries.remove(entry); |
| 112 | skipped++; |
| 113 | } |
| 114 | else { |
| 115 | // remove flow entry from the switch |
| 116 | entry.removeFromSwitch(sw); |
| 117 | removed++; |
| 118 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 119 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 120 | for(FlowEntryWrapper entry : graphEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 121 | // add flow entry to switch |
| 122 | entry.addToSwitch(sw); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 123 | added++; |
| 124 | } |
| 125 | log.debug("Flow entries added "+ added + ", " + |
| 126 | "Flow entries removed "+ removed + ", " + |
| 127 | "Flow entries skipped " + skipped); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 128 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 129 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Read GraphDB to get FlowEntries associated with a switch. |
| 132 | * @return set of FlowEntries |
| 133 | */ |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 134 | private Set<FlowEntryWrapper> getFlowEntriesFromGraph() { |
| 135 | Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 136 | for(IFlowEntry entry : swObj.getFlowEntries()) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 137 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 138 | entries.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 139 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 140 | return entries; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 141 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 142 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Read flow table from switch and derive FlowEntries from table. |
| 145 | * @return set of FlowEntries |
| 146 | */ |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 147 | private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 148 | |
| 149 | int lengthU = 0; |
| 150 | OFMatch match = new OFMatch(); |
| 151 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 152 | |
| 153 | OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest(); |
| 154 | stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE |
| 155 | stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES) |
| 156 | stat.setMatch(match); |
| 157 | List<OFStatistics> stats = new ArrayList<OFStatistics>(); |
| 158 | stats.add(stat); |
| 159 | lengthU += stat.getLength(); |
| 160 | |
| 161 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 162 | req.setStatisticType(OFStatisticsType.FLOW); |
| 163 | req.setStatistics(stats); |
| 164 | lengthU += req.getLengthU(); |
| 165 | req.setLengthU(lengthU); |
| 166 | |
| 167 | List<OFStatistics> entries = null; |
| 168 | try { |
| 169 | Future<List<OFStatistics>> dfuture = sw.getStatistics(req); |
| 170 | entries = dfuture.get(); |
| 171 | } catch (IOException e) { |
| 172 | // TODO Auto-generated catch block |
| 173 | e.printStackTrace(); |
| 174 | } catch (InterruptedException e) { |
| 175 | // TODO Auto-generated catch block |
| 176 | e.printStackTrace(); |
| 177 | } catch (ExecutionException e) { |
| 178 | // TODO Auto-generated catch block |
| 179 | e.printStackTrace(); |
| 180 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 181 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 182 | Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 183 | for(OFStatistics result : entries){ |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 184 | OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 185 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 186 | results.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 187 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 188 | return results; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 189 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 190 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 193 | /** |
| 194 | * FlowEntryWrapper represents abstract FlowEntry which is embodied by IFlowEntry |
| 195 | * (from GraphDB) or OFFlowStatisticsReply (from switch). |
| 196 | * @author Brian |
| 197 | * |
| 198 | */ |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 199 | class FlowEntryWrapper { |
| 200 | FlowEntryId id; |
| 201 | IFlowEntry iflowEntry; |
| 202 | OFFlowStatisticsReply statisticsReply; |
| 203 | |
| 204 | public FlowEntryWrapper(IFlowEntry entry) { |
| 205 | iflowEntry = entry; |
| 206 | id = new FlowEntryId(entry.getFlowEntryId()); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 209 | public FlowEntryWrapper(OFFlowStatisticsReply entry) { |
| 210 | statisticsReply = entry; |
| 211 | id = new FlowEntryId(entry.getCookie()); |
| 212 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 213 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Install this FlowEntry to a switch via FlowPusher. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 216 | * @param sw Switch to which flow will be installed. |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 217 | */ |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 218 | public void addToSwitch(IOFSwitch sw) { |
| 219 | if(iflowEntry != null) { |
| 220 | pusher.add(sw, iflowEntry.getFlow(), iflowEntry); |
| 221 | } |
| 222 | else if(statisticsReply != null) { |
| 223 | log.error("Adding existing flow entry {} to sw {}", |
| 224 | statisticsReply.getCookie(), sw.getId()); |
| 225 | } |
| 226 | } |
| 227 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 228 | /** |
| 229 | * Remove this FlowEntry from a switch via FlowPusher. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 230 | * @param sw Switch from which flow will be removed. |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 231 | */ |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 232 | public void removeFromSwitch(IOFSwitch sw){ |
| 233 | if(iflowEntry != null) { |
| 234 | log.error("Removing non-existent flow entry {} from sw {}", |
| 235 | iflowEntry.getFlowEntryId(), sw.getId()); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 236 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 237 | } |
| 238 | else if(statisticsReply != null) { |
| 239 | // Convert Statistics Reply to Flow Mod, then write it |
| 240 | OFFlowMod fm = new OFFlowMod(); |
| 241 | fm.setCookie(statisticsReply.getCookie()); |
| 242 | fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT); |
| 243 | fm.setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 244 | fm.setMatch(statisticsReply.getMatch()); |
| 245 | fm.setPriority(statisticsReply.getPriority()); |
| 246 | fm.setOutPort(OFPort.OFPP_NONE); |
| 247 | pusher.add(sw, fm); |
| 248 | } |
| 249 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 250 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 251 | /** |
| 252 | * Return the hash code of the Flow Entry ID |
| 253 | */ |
| 254 | @Override |
| 255 | public int hashCode() { |
| 256 | return id.hashCode(); |
| 257 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 258 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 259 | /** |
| 260 | * Returns true of the object is another Flow Entry ID with |
| 261 | * the same value; otherwise, returns false. |
| 262 | * |
| 263 | * @param Object to compare |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 264 | * @return true if the object has the same Flow Entry ID. |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 265 | */ |
| 266 | @Override |
| 267 | public boolean equals(Object obj){ |
| 268 | if(obj.getClass() == this.getClass()) { |
| 269 | FlowEntryWrapper entry = (FlowEntryWrapper) obj; |
| 270 | // TODO: we need to actually compare the match + actions |
| 271 | return this.id.equals(entry.id); |
| 272 | } |
| 273 | return false; |
| 274 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 275 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 276 | @Override |
| 277 | public String toString() { |
| 278 | return id.toString(); |
| 279 | } |
| 280 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 281 | |
| 282 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 285 | |