Add NotNull and Argument check during object build for LISP object

Also correct IP enumeration type into IP4.

Change-Id: Id970ee608c885826cfd2de7b5448b2bc451e079f
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecord.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecord.java
index 37eeb71..0b5407e 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecord.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecord.java
@@ -24,6 +24,7 @@
 import org.onosproject.lisp.msg.types.LispAfiAddress;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
 
 /**
@@ -209,6 +210,9 @@
 
         @Override
         public LispLocatorRecord build() {
+
+            checkNotNull(locatorAfi, "Must specify a locator address");
+
             return new DefaultLispLocatorRecord(priority, weight, multicastPriority,
                     multicastWeight, localLocator, rlocProbed, routed, locatorAfi);
         }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotify.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotify.java
index 14399c2..bfd64fc 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotify.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotify.java
@@ -152,7 +152,7 @@
         private short authDataLength;
         private byte[] authenticationData;
         private byte recordCount;
-        private List<LispMapRecord> mapRecords;
+        private List<LispMapRecord> mapRecords = Lists.newArrayList();
 
         @Override
         public LispType getType() {
@@ -197,8 +197,6 @@
         public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
             if (mapRecords != null) {
                 this.mapRecords = ImmutableList.copyOf(mapRecords);
-            } else {
-                this.mapRecords = Lists.newArrayList();
             }
             return this;
         }
@@ -210,10 +208,6 @@
                 authenticationData = new byte[0];
             }
 
-            if (mapRecords == null) {
-                mapRecords = Lists.newArrayList();
-            }
-
             return new DefaultLispMapNotify(nonce, keyId, authDataLength,
                     authenticationData, recordCount, mapRecords);
         }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecord.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecord.java
index f53d04d..b4e5139 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecord.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecord.java
@@ -28,6 +28,7 @@
 import java.util.List;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
 import static org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordWriter;
 
@@ -163,7 +164,7 @@
         private boolean authoritative;
         private short mapVersionNumber;
         private LispAfiAddress eidPrefixAfi;
-        private List<LispLocatorRecord> locatorRecords;
+        private List<LispLocatorRecord> locatorRecords = Lists.newArrayList();
 
         @Override
         public MapRecordBuilder withRecordTtl(int recordTtl) {
@@ -211,8 +212,6 @@
         public MapRecordBuilder withLocators(List<LispLocatorRecord> records) {
             if (records != null) {
                 this.locatorRecords = ImmutableList.copyOf(records);
-            } else {
-                this.locatorRecords = Lists.newArrayList();
             }
             return this;
         }
@@ -220,9 +219,7 @@
         @Override
         public LispMapRecord build() {
 
-            if (locatorRecords == null) {
-                locatorRecords = Lists.newArrayList();
-            }
+            checkNotNull(eidPrefixAfi, "Must specify an EID prefix");
 
             return new DefaultLispMapRecord(recordTtl, locatorCount, maskLength,
                     action, authoritative, mapVersionNumber, eidPrefixAfi, locatorRecords);
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegister.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegister.java
index b6ed216..e0d1490 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegister.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegister.java
@@ -175,9 +175,9 @@
         private long nonce;
         private short keyId;
         private short authDataLength;
-        private byte[] authenticationData;
+        private byte[] authenticationData = new byte[0];
         private byte recordCount;
-        private List<LispMapRecord> mapRecords;
+        private List<LispMapRecord> mapRecords = Lists.newArrayList();
         private boolean proxyMapReply;
         private boolean wantMapNotify;
 
@@ -226,8 +226,6 @@
         public RegisterBuilder withAuthenticationData(byte[] authenticationData) {
             if (authenticationData != null) {
                 this.authenticationData = authenticationData;
-            } else {
-                this.authenticationData = new byte[0];
             }
             return this;
         }
@@ -236,22 +234,12 @@
         public RegisterBuilder withMapRecords(List<LispMapRecord> mapRecords) {
             if (mapRecords != null) {
                 this.mapRecords = ImmutableList.copyOf(mapRecords);
-            } else {
-                this.mapRecords = Lists.newArrayList();
             }
             return this;
         }
 
         @Override
         public LispMapRegister build() {
-            if (authenticationData == null) {
-                authenticationData = new byte[0];
-            }
-
-            if (mapRecords == null) {
-                mapRecords = Lists.newArrayList();
-            }
-
             return new DefaultLispMapRegister(nonce, keyId, authDataLength,
                     authenticationData, recordCount, mapRecords, proxyMapReply, wantMapNotify);
         }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReply.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReply.java
index f422740..75c1689 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReply.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReply.java
@@ -146,7 +146,7 @@
         private boolean probe;
         private boolean etr;
         private boolean security;
-        private List<LispMapRecord> mapRecords;
+        private List<LispMapRecord> mapRecords = Lists.newArrayList();
 
         @Override
         public LispType getType() {
@@ -185,22 +185,14 @@
 
         @Override
         public ReplyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
-
             if (this.mapRecords != null) {
                 this.mapRecords = ImmutableList.copyOf(mapRecords);
-            } else {
-                this.mapRecords = Lists.newArrayList();
             }
             return this;
         }
 
         @Override
         public LispMapReply build() {
-
-            if (mapRecords == null) {
-                mapRecords = Lists.newArrayList();
-            }
-
             return new DefaultLispMapReply(nonce, recordCount, probe, etr, security, mapRecords);
         }
     }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequest.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequest.java
index b62eed6..c44c909 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequest.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequest.java
@@ -270,23 +270,16 @@
 
         @Override
         public RequestBuilder withItrRlocs(List<LispAfiAddress> itrRlocs) {
-
             if (itrRlocs != null) {
                 this.itrRlocs = ImmutableList.copyOf(itrRlocs);
-            } else {
-                this.itrRlocs = Lists.newArrayList();
             }
-
             return this;
         }
 
         @Override
         public RequestBuilder withEidRecords(List<LispEidRecord> records) {
-
             if (records != null) {
                 this.eidRecords = ImmutableList.copyOf(records);
-            } else {
-                this.eidRecords = Lists.newArrayList();
             }
             return this;
         }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/LispEidRecord.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/LispEidRecord.java
index 35fe975..9ceef6c 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/LispEidRecord.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/protocols/LispEidRecord.java
@@ -21,6 +21,7 @@
 import org.onosproject.lisp.msg.exceptions.LispWriterException;
 import org.onosproject.lisp.msg.types.LispAfiAddress;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
 
 /**
@@ -39,6 +40,9 @@
      */
     public LispEidRecord(byte maskLength, LispAfiAddress prefix) {
         this.maskLength = maskLength;
+
+        checkNotNull(prefix, "Must specify an address prefix");
+
         this.prefix = prefix;
     }
 
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/AddressFamilyIdentifierEnum.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/AddressFamilyIdentifierEnum.java
index 526c8b4..e1a4d97 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/AddressFamilyIdentifierEnum.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/AddressFamilyIdentifierEnum.java
@@ -26,7 +26,7 @@
 public enum AddressFamilyIdentifierEnum {
 
     NO_ADDRESS(0),              // Reserved
-    IP(1),                      // IP (IP version 4)
+    IP4(1),                     // IP4 (IP version 4)
     IP6(2),                     // IP6 (IP version 6)
     DNS(16),                    // Domain Name System
     DISTINGUISHED_NAME(17),     // Distinguished Name
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAfiAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAfiAddress.java
index c7e72c5..aba3475 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAfiAddress.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAfiAddress.java
@@ -90,7 +90,7 @@
             short afiCode = (short) byteBuf.getUnsignedShort(index);
 
             // handle IPv4 and IPv6 address
-            if (afiCode == IP.getIanaCode() ||
+            if (afiCode == IP4.getIanaCode() ||
                 afiCode == IP6.getIanaCode()) {
                 return new LispIpAddress.IpAddressReader().readFrom(byteBuf);
             }
@@ -131,7 +131,7 @@
             byteBuf.writeShort(address.getAfi().getIanaCode());
 
             switch (address.getAfi()) {
-                case IP:
+                case IP4:
                     new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpv4Address) address);
                     break;
                 case IP6:
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAppDataLcafAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAppDataLcafAddress.java
index 56ff2f2..8e4c972 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAppDataLcafAddress.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispAppDataLcafAddress.java
@@ -25,6 +25,7 @@
 import java.util.Objects;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * Application data type LCAF address class.
@@ -308,6 +309,9 @@
          * @return LispAddDataLcafAddress instance
          */
         public LispAppDataLcafAddress build() {
+
+            checkNotNull(address, "Must specify an address");
+
             return new LispAppDataLcafAddress(reserved1, reserved2, flag, length,
                     protocol, ipTos, localPortLow, localPortHigh, remotePortLow,
                     remotePortHigh, address);
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispIpv4Address.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispIpv4Address.java
index a0df8d8..62f1ea06 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispIpv4Address.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispIpv4Address.java
@@ -35,7 +35,7 @@
      * @param address IP address
      */
     public LispIpv4Address(IpAddress address) {
-        super(address, AddressFamilyIdentifierEnum.IP);
+        super(address, AddressFamilyIdentifierEnum.IP4);
         checkArgument(address.isIp4());
     }
 
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispLcafAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispLcafAddress.java
index 85a21d2..7a1763c 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispLcafAddress.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispLcafAddress.java
@@ -19,6 +19,8 @@
 import org.onosproject.lisp.msg.exceptions.LispParseError;
 import org.onosproject.lisp.msg.exceptions.LispReaderException;
 import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Objects;
 
@@ -41,6 +43,8 @@
  */
 public class LispLcafAddress extends LispAfiAddress {
 
+    private static final Logger log = LoggerFactory.getLogger(LispLcafAddress.class);
+
     private final LispCanonicalAddressFormatEnum lcafType;
     private final byte reserved1;
     private final byte reserved2;
@@ -369,6 +373,8 @@
                 return new LispSourceDestLcafAddress.SourceDestLcafAddressReader().readFrom(byteBuf);
             }
 
+            log.warn("Unsupported LCAF type, please specify a correct LCAF type");
+
             return null;
         }
     }
