* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.genius.mdsalutil.packet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.opendaylight.openflowplugin.libraries.liblldp.BitBufferHelper;
// TODO: This has to be outside and it should be possible for osgi
// to add new coming packet classes
@SuppressWarnings("checkstyle:ConstantName") // public constant is used in other projects; too late to rename easily
- public static final Map<Short, Class<? extends Packet>> etherTypeClassMap = new ConcurrentHashMap<>();
+ public static final Map<Short, Supplier<Packet>> etherTypeClassMap = new ConcurrentHashMap<>();
static {
- etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP.class);
- etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP.class);
- etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4.class);
+ etherTypeClassMap.put(EtherTypes.ARP.shortValue(), ARP::new);
+ etherTypeClassMap.put(EtherTypes.LLDP.shortValue(), LLDP::new);
+ etherTypeClassMap.put(EtherTypes.IPv4.shortValue(), IPv4::new);
// TODO: Add support for more classes here
- etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q.class);
- // etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q.class);
- // etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q.class);
- // etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q.class);
+ etherTypeClassMap.put(EtherTypes.VLANTAGGED.shortValue(), IEEE8021Q::new);
+ // etherTypeClassMap.put(EtherTypes.OLDQINQ.shortValue(), IEEE8021Q::new);
+ // etherTypeClassMap.put(EtherTypes.QINQ.shortValue(), IEEE8021Q::new);
+ // etherTypeClassMap.put(EtherTypes.CISCOQINQ.shortValue(), IEEE8021Q::new);
}
@SuppressWarnings("serial")
- private static Map<String, Pair<Integer, Integer>> fieldCoordinates
- = new LinkedHashMap<String, Pair<Integer, Integer>>() { {
+ private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<>() { {
put(DMAC, new ImmutablePair<>(0, 48));
put(SMAC, new ImmutablePair<>(48, 48));
put(ETHT, new ImmutablePair<>(96, 16));
}
};
+
private final Map<String, byte[]> fieldValues;
/**
@Override
public void setHeaderField(String headerField, byte[] readValue) {
if (headerField.equals(ETHT)) {
- payloadClass = etherTypeClassMap.get(BitBufferHelper
- .getShort(readValue));
+ payloadFactory = etherTypeClassMap.get(BitBufferHelper.getShort(readValue));
}
hdrFieldsMap.put(headerField, readValue);
}
*/
package org.opendaylight.genius.mdsalutil.packet;
+import com.google.common.collect.ImmutableMap;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
+import java.util.function.Supplier;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.opendaylight.openflowplugin.libraries.liblldp.BitBufferHelper;
private static final int UNIT_SIZE = 1 << UNIT_SIZE_SHIFT;
private static final int MIN_HEADER_SIZE = 20;
- private static final Map<Byte, Class<? extends Packet>> PROTOCOL_CLASS_MAP;
-
- static {
- PROTOCOL_CLASS_MAP = new HashMap<>();
- PROTOCOL_CLASS_MAP.put(IPProtocols.ICMP.byteValue(), ICMP.class);
- PROTOCOL_CLASS_MAP.put(IPProtocols.UDP.byteValue(), UDP.class);
- PROTOCOL_CLASS_MAP.put(IPProtocols.TCP.byteValue(), TCP.class);
- }
+ private static final Map<Byte, Supplier<Packet>> PROTOCOL_CLASS_MAP = ImmutableMap.of(
+ IPProtocols.ICMP.byteValue(), ICMP::new,
+ IPProtocols.UDP.byteValue(), UDP::new,
+ IPProtocols.TCP.byteValue(), TCP::new);
@SuppressWarnings("serial")
- private static final Map<String, Pair<Integer, Integer>> FIELD_COORDINATES
- = new LinkedHashMap<>() { {
+ private static final Map<String, Pair<Integer, Integer>> FIELD_COORDINATES = new LinkedHashMap<>() { {
put(VERSION, new ImmutablePair<>(0, 4));
put(HEADERLENGTH, new ImmutablePair<>(4, 4));
put(DIFFSERV, new ImmutablePair<>(8, 6));
// Don't set payloadClass if framgment offset is not zero.
byte[] fragoff = hdrFieldsMap.get(FRAGOFFSET);
if (fragoff == null || BitBufferHelper.getShort(fragoff) == 0) {
- payloadClass = PROTOCOL_CLASS_MAP.get(readValue[0]);
+ payloadFactory = PROTOCOL_CLASS_MAP.get(readValue[0]);
}
} else if (headerField.equals(FRAGOFFSET)) {
if (readValue != null && BitBufferHelper.getShort(readValue) != 0) {
// Clear payloadClass because protocol header is not present
// in this packet.
- payloadClass = null;
+ payloadFactory = null;
}
} else if (headerField.equals(OPTIONS)
&& (readValue == null || readValue.length == 0)) {