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; |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 4 | import java.util.Objects; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 5 | import java.util.Set; |
| 6 | |
| 7 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.Dpid; |
| 10 | import net.onrc.onos.core.util.FlowEntryActions; |
| 11 | import net.onrc.onos.core.util.FlowEntryId; |
| 12 | import net.onrc.onos.core.util.FlowEntryUserState; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 13 | |
| 14 | /** |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 15 | * A class to represent an OpenFlow FlowMod. |
| 16 | * It is OpenFlow v.1.0-centric and contains a Match and an Action. |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | public class FlowEntry { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 20 | protected long sw; |
| 21 | protected Match match; |
| 22 | protected Set<Action> actions; |
| 23 | protected Operator operator; |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 24 | protected int hardTimeout = 0; |
| 25 | protected int idleTimeout = 0; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 26 | protected long flowEntryId; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 28 | /** |
| 29 | * Constructor. |
| 30 | * |
| 31 | * @param sw switch's DPID |
| 32 | * @param srcPort source port on switch |
| 33 | * @param dstPort output port on switch |
| 34 | * @param srcMac source Ethernet MAC address |
| 35 | * @param dstMac destination Ethernet MAC address |
| 36 | * @param srcIpAddress source IP address |
| 37 | * @param dstIpAddress destination IP address |
| 38 | * @param operator OpenFlow operation/command (add, remove, etc.) |
| 39 | */ |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 40 | // CHECKSTYLE:OFF suppress the warning about too many parameters |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | public FlowEntry(long sw, long srcPort, long dstPort, |
| 42 | MACAddress srcMac, MACAddress dstMac, |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 43 | int srcIpAddress, int dstIpAddress, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | Operator operator) { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 45 | // CHECKSTYLE:ON |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | this.sw = sw; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 47 | this.match = new Match(sw, srcPort, srcMac, dstMac, srcIpAddress, dstIpAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | this.actions = new HashSet<Action>(); |
| 49 | this.actions.add(new ForwardAction(dstPort)); |
| 50 | this.operator = operator; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 51 | this.flowEntryId = hashCode(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 54 | /** |
| 55 | * Gets the switch for this FlowEntry. |
| 56 | * |
| 57 | * @return the switch's DPID |
| 58 | */ |
| 59 | public long getSwitch() { |
| 60 | return sw; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the operator (i.e. add, remove, error). |
| 65 | * |
| 66 | * @return the operator |
| 67 | */ |
| 68 | public Operator getOperator() { |
| 69 | return operator; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the FlowMod operation (i.e. add, remove, error). |
| 74 | * |
| 75 | * @param op the operator |
| 76 | */ |
| 77 | public void setOperator(Operator op) { |
| 78 | operator = op; |
| 79 | } |
| 80 | |
| 81 | /** |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 82 | * Gets hard timeout value in seconds. |
| 83 | * |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 84 | * @return the hard timeout value in seconds |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 85 | */ |
| 86 | public int getHardTimeout() { |
| 87 | return hardTimeout; |
| 88 | } |
| 89 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 90 | /** |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 91 | * Sets hard timeout value in seconds. |
| 92 | * |
Sho SHIMIZU | a1199fa | 2014-06-10 18:11:12 -0700 | [diff] [blame] | 93 | * @param hardTimeout hard timeout value in seconds |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 94 | */ |
| 95 | public void setHardTimeout(int hardTimeout) { |
| 96 | this.hardTimeout = hardTimeout; |
| 97 | } |
| 98 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 99 | /** |
| 100 | * Gets idle timeout value in seconds. |
| 101 | * |
| 102 | * @return the idle timeout value in seconds |
| 103 | */ |
| 104 | public int getIdleTimeout() { |
| 105 | return idleTimeout; |
| 106 | } |
| 107 | |
| 108 | /** |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 109 | * Sets idle timeout value in seconds. |
| 110 | * |
Sho SHIMIZU | a1199fa | 2014-06-10 18:11:12 -0700 | [diff] [blame] | 111 | * @param idleTimeout idle timeout value in seconds |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 112 | */ |
| 113 | public void setIdleTimeout(int idleTimeout) { |
| 114 | this.idleTimeout = idleTimeout; |
| 115 | } |
| 116 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 117 | /** |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 118 | * Gets flowEntryId. |
| 119 | * |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 120 | * @return the flowEntryId to be set in cookie |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 121 | */ |
| 122 | public long getFlowEntryId() { |
| 123 | return flowEntryId; |
| 124 | } |
| 125 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 126 | /** |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 127 | * Sets flowEntryId. |
| 128 | * |
Sho SHIMIZU | a1199fa | 2014-06-10 18:11:12 -0700 | [diff] [blame] | 129 | * @param flowEntryId flowEntryId to be set in cookie |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 130 | */ |
| 131 | public void setFlowEntryId(long flowEntryId) { |
| 132 | this.flowEntryId = flowEntryId; |
| 133 | } |
| 134 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 135 | /** |
| 136 | * Converts the FlowEntry in to a legacy FlowEntry object. |
| 137 | * |
| 138 | * @return an equivalent legacy FlowEntry object |
| 139 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | public net.onrc.onos.core.util.FlowEntry getFlowEntry() { |
| 141 | net.onrc.onos.core.util.FlowEntry entry = new net.onrc.onos.core.util.FlowEntry(); |
| 142 | entry.setDpid(new Dpid(sw)); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 143 | entry.setFlowEntryId(new FlowEntryId(flowEntryId)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | entry.setFlowEntryMatch(match.getFlowEntryMatch()); |
| 145 | FlowEntryActions flowEntryActions = new FlowEntryActions(); |
| 146 | for (Action action : actions) { |
| 147 | flowEntryActions.addAction(action.getFlowEntryAction()); |
| 148 | } |
| 149 | entry.setFlowEntryActions(flowEntryActions); |
| 150 | switch (operator) { |
| 151 | case ADD: |
| 152 | entry.setFlowEntryUserState(FlowEntryUserState.FE_USER_MODIFY); |
| 153 | break; |
| 154 | case REMOVE: |
| 155 | entry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 156 | break; |
| 157 | default: |
| 158 | break; |
| 159 | } |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 160 | entry.setIdleTimeout(idleTimeout); |
| 161 | entry.setHardTimeout(hardTimeout); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | return entry; |
| 163 | } |
| 164 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 165 | /** |
| 166 | * Returns a String representation of this FlowEntry. |
| 167 | * |
| 168 | * @return the FlowEntry as a String |
| 169 | */ |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 170 | @Override |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 171 | public String toString() { |
| 172 | return match + "->" + actions; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame^] | 175 | /** |
| 176 | * Generates hash using Objects.hash() on the match and actions. |
| 177 | */ |
| 178 | @Override |
| 179 | public int hashCode() { |
| 180 | return Objects.hash(match, actions); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Flow Entries are equal if their matches and action sets are equal. |
| 185 | * |
| 186 | * @return true if equal, false otherwise |
| 187 | */ |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 188 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 189 | public boolean equals(Object o) { |
| 190 | if (!(o instanceof FlowEntry)) { |
| 191 | return false; |
| 192 | } |
| 193 | FlowEntry other = (FlowEntry) o; |
| 194 | // Note: we should not consider the operator for this comparison |
| 195 | return this.match.equals(other.match) |
| 196 | && this.actions.containsAll(other.actions) |
| 197 | && other.actions.containsAll(this.actions); |
| 198 | } |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 199 | } |