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;
20 import java.util.Arrays;
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.openflow.util.U16;
24 import org.openflow.util.U32;
25 import org.openflow.util.U8;
28 * Represents an ofp_packet_in
30 * @author David Erickson (daviderickson@cs.stanford.edu) - Feb 8, 2010
32 public class OFPacketIn extends OFMessage {
33 public static short MINIMUM_LENGTH = 18;
35 public enum OFPacketInReason {
39 protected int bufferId;
40 protected short totalLength;
41 protected short inPort;
42 protected OFPacketInReason reason;
43 protected byte[] packetData;
47 this.type = OFType.PACKET_IN;
48 this.length = U16.t(MINIMUM_LENGTH);
55 public int getBufferId() {
63 public OFPacketIn setBufferId(int bufferId) {
64 this.bufferId = bufferId;
69 * Returns the packet data
72 public byte[] getPacketData() {
73 return this.packetData;
77 * Sets the packet data, and updates the length of this message
80 public OFPacketIn setPacketData(byte[] packetData) {
81 this.packetData = packetData;
82 this.length = U16.t(OFPacketIn.MINIMUM_LENGTH + packetData.length);
90 public short getInPort() {
98 public OFPacketIn setInPort(short inPort) {
107 public OFPacketInReason getReason() {
115 public OFPacketIn setReason(OFPacketInReason reason) {
116 this.reason = reason;
124 public short getTotalLength() {
125 return this.totalLength;
132 public OFPacketIn setTotalLength(short totalLength) {
133 this.totalLength = totalLength;
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);
150 public void writeTo(ChannelBuffer 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);
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;
173 public boolean equals(Object obj) {
177 if (!super.equals(obj)) {
180 if (!(obj instanceof OFPacketIn)) {
183 OFPacketIn other = (OFPacketIn) obj;
184 if (bufferId != other.bufferId) {
187 if (inPort != other.inPort) {
190 if (!Arrays.equals(packetData, other.packetData)) {
193 if (reason == null) {
194 if (other.reason != null) {
197 } else if (!reason.equals(other.reason)) {
200 if (totalLength != other.totalLength) {
206 public String toString() {
207 String myStr = super.toString();
209 ":bufferId=" + U32.f(this.bufferId) + myStr;