Add unit tests for LISP control message serializer and deserializer
Change-Id: Id517db99635ad8e055d6581e5c0f3ac9f45f2869
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 30eba1f..14399c2 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
@@ -99,7 +99,11 @@
@Override
public byte[] getAuthenticationData() {
- return ImmutableByteSequence.copyFrom(authenticationData).asArray();
+ if (authenticationData != null && authenticationData.length != 0) {
+ return ImmutableByteSequence.copyFrom(authenticationData).asArray();
+ } else {
+ return new byte[0];
+ }
}
@Override
@@ -132,12 +136,13 @@
Objects.equal(recordCount, that.recordCount) &&
Objects.equal(keyId, that.keyId) &&
Objects.equal(authDataLength, that.authDataLength) &&
- Objects.equal(authenticationData, that.authenticationData);
+ Arrays.equals(authenticationData, that.authenticationData);
}
@Override
public int hashCode() {
- return Objects.hashCode(nonce, recordCount, keyId, authDataLength, authenticationData);
+ return Objects.hashCode(nonce, recordCount, keyId, authDataLength) +
+ Arrays.hashCode(authenticationData);
}
public static final class DefaultNotifyBuilder implements NotifyBuilder {
@@ -180,18 +185,35 @@
@Override
public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
- this.authenticationData = authenticationData;
+ if (authenticationData != null) {
+ this.authenticationData = authenticationData;
+ } else {
+ this.authenticationData = new byte[0];
+ }
return this;
}
@Override
public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
- this.mapRecords = ImmutableList.copyOf(mapRecords);
+ if (mapRecords != null) {
+ this.mapRecords = ImmutableList.copyOf(mapRecords);
+ } else {
+ this.mapRecords = Lists.newArrayList();
+ }
return this;
}
@Override
public LispMapNotify build() {
+
+ if (authenticationData == null) {
+ 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 a924c7f..f53d04d 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
@@ -209,12 +209,21 @@
@Override
public MapRecordBuilder withLocators(List<LispLocatorRecord> records) {
- this.locatorRecords = ImmutableList.copyOf(records);
+ if (records != null) {
+ this.locatorRecords = ImmutableList.copyOf(records);
+ } else {
+ this.locatorRecords = Lists.newArrayList();
+ }
return this;
}
@Override
public LispMapRecord build() {
+
+ if (locatorRecords == null) {
+ locatorRecords = Lists.newArrayList();
+ }
+
return new DefaultLispMapRecord(recordTtl, locatorCount, maskLength,
action, authoritative, mapVersionNumber, eidPrefixAfi, locatorRecords);
}
@@ -228,6 +237,8 @@
private static final int AUTHORITATIVE_INDEX = 4;
private static final int RESERVED_SKIP_LENGTH = 1;
+ private static final int REPLY_ACTION_SHIFT_BIT = 5;
+
@Override
public LispMapRecord readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
@@ -235,17 +246,22 @@
int recordTtl = byteBuf.readInt();
// Locator count -> 8 bits
- int locatorCount = byteBuf.readUnsignedShort();
+ int locatorCount = byteBuf.readUnsignedByte();
// EID mask length -> 8 bits
byte maskLength = (byte) byteBuf.readUnsignedByte();
- // TODO: need to de-serialize LispMapReplyAction
+ byte actionWithFlag = (byte) byteBuf.readUnsignedByte();
- byte actionWithFlag = byteBuf.readByte();
+ // action -> 3 bit
+ int actionByte = actionWithFlag >> REPLY_ACTION_SHIFT_BIT;
+ LispMapReplyAction action = LispMapReplyAction.valueOf(actionByte);
+ if (action == null) {
+ action = LispMapReplyAction.NoAction;
+ }
// authoritative flag -> 1 bit
- boolean authoritative = ByteOperator.getBit(actionWithFlag, AUTHORITATIVE_INDEX);
+ boolean authoritative = ByteOperator.getBit((byte) (actionWithFlag >> AUTHORITATIVE_INDEX), 0);
// let's skip the reserved field
byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
@@ -264,6 +280,7 @@
.withRecordTtl(recordTtl)
.withLocatorCount(locatorCount)
.withMaskLength(maskLength)
+ .withAction(action)
.withAuthoritative(authoritative)
.withMapVersionNumber(mapVersionNumber)
.withLocators(locators)
@@ -301,9 +318,8 @@
// authoritative bit
byte authoritative = DISABLE_BIT;
if (message.isAuthoritative()) {
- authoritative = ENABLE_BIT;
+ authoritative = ENABLE_BIT << AUTHORITATIVE_FLAG_SHIFT_BIT;
}
- authoritative = (byte) (authoritative << AUTHORITATIVE_FLAG_SHIFT_BIT);
byteBuf.writeByte((byte) (action + authoritative));
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 abaf3c8..b6ed216 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
@@ -29,6 +29,8 @@
import java.util.List;
import static com.google.common.base.MoreObjects.toStringHelper;
+import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordReader;
+import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordWriter;
/**
@@ -117,7 +119,11 @@
@Override
public byte[] getAuthenticationData() {
- return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
+ if (authenticationData != null && authenticationData.length != 0) {
+ return ImmutableByteSequence.copyFrom(authenticationData).asArray();
+ } else {
+ return new byte[0];
+ }
}
@Override
@@ -153,7 +159,7 @@
Objects.equal(recordCount, that.recordCount) &&
Objects.equal(keyId, that.keyId) &&
Objects.equal(authDataLength, that.authDataLength) &&
- Objects.equal(authenticationData, that.authenticationData) &&
+ Arrays.equals(authenticationData, that.authenticationData) &&
Objects.equal(proxyMapReply, that.proxyMapReply) &&
Objects.equal(wantMapNotify, that.wantMapNotify);
}
@@ -161,7 +167,7 @@
@Override
public int hashCode() {
return Objects.hashCode(nonce, recordCount, keyId, authDataLength,
- authenticationData, proxyMapReply, wantMapNotify);
+ proxyMapReply, wantMapNotify) + Arrays.hashCode(authenticationData);
}
public static final class DefaultRegisterBuilder implements RegisterBuilder {
@@ -218,18 +224,34 @@
@Override
public RegisterBuilder withAuthenticationData(byte[] authenticationData) {
- this.authenticationData = authenticationData;
+ if (authenticationData != null) {
+ this.authenticationData = authenticationData;
+ } else {
+ this.authenticationData = new byte[0];
+ }
return this;
}
@Override
public RegisterBuilder withMapRecords(List<LispMapRecord> mapRecords) {
- this.mapRecords = ImmutableList.copyOf(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);
}
@@ -280,7 +302,7 @@
List<LispMapRecord> mapRecords = Lists.newArrayList();
for (int i = 0; i < recordCount; i++) {
- mapRecords.add(new DefaultLispMapRecord.MapRecordReader().readFrom(byteBuf));
+ mapRecords.add(new MapRecordReader().readFrom(byteBuf));
}
return new DefaultRegisterBuilder()
@@ -360,7 +382,7 @@
// TODO: need to implement MAC authentication mechanism
// serialize map records
- DefaultLispMapRecord.MapRecordWriter writer = new DefaultLispMapRecord.MapRecordWriter();
+ MapRecordWriter writer = new MapRecordWriter();
List<LispMapRecord> records = message.getMapRecords();
for (int i = 0; i < records.size(); i++) {
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 220bf7e..f422740 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
@@ -185,12 +185,22 @@
@Override
public ReplyBuilder withMapRecords(List<LispMapRecord> mapRecords) {
- this.mapRecords = ImmutableList.copyOf(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 45e85bf..b62eed6 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
@@ -28,6 +28,8 @@
import java.util.List;
import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.lisp.msg.types.LispAfiAddress.AfiAddressWriter;
import static org.onosproject.lisp.msg.protocols.LispEidRecord.EidRecordWriter;
@@ -179,8 +181,6 @@
return Objects.equal(nonce, that.nonce) &&
Objects.equal(recordCount, that.recordCount) &&
Objects.equal(sourceEid, that.sourceEid) &&
- Objects.equal(itrRlocs, that.itrRlocs) &&
- Objects.equal(eidRecords, that.eidRecords) &&
Objects.equal(authoritative, that.authoritative) &&
Objects.equal(mapDataPresent, that.mapDataPresent) &&
Objects.equal(probe, that.probe) &&
@@ -191,8 +191,8 @@
@Override
public int hashCode() {
- return Objects.hashCode(nonce, recordCount, sourceEid, itrRlocs, eidRecords,
- authoritative, mapDataPresent, probe, smr, pitr, smrInvoked);
+ return Objects.hashCode(nonce, recordCount, sourceEid, authoritative,
+ mapDataPresent, probe, smr, pitr, smrInvoked);
}
public static final class DefaultRequestBuilder implements RequestBuilder {
@@ -270,18 +270,33 @@
@Override
public RequestBuilder withItrRlocs(List<LispAfiAddress> itrRlocs) {
- this.itrRlocs = ImmutableList.copyOf(itrRlocs);
+
+ if (itrRlocs != null) {
+ this.itrRlocs = ImmutableList.copyOf(itrRlocs);
+ } else {
+ this.itrRlocs = Lists.newArrayList();
+ }
+
return this;
}
@Override
public RequestBuilder withEidRecords(List<LispEidRecord> records) {
- this.eidRecords = ImmutableList.copyOf(records);
+
+ if (records != null) {
+ this.eidRecords = ImmutableList.copyOf(records);
+ } else {
+ this.eidRecords = Lists.newArrayList();
+ }
return this;
}
@Override
public LispMapRequest build() {
+
+ checkNotNull(sourceEid, "Must have a source EID");
+ checkArgument((itrRlocs != null) && (itrRlocs.size() > 0), "Must have an ITR RLOC entry");
+
return new DefaultLispMapRequest(nonce, recordCount, sourceEid, itrRlocs,
eidRecords, authoritative, mapDataPresent, probe, smr, pitr, smrInvoked);
}
@@ -331,10 +346,10 @@
// let's skip reserved field, only obtains ITR counter value
// assume that first 3 bits are all set as 0,
// remain 5 bits represent Itr Rloc Counter (IRC)
- int irc = byteBuf.readUnsignedShort();
+ int irc = byteBuf.readUnsignedByte();
// record count -> 8 bits
- int recordCount = byteBuf.readUnsignedShort();
+ int recordCount = byteBuf.readUnsignedByte();
// nonce -> 64 bits
long nonce = byteBuf.readLong();
@@ -387,8 +402,6 @@
private static final int ENABLE_BIT = 1;
private static final int DISABLE_BIT = 0;
- private static final int UNUSED_ZERO = 0;
-
@Override
public void writeTo(ByteBuf byteBuf, LispMapRequest message) throws LispWriterException {
@@ -435,8 +448,8 @@
byteBuf.writeByte((byte) (pitr + smrInvoked));
- // TODO: ITR RLOC count
- byteBuf.writeByte((byte) UNUSED_ZERO);
+ // ITR Rloc count
+ byteBuf.writeByte((byte) message.getItrRlocs().size());
// record count
byteBuf.writeByte(message.getRecordCount());