Adapt to OFP's lldp changing 95/94495/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 8 Jan 2021 06:28:23 +0000 (07:28 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 8 Jan 2021 06:54:55 +0000 (07:54 +0100)
LLDP has cleaned up reflection use, adjust to that.

Change-Id: Ibceb241c224058106438b127e9e132401dbf14a1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/packet/Ethernet.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/packet/IPv4.java

index 0dac3a1e31b238d493bb69e5f94515f8292a9aa6..e27f2673734046a16bc650854ce6c1da2eb43675 100644 (file)
@@ -5,13 +5,13 @@
  * 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;
@@ -31,27 +31,27 @@ public class Ethernet extends Packet {
     // 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;
 
     /**
@@ -78,8 +78,7 @@ public class Ethernet extends Packet {
     @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);
     }
index 5960e68e2d3d5ec56a6195e4c4fcf9c7f6958ac0..8b5ebd0df3288b9a3936ba4bb165661ce97254bd 100644 (file)
@@ -7,11 +7,13 @@
  */
 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;
@@ -47,18 +49,13 @@ public class IPv4 extends Packet {
     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));
@@ -260,13 +257,13 @@ public class IPv4 extends Packet {
             // 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)) {