blob: 370918a6321c068a71a6b650204e94ade82ce4a5 [file] [log] [blame]
Brian O'Connor8c166a72013-11-14 18:41:48 -08001package net.onrc.onos.ofcontroller.flowprogrammer;
Brian O'Connora8e49802013-10-30 20:49:59 -07002
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.Collection;
Brian O'Connora8e49802013-10-30 20:49:59 -07006import java.util.HashMap;
7import java.util.HashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11import java.util.concurrent.ExecutionException;
Brian O'Connora8e49802013-10-30 20:49:59 -070012import java.util.concurrent.Future;
13
Brian O'Connor0d6ba512013-11-05 15:17:44 -080014import org.openflow.protocol.OFFlowMod;
Brian O'Connora8e49802013-10-30 20:49:59 -070015import org.openflow.protocol.OFMatch;
Brian O'Connor0d6ba512013-11-05 15:17:44 -080016import org.openflow.protocol.OFMessage;
17import org.openflow.protocol.OFPacketOut;
18import org.openflow.protocol.OFPort;
Brian O'Connora8e49802013-10-30 20:49:59 -070019import org.openflow.protocol.OFStatisticsRequest;
Brian O'Connor0d6ba512013-11-05 15:17:44 -080020import org.openflow.protocol.action.OFAction;
21import org.openflow.protocol.action.OFActionDataLayerDestination;
22import org.openflow.protocol.action.OFActionDataLayerSource;
23import org.openflow.protocol.action.OFActionEnqueue;
24import org.openflow.protocol.action.OFActionNetworkLayerDestination;
25import org.openflow.protocol.action.OFActionNetworkLayerSource;
26import org.openflow.protocol.action.OFActionNetworkTypeOfService;
27import org.openflow.protocol.action.OFActionOutput;
28import org.openflow.protocol.action.OFActionStripVirtualLan;
29import org.openflow.protocol.action.OFActionTransportLayerDestination;
30import org.openflow.protocol.action.OFActionTransportLayerSource;
31import org.openflow.protocol.action.OFActionVirtualLanIdentifier;
32import org.openflow.protocol.action.OFActionVirtualLanPriorityCodePoint;
Brian O'Connora8e49802013-10-30 20:49:59 -070033import org.openflow.protocol.statistics.OFFlowStatisticsReply;
34import org.openflow.protocol.statistics.OFFlowStatisticsRequest;
35import org.openflow.protocol.statistics.OFStatistics;
36import org.openflow.protocol.statistics.OFStatisticsType;
37import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
Brian O'Connor8c166a72013-11-14 18:41:48 -080040import com.google.common.collect.Lists;
41import com.tinkerpop.blueprints.Direction;
42
Brian O'Connora8e49802013-10-30 20:49:59 -070043import net.floodlightcontroller.core.IFloodlightProviderService;
44import net.floodlightcontroller.core.IOFSwitch;
45import net.floodlightcontroller.core.IOFSwitchListener;
46import net.floodlightcontroller.core.module.FloodlightModuleContext;
47import net.floodlightcontroller.core.module.FloodlightModuleException;
48import net.floodlightcontroller.core.module.IFloodlightModule;
49import net.floodlightcontroller.core.module.IFloodlightService;
Brian O'Connor8c166a72013-11-14 18:41:48 -080050import net.floodlightcontroller.restserver.IRestApiService;
51import net.onrc.onos.datagrid.IDatagridService;
Brian O'Connora8e49802013-10-30 20:49:59 -070052import net.onrc.onos.graph.GraphDBOperation;
53import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
54import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Brian O'Connor8c166a72013-11-14 18:41:48 -080055import net.onrc.onos.ofcontroller.core.module.IOnosService;
56import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService;
Brian O'Connora8e49802013-10-30 20:49:59 -070057import net.onrc.onos.ofcontroller.util.Dpid;
Brian O'Connor0d6ba512013-11-05 15:17:44 -080058import net.onrc.onos.ofcontroller.util.FlowEntryAction;
59import net.onrc.onos.ofcontroller.util.FlowEntryActions;
Brian O'Connora8e49802013-10-30 20:49:59 -070060import net.onrc.onos.ofcontroller.util.FlowEntryId;
Brian O'Connor0d6ba512013-11-05 15:17:44 -080061import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionEnqueue;
62import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionOutput;
63import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetEthernetAddr;
64import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetIPv4Addr;
65import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetIpToS;
66import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetTcpUdpPort;
67import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetVlanId;
68import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionSetVlanPriority;
69import net.onrc.onos.ofcontroller.util.FlowEntryAction.ActionStripVlan;
Brian O'Connor8c166a72013-11-14 18:41:48 -080070import net.onrc.onos.registry.controller.IControllerRegistryService;
Brian O'Connora8e49802013-10-30 20:49:59 -070071
Brian O'Connor8c166a72013-11-14 18:41:48 -080072public class FlowSynchronizer implements IFlowSyncService, IOFSwitchListener {
Brian O'Connora8e49802013-10-30 20:49:59 -070073
Brian O'Connor0d6ba512013-11-05 15:17:44 -080074 protected static Logger log = LoggerFactory.getLogger(FlowSynchronizer.class);
Brian O'Connora8e49802013-10-30 20:49:59 -070075 protected IFloodlightProviderService floodlightProvider;
Brian O'Connor8c166a72013-11-14 18:41:48 -080076 protected IControllerRegistryService registryService;
77 protected IFlowPusherService pusher;
78
79 private GraphDBOperation dbHandler;
80 private Map<IOFSwitch, Thread> switchThread = new HashMap<IOFSwitch, Thread>();
Brian O'Connora8e49802013-10-30 20:49:59 -070081
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'Connor8c166a72013-11-14 18:41:48 -080089// 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'Connora8e49802013-10-30 20:49:59 -070097 this.swObj = dbHandler.searchSwitch(dpid.toString());
Brian O'Connor8c166a72013-11-14 18:41:48 -080098 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'Connora8e49802013-10-30 20:49:59 -0700107 }
108
109 @Override
110 public void run() {
111 //TODO: use a FlowEntryId, FlowEntry HashMap
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800112 Set<FlowEntryWrapper> graphEntries = getFlowEntriesFromGraph();
113 Set<FlowEntryWrapper> switchEntries = getFlowEntriesFromSwitch();
Brian O'Connora8e49802013-10-30 20:49:59 -0700114 compare(graphEntries, switchEntries);
115 }
116
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800117 private void compare(Set<FlowEntryWrapper> graphEntries, Set<FlowEntryWrapper> switchEntries) {
118
119 /* old impl
Brian O'Connora8e49802013-10-30 20:49:59 -0700120 System.out.println("graph entries: " + graphEntries);
121 System.out.println("switch entries: " + switchEntries);
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800122 Set<FlowEntryWrapper> entriesToAdd = new HashSet<FlowEntryWrapper>(graphEntries);
Brian O'Connora8e49802013-10-30 20:49:59 -0700123 entriesToAdd.removeAll(switchEntries);
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800124 Set<FlowEntryWrapper> entriesToRemove = switchEntries;
Brian O'Connora8e49802013-10-30 20:49:59 -0700125 entriesToRemove.removeAll(graphEntries);
126 System.out.println("add: " + entriesToAdd);
127 System.out.println("remove: " + entriesToRemove);
128 //FlowDatabaseOperation for converting flowentries
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800129 */
Brian O'Connora8e49802013-10-30 20:49:59 -0700130
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800131 /* 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'Connora8e49802013-10-30 20:49:59 -0700138 }
139 else {
140 // remove fid from the switch
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800141 System.out.println("** remove entry " + entry.id);
142 // TODO: use remove strict message
143 writeToSwitch(entry.getOFMessage());
144 removed++;
Brian O'Connora8e49802013-10-30 20:49:59 -0700145 }
146 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800147 for(FlowEntryWrapper entry : graphEntries) {
Brian O'Connora8e49802013-10-30 20:49:59 -0700148 // add fid to switch
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800149 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'Connora8e49802013-10-30 20:49:59 -0700157 }
158
Brian O'Connor8c166a72013-11-14 18:41:48 -0800159 //TODO: replace this with FlowPusher
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800160 private void writeToSwitch(OFMessage msg) {
Brian O'Connor8c166a72013-11-14 18:41:48 -0800161// 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'Connor0d6ba512013-11-05 15:17:44 -0800171 }
172
173 private Set<FlowEntryWrapper> getFlowEntriesFromGraph() {
174 Set<FlowEntryWrapper> entries = new HashSet<FlowEntryWrapper>();
Brian O'Connora8e49802013-10-30 20:49:59 -0700175 for(IFlowEntry entry : swObj.getFlowEntries()) {
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800176 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
177 entries.add(fe);
Brian O'Connora8e49802013-10-30 20:49:59 -0700178 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800179 System.out.println("Got " + entries.size() + " entries from graph");
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800180 return entries;
Brian O'Connora8e49802013-10-30 20:49:59 -0700181 }
182
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800183 private Set<FlowEntryWrapper> getFlowEntriesFromSwitch() {
Brian O'Connora8e49802013-10-30 20:49:59 -0700184
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'Connor0d6ba512013-11-05 15:17:44 -0800218 Set<FlowEntryWrapper> results = new HashSet<FlowEntryWrapper>();
Brian O'Connora8e49802013-10-30 20:49:59 -0700219 for(OFStatistics result : entries){
220 //System.out.println(result.getClass());
221 OFFlowStatisticsReply entry = (OFFlowStatisticsReply) result;
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800222 FlowEntryWrapper fe = new FlowEntryWrapper(entry);
223 results.add(fe);
Brian O'Connora8e49802013-10-30 20:49:59 -0700224 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800225 return results;
Brian O'Connora8e49802013-10-30 20:49:59 -0700226 }
227
228 }
229
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800230 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'Connor8c166a72013-11-14 18:41:48 -0800236
Brian O'Connora8e49802013-10-30 20:49:59 -0700237 @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'Connor8c166a72013-11-14 18:41:48 -0800243// if (registryService.hasControl(sw.getId())) {
244
245 synchronize(sw);
246// }
Brian O'Connora8e49802013-10-30 20:49:59 -0700247 }
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'Connora8e49802013-10-30 20:49:59 -0700263 }
264
265 @Override
266 public String getName() {
267 // TODO Auto-generated method stub
268 return "FlowSynchronizer";
269 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800270
271 public FlowSynchronizer() {
272 System.out.println("Initializing FlowSync...");
273 dbHandler = new GraphDBOperation("");
274 }
Brian O'Connora8e49802013-10-30 20:49:59 -0700275
Brian O'Connor8c166a72013-11-14 18:41:48 -0800276
277 //@Override
Brian O'Connora8e49802013-10-30 20:49:59 -0700278 public void init(FloodlightModuleContext context)
279 throws FloodlightModuleException {
Brian O'Connora8e49802013-10-30 20:49:59 -0700280 System.out.println("********* Starting flow sync....");
281 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800282 registryService = context.getServiceImpl(IControllerRegistryService.class);
283 pusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connora8e49802013-10-30 20:49:59 -0700284 }
285
Brian O'Connor8c166a72013-11-14 18:41:48 -0800286 //@Override
Brian O'Connora8e49802013-10-30 20:49:59 -0700287 public void startUp(FloodlightModuleContext context) {
Brian O'Connora8e49802013-10-30 20:49:59 -0700288 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800289 }
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800290
291}
292
293class 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'Connora8e49802013-10-30 20:49:59 -0700553 }
554
Brian O'Connor0d6ba512013-11-05 15:17:44 -0800555 /**
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'Connora8e49802013-10-30 20:49:59 -0700574}