1 package org.openflow.protocol.action;
3 import java.io.Serializable;
4 import java.nio.ByteBuffer;
6 import org.openflow.util.U16;
9 * The base class for all OpenFlow Actions.
11 * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 11, 2010
13 public class OFAction implements Cloneable, Serializable{
15 * Note the true minimum length for this header is 8 including a pad to 64
16 * bit alignment, however as this base class is used for demuxing an
17 * incoming Action, it is only necessary to read the first 4 bytes. All
18 * Actions extending this class are responsible for reading/writing the
19 * first 8 bytes, including the pad if necessary.
21 public static int MINIMUM_LENGTH = 4;
22 public static int OFFSET_LENGTH = 2;
23 public static int OFFSET_TYPE = 0;
25 protected OFActionType type;
26 protected short length;
29 * Get the length of this message
33 public short getLength() {
38 * Get the length of this message, unsigned
42 public int getLengthU() {
47 * Set the length of this message
51 public OFAction setLength(short length) {
57 * Get the type of this message
59 * @return OFActionType enum
61 public OFActionType getType() {
66 * Set the type of this message
70 public void setType(OFActionType type) {
75 * Returns a summary of the message
76 * @return "ofmsg=v=$version;t=$type:l=$len:xid=$xid"
78 public String toString() {
80 ";t=" + this.getType() +
81 ";l=" + this.getLength();
85 * Given the output from toString(),
86 * create a new OFAction
90 public static OFAction fromString(String val) {
91 String tokens[] = val.split(";");
92 if (!tokens[0].equals("ofaction"))
93 throw new IllegalArgumentException("expected 'ofaction' but got '" +
95 String type_tokens[] = tokens[1].split("=");
96 String len_tokens[] = tokens[2].split("=");
97 OFAction action = new OFAction();
98 action.setLength(Short.valueOf(len_tokens[1]));
99 action.setType(OFActionType.valueOf(type_tokens[1]));
103 public void readFrom(ByteBuffer data) {
104 this.type = OFActionType.valueOf(data.getShort());
105 this.length = data.getShort();
106 // Note missing PAD, see MINIMUM_LENGTH comment for details
109 public void writeTo(ByteBuffer data) {
110 data.putShort(type.getTypeValue());
111 data.putShort(length);
112 // Note missing PAD, see MINIMUM_LENGTH comment for details
116 public int hashCode() {
117 final int prime = 347;
119 result = prime * result + length;
120 result = prime * result + ((type == null) ? 0 : type.hashCode());
125 public boolean equals(Object obj) {
132 if (!(obj instanceof OFAction)) {
135 OFAction other = (OFAction) obj;
136 if (length != other.length) {
140 if (other.type != null) {
143 } else if (!type.equals(other.type)) {
150 * @see java.lang.Object#clone()
153 public OFAction clone() throws CloneNotSupportedException {
154 return (OFAction) super.clone();