Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
| 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 | |
| 40 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 41 | import net.floodlightcontroller.core.IOFSwitch; |
| 42 | import net.floodlightcontroller.core.IOFSwitchListener; |
| 43 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 44 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 45 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 46 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 47 | import net.onrc.onos.graph.GraphDBOperation; |
| 48 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 49 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 50 | import net.onrc.onos.ofcontroller.util.Dpid; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 51 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 52 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 53 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 54 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionEnqueue; |
| 55 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionOutput; |
| 56 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetEthernetAddr; |
| 57 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetIPv4Addr; |
| 58 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetIpToS; |
| 59 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetTcpUdpPort; |
| 60 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetVlanId; |
| 61 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetVlanPriority; |
| 62 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionStripVlan; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 63 | |
| 64 | public class FlowSynchronizer implements IOFSwitchListener, |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 65 | IFlowSyncService { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 66 | |
| 67 | protected GraphDBOperation dbHandler = new GraphDBOperation(""); //TODO: conf |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 68 | protected static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 69 | protected IFloodlightProviderService floodlightProvider; |
| 70 | protected Map<IOFSwitch, Thread> switchThread = new HashMap<IOFSwitch, Thread>(); |
| 71 | |
| 72 | protected class Synchroizer implements Runnable { |
| 73 | IOFSwitch sw; |
| 74 | ISwitchObject swObj; |
| 75 | |
| 76 | public Synchroizer(IOFSwitch sw) { |
| 77 | this.sw = sw; |
| 78 | Dpid dpid = new Dpid(sw.getId()); |
| 79 | this.swObj = dbHandler.searchSwitch(dpid.toString()); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void run() { |
| 84 | //TODO: use a FlowEntryId, FlowEntry HashMap |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 85 | Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph(); |
| 86 | Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 87 | compare(graphEntries, switchEntries); |
| 88 | } |
| 89 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 90 | private void compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) { |
| 91 | |
| 92 | /* old impl |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 93 | System.out.println("graph entries: " + graphEntries); |
| 94 | System.out.println("switch entries: " + switchEntries); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 95 | Set<FlowEntryWrapper> entriesToAdd = new HashSet<FlowEntryWrapper>(graphEntries); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 96 | entriesToAdd.removeAll(switchEntries); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 97 | Set<FlowEntryWrapper> entriesToRemove = switchEntries; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 98 | entriesToRemove.removeAll(graphEntries); |
| 99 | System.out.println("add: " + entriesToAdd); |
| 100 | System.out.println("remove: " + entriesToRemove); |
| 101 | //FlowDatabaseOperation for converting flowentries |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 102 | */ |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 103 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 104 | /* TODO: new implementation with graph */ |
| 105 | int added = 0, removed = 0, skipped = 0; |
| 106 | for(FlowEntryWrapper entry : switchEntries) { |
| 107 | if(graphEntries.contains(entry)) { |
| 108 | graphEntries.remove(entry); |
| 109 | System.out.println("** skipping entry " + entry.id); |
| 110 | skipped++; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 111 | } |
| 112 | else { |
| 113 | // remove fid from the switch |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 114 | System.out.println("** remove entry " + entry.id); |
| 115 | // TODO: use remove strict message |
| 116 | writeToSwitch(entry.getOFMessage()); |
| 117 | removed++; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 120 | for(FlowEntryWrapper entry : graphEntries) { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 121 | // add fid to switch |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 122 | System.out.println("** add entry " + entry.id); |
| 123 | // TODO: use modify strict message |
| 124 | writeToSwitch(entry.getOFMessage()); |
| 125 | added++; |
| 126 | } |
| 127 | log.debug("Flow entries added "+ added + ", " + |
| 128 | "Flow entries removed "+ removed + ", " + |
| 129 | "Flow entries skipped " + skipped); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 132 | private void writeToSwitch(OFMessage msg) { |
| 133 | try { |
| 134 | sw.write(msg, null); // TODO: what is context? |
| 135 | sw.flush(); |
| 136 | } catch (IOException e) { |
| 137 | // TODO Auto-generated catch block |
| 138 | System.out.println("ERROR*****"); |
| 139 | e.printStackTrace(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private Set<FlowEntryWrapper> getFlowEntriesFromGraph() { |
| 144 | Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 145 | for(IFlowEntry entry : swObj.getFlowEntries()) { |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 146 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 147 | entries.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 148 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 149 | return entries; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 152 | private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 153 | |
| 154 | int lengthU = 0; |
| 155 | OFMatch match = new OFMatch(); |
| 156 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 157 | |
| 158 | OFFlowStatisticsRequest stat = new OFFlowStatisticsRequest(); |
| 159 | stat.setOutPort((short) 0xffff); //TODO: OFPort.OFPP_NONE |
| 160 | stat.setTableId((byte) 0xff); // TODO: fix this with enum (ALL TABLES) |
| 161 | stat.setMatch(match); |
| 162 | List<OFStatistics> stats = new ArrayList<OFStatistics>(); |
| 163 | stats.add(stat); |
| 164 | lengthU += stat.getLength(); |
| 165 | |
| 166 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 167 | req.setStatisticType(OFStatisticsType.FLOW); |
| 168 | req.setStatistics(stats); |
| 169 | lengthU += req.getLengthU(); |
| 170 | req.setLengthU(lengthU); |
| 171 | |
| 172 | List<OFStatistics> entries = null; |
| 173 | try { |
| 174 | Future<List<OFStatistics>> dfuture = sw.getStatistics(req); |
| 175 | entries = dfuture.get(); |
| 176 | } catch (IOException e) { |
| 177 | // TODO Auto-generated catch block |
| 178 | e.printStackTrace(); |
| 179 | } catch (InterruptedException e) { |
| 180 | // TODO Auto-generated catch block |
| 181 | e.printStackTrace(); |
| 182 | } catch (ExecutionException e) { |
| 183 | // TODO Auto-generated catch block |
| 184 | e.printStackTrace(); |
| 185 | } |
| 186 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 187 | Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 188 | for(OFStatistics result : entries){ |
| 189 | //System.out.println(result.getClass()); |
| 190 | OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result; |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 191 | FlowEntryWrapper fe = new FlowEntryWrapper(entry); |
| 192 | results.add(fe); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 193 | } |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 194 | return results; |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | } |
| 198 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 199 | public void synchronize(IOFSwitch sw) { |
| 200 | Synchroizer sync = new Synchroizer(sw); |
| 201 | Thread t = new Thread(sync); |
| 202 | t.start(); |
| 203 | switchThread.put(sw, t); |
| 204 | } |
| 205 | |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public void addedSwitch(IOFSwitch sw) { |
| 208 | // TODO Auto-generated method stub |
| 209 | System.out.println("added switch in flow sync: " + sw); |
| 210 | |
| 211 | // TODO: look at how this is spawned |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 212 | synchronize(sw); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void removedSwitch(IOFSwitch sw) { |
| 217 | // TODO Auto-generated method stub |
| 218 | System.out.println("removed switch in flow sync: " + sw); |
| 219 | Thread t = switchThread.remove(sw); |
| 220 | if(t != null) { |
| 221 | t.interrupt(); |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void switchPortChanged(Long switchId) { |
| 228 | // TODO Auto-generated method stub |
| 229 | |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public String getName() { |
| 234 | // TODO Auto-generated method stub |
| 235 | return "FlowSynchronizer"; |
| 236 | } |
| 237 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 238 | /* |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void init(FloodlightModuleContext context) |
| 241 | throws FloodlightModuleException { |
| 242 | // TODO Auto-generated method stub |
| 243 | System.out.println("********* Starting flow sync...."); |
| 244 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 245 | System.out.println(context.getAllServices()); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void startUp(FloodlightModuleContext context) { |
| 250 | // TODO Auto-generated method stub |
| 251 | floodlightProvider.addOFSwitchListener(this); |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 252 | } |
| 253 | */ |
| 254 | |
| 255 | } |
| 256 | |
| 257 | class FlowEntryWrapper { |
| 258 | FlowEntryId id; |
| 259 | IFlowEntry iflow; |
| 260 | OFFlowStatisticsReply stat; |
| 261 | |
| 262 | public FlowEntryWrapper(IFlowEntry entry) { |
| 263 | // TODO Auto-generated constructor stub |
| 264 | iflow = entry; |
| 265 | id = new FlowEntryId(entry.getFlowEntryId()); |
| 266 | } |
| 267 | |
| 268 | public FlowEntryWrapper(OFFlowStatisticsReply entry) { |
| 269 | stat = entry; |
| 270 | id = new FlowEntryId(entry.getCookie()); |
| 271 | } |
| 272 | |
| 273 | public OFMessage getOFMessage() { |
| 274 | if(iflow != null) { |
| 275 | //convert iflow |
| 276 | OFFlowMod fm = new OFFlowMod(); |
| 277 | fm.setCommand(OFFlowMod.OFPFC_MODIFY_STRICT); |
| 278 | |
| 279 | // ************* COPIED |
| 280 | OFMatch match = new OFMatch(); |
| 281 | match.setWildcards(OFMatch.OFPFW_ALL); |
| 282 | |
| 283 | // Match the Incoming Port |
| 284 | Short matchInPort = iflow.getMatchInPort(); |
| 285 | if (matchInPort != null) { |
| 286 | match.setInputPort(matchInPort); |
| 287 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 288 | } |
| 289 | |
| 290 | // Match the Source MAC address |
| 291 | String matchSrcMac = iflow.getMatchSrcMac(); |
| 292 | if (matchSrcMac != null) { |
| 293 | match.setDataLayerSource(matchSrcMac); |
| 294 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 295 | } |
| 296 | |
| 297 | // Match the Destination MAC address |
| 298 | String matchDstMac = iflow.getMatchDstMac(); |
| 299 | if (matchDstMac != null) { |
| 300 | match.setDataLayerDestination(matchDstMac); |
| 301 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 302 | } |
| 303 | |
| 304 | // Match the Ethernet Frame Type |
| 305 | Short matchEthernetFrameType = iflow.getMatchEthernetFrameType(); |
| 306 | if (matchEthernetFrameType != null) { |
| 307 | match.setDataLayerType(matchEthernetFrameType); |
| 308 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 309 | } |
| 310 | |
| 311 | // Match the VLAN ID |
| 312 | Short matchVlanId = iflow.getMatchVlanId(); |
| 313 | if (matchVlanId != null) { |
| 314 | match.setDataLayerVirtualLan(matchVlanId); |
| 315 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN); |
| 316 | } |
| 317 | |
| 318 | // Match the VLAN priority |
| 319 | Byte matchVlanPriority = iflow.getMatchVlanPriority(); |
| 320 | if (matchVlanPriority != null) { |
| 321 | match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority); |
| 322 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN_PCP); |
| 323 | } |
| 324 | |
| 325 | // Match the Source IPv4 Network prefix |
| 326 | String matchSrcIPv4Net = iflow.getMatchSrcIPv4Net(); |
| 327 | if (matchSrcIPv4Net != null) { |
| 328 | match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC); |
| 329 | } |
| 330 | |
| 331 | // Natch the Destination IPv4 Network prefix |
| 332 | String matchDstIPv4Net = iflow.getMatchDstIPv4Net(); |
| 333 | if (matchDstIPv4Net != null) { |
| 334 | match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST); |
| 335 | } |
| 336 | |
| 337 | // Match the IP protocol |
| 338 | Byte matchIpProto = iflow.getMatchIpProto(); |
| 339 | if (matchIpProto != null) { |
| 340 | match.setNetworkProtocol(matchIpProto); |
| 341 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO); |
| 342 | } |
| 343 | |
| 344 | // Match the IP ToS (DSCP field, 6 bits) |
| 345 | Byte matchIpToS = iflow.getMatchIpToS(); |
| 346 | if (matchIpToS != null) { |
| 347 | match.setNetworkTypeOfService(matchIpToS); |
| 348 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS); |
| 349 | } |
| 350 | |
| 351 | // Match the Source TCP/UDP port |
| 352 | Short matchSrcTcpUdpPort = iflow.getMatchSrcTcpUdpPort(); |
| 353 | if (matchSrcTcpUdpPort != null) { |
| 354 | match.setTransportSource(matchSrcTcpUdpPort); |
| 355 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 356 | } |
| 357 | |
| 358 | // Match the Destination TCP/UDP port |
| 359 | Short matchDstTcpUdpPort = iflow.getMatchDstTcpUdpPort(); |
| 360 | if (matchDstTcpUdpPort != null) { |
| 361 | match.setTransportDestination(matchDstTcpUdpPort); |
| 362 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 363 | } |
| 364 | |
| 365 | // |
| 366 | // Fetch the actions |
| 367 | // |
| 368 | // Short actionOutputPort = null; |
| 369 | List<OFAction> openFlowActions = new ArrayList<OFAction>(); |
| 370 | int actionsLen = 0; |
| 371 | FlowEntryActions flowEntryActions = null; |
| 372 | String actionsStr = iflow.getActions(); |
| 373 | if (actionsStr != null) |
| 374 | flowEntryActions = new FlowEntryActions(actionsStr); |
| 375 | for (FlowEntryAction action : flowEntryActions.actions()) { |
| 376 | // ActionOutput actionOutput = action.actionOutput(); |
| 377 | ActionSetVlanId actionSetVlanId = action.actionSetVlanId(); |
| 378 | ActionSetVlanPriority actionSetVlanPriority = action.actionSetVlanPriority(); |
| 379 | ActionStripVlan actionStripVlan = action.actionStripVlan(); |
| 380 | ActionSetEthernetAddr actionSetEthernetSrcAddr = action.actionSetEthernetSrcAddr(); |
| 381 | ActionSetEthernetAddr actionSetEthernetDstAddr = action.actionSetEthernetDstAddr(); |
| 382 | ActionSetIPv4Addr actionSetIPv4SrcAddr = action.actionSetIPv4SrcAddr(); |
| 383 | ActionSetIPv4Addr actionSetIPv4DstAddr = action.actionSetIPv4DstAddr(); |
| 384 | ActionSetIpToS actionSetIpToS = action.actionSetIpToS(); |
| 385 | ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action.actionSetTcpUdpSrcPort(); |
| 386 | ActionSetTcpUdpPort actionSetTcpUdpDstPort = action.actionSetTcpUdpDstPort(); |
| 387 | ActionEnqueue actionEnqueue = action.actionEnqueue(); |
| 388 | |
| 389 | // if (actionOutput != null) { |
| 390 | // actionOutputPort = actionOutput.port().value(); |
| 391 | // // XXX: The max length is hard-coded for now |
| 392 | // OFActionOutput ofa = |
| 393 | // new OFActionOutput(actionOutput.port().value(), |
| 394 | // (short)0xffff); |
| 395 | // openFlowActions.add(ofa); |
| 396 | // actionsLen += ofa.getLength(); |
| 397 | // } |
| 398 | |
| 399 | if (actionSetVlanId != null) { |
| 400 | OFActionVirtualLanIdentifier ofa = |
| 401 | new OFActionVirtualLanIdentifier(actionSetVlanId.vlanId()); |
| 402 | openFlowActions.add(ofa); |
| 403 | actionsLen += ofa.getLength(); |
| 404 | } |
| 405 | |
| 406 | if (actionSetVlanPriority != null) { |
| 407 | OFActionVirtualLanPriorityCodePoint ofa = |
| 408 | new OFActionVirtualLanPriorityCodePoint(actionSetVlanPriority.vlanPriority()); |
| 409 | openFlowActions.add(ofa); |
| 410 | actionsLen += ofa.getLength(); |
| 411 | } |
| 412 | |
| 413 | if (actionStripVlan != null) { |
| 414 | if (actionStripVlan.stripVlan() == true) { |
| 415 | OFActionStripVirtualLan ofa = new OFActionStripVirtualLan(); |
| 416 | openFlowActions.add(ofa); |
| 417 | actionsLen += ofa.getLength(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (actionSetEthernetSrcAddr != null) { |
| 422 | OFActionDataLayerSource ofa = |
| 423 | new OFActionDataLayerSource(actionSetEthernetSrcAddr.addr().toBytes()); |
| 424 | openFlowActions.add(ofa); |
| 425 | actionsLen += ofa.getLength(); |
| 426 | } |
| 427 | |
| 428 | if (actionSetEthernetDstAddr != null) { |
| 429 | OFActionDataLayerDestination ofa = |
| 430 | new OFActionDataLayerDestination(actionSetEthernetDstAddr.addr().toBytes()); |
| 431 | openFlowActions.add(ofa); |
| 432 | actionsLen += ofa.getLength(); |
| 433 | } |
| 434 | |
| 435 | if (actionSetIPv4SrcAddr != null) { |
| 436 | OFActionNetworkLayerSource ofa = |
| 437 | new OFActionNetworkLayerSource(actionSetIPv4SrcAddr.addr().value()); |
| 438 | openFlowActions.add(ofa); |
| 439 | actionsLen += ofa.getLength(); |
| 440 | } |
| 441 | |
| 442 | if (actionSetIPv4DstAddr != null) { |
| 443 | OFActionNetworkLayerDestination ofa = |
| 444 | new OFActionNetworkLayerDestination(actionSetIPv4DstAddr.addr().value()); |
| 445 | openFlowActions.add(ofa); |
| 446 | actionsLen += ofa.getLength(); |
| 447 | } |
| 448 | |
| 449 | if (actionSetIpToS != null) { |
| 450 | OFActionNetworkTypeOfService ofa = |
| 451 | new OFActionNetworkTypeOfService(actionSetIpToS.ipToS()); |
| 452 | openFlowActions.add(ofa); |
| 453 | actionsLen += ofa.getLength(); |
| 454 | } |
| 455 | |
| 456 | if (actionSetTcpUdpSrcPort != null) { |
| 457 | OFActionTransportLayerSource ofa = |
| 458 | new OFActionTransportLayerSource(actionSetTcpUdpSrcPort.port()); |
| 459 | openFlowActions.add(ofa); |
| 460 | actionsLen += ofa.getLength(); |
| 461 | } |
| 462 | |
| 463 | if (actionSetTcpUdpDstPort != null) { |
| 464 | OFActionTransportLayerDestination ofa = |
| 465 | new OFActionTransportLayerDestination(actionSetTcpUdpDstPort.port()); |
| 466 | openFlowActions.add(ofa); |
| 467 | actionsLen += ofa.getLength(); |
| 468 | } |
| 469 | |
| 470 | if (actionEnqueue != null) { |
| 471 | OFActionEnqueue ofa = |
| 472 | new OFActionEnqueue(actionEnqueue.port().value(), |
| 473 | actionEnqueue.queueId()); |
| 474 | openFlowActions.add(ofa); |
| 475 | actionsLen += ofa.getLength(); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | fm.setIdleTimeout((short) 0) |
| 480 | .setHardTimeout((short) 0) |
| 481 | .setPriority((short) 100) |
| 482 | .setBufferId(OFPacketOut.BUFFER_ID_NONE); |
| 483 | fm |
| 484 | .setCookie(id.value()) |
| 485 | .setMatch(match) |
| 486 | .setActions(openFlowActions) |
| 487 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen); |
| 488 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 489 | |
| 490 | // ********* END COPIED |
| 491 | |
| 492 | return fm; |
| 493 | } |
| 494 | else if(stat != null) { |
| 495 | // convert stat |
| 496 | OFFlowMod fm = new OFFlowMod(); |
| 497 | fm.setCookie(stat.getCookie()); |
| 498 | fm.setCommand(OFFlowMod.OFPFC_DELETE_STRICT); |
| 499 | fm.setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 500 | fm.setMatch(stat.getMatch()); |
| 501 | fm.setPriority(stat.getPriority()); |
| 502 | fm.setOutPort(OFPort.OFPP_NONE); |
| 503 | // fm.setActions(stat.getActions()); |
| 504 | // fm.setIdleTimeout(stat.getIdleTimeout()); |
| 505 | // fm.setHardTimeout(stat.getHardTimeout()); |
| 506 | return fm; |
| 507 | } |
| 508 | return null; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Return the hash code of the Flow Entry ID |
| 513 | */ |
| 514 | @Override |
| 515 | public int hashCode() { |
| 516 | return id.hashCode(); |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Brian O'Connor | 0d6ba51 | 2013-11-05 15:17:44 -0800 | [diff] [blame] | 519 | /** |
| 520 | * Returns true of the object is another Flow Entry ID with |
| 521 | * the same value; otherwise, returns false. |
| 522 | * |
| 523 | * @param Object to compare |
| 524 | */ |
| 525 | @Override |
| 526 | public boolean equals(Object obj){ |
| 527 | if(obj.getClass() == this.getClass()) { |
| 528 | FlowEntryWrapper entry = (FlowEntryWrapper) obj; |
| 529 | return this.id.equals(entry.id); |
| 530 | } |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public String toString() { |
| 536 | return id.toString(); |
| 537 | } |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 538 | } |