Charles Chan
Committed by Gerrit Code Review

Fix GroupKey interpretation in REST

Input string from REST should not be converted into byte array directly.
Before: "1" -> ascii 49 -> 0x31
After: "0x01" -> 0x01
GroupKey is a byte array with arbitrary length and represented by hex string

Change-Id: If27101f0e5522212c7e434fab58b66e67e9676d7
......@@ -23,7 +23,6 @@ import java.util.Arrays;
* Default implementation of group key interface.
*/
public class DefaultGroupKey implements GroupKey {
private final byte[] key;
protected static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
......@@ -66,7 +65,6 @@ public class DefaultGroupKey implements GroupKey {
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return "GroupKey:0x" + new String(hexChars);
return "0x" + new String(hexChars);
}
}
\ No newline at end of file
......
......@@ -18,6 +18,7 @@ package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.util.HexString;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
......@@ -118,7 +119,11 @@ public final class GroupCodec extends JsonCodec<Group> {
// parse group key (appCookie)
String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE),
APP_COOKIE + MISSING_MEMBER_MESSAGE).asText();
GroupKey groupKey = new DefaultGroupKey(groupKeyStr.getBytes());
if (!groupKeyStr.startsWith("0x")) {
throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x");
}
GroupKey groupKey = new DefaultGroupKey(HexString.fromHexString(
groupKeyStr.split("0x")[1], ""));
// parse device id
DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
......
......@@ -44,6 +44,7 @@ import static org.easymock.EasyMock.replay;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.equalTo;
import static org.onosproject.codec.impl.GroupJsonMatcher.matchesGroup;
import static org.onosproject.net.NetTestTools.APP_ID;
......@@ -109,6 +110,11 @@ public class GroupCodecTest {
assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.portNumber(2)));
}
@Test(expected = IllegalArgumentException.class)
public void invalidGroupTest() throws IOException {
Group group = getGroup("invalid-group.json");
}
/**
* Checks that the data shared by all the resource is correct for a given group.
*
......@@ -118,7 +124,8 @@ public class GroupCodecTest {
assertThat(group.appId(), is(APP_ID));
assertThat(group.deviceId().toString(), is("of:0000000000000001"));
assertThat(group.type().toString(), is("ALL"));
assertThat(group.appCookie().key(), is("1".getBytes()));
assertThat(group.appCookie().key(),
equalTo(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0xAB, (byte) 0xCD}));
assertThat(group.id().id(), is(1));
}
......
{
"type": "ALL",
"deviceId": "of:0000000000000001",
"appCookie": "1234abCD",
"groupId": "1",
"buckets": [
{
"treatment": {
"instructions": [
{
"type": "OUTPUT",
"port": 2
}
]
}
}
]
}
\ No newline at end of file
{
"type": "ALL",
"deviceId": "of:0000000000000001",
"appCookie": "1",
"appCookie": "0x1234abCD",
"groupId": "1",
"buckets": [
{
......
......@@ -67,7 +67,7 @@
"appCookie": {
"type": "string",
"description": "application cookie",
"example": "1"
"example": "0x1234abcd"
},
"buckets": {
"type": "array",
......
......@@ -67,11 +67,6 @@
"description": "types of the group",
"example": "ALL"
},
"deviceId": {
"type": "string",
"description": "device identifier",
"example": "of:0000000000000003"
},
"appId": {
"type": "string",
"description": "application identifier",
......@@ -80,7 +75,7 @@
"appCookie": {
"type": "string",
"description": "application cookie",
"example": "1"
"example": "0x1234abcd"
},
"buckets": {
"type": "array",
......
......@@ -19,7 +19,8 @@
},
"appCookie": {
"type": "string",
"example": "1"
"description": "application cookie. Arbitrary length byte array represented in hex string",
"example": "0x1234abcd"
},
"groupId": {
"type": "string",
......
{
"type": "ALL",
"deviceId": "of:0000000000000001",
"appCookie": "1",
"appCookie": "0x1",
"groupId": "1",
"buckets": [
{
......