Jian Li
Committed by Gerrit Code Review

Add NotNull and Argument check during object build for LISP object

Also correct IP enumeration type into IP4.

Change-Id: Id970ee608c885826cfd2de7b5448b2bc451e079f
......@@ -24,6 +24,7 @@ import org.onosproject.lisp.msg.exceptions.LispWriterException;
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 @@ public final class DefaultLispLocatorRecord implements LispLocatorRecord {
@Override
public LispLocatorRecord build() {
checkNotNull(locatorAfi, "Must specify a locator address");
return new DefaultLispLocatorRecord(priority, weight, multicastPriority,
multicastWeight, localLocator, rlocProbed, routed, locatorAfi);
}
......
......@@ -152,7 +152,7 @@ public final class DefaultLispMapNotify implements LispMapNotify {
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 final class DefaultLispMapNotify implements LispMapNotify {
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 @@ public final class DefaultLispMapNotify implements LispMapNotify {
authenticationData = new byte[0];
}
if (mapRecords == null) {
mapRecords = Lists.newArrayList();
}
return new DefaultLispMapNotify(nonce, keyId, authDataLength,
authenticationData, recordCount, mapRecords);
}
......
......@@ -28,6 +28,7 @@ import org.onosproject.lisp.msg.types.LispAfiAddress;
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 @@ public final class DefaultLispMapRecord implements LispMapRecord {
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 final class DefaultLispMapRecord implements LispMapRecord {
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 @@ public final class DefaultLispMapRecord implements LispMapRecord {
@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);
......
......@@ -175,9 +175,9 @@ public final class DefaultLispMapRegister implements LispMapRegister {
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 final class DefaultLispMapRegister implements LispMapRegister {
public RegisterBuilder withAuthenticationData(byte[] authenticationData) {
if (authenticationData != null) {
this.authenticationData = authenticationData;
} else {
this.authenticationData = new byte[0];
}
return this;
}
......@@ -236,22 +234,12 @@ public final class DefaultLispMapRegister implements LispMapRegister {
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);
}
......
......@@ -146,7 +146,7 @@ public final class DefaultLispMapReply implements LispMapReply {
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 @@ public final class DefaultLispMapReply implements LispMapReply {
@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);
}
}
......
......@@ -270,23 +270,16 @@ public final class DefaultLispMapRequest implements LispMapRequest {
@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;
}
......
......@@ -21,6 +21,7 @@ import org.onosproject.lisp.msg.exceptions.LispReaderException;
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 final class LispEidRecord {
*/
public LispEidRecord(byte maskLength, LispAfiAddress prefix) {
this.maskLength = maskLength;
checkNotNull(prefix, "Must specify an address prefix");
this.prefix = prefix;
}
......
......@@ -26,7 +26,7 @@ package org.onosproject.lisp.msg.types;
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
......
......@@ -90,7 +90,7 @@ public abstract class LispAfiAddress {
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 @@ public abstract class LispAfiAddress {
byteBuf.writeShort(address.getAfi().getIanaCode());
switch (address.getAfi()) {
case IP:
case IP4:
new LispIpAddress.IpAddressWriter().writeTo(byteBuf, (LispIpv4Address) address);
break;
case IP6:
......
......@@ -25,6 +25,7 @@ import java.util.Arrays;
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 @@ public final class LispAppDataLcafAddress extends LispLcafAddress {
* @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);
......
......@@ -35,7 +35,7 @@ public class LispIpv4Address extends LispIpAddress {
* @param address IP address
*/
public LispIpv4Address(IpAddress address) {
super(address, AddressFamilyIdentifierEnum.IP);
super(address, AddressFamilyIdentifierEnum.IP4);
checkArgument(address.isIp4());
}
......
......@@ -19,6 +19,8 @@ import io.netty.buffer.ByteBuf;
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 @@ import static org.onosproject.lisp.msg.types.LispCanonicalAddressFormatEnum.*;
*/
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 @@ public class LispLcafAddress extends LispAfiAddress {
return new LispSourceDestLcafAddress.SourceDestLcafAddressReader().readFrom(byteBuf);
}
log.warn("Unsupported LCAF type, please specify a correct LCAF type");
return null;
}
}
......@@ -397,7 +403,9 @@ public class LispLcafAddress extends LispAfiAddress {
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;
}
}
}
......
......@@ -25,6 +25,7 @@ import java.util.List;
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 final class LispListLcafAddress extends LispLcafAddress {
*/
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 final class LispListLcafAddress extends LispLcafAddress {
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
......
......@@ -23,6 +23,7 @@ import org.onosproject.lisp.msg.exceptions.LispWriterException;
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 @@ public final class LispSegmentLcafAddress extends LispLcafAddress {
* @return LispSegmentLcafAddress instance
*/
public LispSegmentLcafAddress build() {
checkNotNull(address, "Must specify an address");
return new LispSegmentLcafAddress(reserved1, idMaskLength, flag,
length, instanceId, address);
}
......
......@@ -23,6 +23,7 @@ import org.onosproject.lisp.msg.exceptions.LispWriterException;
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 @@ public final class LispSourceDestLcafAddress extends LispLcafAddress {
* @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);
}
......