Initial opendaylight infrastructure commit!!
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / OFPacketIn.java
1 package org.openflow.protocol;
2
3 import java.nio.ByteBuffer;
4 import java.util.Arrays;
5
6 import org.openflow.util.U16;
7 import org.openflow.util.U8;
8
9 /**
10  * Represents an ofp_packet_in
11  *
12  * @author David Erickson (daviderickson@cs.stanford.edu) - Feb 8, 2010
13  */
14 public class OFPacketIn extends OFMessage {
15     public static int MINIMUM_LENGTH = 18;
16
17     public enum OFPacketInReason {
18         NO_MATCH, ACTION
19     }
20
21     protected int bufferId;
22     protected short totalLength;
23     protected short inPort;
24     protected OFPacketInReason reason;
25     protected byte[] packetData;
26
27     public OFPacketIn() {
28         super();
29         this.type = OFType.PACKET_IN;
30         this.length = U16.t(MINIMUM_LENGTH);
31     }
32
33     /**
34      * Get buffer_id
35      * @return
36      */
37     public int getBufferId() {
38         return this.bufferId;
39     }
40
41     /**
42      * Set buffer_id
43      * @param bufferId
44      */
45     public OFPacketIn setBufferId(int bufferId) {
46         this.bufferId = bufferId;
47         return this;
48     }
49
50     /**
51      * Returns the packet data
52      * @return
53      */
54     public byte[] getPacketData() {
55         return this.packetData;
56     }
57
58     /**
59      * Sets the packet data, and updates the length of this message
60      * @param packetData
61      */
62     public OFPacketIn setPacketData(byte[] packetData) {
63         this.packetData = packetData;
64         this.length = U16.t(OFPacketIn.MINIMUM_LENGTH + packetData.length);
65         return this;
66     }
67
68     /**
69      * Get in_port
70      * @return
71      */
72     public short getInPort() {
73         return this.inPort;
74     }
75
76     /**
77      * Set in_port
78      * @param inPort
79      */
80     public OFPacketIn setInPort(short inPort) {
81         this.inPort = inPort;
82         return this;
83     }
84
85     /**
86      * Get reason
87      * @return
88      */
89     public OFPacketInReason getReason() {
90         return this.reason;
91     }
92
93     /**
94      * Set reason
95      * @param reason
96      */
97     public OFPacketIn setReason(OFPacketInReason reason) {
98         this.reason = reason;
99         return this;
100     }
101
102     /**
103      * Get total_len
104      * @return
105      */
106     public short getTotalLength() {
107         return this.totalLength;
108     }
109
110     /**
111      * Set total_len
112      * @param totalLength
113      */
114     public OFPacketIn setTotalLength(short totalLength) {
115         this.totalLength = totalLength;
116         return this;
117     }
118
119     @Override
120     public void readFrom(ByteBuffer data) {
121         super.readFrom(data);
122         this.bufferId = data.getInt();
123         this.totalLength = data.getShort();
124         this.inPort = data.getShort();
125         this.reason = OFPacketInReason.values()[U8.f(data.get())];
126         data.get(); // pad
127         this.packetData = new byte[getLengthU() - MINIMUM_LENGTH];
128         data.get(this.packetData);
129     }
130
131     @Override
132     public void writeTo(ByteBuffer data) {
133         super.writeTo(data);
134         data.putInt(bufferId);
135         data.putShort(totalLength);
136         data.putShort(inPort);
137         data.put((byte) reason.ordinal());
138         data.put((byte) 0x0); // pad
139         data.put(this.packetData);
140     }
141
142     @Override
143     public int hashCode() {
144         final int prime = 283;
145         int result = super.hashCode();
146         result = prime * result + bufferId;
147         result = prime * result + inPort;
148         result = prime * result + Arrays.hashCode(packetData);
149         result = prime * result + ((reason == null) ? 0 : reason.hashCode());
150         result = prime * result + totalLength;
151         return result;
152     }
153
154     @Override
155     public boolean equals(Object obj) {
156         if (this == obj) {
157             return true;
158         }
159         if (!super.equals(obj)) {
160             return false;
161         }
162         if (!(obj instanceof OFPacketIn)) {
163             return false;
164         }
165         OFPacketIn other = (OFPacketIn) obj;
166         if (bufferId != other.bufferId) {
167             return false;
168         }
169         if (inPort != other.inPort) {
170             return false;
171         }
172         if (!Arrays.equals(packetData, other.packetData)) {
173             return false;
174         }
175         if (reason == null) {
176             if (other.reason != null) {
177                 return false;
178             }
179         } else if (!reason.equals(other.reason)) {
180             return false;
181         }
182         if (totalLength != other.totalLength) {
183             return false;
184         }
185         return true;
186     }
187 }