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; |
| 5 | import java.util.Collection; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 6 | import java.util.HashMap; |
| 7 | import java.util.HashSet; |
| 8 | import java.util.List; |
| 9 | import java.util.Map; |
| 10 | import java.util.Set; |
| 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; |
| 13 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 14 | import org.openflow.protocol.OFFlowMod; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 15 | import org.openflow.protocol.OFMatch; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 16 | import org.openflow.protocol.OFMessage; |
| 17 | import org.openflow.protocol.OFPacketOut; |
| 18 | import org.openflow.protocol.OFPort; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 19 | import org.openflow.protocol.OFStatisticsRequest; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 20 | import org.openflow.protocol.action.OFAction; |
| 21 | import org.openflow.protocol.action.OFActionDataLayerDestination; |
| 22 | import org.openflow.protocol.action.OFActionDataLayerSource; |
| 23 | import org.openflow.protocol.action.OFActionEnqueue; |
| 24 | import org.openflow.protocol.action.OFActionNetworkLayerDestination; |
| 25 | import org.openflow.protocol.action.OFActionNetworkLayerSource; |
| 26 | import org.openflow.protocol.action.OFActionNetworkTypeOfService; |
| 27 | import org.openflow.protocol.action.OFActionOutput; |
| 28 | import org.openflow.protocol.action.OFActionStripVirtualLan; |
| 29 | import org.openflow.protocol.action.OFActionTransportLayerDestination; |
| 30 | import org.openflow.protocol.action.OFActionTransportLayerSource; |
| 31 | import org.openflow.protocol.action.OFActionVirtualLanIdentifier; |
| 32 | import org.openflow.protocol.action.OFActionVirtualLanPriorityCodePoint; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 33 | import org.openflow.protocol.statistics.OFFlowStatisticsReply; |
| 34 | import org.openflow.protocol.statistics.OFFlowStatisticsRequest; |
| 35 | import org.openflow.protocol.statistics.OFStatistics; |
| 36 | import org.openflow.protocol.statistics.OFStatisticsType; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 40 | import com.google.common.collect.Lists; |
| 41 | import com.tinkerpop.blueprints.Direction; |
| 42 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 43 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 44 | import net.floodlightcontroller.core.IOFSwitch; |
| 45 | import net.floodlightcontroller.core.IOFSwitchListener; |
| 46 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 47 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 48 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 49 | import net.floodlightcontroller.core.module.IFloodlightService; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 50 | import net.floodlightcontroller.restserver.IRestApiService; |
| 51 | import net.onrc.onos.datagrid.IDatagridService; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 52 | import net.onrc.onos.graph.GraphDBOperation; |
| 53 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 54 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 55 | import net.onrc.onos.ofcontroller.core.module.IOnosService; |
| 56 | import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 57 | import net.onrc.onos.ofcontroller.util.Dpid; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 58 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 59 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 60 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 61 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionEnqueue; |
| 62 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionOutput; |
| 63 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetEthernetAddr; |
| 64 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetIPv4Addr; |
| 65 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetIpToS; |
| 66 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetTcpUdpPort; |
| 67 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetVlanId; |
| 68 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetVlanPriority; |
| 69 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionStripVlan; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 70 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 71 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 72 | public class FlowSynchronizer implements IFlowSyncService, IOFSwitchListener { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 73 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 74 | protected static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 75 | protected IFloodlightProviderService floodlightProvider; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 76 | protected IControllerRegistryService registryService; |
| 77 | protected IFlowPusherService pusher; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 78 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 79 | private GraphDBOperation dbHandler; |
| 80 | private Map<IOFSwitch, Thread> switchThread = new HashMap<IOFSwitch, Thread>(); |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 81 | |
| 82 | public FlowSynchronizer() { |
| 83 | dbHandler = new GraphDBOperation(""); |
| 84 | } |
| 85 | |
| 86 | public void synchronize(IOFSwitch sw) { |
| 87 | Synchroizer sync = new Synchroizer(sw); |
| 88 | Thread t = new Thread(sync); |
| 89 | t.start(); |
| 90 | switchThread.put(sw, t); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void addedSwitch(IOFSwitch sw) { |
| 95 | log.debug("Switch added: {}", sw.getId()); |
| 96 | |
| 97 | if (registryService.hasControl(sw.getId())) { |
| 98 | synchronize(sw); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void removedSwitch(IOFSwitch sw) { |
| 104 | log.debug("Switch removed: {}", sw.getId()); |
| 105 | |
| 106 | Thread t = switchThread.remove(sw); |
| 107 | if(t != null) { |
| 108 | t.interrupt(); |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void switchPortChanged(Long switchId) { |
| 115 | // TODO Auto-generated method stub |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public String getName() { |
| 120 | return "FlowSynchronizer"; |
| 121 | } |
| 122 | |
| 123 | //@Override |
| 124 | public void init(FloodlightModuleContext context) |
| 125 | throws FloodlightModuleException { |
| 126 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 127 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
| 128 | pusher = context.getServiceImpl(IFlowPusherService.class); |
| 129 | } |
| 130 | |
| 131 | //@Override |
| 132 | public void startUp(FloodlightModuleContext context) { |
| 133 | floodlightProvider.addOFSwitchListener(this); |
| 134 | } |
| 135 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 136 | protected class Synchroizer implements Runnable { |
| 137 | IOFSwitch sw; |
| 138 | ISwitchObject swObj; |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 139 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 140 | public Synchroizer(IOFSwitch sw) { |
| 141 | this.sw = sw; |
| 142 | Dpid dpid = new Dpid(sw.getId()); |
| 143 | this.swObj = dbHandler.searchSwitch(dpid.toString()); |
| 144 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 145 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public void run() { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 148 | Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph(); |
| 149 | Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 150 | compare(graphEntries, switchEntries); |
| 151 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 152 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 153 | private void compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 154 | int added = 0, removed = 0, skipped = 0; |
| 155 | for(FlowEntryWrapper entry : switchEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 156 | if(graphEntries.contains(entry)) { |
| 157 | graphEntries.remove(entry); |
| 158 | skipped++; |
| 159 | } |
| 160 | else { |
| 161 | // remove flow entry from the switch |
| 162 | entry.removeFromSwitch(sw); |
| 163 | removed++; |
| 164 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 165 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 166 | for(FlowEntryWrapper entry : graphEntries) { |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 167 | // add flow entry to switch |
| 168 | entry.addToSwitch(sw); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 169 | added++; |
| 170 | } |
| 171 | log.debug("Flow entries added "+ added + ", " + |
| 172 | "Flow entries removed "+ removed + ", " + |
| 173 | "Flow entries skipped " + skipped); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 174 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 175 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 176 | private Set<FlowEntryWrapper> getFlowEntriesFromGraph() { |
| 177 | Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 178 | for(IFlowEntry entry : swObj.getFlowEntries()) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 179 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 180 | entries.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 181 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 182 | return entries; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 183 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 184 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 185 | private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 186 | |
| 187 | int lengthU = 0; |
| 188 | OFMatch match = new OFMatch(); |
| 189 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 190 | |
| 191 | OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest(); |
| 192 | stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE |
| 193 | stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES) |
| 194 | stat.setMatch(match); |
| 195 | List<OFStatistics> stats = new ArrayList<OFStatistics>(); |
| 196 | stats.add(stat); |
| 197 | lengthU += stat.getLength(); |
| 198 | |
| 199 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 200 | req.setStatisticType(OFStatisticsType.FLOW); |
| 201 | req.setStatistics(stats); |
| 202 | lengthU += req.getLengthU(); |
| 203 | req.setLengthU(lengthU); |
| 204 | |
| 205 | List<OFStatistics> entries = null; |
| 206 | try { |
| 207 | Future<List<OFStatistics>> dfuture = sw.getStatistics(req); |
| 208 | entries = dfuture.get(); |
| 209 | } catch (IOException e) { |
| 210 | // TODO Auto-generated catch block |
| 211 | e.printStackTrace(); |
| 212 | } catch (InterruptedException e) { |
| 213 | // TODO Auto-generated catch block |
| 214 | e.printStackTrace(); |
| 215 | } catch (ExecutionException e) { |
| 216 | // TODO Auto-generated catch block |
| 217 | e.printStackTrace(); |
| 218 | } |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 219 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 220 | Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 221 | for(OFStatistics result : entries){ |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 222 | OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 223 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 224 | results.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 225 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 226 | return results; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 227 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 228 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 231 | class FlowEntryWrapper { |
| 232 | FlowEntryId id; |
| 233 | IFlowEntry iflowEntry; |
| 234 | OFFlowStatisticsReply statisticsReply; |
| 235 | |
| 236 | public FlowEntryWrapper(IFlowEntry entry) { |
| 237 | iflowEntry = entry; |
| 238 | id = new FlowEntryId(entry.getFlowEntryId()); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 241 | public FlowEntryWrapper(OFFlowStatisticsReply entry) { |
| 242 | statisticsReply = entry; |
| 243 | id = new FlowEntryId(entry.getCookie()); |
| 244 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 245 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 246 | public void addToSwitch(IOFSwitch sw) { |
| 247 | if(iflowEntry != null) { |
| 248 | pusher.add(sw, iflowEntry.getFlow(), iflowEntry); |
| 249 | } |
| 250 | else if(statisticsReply != null) { |
| 251 | log.error("Adding existing flow entry {} to sw {}", |
| 252 | statisticsReply.getCookie(), sw.getId()); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public void removeFromSwitch(IOFSwitch sw){ |
| 257 | if(iflowEntry != null) { |
| 258 | log.error("Removing non-existent flow entry {} from sw {}", |
| 259 | iflowEntry.getFlowEntryId(), sw.getId()); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 260 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 261 | } |
| 262 | else if(statisticsReply != null) { |
| 263 | // Convert Statistics Reply to Flow Mod, then write it |
| 264 | OFFlowMod fm = new OFFlowMod(); |
| 265 | fm.setCookie(statisticsReply.getCookie()); |
| 266 | fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT); |
| 267 | fm.setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 268 | fm.setMatch(statisticsReply.getMatch()); |
| 269 | fm.setPriority(statisticsReply.getPriority()); |
| 270 | fm.setOutPort(OFPort.OFPP_NONE); |
| 271 | pusher.add(sw, fm); |
| 272 | } |
| 273 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 274 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 275 | /** |
| 276 | * Return the hash code of the Flow Entry ID |
| 277 | */ |
| 278 | @Override |
| 279 | public int hashCode() { |
| 280 | return id.hashCode(); |
| 281 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 282 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 283 | /** |
| 284 | * Returns true of the object is another Flow Entry ID with |
| 285 | * the same value; otherwise, returns false. |
| 286 | * |
| 287 | * @param Object to compare |
| 288 | */ |
| 289 | @Override |
| 290 | public boolean equals(Object obj){ |
| 291 | if(obj.getClass() == this.getClass()) { |
| 292 | FlowEntryWrapper entry = (FlowEntryWrapper) obj; |
| 293 | // TODO: we need to actually compare the match + actions |
| 294 | return this.id.equals(entry.id); |
| 295 | } |
| 296 | return false; |
| 297 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 298 | |
Brian O'Connor | e46492e | 2013-11-14 21:11:50 -0800 | [diff] [blame] | 299 | @Override |
| 300 | public String toString() { |
| 301 | return id.toString(); |
| 302 | } |
| 303 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 306 | |