1 package org.openflow.protocol;
3 import java.nio.ByteBuffer;
4 import java.util.LinkedList;
7 import org.openflow.protocol.action.OFAction;
8 import org.openflow.protocol.factory.OFActionFactory;
9 import org.openflow.protocol.factory.OFActionFactoryAware;
10 import org.openflow.util.U16;
13 * Represents an ofp_flow_mod message
14 * @author David Erickson (daviderickson@cs.stanford.edu)
17 public class OFFlowMod extends OFMessage implements OFActionFactoryAware, Cloneable {
18 public static int MINIMUM_LENGTH = 72;
20 public static final short OFPFC_ADD = 0; /* New flow. */
21 public static final short OFPFC_MODIFY = 1; /* Modify all matching flows. */
22 public static final short OFPFC_MODIFY_STRICT = 2; /* Modify entry strictly matching wildcards */
23 public static final short OFPFC_DELETE=3; /* Delete all matching flows. */
24 public static final short OFPFC_DELETE_STRICT =4; /* Strictly match wildcards and priority. */
26 protected OFActionFactory actionFactory;
27 protected OFMatch match;
28 protected long cookie;
29 protected short command;
30 protected short idleTimeout;
31 protected short hardTimeout;
32 protected short priority;
33 protected int bufferId;
34 protected short outPort;
35 protected short flags;
36 protected List<OFAction> actions;
40 this.type = OFType.FLOW_MOD;
41 this.length = U16.t(MINIMUM_LENGTH);
48 public int getBufferId() {
56 public OFFlowMod setBufferId(int bufferId) {
57 this.bufferId = bufferId;
65 public long getCookie() {
73 public OFFlowMod setCookie(long cookie) {
82 public short getCommand() {
90 public OFFlowMod setCommand(short command) {
91 this.command = command;
99 public short getFlags() {
107 public OFFlowMod setFlags(short flags) {
116 public short getHardTimeout() {
117 return this.hardTimeout;
124 public OFFlowMod setHardTimeout(short hardTimeout) {
125 this.hardTimeout = hardTimeout;
133 public short getIdleTimeout() {
134 return this.idleTimeout;
141 public OFFlowMod setIdleTimeout(short idleTimeout) {
142 this.idleTimeout = idleTimeout;
147 * Gets a copy of the OFMatch object for this FlowMod, changes to this
148 * object do not modify the FlowMod
151 public OFMatch getMatch() {
159 public OFFlowMod setMatch(OFMatch match) {
168 public short getOutPort() {
176 public OFFlowMod setOutPort(short outPort) {
177 this.outPort = outPort;
185 public OFFlowMod setOutPort(OFPort port) {
186 this.outPort = port.getValue();
194 public short getPriority() {
195 return this.priority;
202 public OFFlowMod setPriority(short priority) {
203 this.priority = priority;
208 * Returns read-only copies of the actions contained in this Flow Mod
209 * @return a list of ordered OFAction objects
211 public List<OFAction> getActions() {
216 * Sets the list of actions this Flow Mod contains
217 * @param actions a list of ordered OFAction objects
219 public OFFlowMod setActions(List<OFAction> actions) {
220 this.actions = actions;
225 public void readFrom(ByteBuffer data) {
226 super.readFrom(data);
227 if (this.match == null)
228 this.match = new OFMatch();
229 this.match.readFrom(data);
230 this.cookie = data.getLong();
231 this.command = data.getShort();
232 this.idleTimeout = data.getShort();
233 this.hardTimeout = data.getShort();
234 this.priority = data.getShort();
235 this.bufferId = data.getInt();
236 this.outPort = data.getShort();
237 this.flags = data.getShort();
238 if (this.actionFactory == null)
239 throw new RuntimeException("OFActionFactory not set");
240 this.actions = this.actionFactory.parseActions(data, getLengthU() -
245 public void writeTo(ByteBuffer data) {
247 this.match.writeTo(data);
248 data.putLong(cookie);
249 data.putShort(command);
250 data.putShort(idleTimeout);
251 data.putShort(hardTimeout);
252 data.putShort(priority);
253 data.putInt(bufferId);
254 data.putShort(outPort);
255 data.putShort(flags);
256 if (actions != null) {
257 for (OFAction action : actions) {
258 action.writeTo(data);
264 public void setActionFactory(OFActionFactory actionFactory) {
265 this.actionFactory = actionFactory;
269 public int hashCode() {
270 final int prime = 227;
271 int result = super.hashCode();
272 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
273 result = prime * result + bufferId;
274 result = prime * result + command;
275 result = prime * result + (int) (cookie ^ (cookie >>> 32));
276 result = prime * result + flags;
277 result = prime * result + hardTimeout;
278 result = prime * result + idleTimeout;
279 result = prime * result + ((match == null) ? 0 : match.hashCode());
280 result = prime * result + outPort;
281 result = prime * result + priority;
286 public boolean equals(Object obj) {
290 if (!super.equals(obj)) {
293 if (!(obj instanceof OFFlowMod)) {
296 OFFlowMod other = (OFFlowMod) obj;
297 if (actions == null) {
298 if (other.actions != null) {
301 } else if (!actions.equals(other.actions)) {
304 if (bufferId != other.bufferId) {
307 if (command != other.command) {
310 if (cookie != other.cookie) {
313 if (flags != other.flags) {
316 if (hardTimeout != other.hardTimeout) {
319 if (idleTimeout != other.idleTimeout) {
323 if (other.match != null) {
326 } else if (!match.equals(other.match)) {
329 if (outPort != other.outPort) {
332 if (priority != other.priority) {
339 * @see java.lang.Object#clone()
342 public OFFlowMod clone() {
344 OFMatch neoMatch = match.clone();
345 OFFlowMod flowMod = (OFFlowMod) super.clone();
346 flowMod.setMatch(neoMatch);
347 List<OFAction> neoActions = new LinkedList<OFAction>();
348 for(OFAction action: this.actions)
349 neoActions.add((OFAction) action.clone());
350 flowMod.setActions(neoActions);
352 } catch (CloneNotSupportedException e) {
354 throw new RuntimeException(e);
359 * @see java.lang.Object#toString()
362 public String toString() {
363 return "OFFlowMod [actionFactory=" + actionFactory + ", actions="
364 + actions + ", bufferId=" + bufferId + ", command=" + command
365 + ", cookie=" + cookie + ", flags=" + flags + ", hardTimeout="
366 + hardTimeout + ", idleTimeout=" + idleTimeout + ", match="
367 + match + ", outPort=" + outPort + ", priority=" + priority
368 + ", length=" + length + ", type=" + type + ", version="
369 + version + ", xid=" + xid + "]";