@@ -397,7 +403,9 @@
                     new LispSourceDestLcafAddress.SourceDestLcafAddressWriter().writeTo(byteBuf,
                             (LispSourceDestLcafAddress) address);
                     break;
-                default: break; // TODO: need to log warning message
+                default:
+                    log.warn("Unsupported LCAF type, please specify a correct LCAF type");
+                    break;
             }
         }
     }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispListLcafAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispListLcafAddress.java
index 68834db..c51a8aa 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispListLcafAddress.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispListLcafAddress.java
@@ -25,6 +25,7 @@
 import java.util.Objects;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
 
 /**
  * List type LCAF address class.
@@ -67,6 +68,10 @@
      */
     public LispListLcafAddress(List<LispAfiAddress> addresses) {
         super(LispCanonicalAddressFormatEnum.LIST, LENGTH);
+
+        checkArgument(checkAddressValidity(addresses), "Malformed addresses, please " +
+                "specify IPv4 address first, and then specify IPv6 address");
+
         this.addresses = addresses;
     }
 
@@ -81,10 +86,42 @@
     public LispListLcafAddress(byte reserved1, byte reserved2, byte flag,
                                List<LispAfiAddress> addresses) {
         super(LispCanonicalAddressFormatEnum.LIST, reserved1, flag, reserved2, LENGTH);
+
+        checkArgument(checkAddressValidity(addresses), "Malformed addresses, please " +
+                "specify IPv4 address first, and then specify IPv6 address");
+
         this.addresses = addresses;
     }
 
     /**
+     * Checks the validity of a collection of input addresses.
+     *
+     * @param addresses a collection of addresses
+     * @return validity result
+     */
+    private boolean checkAddressValidity(List<LispAfiAddress> addresses) {
+        // we need to make sure that the address collection is comprised of
+        // one IPv4 address and one IPv6 address
+
+        if (addresses == null || addresses.size() != 2) {
+            return false;
+        }
+
+        LispAfiAddress ipv4 = addresses.get(0);
+        LispAfiAddress ipv6 = addresses.get(1);
+
+        if (ipv4.getAfi() != AddressFamilyIdentifierEnum.IP4) {
+            return false;
+        }
+
+        if (ipv6.getAfi() != AddressFamilyIdentifierEnum.IP6) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
      * Obtains a set of AFI addresses including IPv4 and IPv6.
      *
      * @return a set of AFI addresses
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSegmentLcafAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSegmentLcafAddress.java
index c6a84cc..0004965 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSegmentLcafAddress.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSegmentLcafAddress.java
@@ -23,6 +23,7 @@
 import java.util.Objects;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * Instance ID type LCAF address class.
@@ -181,6 +182,9 @@
          * @return LispSegmentLcafAddress instance
          */
         public LispSegmentLcafAddress build() {
+
+            checkNotNull(address, "Must specify an address");
+
             return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
                                               length, instanceId, address);
         }
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSourceDestLcafAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSourceDestLcafAddress.java
index 80dd538..954c2b4 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSourceDestLcafAddress.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/LispSourceDestLcafAddress.java
@@ -23,6 +23,7 @@
 import java.util.Objects;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * Source/Dest key type LCAF address class.
@@ -248,6 +249,10 @@
          * @return LispSourceDestLcafAddress instance
          */
         public LispSourceDestLcafAddress build() {
+
+            checkNotNull(srcPrefix, "Must specify a source address prefix");
+            checkNotNull(dstPrefix, "Must specify a destination address prefix");
+
             return new LispSourceDestLcafAddress(reserved1, reserved2, flag, length,
                     reserved, srcMaskLength, dstMaskLength, srcPrefix, dstPrefix);
         }