Move authData calculation logic to each LISP message builder classs

Change-Id: I61f13cbb77c58f06efce7e9450f11d328749b247
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispInfoRequest.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispInfoRequest.java
index afe473f..a24fd1a 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispInfoRequest.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispInfoRequest.java
@@ -17,36 +17,44 @@
 
 import com.google.common.base.Objects;
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.onosproject.lisp.msg.authentication.LispAuthenticationFactory;
+import org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum;
 import org.onosproject.lisp.msg.exceptions.LispParseError;
 import org.onosproject.lisp.msg.exceptions.LispReaderException;
 import org.onosproject.lisp.msg.exceptions.LispWriterException;
 import org.onosproject.lisp.msg.types.LispAfiAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Arrays;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf;
 
 /**
  * Default LISP info request message class.
  */
 public class DefaultLispInfoRequest extends DefaultLispInfo implements LispInfoRequest {
 
+    private static final Logger log = LoggerFactory.getLogger(DefaultLispInfoRequest.class);
+
     /**
      * A private constructor that protects object instantiation from external.
      *
-     * @param infoReply          info reply flag
-     * @param nonce              nonce
-     * @param keyId              key identifier
-     * @param authDataLength     authentication data length
-     * @param authenticationData authentication data
-     * @param ttl                Time-To-Live value
-     * @param maskLength         EID prefix mask length
-     * @param eidPrefix          EID prefix
+     * @param infoReply      info reply flag
+     * @param nonce          nonce
+     * @param keyId          key identifier
+     * @param authDataLength authentication data length
+     * @param authData       authentication data
+     * @param ttl            Time-To-Live value
+     * @param maskLength     EID prefix mask length
+     * @param eidPrefix      EID prefix
      */
     protected DefaultLispInfoRequest(boolean infoReply, long nonce, short keyId, short authDataLength,
-                                     byte[] authenticationData, int ttl, byte maskLength,
+                                     byte[] authData, int ttl, byte maskLength,
                                      LispAfiAddress eidPrefix) {
-        super(infoReply, nonce, keyId, authDataLength, authenticationData, ttl, maskLength, eidPrefix);
+        super(infoReply, nonce, keyId, authDataLength, authData, ttl, maskLength, eidPrefix);
     }
 
     @Override
@@ -56,7 +64,7 @@
                 .add("nonce", nonce)
                 .add("keyId", keyId)
                 .add("authentication data length", authDataLength)
-                .add("authentication data", authenticationData)
+                .add("authentication data", authData)
                 .add("TTL", ttl)
                 .add("EID mask length", maskLength)
                 .add("EID prefix", eidPrefix).toString();
@@ -75,7 +83,7 @@
         return Objects.equal(nonce, that.nonce) &&
                 Objects.equal(keyId, that.keyId) &&
                 Objects.equal(authDataLength, that.authDataLength) &&
-                Arrays.equals(authenticationData, that.authenticationData) &&
+                Arrays.equals(authData, that.authData) &&
                 Objects.equal(ttl, that.ttl) &&
                 Objects.equal(maskLength, that.maskLength) &&
                 Objects.equal(eidPrefix, that.eidPrefix);
@@ -84,7 +92,7 @@
     @Override
     public int hashCode() {
         return Objects.hashCode(nonce, keyId, authDataLength, ttl, maskLength,
-                eidPrefix) + Arrays.hashCode(authenticationData);
+                eidPrefix) + Arrays.hashCode(authData);
     }
 
     public static final class DefaultInfoRequestBuilder implements InfoRequestBuilder {
@@ -93,7 +101,8 @@
         private long nonce;
         private short keyId;
         private short authDataLength;
-        private byte[] authenticationData = new byte[0];
+        private byte[] authData;
+        private String authKey;
         private int ttl;
         private byte maskLength;
         private LispAfiAddress eidPrefix;
@@ -129,14 +138,20 @@
         }
 
         @Override
-        public InfoRequestBuilder withAuthenticationData(byte[] authenticationData) {
+        public InfoRequestBuilder withAuthData(byte[] authenticationData) {
             if (authenticationData != null) {
-                this.authenticationData = authenticationData;
+                this.authData = authenticationData;
             }
             return this;
         }
 
         @Override
+        public InfoRequestBuilder withAuthKey(String key) {
+            this.authKey = key;
+            return this;
+        }
+
+        @Override
         public InfoRequestBuilder withTtl(int ttl) {
             this.ttl = ttl;
             return this;
@@ -156,8 +171,36 @@
 
         @Override
         public LispInfoRequest build() {
+
+            // if authentication data is not specified, we will calculate it
+            if (authData == null) {
+                LispAuthenticationFactory factory = LispAuthenticationFactory.getInstance();
+
+                authDataLength = LispAuthenticationKeyEnum.valueOf(keyId).getHashLength();
+                byte[] tmpAuthData = new byte[authDataLength];
+                Arrays.fill(tmpAuthData, (byte) 0);
+                authData = tmpAuthData;
+
+                ByteBuf byteBuf = Unpooled.buffer();
+                try {
+                    new DefaultLispInfoRequest(infoReply, nonce, keyId, authDataLength,
+                            authData, ttl, maskLength, eidPrefix).writeTo(byteBuf);
+                } catch (LispWriterException e) {
+                    log.warn("Failed to serialize info request", e);
+                }
+
+                byte[] bytes = new byte[byteBuf.readableBytes()];
+                byteBuf.readBytes(bytes);
+
+                if (authKey == null) {
+                    log.warn("Must specify authentication key");
+                }
+
+                authData = factory.createAuthenticationData(valueOf(keyId), authKey, bytes);
+            }
+
             return new DefaultLispInfoRequest(infoReply, nonce, keyId,
-                    authDataLength, authenticationData, ttl, maskLength, eidPrefix);
+                    authDataLength, authData, ttl, maskLength, eidPrefix);
         }
     }
 
@@ -172,11 +215,11 @@
             LispInfo lispInfo = DefaultLispInfo.deserialize(byteBuf);
 
             return new DefaultInfoRequestBuilder()
-                    .withInfoReply(lispInfo.hasInfoReply())
+                    .withInfoReply(lispInfo.isInfoReply())
                     .withNonce(lispInfo.getNonce())
                     .withKeyId(lispInfo.getKeyId())
                     .withAuthDataLength(lispInfo.getAuthDataLength())
-                    .withAuthenticationData(lispInfo.getAuthenticationData())
+                    .withAuthData(lispInfo.getAuthData())
                     .withTtl(lispInfo.getTtl())
                     .withMaskLength(lispInfo.getMaskLength())
                     .withEidPrefix(lispInfo.getPrefix()).build();