Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.HashSet; |
| 4 | import java.util.Set; |
| 5 | |
| 6 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.util.Dpid; |
| 9 | import net.onrc.onos.core.util.FlowEntryActions; |
| 10 | import net.onrc.onos.core.util.FlowEntryId; |
| 11 | import net.onrc.onos.core.util.FlowEntryUserState; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 12 | |
| 13 | /** |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 14 | * @author Brian O'Connor <bocon@onlab.us> |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | public class FlowEntry { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | protected long sw; |
| 19 | protected Match match; |
| 20 | protected Set<Action> actions; |
| 21 | protected Operator operator; |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame^] | 22 | protected int hardTimeout = 0; |
| 23 | protected int idleTimeout = 0; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | |
| 25 | public FlowEntry(long sw, long srcPort, long dstPort, |
| 26 | MACAddress srcMac, MACAddress dstMac, |
| 27 | Operator operator) { |
| 28 | this.sw = sw; |
| 29 | this.match = new Match(sw, srcPort, srcMac, dstMac); |
| 30 | this.actions = new HashSet<Action>(); |
| 31 | this.actions.add(new ForwardAction(dstPort)); |
| 32 | this.operator = operator; |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame^] | 33 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | } |
| 35 | |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame^] | 36 | /*** |
| 37 | * Gets hard timeout value in seconds. |
| 38 | * |
| 39 | * @return hardTimeout |
| 40 | */ |
| 41 | public int getHardTimeout() { |
| 42 | return hardTimeout; |
| 43 | } |
| 44 | |
| 45 | /*** |
| 46 | * Gets idle timeout value in seconds. |
| 47 | * |
| 48 | * @return idleTimeout |
| 49 | */ |
| 50 | public int getIdleTimeout() { |
| 51 | return idleTimeout; |
| 52 | } |
| 53 | |
| 54 | /*** |
| 55 | * Sets hard timeout value in seconds. |
| 56 | * |
| 57 | * @param hardTimeout |
| 58 | */ |
| 59 | public void setHardTimeout(int hardTimeout) { |
| 60 | this.hardTimeout = hardTimeout; |
| 61 | } |
| 62 | |
| 63 | /*** |
| 64 | * Sets idle timeout value in seconds. |
| 65 | * |
| 66 | * @param idleTimeout |
| 67 | */ |
| 68 | public void setIdleTimeout(int idleTimeout) { |
| 69 | this.idleTimeout = idleTimeout; |
| 70 | } |
| 71 | |
| 72 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | public String toString() { |
| 74 | return match + "->" + actions; |
| 75 | } |
| 76 | |
| 77 | public long getSwitch() { |
| 78 | return sw; |
| 79 | } |
| 80 | |
| 81 | public Operator getOperator() { |
| 82 | return operator; |
| 83 | } |
| 84 | |
| 85 | public void setOperator(Operator op) { |
| 86 | operator = op; |
| 87 | } |
| 88 | |
| 89 | public net.onrc.onos.core.util.FlowEntry getFlowEntry() { |
| 90 | net.onrc.onos.core.util.FlowEntry entry = new net.onrc.onos.core.util.FlowEntry(); |
| 91 | entry.setDpid(new Dpid(sw)); |
| 92 | entry.setFlowEntryId(new FlowEntryId(hashCode())); // naive, but useful for now |
| 93 | entry.setFlowEntryMatch(match.getFlowEntryMatch()); |
| 94 | FlowEntryActions flowEntryActions = new FlowEntryActions(); |
| 95 | for (Action action : actions) { |
| 96 | flowEntryActions.addAction(action.getFlowEntryAction()); |
| 97 | } |
| 98 | entry.setFlowEntryActions(flowEntryActions); |
| 99 | switch (operator) { |
| 100 | case ADD: |
| 101 | entry.setFlowEntryUserState(FlowEntryUserState.FE_USER_MODIFY); |
| 102 | break; |
| 103 | case REMOVE: |
| 104 | entry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 105 | break; |
| 106 | default: |
| 107 | break; |
| 108 | } |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame^] | 109 | entry.setIdleTimeout(idleTimeout); |
| 110 | entry.setHardTimeout(hardTimeout); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | return entry; |
| 112 | } |
| 113 | |
| 114 | |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame^] | 115 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 116 | public int hashCode() { |
| 117 | return match.hashCode(); |
| 118 | } |
| 119 | |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame^] | 120 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | public boolean equals(Object o) { |
| 122 | if (!(o instanceof FlowEntry)) { |
| 123 | return false; |
| 124 | } |
| 125 | FlowEntry other = (FlowEntry) o; |
| 126 | // Note: we should not consider the operator for this comparison |
| 127 | return this.match.equals(other.match) |
| 128 | && this.actions.containsAll(other.actions) |
| 129 | && other.actions.containsAll(this.actions); |
| 130 | } |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 131 | } |