Fix checkstyle warnings in netconf-cli
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / action / OFAction.java
1 package org.openflow.protocol.action;
2
3 import java.io.Serializable;
4 import java.nio.ByteBuffer;
5
6 import org.openflow.util.U16;
7
8 /**
9  * The base class for all OpenFlow Actions.
10  *
11  * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 11, 2010
12  */
13 public class OFAction implements Cloneable, Serializable{
14     /**
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.
20      */
21     public static int MINIMUM_LENGTH = 4;
22     public static int OFFSET_LENGTH = 2;
23     public static int OFFSET_TYPE = 0;
24
25     protected OFActionType type;
26     protected short length;
27
28     /**
29      * Get the length of this message
30      *
31      * @return
32      */
33     public short getLength() {
34         return length;
35     }
36
37     /**
38      * Get the length of this message, unsigned
39      *
40      * @return
41      */
42     public int getLengthU() {
43         return U16.f(length);
44     }
45
46     /**
47      * Set the length of this message
48      *
49      * @param length
50      */
51     public OFAction setLength(short length) {
52         this.length = length;
53         return this;
54     }
55
56     /**
57      * Get the type of this message
58      *
59      * @return OFActionType enum
60      */
61     public OFActionType getType() {
62         return this.type;
63     }
64
65     /**
66      * Set the type of this message
67      *
68      * @param type
69      */
70     public void setType(OFActionType type) {
71         this.type = type;
72     }
73
74     /**
75      * Returns a summary of the message
76      * @return "ofmsg=v=$version;t=$type:l=$len:xid=$xid"
77      */
78     public String toString() {
79         return "ofaction" +
80             ";t=" + this.getType() +
81             ";l=" + this.getLength();
82     }
83     
84     /**
85      * Given the output from toString(), 
86      * create a new OFAction
87      * @param val
88      * @return
89      */
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 '" + 
94                     tokens[0] + "'");
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]));
100         return action;
101     }
102
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
107     }
108
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
113     }
114
115     @Override
116     public int hashCode() {
117         final int prime = 347;
118         int result = 1;
119         result = prime * result + length;
120         result = prime * result + ((type == null) ? 0 : type.hashCode());
121         return result;
122     }
123
124     @Override
125     public boolean equals(Object obj) {
126         if (this == obj) {
127             return true;
128         }
129         if (obj == null) {
130             return false;
131         }
132         if (!(obj instanceof OFAction)) {
133             return false;
134         }
135         OFAction other = (OFAction) obj;
136         if (length != other.length) {
137             return false;
138         }
139         if (type == null) {
140             if (other.type != null) {
141                 return false;
142             }
143         } else if (!type.equals(other.type)) {
144             return false;
145         }
146         return true;
147     }
148
149     /* (non-Javadoc)
150      * @see java.lang.Object#clone()
151      */
152     @Override
153     public OFAction clone() throws CloneNotSupportedException {
154         return (OFAction) super.clone();
155     }
156     
157 }