Merge "OPNFLWPLUG-898 Improve code quality in liblldp module"
authorAnil Vishnoi <vishnoianil@gmail.com>
Sun, 3 Jun 2018 19:47:25 +0000 (19:47 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Sun, 3 Jun 2018 19:47:25 +0000 (19:47 +0000)
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/BitBufferHelper.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/CustomTLVKey.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/EtherTypes.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/EthernetAddress.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/HexEncode.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/LLDP.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/LLDPTLV.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/NetUtils.java
libraries/liblldp/src/main/java/org/opendaylight/openflowplugin/libraries/liblldp/Packet.java

index 07d1e618dbaf43cebd711aee8942cf51832491ce..6d9ff899c7534cb44b939fc20ad91c0690514b3a 100644 (file)
@@ -169,7 +169,7 @@ public abstract class BitBufferHelper {
      */
     @Nonnull
     public static byte[] getBits(final byte[] data, final int startOffset, final int numBits) throws BufferException {
-        int startByteOffset = 0;
+        int startByteOffset;
         int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
         final int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
         int numBytes = numBits % NetUtils.NUM_BITS_IN_A_BYTE != 0 ? 1 + numBits / NetUtils.NUM_BITS_IN_A_BYTE
@@ -305,7 +305,7 @@ public abstract class BitBufferHelper {
     public static long toNumber(final byte[] array) {
         long ret = 0;
         long length = array.length;
-        int value = 0;
+        int value;
         for (int i = 0; i < length; i++) {
             value = array[i];
             if (value < 0) {
@@ -326,7 +326,7 @@ public abstract class BitBufferHelper {
         int bitsRest = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
         int startOffset = array.length - length;
         long ret = 0;
-        int value = 0;
+        int value;
 
         value = array[startOffset - 1] & getLSBMask(bitsRest);
         value = array[startOffset - 1] < 0 ? array[startOffset - 1] + 256 : array[startOffset - 1];
@@ -350,7 +350,7 @@ public abstract class BitBufferHelper {
      */
     public static byte[] toByteArray(final Number input) {
         Class<? extends Number> dataType = input.getClass();
-        short size = 0;
+        short size;
         long longValue = input.longValue();
 
         if (dataType == Byte.class || dataType == byte.class) {
@@ -387,7 +387,7 @@ public abstract class BitBufferHelper {
      */
     public static byte[] toByteArray(final Number input, final int numBits) {
         Class<? extends Number> dataType = input.getClass();
-        short size = 0;
+        short size;
         long longValue = input.longValue();
 
         if (dataType == Short.class) {
@@ -411,7 +411,7 @@ public abstract class BitBufferHelper {
         }
 
         if (bytes[0] == 0 && dataType == Long.class || bytes[0] == 0 && dataType == Integer.class) {
-            int index = 0;
+            int index;
             for (index = 0; index < length; ++index) {
                 if (bytes[index] != 0) {
                     bytes[0] = bytes[index];
@@ -446,9 +446,9 @@ public abstract class BitBufferHelper {
      * @return byte[]
      */
     public static byte[] shiftBitsToMSB(final byte[] inputBytes, final int numBits) {
-        int numBitstoShiftBy = 0;
+        int numBitstoShiftBy;
         int leadZeroesMSB = 8;
-        int numEndRestBits = 0;
+        int numEndRestBits;
         int size = inputBytes.length;
         byte[] shiftedBytes = new byte[size];
 
@@ -517,8 +517,8 @@ public abstract class BitBufferHelper {
         int numBytes = inputBytes.length;
         int numBitstoShift = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
         byte[] shiftedBytes = new byte[numBytes];
-        int inputLsb = 0;
-        int inputMsb = 0;
+        int inputLsb;
+        int inputMsb;
 
         if (numBitstoShift == 0) {
             return inputBytes;
@@ -555,7 +555,7 @@ public abstract class BitBufferHelper {
         int extraOffsetBits = startOffset % NetUtils.NUM_BITS_IN_A_BYTE;
         int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
         int restBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
-        int inputMSBbits = 0;
+        int inputMSBbits;
         int inputLSBbits = 0;
 
         if (numBits == 0) {
index 279446ae80e6c0d062daf988a9c2f23a81fbad3b..8f884fc9d6b8c881caca9ca9657a11d62f73f197 100644 (file)
@@ -56,15 +56,7 @@ public class CustomTLVKey {
         }
 
         CustomTLVKey other = (CustomTLVKey) obj;
-        if (oui != other.oui) {
-            return false;
-        }
-
-        if (subtype != other.subtype) {
-            return false;
-        }
-
-        return true;
+        return oui == other.oui && subtype == other.subtype;
     }
 
 }
index 21ca56a2408f8e2498269746d115105182e83b8e..a6e98d153a4b910623f0cf96044ef11e92695a7f 100644 (file)
@@ -26,8 +26,8 @@ public enum EtherTypes {
     CISCOQINQ("Cisco QINQ", 0x9200); // Cisco non-standard QinQ
 
     private static final String REGEX_NUMBER_STRING = "^[0-9]+$";
-    private String description;
-    private int number;
+    private final String description;
+    private final int number;
 
     EtherTypes(final String description, final int number) {
         this.description = description;
index 72433a6d89118d0a85cf16fe20ed168f0b856a6d..9cce96e0caf6be656142f80c6bc158dd029f5d7d 100644 (file)
@@ -94,10 +94,7 @@ public class EthernetAddress extends DataLinkAddress {
             return false;
         }
         EthernetAddress other = (EthernetAddress) obj;
-        if (!Arrays.equals(macAddress, other.macAddress)) {
-            return false;
-        }
-        return true;
+        return Arrays.equals(macAddress, other.macAddress);
     }
 
     @Override
index 02bc634653152d61c7feb1f98600bbddd1f17c30..f003b7a4987f1de3b1b02d6576291c0cd1f9b7b5 100644 (file)
@@ -79,8 +79,7 @@ public final class HexEncode {
     }
 
     public static long stringToLong(final String values) {
-        long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();
-        return value;
+        return new BigInteger(values.replaceAll(":", ""), 16).longValue();
     }
 
     /**
index 56a02443dc632180b78e4784ce4b9ba880d0d6cf..0e10b316f112a9113c2fefec7568d816144ad335 100644 (file)
@@ -55,25 +55,26 @@ public class LLDP extends Packet {
     /**
      * Returns the TLV byte type.
      *
-     * @param String description of the type of TLV
+     * @param typeDesc description of the type of TLV
      * @return byte type of TLV
      */
     private byte getType(final String typeDesc) {
-        if (typeDesc.equals(CHASSISID)) {
-            return LLDPTLV.TLVType.ChassisID.getValue();
-        } else if (typeDesc.equals(PORTID)) {
-            return LLDPTLV.TLVType.PortID.getValue();
-        } else if (typeDesc.equals(TTL)) {
-            return LLDPTLV.TLVType.TTL.getValue();
-        } else if (typeDesc.equals(SYSTEMNAMEID)) {
-            return LLDPTLV.TLVType.SystemName.getValue();
-        } else {
-            return LLDPTLV.TLVType.Unknown.getValue();
+        switch (typeDesc) {
+            case CHASSISID:
+                return LLDPTLV.TLVType.ChassisID.getValue();
+            case PORTID:
+                return LLDPTLV.TLVType.PortID.getValue();
+            case TTL:
+                return LLDPTLV.TLVType.TTL.getValue();
+            case SYSTEMNAMEID:
+                return LLDPTLV.TLVType.SystemName.getValue();
+            default:
+                return LLDPTLV.TLVType.Unknown.getValue();
         }
     }
 
     private LLDPTLV getFromTLVs(final Byte type) {
-        LLDPTLV tlv = null;
+        LLDPTLV tlv;
         tlv = mandatoryTLVs.get(type);
         if (tlv == null) {
             tlv = optionalTLVs.get(type);
@@ -197,8 +198,7 @@ public class LLDP extends Packet {
         int lldpSize = size; // LLDP size
 
         if (LOG.isTraceEnabled()) {
-            LOG.trace("LLDP: {} (offset {} bitsize {})", new Object[] { HexEncode.bytesToHexString(data),
-                lldpOffset, lldpSize });
+            LOG.trace("LLDP: {} (offset {} bitsize {})", HexEncode.bytesToHexString(data), lldpOffset, lldpSize);
         }
         /*
          * Deserialize the TLVs until we reach the end of the packet
index 9af1e74130a68ea84ee578aecbcf86bbeff56ca3..6f3bfd339d78d721cde4a054d1580e80ec06d8c1 100644 (file)
@@ -50,7 +50,7 @@ public class LLDPTLV extends Packet {
                 (byte) 4), SystemName((byte) 5), SystemDesc((byte) 6), Custom(
                         (byte) 127);
 
-        private byte value;
+        private final byte value;
 
         TLVType(final byte value) {
             this.value = value;
@@ -95,7 +95,7 @@ public class LLDPTLV extends Packet {
      */
     public int getLength() {
         return (int) BitBufferHelper.toNumber(fieldValues.get(LENGTH),
-                FIELD_COORDINATES.get(LENGTH).getRight().intValue());
+                FIELD_COORDINATES.get(LENGTH).getRight());
     }
 
     /**
@@ -186,8 +186,7 @@ public class LLDPTLV extends Packet {
     public int getfieldnumBits(final String fieldName) {
         if (fieldName.equals(VALUE)) {
             return NetUtils.NUM_BITS_IN_A_BYTE * BitBufferHelper.getShort(
-                    fieldValues.get(LENGTH), FIELD_COORDINATES.get(LENGTH)
-                    .getRight().intValue());
+                    fieldValues.get(LENGTH), FIELD_COORDINATES.get(LENGTH).getRight());
         }
         return FIELD_COORDINATES.get(fieldName).getRight();
     }
index 59d135a5ae6a3702ae659fffbbbf0b2839e17abe..1425f0b7b385b8240dc96c54bfe71b1ed549c7d0 100644 (file)
@@ -360,10 +360,7 @@ public abstract class NetUtils {
     }
 
     public static boolean fieldsConflict(final int field1, final int field2) {
-        if (field1 == 0 || field2 == 0 || field1 == field2) {
-            return false;
-        }
-        return true;
+        return field1 != 0 && field2 != 0 && field1 != field2;
     }
 
     public static InetAddress parseInetAddress(final String addressString) {
index 4183cc7c5d4455b293354eb899e80e405aea47bc..7b1ffa6070c0336ea131caaba9ab454b7b511f1a 100644 (file)
@@ -93,7 +93,7 @@ public abstract class Packet {
             startOffset = bitOffset + this.getfieldOffset(hdrField);
             numBits = this.getfieldnumBits(hdrField);
 
-            byte[] hdrFieldBytes = null;
+            byte[] hdrFieldBytes;
             try {
                 hdrFieldBytes = BitBufferHelper.getBits(data, startOffset,
                         numBits);