Charles Chan
Committed by Gerrit Code Review

Expose cookie information in packet context

Change-Id: I7f2cb331a19aeca1a578aade6488a6480d15496c
......@@ -20,6 +20,7 @@ import org.onlab.packet.Ethernet;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.Optional;
import static com.google.common.base.MoreObjects.toStringHelper;
......@@ -31,6 +32,7 @@ public final class DefaultInboundPacket implements InboundPacket {
private final ConnectPoint receivedFrom;
private final Ethernet parsed;
private final ByteBuffer unparsed;
private final Optional<Long> cookie;
/**
* Creates an immutable inbound packet.
......@@ -39,11 +41,25 @@ public final class DefaultInboundPacket implements InboundPacket {
* @param parsed parsed ethernet frame
* @param unparsed unparsed raw bytes
*/
public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed,
public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed,
ByteBuffer unparsed) {
this(receivedFrom, parsed, unparsed, Optional.empty());
}
/**
* Creates an immutable inbound packet with cookie.
*
* @param receivedFrom connection point where received
* @param parsed parsed ethernet frame
* @param unparsed unparsed raw bytes
* @param cookie cookie
*/
public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed,
ByteBuffer unparsed, Optional<Long> cookie) {
this.receivedFrom = receivedFrom;
this.parsed = parsed;
this.unparsed = unparsed;
this.cookie = cookie;
}
@Override
......@@ -63,6 +79,11 @@ public final class DefaultInboundPacket implements InboundPacket {
}
@Override
public Optional<Long> cookie() {
return cookie;
}
@Override
public int hashCode() {
return Objects.hash(receivedFrom, parsed, unparsed);
}
......
......@@ -19,6 +19,7 @@ import org.onosproject.net.ConnectPoint;
import org.onlab.packet.Ethernet;
import java.nio.ByteBuffer;
import java.util.Optional;
/**
* Represents a data packet intercepted from an infrastructure device.
......@@ -47,4 +48,10 @@ public interface InboundPacket {
*/
ByteBuffer unparsed();
/**
* Returns the cookie in the packet in message.
*
* @return optional flow cookie
*/
Optional<Long> cookie();
}
......
......@@ -16,6 +16,7 @@
package org.onosproject.net.packet;
import java.nio.ByteBuffer;
import java.util.Optional;
import org.junit.Test;
import org.onlab.packet.Ethernet;
......@@ -41,15 +42,22 @@ public class DefaultInboundPacketTest {
final DefaultInboundPacket packet1 =
new DefaultInboundPacket(connectPoint("d1", 1),
eth,
byteBuffer);
byteBuffer,
Optional.of(1L));
final DefaultInboundPacket sameAsPacket1 =
new DefaultInboundPacket(connectPoint("d1", 1),
eth,
byteBuffer);
byteBuffer,
Optional.of(1L));
final DefaultInboundPacket packet2 =
new DefaultInboundPacket(connectPoint("d2", 1),
eth,
byteBuffer);
final DefaultInboundPacket sameAsPacket2 =
new DefaultInboundPacket(connectPoint("d2", 1),
eth,
byteBuffer,
Optional.empty());
/**
* Checks that the DefaultInboundPacket class is immutable.
*/
......@@ -65,7 +73,7 @@ public class DefaultInboundPacketTest {
public void testEquals() {
new EqualsTester()
.addEqualityGroup(packet1, sameAsPacket1)
.addEqualityGroup(packet2)
.addEqualityGroup(packet2, sameAsPacket2)
.testEquals();
}
......@@ -77,5 +85,6 @@ public class DefaultInboundPacketTest {
assertThat(packet1.receivedFrom(), equalTo(connectPoint("d1", 1)));
assertThat(packet1.parsed(), equalTo(eth));
assertThat(packet1.unparsed(), notNullValue());
assertThat(packet1.cookie(), equalTo(Optional.of(1L)));
}
}
......
......@@ -30,6 +30,7 @@ import org.slf4j.LoggerFactory;
import java.nio.BufferUnderflowException;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.onosproject.security.AppGuard.checkPermission;
......@@ -179,4 +180,14 @@ public final class DefaultOpenFlowPacketContext implements OpenFlowPacketContext
return isBuffered;
}
@Override
public Optional<Long> cookie() {
checkPermission(PACKET_READ);
if (pktin.getVersion() != OFVersion.OF_10) {
return Optional.of(pktin.getCookie().getValue());
} else {
return Optional.empty();
}
}
}
......
......@@ -18,6 +18,8 @@ package org.onosproject.openflow.controller;
import org.onlab.packet.Ethernet;
import org.projectfloodlight.openflow.types.OFPort;
import java.util.Optional;
/**
* A representation of a packet context which allows any provider
* to view a packet in event, but may block the response to the
......@@ -29,12 +31,14 @@ public interface OpenFlowPacketContext {
/**
* Blocks further responses (ie. send() calls) on this
* packet in event.
*
* @return true if blocks
*/
boolean block();
/**
* Checks whether the packet has been handled.
*
* @return true if handled, false otherwise.
*/
boolean isHandled();
......@@ -47,12 +51,14 @@ public interface OpenFlowPacketContext {
/**
* Build the packet out in response to this packet in event.
*
* @param outPort the out port to send to packet out of.
*/
void build(OFPort outPort);
/**
* Build the packet out in response to this packet in event.
*
* @param ethFrame the actual packet to send out.
* @param outPort the out port to send to packet out of.
*/
......@@ -60,31 +66,43 @@ public interface OpenFlowPacketContext {
/**
* Provided a handle onto the parsed payload.
*
* @return the parsed form of the payload.
*/
Ethernet parsed();
/**
* Provide an unparsed copy of the data.
*
* @return the unparsed form of the payload.
*/
byte[] unparsed();
/**
* Provide the dpid of the switch where the packet in arrived.
*
* @return the dpid of the switch.
*/
Dpid dpid();
/**
* Provide the port on which the packet arrived.
*
* @return the port
*/
Integer inPort();
/**
* Indicates that this packet is buffered at the switch.
*
* @return buffer indication
*/
boolean isBuffered();
/**
* Provide the cookie in the packet in message.
*
* @return optional flow cookie
*/
Optional<Long> cookie();
}
......
......@@ -156,7 +156,8 @@ public class OpenFlowPacketProvider extends AbstractProvider implements PacketPr
DefaultInboundPacket inPkt = new DefaultInboundPacket(
new ConnectPoint(id, PortNumber.portNumber(pktCtx.inPort())),
pktCtx.parsed(), ByteBuffer.wrap(pktCtx.unparsed()));
pktCtx.parsed(), ByteBuffer.wrap(pktCtx.unparsed()),
pktCtx.cookie());
DefaultOutboundPacket outPkt = null;
if (!pktCtx.isBuffered()) {
......