Fix spotbugs logging complaints
[openflowplugin.git] / applications / topology-lldp-discovery / src / main / java / org / opendaylight / openflowplugin / applications / topology / lldp / utils / LLDPDiscoveryUtils.java
index 5d39586995d1ed02a801f03193db1a8bcffc55e1..5681e18ceaf6acce49301403d8638f98457ce8c6 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.openflowplugin.applications.topology.lldp.utils;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.hash.HashCode;
 import com.google.common.hash.HashFunction;
@@ -16,16 +15,14 @@ import com.google.common.hash.Hashing;
 import java.lang.management.ManagementFactory;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
-import java.security.NoSuchAlgorithmException;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
-import java.util.Objects;
+import java.util.Optional;
 import org.apache.commons.lang3.ArrayUtils;
 import org.opendaylight.mdsal.eos.binding.api.Entity;
 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
 import org.opendaylight.openflowplugin.applications.topology.lldp.LLDPActivator;
-import org.opendaylight.openflowplugin.libraries.liblldp.BitBufferHelper;
-import org.opendaylight.openflowplugin.libraries.liblldp.CustomTLVKey;
 import org.opendaylight.openflowplugin.libraries.liblldp.Ethernet;
 import org.opendaylight.openflowplugin.libraries.liblldp.LLDP;
 import org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV;
@@ -92,7 +89,7 @@ public final class LLDPDiscoveryUtils {
             try {
                 ethPkt.deserialize(payload, 0, payload.length * NetUtils.NUM_BITS_IN_A_BYTE);
             } catch (PacketException e) {
-                LOG.warn("Failed to decode LLDP packet {}", e);
+                LOG.warn("Failed to decode LLDP packet", e);
                 return nodeConnectorRef;
             }
 
@@ -110,8 +107,7 @@ public final class LLDPDiscoveryUtils {
                     throw new Exception("Node id wasn't specified via systemNameId in LLDP packet.");
                 }
 
-                final LLDPTLV nodeConnectorIdLldptlv = lldp.getCustomTLV(new CustomTLVKey(
-                        BitBufferHelper.getInt(LLDPTLV.OFOUI), LLDPTLV.CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID[0]));
+                final LLDPTLV nodeConnectorIdLldptlv = lldp.getCustomTLV(LLDPTLV.createPortSubTypeCustomTLVKey());
                 if (nodeConnectorIdLldptlv != null) {
                     srcNodeConnectorId = new NodeConnectorId(LLDPTLV.getCustomString(
                             nodeConnectorIdLldptlv.getValue(), nodeConnectorIdLldptlv.getLength()));
@@ -131,7 +127,7 @@ public final class LLDPDiscoveryUtils {
                 InstanceIdentifier<NodeConnector> srcInstanceId = InstanceIdentifier.builder(Nodes.class)
                         .child(Node.class, new NodeKey(srcNodeId))
                         .child(NodeConnector.class, new NodeConnectorKey(srcNodeConnectorId))
-                        .toInstance();
+                        .build();
                 nodeConnectorRef = new NodeConnectorRef(srcInstanceId);
             } catch (Exception e) {
                 LOG.debug("Caught exception while parsing out lldp optional and custom fields", e);
@@ -146,8 +142,7 @@ public final class LLDPDiscoveryUtils {
      * @param nodeConnectorId the NodeConnectorId
      * @return extra authenticator for lldp security
      */
-    public static byte[] getValueForLLDPPacketIntegrityEnsuring(final NodeConnectorId nodeConnectorId)
-            throws NoSuchAlgorithmException {
+    public static byte[] getValueForLLDPPacketIntegrityEnsuring(final NodeConnectorId nodeConnectorId) {
         String finalKey;
         if (LLDPActivator.getLldpSecureKey() != null && !LLDPActivator.getLldpSecureKey().isEmpty()) {
             finalKey = LLDPActivator.getLldpSecureKey();
@@ -156,17 +151,15 @@ public final class LLDPDiscoveryUtils {
         }
         final String pureValue = nodeConnectorId + finalKey;
 
-        final byte[] pureBytes = pureValue.getBytes();
+        final byte[] pureBytes = pureValue.getBytes(StandardCharsets.UTF_8);
         HashFunction hashFunction = Hashing.md5();
         Hasher hasher = hashFunction.newHasher();
         HashCode hashedValue = hasher.putBytes(pureBytes).hash();
         return hashedValue.asBytes();
     }
 
-    private static boolean checkExtraAuthenticator(LLDP lldp, NodeConnectorId srcNodeConnectorId)
-            throws NoSuchAlgorithmException {
-        final LLDPTLV hashLldptlv = lldp.getCustomTLV(
-                new CustomTLVKey(BitBufferHelper.getInt(LLDPTLV.OFOUI), LLDPTLV.CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC[0]));
+    private static boolean checkExtraAuthenticator(LLDP lldp, NodeConnectorId srcNodeConnectorId) {
+        final LLDPTLV hashLldptlv = lldp.getCustomTLV(LLDPTLV.createSecSubTypeCustomTLVKey());
         boolean secAuthenticatorOk = false;
         if (hashLldptlv != null) {
             byte[] rawTlvValue = hashLldptlv.getValue();
@@ -181,7 +174,7 @@ public final class LLDPDiscoveryUtils {
     }
 
     private static boolean isLLDP(final byte[] packet) {
-        if (Objects.isNull(packet) || packet.length < MINIMUM_LLDP_SIZE) {
+        if (packet == null || packet.length < MINIMUM_LLDP_SIZE) {
             return false;
         }
 
@@ -200,7 +193,7 @@ public final class LLDPDiscoveryUtils {
         Preconditions.checkNotNull(eos, "Entity ownership service must not be null");
 
         EntityOwnershipState state = null;
-        java.util.Optional<EntityOwnershipState> status = getCurrentOwnershipStatus(eos, nodeId);
+        Optional<EntityOwnershipState> status = getCurrentOwnershipStatus(eos, nodeId);
         if (status.isPresent()) {
             state = status.get();
         } else {
@@ -209,16 +202,15 @@ public final class LLDPDiscoveryUtils {
         return state != null && state.equals(EntityOwnershipState.IS_OWNER);
     }
 
-    private static java.util.Optional<EntityOwnershipState> getCurrentOwnershipStatus(final EntityOwnershipService eos,
+    private static Optional<EntityOwnershipState> getCurrentOwnershipStatus(final EntityOwnershipService eos,
             final String nodeId) {
         Entity entity = createNodeEntity(nodeId);
         Optional<EntityOwnershipState> ownershipStatus = eos.getOwnershipState(entity);
 
         if (ownershipStatus.isPresent()) {
             LOG.debug("Fetched ownership status for node {} is {}", nodeId, ownershipStatus.get());
-            return java.util.Optional.of(ownershipStatus.get());
         }
-        return java.util.Optional.empty();
+        return ownershipStatus;
     }
 
     private static Entity createNodeEntity(final String nodeId) {