blob: c738cbc9573f231e14189cf0d5b65fbbab25e5d8 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Brian O'Connor7f8e3012014-02-15 23:59:29 -08002
3import java.util.HashSet;
Brian O'Connora84723c2014-06-13 00:26:49 -07004import java.util.Objects;
Brian O'Connor7f8e3012014-02-15 23:59:29 -08005import java.util.Set;
6
7import net.floodlightcontroller.util.MACAddress;
Jonathan Hartaa380972014-04-03 10:24:46 -07008import net.onrc.onos.core.intent.IntentOperation.Operator;
Jonathan Hart23701d12014-04-03 10:45:48 -07009import net.onrc.onos.core.util.Dpid;
10import net.onrc.onos.core.util.FlowEntryActions;
11import net.onrc.onos.core.util.FlowEntryId;
12import net.onrc.onos.core.util.FlowEntryUserState;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080013
14/**
Brian O'Connora84723c2014-06-13 00:26:49 -070015 * A class to represent an OpenFlow FlowMod.
16 * It is OpenFlow v.1.0-centric and contains a Match and an Action.
Brian O'Connor7f8e3012014-02-15 23:59:29 -080017 */
18
19public class FlowEntry {
Ray Milkey269ffb92014-04-03 14:43:30 -070020 protected long sw;
21 protected Match match;
22 protected Set<Action> actions;
23 protected Operator operator;
TeruU30c0c932014-05-15 16:47:41 -070024 protected int hardTimeout = 0;
25 protected int idleTimeout = 0;
TeruU9e530662014-05-18 11:49:37 -070026 protected long flowEntryId;
Ray Milkey269ffb92014-04-03 14:43:30 -070027
Brian O'Connora84723c2014-06-13 00:26:49 -070028 /**
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 Shah399a2922014-05-28 01:57:40 -070040// CHECKSTYLE:OFF suppress the warning about too many parameters
Ray Milkey269ffb92014-04-03 14:43:30 -070041 public FlowEntry(long sw, long srcPort, long dstPort,
42 MACAddress srcMac, MACAddress dstMac,
Komal Shah399a2922014-05-28 01:57:40 -070043 int srcIpAddress, int dstIpAddress,
Ray Milkey269ffb92014-04-03 14:43:30 -070044 Operator operator) {
Komal Shah399a2922014-05-28 01:57:40 -070045// CHECKSTYLE:ON
Ray Milkey269ffb92014-04-03 14:43:30 -070046 this.sw = sw;
Komal Shah399a2922014-05-28 01:57:40 -070047 this.match = new Match(sw, srcPort, srcMac, dstMac, srcIpAddress, dstIpAddress);
Ray Milkey269ffb92014-04-03 14:43:30 -070048 this.actions = new HashSet<Action>();
49 this.actions.add(new ForwardAction(dstPort));
50 this.operator = operator;
TeruU9e530662014-05-18 11:49:37 -070051 this.flowEntryId = hashCode();
Ray Milkey269ffb92014-04-03 14:43:30 -070052 }
53
Brian O'Connora84723c2014-06-13 00:26:49 -070054 /**
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 /**
TeruU30c0c932014-05-15 16:47:41 -070082 * Gets hard timeout value in seconds.
83 *
TeruU9e530662014-05-18 11:49:37 -070084 * @return the hard timeout value in seconds
TeruU30c0c932014-05-15 16:47:41 -070085 */
86 public int getHardTimeout() {
87 return hardTimeout;
88 }
89
Brian O'Connora84723c2014-06-13 00:26:49 -070090 /**
TeruU30c0c932014-05-15 16:47:41 -070091 * Sets hard timeout value in seconds.
92 *
Sho SHIMIZUa1199fa2014-06-10 18:11:12 -070093 * @param hardTimeout hard timeout value in seconds
TeruU30c0c932014-05-15 16:47:41 -070094 */
95 public void setHardTimeout(int hardTimeout) {
96 this.hardTimeout = hardTimeout;
97 }
98
Brian O'Connora84723c2014-06-13 00:26:49 -070099 /**
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 /**
TeruU30c0c932014-05-15 16:47:41 -0700109 * Sets idle timeout value in seconds.
110 *
Sho SHIMIZUa1199fa2014-06-10 18:11:12 -0700111 * @param idleTimeout idle timeout value in seconds
TeruU30c0c932014-05-15 16:47:41 -0700112 */
113 public void setIdleTimeout(int idleTimeout) {
114 this.idleTimeout = idleTimeout;
115 }
116
Brian O'Connora84723c2014-06-13 00:26:49 -0700117 /**
TeruU9e530662014-05-18 11:49:37 -0700118 * Gets flowEntryId.
119 *
Brian O'Connora84723c2014-06-13 00:26:49 -0700120 * @return the flowEntryId to be set in cookie
TeruU9e530662014-05-18 11:49:37 -0700121 */
122 public long getFlowEntryId() {
123 return flowEntryId;
124 }
125
Brian O'Connora84723c2014-06-13 00:26:49 -0700126 /**
TeruU9e530662014-05-18 11:49:37 -0700127 * Sets flowEntryId.
128 *
Sho SHIMIZUa1199fa2014-06-10 18:11:12 -0700129 * @param flowEntryId flowEntryId to be set in cookie
TeruU9e530662014-05-18 11:49:37 -0700130 */
131 public void setFlowEntryId(long flowEntryId) {
132 this.flowEntryId = flowEntryId;
133 }
134
Brian O'Connora84723c2014-06-13 00:26:49 -0700135 /**
136 * Converts the FlowEntry in to a legacy FlowEntry object.
137 *
138 * @return an equivalent legacy FlowEntry object
139 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 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));
TeruU9e530662014-05-18 11:49:37 -0700143 entry.setFlowEntryId(new FlowEntryId(flowEntryId));
Ray Milkey269ffb92014-04-03 14:43:30 -0700144 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 }
TeruU30c0c932014-05-15 16:47:41 -0700160 entry.setIdleTimeout(idleTimeout);
161 entry.setHardTimeout(hardTimeout);
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 return entry;
163 }
164
Brian O'Connora84723c2014-06-13 00:26:49 -0700165 /**
166 * Returns a String representation of this FlowEntry.
167 *
168 * @return the FlowEntry as a String
169 */
TeruU30c0c932014-05-15 16:47:41 -0700170 @Override
Brian O'Connora84723c2014-06-13 00:26:49 -0700171 public String toString() {
172 return match + "->" + actions;
Ray Milkey269ffb92014-04-03 14:43:30 -0700173 }
174
Brian O'Connora84723c2014-06-13 00:26:49 -0700175 /**
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 */
TeruU30c0c932014-05-15 16:47:41 -0700188 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700189 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'Connor6dc44e92014-02-24 21:23:46 -0800199}