Fix sonar warnings of LISP authentication package

- Restrict access level from public to package-private
- Log and pass original exception's message
- Prevent an utility class from instantiation

Change-Id: Ida35121edeb0675ea86ab8d788f4f8c386cb19ba
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationFactory.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationFactory.java
index 5224aee..d49c05c 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationFactory.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispAuthenticationFactory.java
@@ -21,6 +21,12 @@
 public final class LispAuthenticationFactory {
 
     /**
+     * Prevents object instantiation from external.
+     */
+    private LispAuthenticationFactory() {
+    }
+
+    /**
      * Obtains a factory singleton instance.
      *
      * @return factory singleton instance
@@ -57,13 +63,15 @@
     }
 
     /**
-     * Prevents object instantiation from external.
+     * A private singleton helper class.
      */
-    private LispAuthenticationFactory() {
-    }
-
-    private static class SingletonHelper {
+    private static final class SingletonHelper {
         private static final LispAuthenticationFactory INSTANCE =
-                new LispAuthenticationFactory();
+                                                new LispAuthenticationFactory();
+        private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class.";
+
+        private SingletonHelper() {
+            throw new IllegalAccessError(ILLEGAL_ACCESS_MSG);
+        }
     }
 }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispMacAuthentication.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispMacAuthentication.java
index 905fe46..166a88e 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispMacAuthentication.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/authentication/LispMacAuthentication.java
@@ -33,23 +33,25 @@
 
     private static final Logger log = LoggerFactory.getLogger(LispMacAuthentication.class);
 
-    private String algorithm;
-    private int authenticationLength;
+    private static final String NOT_SUPPORT_ALGORITHM_MSG =
+                                "Not support provided algorithm {}";
+    private static final String INVALID_KEY_MSG = "Provided key {} is invalid";
 
-    public LispMacAuthentication(LispAuthenticationKeyEnum authType) {
+    private String algorithm;
+
+    /**
+     * Default constructor with given authentication key type.
+     *
+     * @param authType authentication key type
+     */
+    LispMacAuthentication(LispAuthenticationKeyEnum authType) {
 
         if (authType == SHA1 || authType == SHA256) {
             algorithm = authType.getName();
         } else {
-            log.warn("Not support provided algorithm {}", authType.getName());
+            log.warn(NOT_SUPPORT_ALGORITHM_MSG, authType.getName());
             return;
         }
-
-        try {
-            authenticationLength = Mac.getInstance(algorithm).getMacLength();
-        } catch (NoSuchAlgorithmException e) {
-            log.warn("Not support provided algorithm {}", algorithm);
-        }
     }
 
     /**
@@ -57,18 +59,18 @@
      *
      * @return dummy authentication data
      */
-    public byte[] getAuthenticationData() {
+    byte[] getAuthenticationData() {
         return new byte[0];
     }
 
     /**
      * Obtains authentication data with given key and algorithm.
      *
-     * @param key  authentication key (e.g., EID)
+     * @param key  authentication key
      * @param data array of byte buffer for place holder
      * @return authentication data
      */
-    public byte[] getAuthenticationData(String key, byte[] data) {
+    byte[] getAuthenticationData(String key, byte[] data) {
         try {
             SecretKeySpec signKey = new SecretKeySpec(key.getBytes(), algorithm);
             Mac mac = Mac.getInstance(algorithm);
@@ -76,20 +78,12 @@
 
             return mac.doFinal(data);
         } catch (NoSuchAlgorithmException e) {
-            log.warn("Not support provided algorithm {}", algorithm);
+            log.warn(NOT_SUPPORT_ALGORITHM_MSG, algorithm, e.getMessage());
+            throw new RuntimeException(e);
         } catch (InvalidKeyException e) {
-            log.warn("Provided key {} is invalid", key);
+            log.warn(INVALID_KEY_MSG, key, e.getMessage());
+            throw new RuntimeException(e);
         }
-        return null;
-    }
-
-    /**
-     * Obtains authentication data length.
-     *
-     * @return authentication data length
-     */
-    public int getAuthenticationLength() {
-        return authenticationLength;
     }
 
     /**
@@ -97,7 +91,7 @@
      *
      * @return authentication algorithm
      */
-    public String getAlgorithm() {
+    String getAlgorithm() {
         return algorithm;
     }
 }
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/authentication/LispAuthenticationTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/authentication/LispAuthenticationTest.java
index 2e994b6..414670d 100644
--- a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/authentication/LispAuthenticationTest.java
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/authentication/LispAuthenticationTest.java
@@ -48,4 +48,28 @@
         assertThat(sha1AuthData.length, is(20));
         assertThat(sha256AuthData.length, is(32));
     }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidAuthType() {
+        LispAuthenticationKeyEnum authType = LispAuthenticationKeyEnum.valueOf((short) 0);
+        LispMacAuthentication macAuth = new LispMacAuthentication(authType);
+
+        macAuth.getAuthenticationData("onos", new byte[0]);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testNullAuthKey() {
+        LispAuthenticationKeyEnum authType = LispAuthenticationKeyEnum.valueOf((short) 1);
+
+        LispMacAuthentication macAuth = new LispMacAuthentication(authType);
+        macAuth.getAuthenticationData(null, new byte[0]);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidAuthKey() {
+        LispAuthenticationKeyEnum authType = LispAuthenticationKeyEnum.valueOf((short) 1);
+
+        LispMacAuthentication macAuth = new LispMacAuthentication(authType);
+        macAuth.getAuthenticationData("", new byte[0]);
+    }
 }