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 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 31 | public class FlowSynchronizer implements IFlowSyncService { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 32 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 33 | private static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 34 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 35 | private GraphDBOperation dbHandler; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 36 | protected IFlowPusherService pusher; |
| 37 | private Map<IOFSwitch, Thread> switchThreads; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 38 | |
| 39 | public FlowSynchronizer() { |
| 40 | dbHandler = new GraphDBOperation(""); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 41 | switchThreads = new HashMap<IOFSwitch, Thread>(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 44 | @Override |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 45 | public void synchronize(IOFSwitch sw) { |
Pavlin Radoslavov | f8c7855 | 2013-11-26 10:56:59 -0800 | [diff] [blame] | 46 | Synchronizer sync = new Synchronizer(sw); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 47 | Thread t = new Thread(sync); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 48 | switchThreads.put(sw, t); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 49 | t.start(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 50 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 51 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 52 | @Override |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 53 | public void interrupt(IOFSwitch sw) { |
| 54 | Thread t = switchThreads.remove(sw); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 55 | if(t != null) { |
| 56 | t.interrupt(); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 57 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 60 | public void init(IFlowPusherService pusherService) { |
| 61 | pusher = pusherService; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Pavlin Radoslavov | f8c7855 | 2013-11-26 10:56:59 -0800 | [diff] [blame] | 64 | protected class Synchronizer implements Runnable { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 65 | IOFSwitch sw; |
| 66 | ISwitchObject swObj; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 67 | |
Pavlin Radoslavov | f8c7855 | 2013-11-26 10:56:59 -0800 | [diff] [blame] | 68 | public Synchronizer(IOFSwitch sw) { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 69 | this.sw = sw; |
| 70 | Dpid dpid = new Dpid(sw.getId()); |
| 71 | this.swObj = dbHandler.searchSwitch(dpid.toString()); |
| 72 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 73 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public void run() { |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 76 | // TODO: stop adding other flow entries while synchronizing |
| 77 | //pusher.suspend(sw); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 78 | Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph(); |
| 79 | Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 80 | compare(graphEntries, switchEntries); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 81 | //pusher.resume(sw); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 82 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 83 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 84 | private void compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 85 | int added = 0, removed = 0, skipped = 0; |
| 86 | for(FlowEntryWrapper entry : switchEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 87 | if(graphEntries.contains(entry)) { |
| 88 | graphEntries.remove(entry); |
| 89 | skipped++; |
| 90 | } |
| 91 | else { |
| 92 | // remove flow entry from the switch |
| 93 | entry.removeFromSwitch(sw); |
| 94 | removed++; |
| 95 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 96 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 97 | for(FlowEntryWrapper entry : graphEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 98 | // add flow entry to switch |
| 99 | entry.addToSwitch(sw); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 100 | added++; |
| 101 | } |
| 102 | log.debug("Flow entries added "+ added + ", " + |
| 103 | "Flow entries removed "+ removed + ", " + |
| 104 | "Flow entries skipped " + skipped); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 105 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 106 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 107 | private Set<FlowEntryWrapper> getFlowEntriesFromGraph() { |
| 108 | Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 109 | for(IFlowEntry entry : swObj.getFlowEntries()) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 110 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 111 | entries.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 112 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 113 | return entries; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 114 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 115 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 116 | private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 117 | |
| 118 | int lengthU = 0; |
| 119 | OFMatch match = new OFMatch(); |
| 120 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 121 | |
| 122 | OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest(); |
| 123 | stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE |
| 124 | stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES) |
| 125 | stat.setMatch(match); |
| 126 | List<OFStatistics> stats = new ArrayList<OFStatistics>(); |
| 127 | stats.add(stat); |
| 128 | lengthU += stat.getLength(); |
| 129 | |
| 130 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 131 | req.setStatisticType(OFStatisticsType.FLOW); |
| 132 | req.setStatistics(stats); |
| 133 | lengthU += req.getLengthU(); |
| 134 | req.setLengthU(lengthU); |
| 135 | |
| 136 | List<OFStatistics> entries = null; |
| 137 | try { |
| 138 | Future<List<OFStatistics>> dfuture = sw.getStatistics(req); |
| 139 | entries = dfuture.get(); |
| 140 | } catch (IOException e) { |
| 141 | // TODO Auto-generated catch block |
| 142 | e.printStackTrace(); |
| 143 | } catch (InterruptedException e) { |
| 144 | // TODO Auto-generated catch block |
| 145 | e.printStackTrace(); |
| 146 | } catch (ExecutionException e) { |
| 147 | // TODO Auto-generated catch block |
| 148 | e.printStackTrace(); |
| 149 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 150 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 151 | Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 152 | for(OFStatistics result : entries){ |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 153 | OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 154 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 155 | results.add(fe); |
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 | return results; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 158 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 159 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 162 | class FlowEntryWrapper { |
| 163 | FlowEntryId id; |
| 164 | IFlowEntry iflowEntry; |
| 165 | OFFlowStatisticsReply statisticsReply; |
| 166 | |
| 167 | public FlowEntryWrapper(IFlowEntry entry) { |
| 168 | iflowEntry = entry; |
| 169 | id = new FlowEntryId(entry.getFlowEntryId()); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 172 | public FlowEntryWrapper(OFFlowStatisticsReply entry) { |
| 173 | statisticsReply = entry; |
| 174 | id = new FlowEntryId(entry.getCookie()); |
| 175 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 176 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 177 | public void addToSwitch(IOFSwitch sw) { |
| 178 | if(iflowEntry != null) { |
| 179 | pusher.add(sw, iflowEntry.getFlow(), iflowEntry); |
| 180 | } |
| 181 | else if(statisticsReply != null) { |
| 182 | log.error("Adding existing flow entry {} to sw {}", |
| 183 | statisticsReply.getCookie(), sw.getId()); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | public void removeFromSwitch(IOFSwitch sw){ |
| 188 | if(iflowEntry != null) { |
| 189 | log.error("Removing non-existent flow entry {} from sw {}", |
| 190 | iflowEntry.getFlowEntryId(), sw.getId()); |
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 | } |
| 193 | else if(statisticsReply != null) { |
| 194 | // Convert Statistics Reply to Flow Mod, then write it |
| 195 | OFFlowMod fm = new OFFlowMod(); |
| 196 | fm.setCookie(statisticsReply.getCookie()); |
| 197 | fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT); |
| 198 | fm.setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 199 | fm.setMatch(statisticsReply.getMatch()); |
| 200 | fm.setPriority(statisticsReply.getPriority()); |
| 201 | fm.setOutPort(OFPort.OFPP_NONE); |
| 202 | pusher.add(sw, fm); |
| 203 | } |
| 204 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 205 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Return the hash code of the Flow Entry ID |
| 208 | */ |
| 209 | @Override |
| 210 | public int hashCode() { |
| 211 | return id.hashCode(); |
| 212 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 213 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Returns true of the object is another Flow Entry ID with |
| 216 | * the same value; otherwise, returns false. |
| 217 | * |
| 218 | * @param Object to compare |
| 219 | */ |
| 220 | @Override |
| 221 | public boolean equals(Object obj){ |
| 222 | if(obj.getClass() == this.getClass()) { |
| 223 | FlowEntryWrapper entry = (FlowEntryWrapper) obj; |
| 224 | // TODO: we need to actually compare the match + actions |
| 225 | return this.id.equals(entry.id); |
| 226 | } |
| 227 | return false; |
| 228 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 229 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 230 | @Override |
| 231 | public String toString() { |
| 232 | return id.toString(); |
| 233 | } |
| 234 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 235 | |
| 236 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 239 | |