blob: c37c9184cc0bd710989a0b8197aab5146b6d8ad2 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3* University
4*
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
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
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
15* under the License.
16**/
17
18package org.openflow.protocol;
19
20import java.util.Arrays;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.openflow.util.U16;
24import org.openflow.util.U32;
25import org.openflow.util.U8;
26
27/**
28 * Represents an ofp_packet_in
29 *
30 * @author David Erickson (daviderickson@cs.stanford.edu) - Feb 8, 2010
31 */
32public class OFPacketIn extends OFMessage {
33 public static short MINIMUM_LENGTH = 18;
34
35 public enum OFPacketInReason {
36 NO_MATCH, ACTION
37 }
38
39 protected int bufferId;
40 protected short totalLength;
41 protected short inPort;
42 protected OFPacketInReason reason;
43 protected byte[] packetData;
44
45 public OFPacketIn() {
46 super();
47 this.type = OFType.PACKET_IN;
48 this.length = U16.t(MINIMUM_LENGTH);
49 }
50
51 /**
52 * Get buffer_id
53 * @return
54 */
55 public int getBufferId() {
56 return this.bufferId;
57 }
58
59 /**
60 * Set buffer_id
61 * @param bufferId
62 */
63 public OFPacketIn setBufferId(int bufferId) {
64 this.bufferId = bufferId;
65 return this;
66 }
67
68 /**
69 * Returns the packet data
70 * @return
71 */
72 public byte[] getPacketData() {
73 return this.packetData;
74 }
75
76 /**
77 * Sets the packet data, and updates the length of this message
78 * @param packetData
79 */
80 public OFPacketIn setPacketData(byte[] packetData) {
81 this.packetData = packetData;
82 this.length = U16.t(OFPacketIn.MINIMUM_LENGTH + packetData.length);
83 return this;
84 }
85
86 /**
87 * Get in_port
88 * @return
89 */
90 public short getInPort() {
91 return this.inPort;
92 }
93
94 /**
95 * Set in_port
96 * @param inPort
97 */
98 public OFPacketIn setInPort(short inPort) {
99 this.inPort = inPort;
100 return this;
101 }
102
103 /**
104 * Get reason
105 * @return
106 */
107 public OFPacketInReason getReason() {
108 return this.reason;
109 }
110
111 /**
112 * Set reason
113 * @param reason
114 */
115 public OFPacketIn setReason(OFPacketInReason reason) {
116 this.reason = reason;
117 return this;
118 }
119
120 /**
121 * Get total_len
122 * @return
123 */
124 public short getTotalLength() {
125 return this.totalLength;
126 }
127
128 /**
129 * Set total_len
130 * @param totalLength
131 */
132 public OFPacketIn setTotalLength(short totalLength) {
133 this.totalLength = totalLength;
134 return this;
135 }
136
137 @Override
138 public void readFrom(ChannelBuffer data) {
139 super.readFrom(data);
140 this.bufferId = data.readInt();
141 this.totalLength = data.readShort();
142 this.inPort = data.readShort();
143 this.reason = OFPacketInReason.values()[U8.f(data.readByte())];
144 data.readByte(); // pad
145 this.packetData = new byte[getLengthU() - MINIMUM_LENGTH];
146 data.readBytes(this.packetData);
147 }
148
149 @Override
150 public void writeTo(ChannelBuffer data) {
151 super.writeTo(data);
152 data.writeInt(bufferId);
153 data.writeShort(totalLength);
154 data.writeShort(inPort);
155 data.writeByte((byte) reason.ordinal());
156 data.writeByte((byte) 0x0); // pad
157 data.writeBytes(this.packetData);
158 }
159
160 @Override
161 public int hashCode() {
162 final int prime = 283;
163 int result = super.hashCode();
164 result = prime * result + bufferId;
165 result = prime * result + inPort;
166 result = prime * result + Arrays.hashCode(packetData);
167 result = prime * result + ((reason == null) ? 0 : reason.hashCode());
168 result = prime * result + totalLength;
169 return result;
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (this == obj) {
175 return true;
176 }
177 if (!super.equals(obj)) {
178 return false;
179 }
180 if (!(obj instanceof OFPacketIn)) {
181 return false;
182 }
183 OFPacketIn other = (OFPacketIn) obj;
184 if (bufferId != other.bufferId) {
185 return false;
186 }
187 if (inPort != other.inPort) {
188 return false;
189 }
190 if (!Arrays.equals(packetData, other.packetData)) {
191 return false;
192 }
193 if (reason == null) {
194 if (other.reason != null) {
195 return false;
196 }
197 } else if (!reason.equals(other.reason)) {
198 return false;
199 }
200 if (totalLength != other.totalLength) {
201 return false;
202 }
203 return true;
204 }
205
206 public String toString() {
207 String myStr = super.toString();
208 return "packetIn" +
209 ":bufferId=" + U32.f(this.bufferId) + myStr;
210 }
211}