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