2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
18 package org.openflow.protocol.action;
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.openflow.util.U16;
25 * The base class for all OpenFlow Actions.
27 * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 11, 2010
29 public class OFAction implements Cloneable {
31 * Note the true minimum length for this header is 8 including a pad to 64
32 * bit alignment, however as this base class is used for demuxing an
33 * incoming Action, it is only necessary to read the first 4 bytes. All
34 * Actions extending this class are responsible for reading/writing the
35 * first 8 bytes, including the pad if necessary.
37 public static int MINIMUM_LENGTH = 4;
38 public static int OFFSET_LENGTH = 2;
39 public static int OFFSET_TYPE = 0;
41 protected OFActionType type;
42 protected short length;
45 * Get the length of this message
49 public short getLength() {
54 * Get the length of this message, unsigned
58 public int getLengthU() {
63 * Set the length of this message
67 public OFAction setLength(short length) {
73 * Get the type of this message
75 * @return OFActionType enum
77 public OFActionType getType() {
82 * Set the type of this message
86 public void setType(OFActionType type) {
91 * Returns a summary of the message
92 * @return "ofmsg=v=$version;t=$type:l=$len:xid=$xid"
94 public String toString() {
96 ";t=" + this.getType() +
97 ";l=" + this.getLength();
101 * Given the output from toString(),
102 * create a new OFAction
106 public static OFAction fromString(String val) {
107 String tokens[] = val.split(";");
108 if (!tokens[0].equals("ofaction"))
109 throw new IllegalArgumentException("expected 'ofaction' but got '" +
111 String type_tokens[] = tokens[1].split("=");
112 String len_tokens[] = tokens[2].split("=");
113 OFAction action = new OFAction();
114 action.setLength(Short.valueOf(len_tokens[1]));
115 action.setType(OFActionType.valueOf(type_tokens[1]));
119 public void readFrom(ChannelBuffer data) {
120 this.type = OFActionType.valueOf(data.readShort());
121 this.length = data.readShort();
122 // Note missing PAD, see MINIMUM_LENGTH comment for details
125 public void writeTo(ChannelBuffer data) {
126 data.writeShort(type.getTypeValue());
127 data.writeShort(length);
128 // Note missing PAD, see MINIMUM_LENGTH comment for details
132 public int hashCode() {
133 final int prime = 347;
135 result = prime * result + length;
136 result = prime * result + ((type == null) ? 0 : type.hashCode());
141 public boolean equals(Object obj) {
148 if (!(obj instanceof OFAction)) {
151 OFAction other = (OFAction) obj;
152 if (length != other.length) {
156 if (other.type != null) {
159 } else if (!type.equals(other.type)) {
166 * @see java.lang.Object#clone()
169 public OFAction clone() throws CloneNotSupportedException {
170 return (OFAction) super.clone();