alshabib

small fixes to ethtype pattern

Change-Id: Ic58c426821952f66aa21bc828d36fd4f83d8da0d
......@@ -16,11 +16,13 @@
package org.onosproject.aaa.packet;
import org.onlab.packet.ARP;
import org.onlab.packet.Deserializer;
import org.onlab.packet.EthType;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPv6;
import org.onlab.packet.LLDP;
import org.onlab.packet.IPacket;
import java.util.HashMap;
import java.util.Map;
/**
* Created by jono on 5/19/15.
......@@ -29,17 +31,21 @@ public final class EAPEthernet extends Ethernet {
public static final short TYPE_PAE = (short) 0x888e;
private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP =
new HashMap<>();
private EAPEthernet() {
}
static {
Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_ARP, ARP.class);
org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_RARP, ARP.class);
org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_IPV4, IPv4.class);
org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_IPV6, IPv6.class);
org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_LLDP, LLDP.class);
org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(org.onlab.packet.Ethernet.TYPE_BSN, LLDP.class);
org.onlab.packet.Ethernet.ETHER_TYPE_CLASS_MAP.put(TYPE_PAE, EAPOL.class);
for (EthType.EtherType ethType : EthType.EtherType.values()) {
if (ethType.deserializer() != null) {
ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(),
ethType.deserializer());
}
}
ETHERTYPE_DESERIALIZER_MAP.put((short) 0x888e, EAPOL.deserializer());
}
}
......
......@@ -19,12 +19,15 @@
package org.onosproject.aaa.packet;
import org.onlab.packet.BasePacket;
import org.onlab.packet.Deserializer;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPacket;
import org.onlab.packet.MacAddress;
import java.nio.ByteBuffer;
import static org.onlab.packet.PacketUtils.checkInput;
/**
*
*/
......@@ -133,29 +136,8 @@ public class EAPOL extends BasePacket {
return data;
}
@Override
public IPacket deserialize(final byte[] data, final int offset,
final int length) {
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
//deserialize the EAPOL header
this.version = bb.get();
this.eapolType = bb.get();
this.packetLength = bb.getShort();
if (this.packetLength > 0) {
//deserialize the EAP Payload
this.payload = new EAP();
this.payload = this.payload.deserialize(data, bb.position(), length - 4);
this.payload.setParent(this);
}
return this;
}
@Override
public int hashCode() {
final int prime = 3889;
......@@ -196,5 +178,51 @@ public class EAPOL extends BasePacket {
eth.setPad(true);
return eth;
}
public static Deserializer<EAPOL> deserializer() {
return (data, offset, length) -> {
checkInput(data, offset, length, 0);
EAPOL eapol = new EAPOL();
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
eapol.setVersion(bb.get());
eapol.setEapolType(bb.get());
eapol.setPacketLength(bb.getShort());
if (eapol.packetLength > 0) {
//deserialize the EAP Payload
eapol.payload = new EAP();
eapol.payload = eapol.payload.deserialize(data, bb.position(), length - 4);
eapol.payload.setParent(eapol);
}
return eapol;
};
}
@Override
public IPacket deserialize(final byte[] data, final int offset,
final int length) {
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
//deserialize the EAPOL header
this.version = bb.get();
this.eapolType = bb.get();
this.packetLength = bb.getShort();
if (this.packetLength > 0) {
//deserialize the EAP Payload
this.payload = new EAP();
this.payload = this.payload.deserialize(data, bb.position(), length - 4);
this.payload.setParent(this);
}
return this;
}
}
......
......@@ -19,6 +19,7 @@ import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.collections.ListUtils;
import org.onlab.packet.EthType;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.MplsLabel;
......@@ -309,6 +310,11 @@ public final class DefaultTrafficTreatment implements TrafficTreatment {
@Override
public Builder popMpls(int etherType) {
return add(Instructions.popMpls(new EthType(etherType)));
}
@Override
public Builder popMpls(EthType etherType) {
return add(Instructions.popMpls(etherType));
}
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.net.flow;
import org.onlab.packet.EthType;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.MplsLabel;
......@@ -197,9 +198,18 @@ public interface TrafficTreatment {
* @param etherType an ether type
* @return a treatment builder.
*/
@Deprecated
Builder popMpls(int etherType);
/**
* Pops MPLS ether type and set the new ethertype.
*
* @param etherType an ether type
* @return a treatment builder.
*/
Builder popMpls(EthType etherType);
/**
* Sets the mpls label.
*
* @param mplsLabel MPLS label.
......
......@@ -25,8 +25,9 @@ import static com.google.common.base.MoreObjects.toStringHelper;
* Implementation of Ethernet type criterion (16 bits unsigned integer).
*/
public final class EthTypeCriterion implements Criterion {
private static final int MASK = 0xffff;
private final EthType ethType; // Ethernet type value: 16 bits
private final EthType ethType;
/**
* Constructor.
......@@ -35,7 +36,7 @@ public final class EthTypeCriterion implements Criterion {
* integer)
*/
EthTypeCriterion(int ethType) {
this.ethType = new EthType(ethType & MASK);
this.ethType = new EthType(ethType);
}
/**
......
......@@ -15,7 +15,7 @@
*/
package org.onosproject.net.flow.instructions;
import org.onlab.packet.Ethernet;
import org.onlab.packet.EthType;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.MplsLabel;
......@@ -265,7 +265,7 @@ public final class Instructions {
public static Instruction pushMpls() {
return new L2ModificationInstruction.PushHeaderInstructions(
L2ModificationInstruction.L2SubType.MPLS_PUSH,
Ethernet.MPLS_UNICAST);
EthType.EtherType.MPLS_UNICAST.ethType());
}
/**
......@@ -276,7 +276,7 @@ public final class Instructions {
public static Instruction popMpls() {
return new L2ModificationInstruction.PushHeaderInstructions(
L2ModificationInstruction.L2SubType.MPLS_POP,
Ethernet.MPLS_UNICAST);
EthType.EtherType.MPLS_UNICAST.ethType());
}
/**
......@@ -285,9 +285,23 @@ public final class Instructions {
* @param etherType Ethernet type to set
* @return a L2 modification.
*/
@Deprecated
public static Instruction popMpls(int etherType) {
checkNotNull(etherType, "Ethernet type cannot be null");
return new L2ModificationInstruction.PushHeaderInstructions(
L2ModificationInstruction.L2SubType.MPLS_POP, new EthType(etherType));
}
/**
* Creates a pop MPLS header instruction with a particular ethertype.
*
* @param etherType Ethernet type to set
* @return a L2 modification.
*/
public static Instruction popMpls(EthType etherType) {
checkNotNull(etherType, "Ethernet type cannot be null");
return new L2ModificationInstruction.PushHeaderInstructions(
L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
}
......@@ -308,7 +322,8 @@ public final class Instructions {
*/
public static Instruction pushVlan() {
return new L2ModificationInstruction.PushHeaderInstructions(
L2ModificationInstruction.L2SubType.VLAN_PUSH, Ethernet.TYPE_VLAN);
L2ModificationInstruction.L2SubType.VLAN_PUSH,
EthType.EtherType.VLAN.ethType());
}
/**
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.net.flow.instructions;
import org.onlab.packet.EthType;
import org.onlab.packet.MacAddress;
import org.onlab.packet.MplsLabel;
import org.onlab.packet.VlanId;
......@@ -145,16 +146,16 @@ public abstract class L2ModificationInstruction implements Instruction {
public static final class PushHeaderInstructions extends
L2ModificationInstruction {
private static final int MASK = 0xffff;
private final L2SubType subtype;
private final int ethernetType; // Ethernet type value: 16 bits
private final EthType ethernetType; // Ethernet type value: 16 bits
PushHeaderInstructions(L2SubType subType, int ethernetType) {
PushHeaderInstructions(L2SubType subType, EthType ethernetType) {
this.subtype = subType;
this.ethernetType = ethernetType & MASK;
this.ethernetType = ethernetType;
}
public int ethernetType() {
public EthType ethernetType() {
return ethernetType;
}
......@@ -166,7 +167,7 @@ public abstract class L2ModificationInstruction implements Instruction {
@Override
public String toString() {
return toStringHelper(subtype().toString())
.add("ethernetType", String.format("0x%04x", ethernetType()))
.add("ethernetType", ethernetType())
.toString();
}
......
......@@ -119,7 +119,8 @@ public final class EncodeInstructionCodec {
final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
(L2ModificationInstruction.PushHeaderInstructions) instruction;
result.put(InstructionCodec.ETHERNET_TYPE, pushHeaderInstructions.ethernetType());
result.put(InstructionCodec.ETHERNET_TYPE,
pushHeaderInstructions.ethernetType().toShort());
break;
default:
......
......@@ -247,15 +247,15 @@ public class FlowRuleCodecTest {
instruction = getInstruction(Instruction.Type.L2MODIFICATION,
L2ModificationInstruction.L2SubType.MPLS_PUSH.name());
assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
assertThat((short) ((L2ModificationInstruction.PushHeaderInstructions) instruction)
.ethernetType(),
assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction)
.ethernetType().toShort(),
is(Ethernet.MPLS_UNICAST));
instruction = getInstruction(Instruction.Type.L2MODIFICATION,
L2ModificationInstruction.L2SubType.MPLS_POP.name());
assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
assertThat((short) ((L2ModificationInstruction.PushHeaderInstructions) instruction)
.ethernetType(),
assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction)
.ethernetType().toShort(),
is(Ethernet.MPLS_UNICAST));
instruction = getInstruction(Instruction.Type.L2MODIFICATION,
......
......@@ -66,7 +66,7 @@ public final class InstructionJsonMatcher extends TypeSafeDiagnosingMatcher<Json
return false;
}
if (instructionToMatch.ethernetType() != ethJson.asInt()) {
if (instructionToMatch.ethernetType().toShort() != ethJson.asInt()) {
description.appendText("ethernetType was " + ethJson);
return false;
}
......
......@@ -316,12 +316,12 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
PushHeaderInstructions pushHeaderInstructions =
(PushHeaderInstructions) l2m;
return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
.ethernetType()));
.ethernetType().toShort()));
case MPLS_POP:
PushHeaderInstructions popHeaderInstructions =
(PushHeaderInstructions) l2m;
return factory().actions().popMpls(EthType.of(popHeaderInstructions
.ethernetType()));
.ethernetType().toShort()));
case MPLS_LABEL:
ModMplsLabelInstruction mplsLabel =
(ModMplsLabelInstruction) l2m;
......@@ -335,7 +335,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
case VLAN_PUSH:
PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
return factory().actions().pushVlan(
EthType.of(pushVlanInstruction.ethernetType()));
EthType.of(pushVlanInstruction.ethernetType().toShort()));
default:
log.warn("Unimplemented action type {}.", l2m.subtype());
break;
......
......@@ -269,17 +269,17 @@ public final class GroupModBuilder {
L2ModificationInstruction.PushHeaderInstructions pushVlanInstruction
= (L2ModificationInstruction.PushHeaderInstructions) l2m;
return factory.actions().pushVlan(
EthType.of(pushVlanInstruction.ethernetType()));
EthType.of(pushVlanInstruction.ethernetType().toShort()));
case MPLS_PUSH:
L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
(L2ModificationInstruction.PushHeaderInstructions) l2m;
return factory.actions().pushMpls(EthType.of(pushHeaderInstructions
.ethernetType()));
.ethernetType().toShort()));
case MPLS_POP:
L2ModificationInstruction.PushHeaderInstructions popHeaderInstructions =
(L2ModificationInstruction.PushHeaderInstructions) l2m;
return factory.actions().popMpls(EthType.of(popHeaderInstructions
.ethernetType()));
.ethernetType().toShort()));
case MPLS_LABEL:
L2ModificationInstruction.ModMplsLabelInstruction mplsLabel =
(L2ModificationInstruction.ModMplsLabelInstruction) l2m;
......
......@@ -15,52 +15,106 @@
*/
package org.onlab.packet;
import com.google.common.collect.Maps;
import java.util.Map;
/**
* Representation of an Ethertype.
*/
public class EthType {
public static final short ARP = EtherType.ARP.ethType.toShort();
public static final short RARP = EtherType.RARP.ethType.toShort();
public static final short VLAN = EtherType.VLAN.ethType.toShort();
public static final short IPV4 = EtherType.IPV4.ethType.toShort();
public static final short IPV6 = EtherType.IPV6.ethType.toShort();
public static final short LLDP = EtherType.LLDP.ethType.toShort();
public static final short BDDP = EtherType.BDDP.ethType.toShort();
public static final short MPLS_MULTICAST = EtherType.MPLS_UNICAST.ethType.toShort();
public static final short MPLS_UNICAST = EtherType.MPLS_UNICAST.ethType.toShort();
/**
* A list of known ethertypes. Adding a fully defined enum here will
* associated the ethertype with a textual representation and a parsing
* class.
*/
public enum EtherType {
ARP(0x806, "arp", org.onlab.packet.ARP.deserializer()),
RARP(0x8035, "rarp", org.onlab.packet.ARP.deserializer()),
IPV4(0x800, "ipv4", org.onlab.packet.IPv4.deserializer()),
IPV6(0x86dd, "ipv6", org.onlab.packet.IPv6.deserializer()),
LLDP(0x88cc, "lldp", org.onlab.packet.LLDP.deserializer()),
VLAN(0x8100, "vlan", null),
BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()),
MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()),
MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer());
private final EthType etherType;
private final String type;
private final Deserializer<?> deserializer;
/**
* Constucts a new ethertype.
*
* @param ethType The actual ethertype
* @param type it's textual representation
* @param deserializer a parser for this ethertype
*/
EtherType(int ethType, String type, Deserializer<?> deserializer) {
this.etherType = new EthType(ethType);
this.type = type;
this.deserializer = deserializer;
}
private short etherType;
public EthType ethType() {
return etherType;
}
/*
* Reverse-lookup map for getting a EtherType enum
*/
private static final Map<Short, EtherType> LOOKUP = Maps.newHashMap();
@Override
public String toString() {
return type;
}
static {
for (EtherType eth : EtherType.values()) {
LOOKUP.put(eth.ethType().toShort(), eth);
public Deserializer<?> deserializer() {
return deserializer;
}
}
private final short etherType;
/**
* Builds the EthType.
*
* @param etherType an integer representing an ethtype
*/
public EthType(int etherType) {
this.etherType = (short) (etherType & 0xFFFF);
}
/**
* Builds the EthType.
*
* @param etherType a short representing the ethtype
*/
public EthType(short etherType) {
this.etherType = etherType;
}
/**
* Returns the short value for this ethtype.
*
* @return a short value
*/
public short toShort() {
return etherType;
}
public static EtherType lookup(short etherType) {
return LOOKUP.get(etherType);
/**
* Looks up the ethertype by it's numerical representation
* and returns it's textual format.
*
* @param etherType the short value of the ethertype
* @return a textual representation
*/
public EtherType lookup(short etherType) {
for (EtherType ethType : EtherType.values()) {
if (ethType.ethType().toShort() == etherType) {
return ethType;
}
}
return null;
}
@Override
......@@ -88,49 +142,8 @@ public class EthType {
public String toString() {
EtherType ethType = lookup(this.etherType);
return (ethType == null ? "unknown" : ethType.toString());
return (ethType == null ? String.format("0x%04x", etherType) :
ethType.toString());
}
public static enum EtherType {
ARP(0x806, "arp", ARP.class, org.onlab.packet.ARP.deserializer()),
RARP(0x8035, "rarp", null, org.onlab.packet.ARP.deserializer()),
IPV4(0x800, "ipv4", IPv4.class, org.onlab.packet.IPv4.deserializer()),
IPV6(0x86dd, "ipv6", IPv6.class, org.onlab.packet.IPv6.deserializer()),
LLDP(0x88cc, "lldp", LLDP.class, org.onlab.packet.LLDP.deserializer()),
VLAN(0x8100, "vlan", null, null),
BDDP(0x8942, "bddp", LLDP.class, org.onlab.packet.LLDP.deserializer()),
MPLS_UNICAST(0x8847, "mpls_unicast", null, org.onlab.packet.MPLS.deserializer()),
MPLS_MULTICAST(0x8848, "mpls_unicast", null, org.onlab.packet.MPLS.deserializer());
private final Class clazz;
private EthType ethType;
private String type;
private Deserializer<?> deserializer;
EtherType(int ethType, String type, Class clazz, Deserializer deserializer) {
this.ethType = new EthType(ethType);
this.type = type;
this.clazz = clazz;
this.deserializer = deserializer;
}
public EthType ethType() {
return ethType;
}
@Override
public String toString() {
return type;
}
public Class clazz() {
return clazz;
}
public Deserializer<?> deserializer() {
return deserializer;
}
}
}
......
......@@ -32,16 +32,17 @@ import static org.onlab.packet.PacketUtils.checkInput;
*/
public class Ethernet extends BasePacket {
private static final String HEXES = "0123456789ABCDEF";
public static final short TYPE_ARP = EthType.ARP;
public static final short TYPE_RARP = EthType.RARP;
public static final short TYPE_IPV4 = EthType.IPV4;
public static final short TYPE_IPV6 = EthType.IPV6;
public static final short TYPE_LLDP = EthType.LLDP;
public static final short TYPE_VLAN = EthType.VLAN;
public static final short TYPE_BSN = EthType.BDDP;
public static final short MPLS_UNICAST = EthType.MPLS_UNICAST;
public static final short MPLS_MULTICAST = EthType.MPLS_MULTICAST;
public static final short TYPE_ARP = EthType.EtherType.ARP.ethType().toShort();
public static final short TYPE_RARP = EthType.EtherType.RARP.ethType().toShort();
public static final short TYPE_IPV4 = EthType.EtherType.IPV4.ethType().toShort();
public static final short TYPE_IPV6 = EthType.EtherType.IPV6.ethType().toShort();
public static final short TYPE_LLDP = EthType.EtherType.LLDP.ethType().toShort();
public static final short TYPE_VLAN = EthType.EtherType.VLAN.ethType().toShort();
public static final short TYPE_BSN = EthType.EtherType.BDDP.ethType().toShort();
public static final short MPLS_UNICAST = EthType.EtherType.MPLS_UNICAST.ethType().toShort();;
public static final short MPLS_MULTICAST = EthType.EtherType.MPLS_MULTICAST.ethType().toShort();
public static final short VLAN_UNTAGGED = (short) 0xffff;
......@@ -56,7 +57,7 @@ public class Ethernet extends BasePacket {
static {
for (EthType.EtherType ethType : EthType.EtherType.values()) {
if (ethType.clazz() != null) {
if (ethType.deserializer() != null) {
ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(), ethType.deserializer());
}
}
......