cb671cd6bafc20c0d1b0263545e71a4b8d17fe11
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / instruction / OFPInstruction.java
1 package org.openflow.codec.protocol.instruction;
2
3 import java.io.Serializable;
4
5 import org.openflow.codec.io.IDataBuffer;
6 import org.openflow.codec.util.U16;
7
8 /**
9  * correspond to struct ofp_instruction openflow structure Instruction header
10  * that is common to all instructions. The length field includes the header and
11  * any padding used to make the instruction 64-bit aligned.
12  *
13  * @author AnilGujele
14  *
15  */
16 public class OFPInstruction implements Cloneable, Serializable {
17
18     private static final long serialVersionUID = 1L;
19
20     public static final short MINIMUM_LENGTH = 4;
21
22     protected short length;
23     protected OFPInstructionType type;
24
25     /**
26      * get the length of instruction structure
27      *
28      * @return
29      */
30     public short getLength() {
31         return length;
32     }
33
34     /**
35      * set the length of instruction structure
36      *
37      * @param length
38      */
39     public void setLength(short length) {
40         this.length = length;
41     }
42
43     /**
44      * get the unsigned length of instruction structure
45      *
46      * @return
47      */
48     public int getLengthU() {
49         return U16.f(length);
50     }
51
52     /**
53      * get the OFInstrutionType
54      *
55      * @return
56      */
57     public OFPInstructionType getOFInstructionType() {
58         return type;
59     }
60
61     /**
62      * set the OFPInstructionType
63      *
64      * @param type
65      */
66     public void setOFInstructionType(OFPInstructionType type) {
67         this.type = type;
68     }
69
70     /**
71      * read OFPInstruction object state from buffer
72      *
73      * @param data
74      */
75     public void readFrom(IDataBuffer data) {
76         this.type = OFPInstructionType.valueOf(data.getShort());
77         this.length = data.getShort();
78     }
79
80     /**
81      * write OFPInstruction object state to buffer
82      *
83      * @param data
84      */
85     public void writeTo(IDataBuffer data) {
86         data.putShort(type.getTypeValue());
87         data.putShort(this.length);
88
89     }
90
91     @Override
92     public int hashCode() {
93         final int prime = 741;
94         int result = 1;
95         result = prime * result + length;
96         result = prime * result + ((type == null) ? 0 : type.hashCode());
97         return result;
98     }
99
100     @Override
101     public boolean equals(Object obj) {
102         if (this == obj) {
103             return true;
104         }
105         if (obj == null) {
106             return false;
107         }
108         if (!(obj instanceof OFPInstruction)) {
109             return false;
110         }
111         OFPInstruction other = (OFPInstruction) obj;
112         if (length != other.length) {
113             return false;
114         }
115         if (type == null) {
116             if (other.type != null) {
117                 return false;
118             }
119         } else if (!type.equals(other.type)) {
120             return false;
121         }
122         return true;
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see java.lang.Object#clone()
129      */
130     @Override
131     public OFPInstruction clone() throws CloneNotSupportedException {
132         return (OFPInstruction) super.clone();
133     }
134
135 }