diff --git a/core/api/src/main/java/org/onosproject/net/group/DefaultGroupKey.java b/core/api/src/main/java/org/onosproject/net/group/DefaultGroupKey.java index 10c6ad1cfa..a6d725bd7f 100644 --- a/core/api/src/main/java/org/onosproject/net/group/DefaultGroupKey.java +++ b/core/api/src/main/java/org/onosproject/net/group/DefaultGroupKey.java @@ -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 diff --git a/core/common/src/main/java/org/onosproject/codec/impl/GroupCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/GroupCodec.java index 60ef0cf745..10ddd4de89 100644 --- a/core/common/src/main/java/org/onosproject/codec/impl/GroupCodec.java +++ b/core/common/src/main/java/org/onosproject/codec/impl/GroupCodec.java @@ -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 { // 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), diff --git a/core/common/src/test/java/org/onosproject/codec/impl/GroupCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/GroupCodecTest.java index 2a3b7d403e..93473a516e 100644 --- a/core/common/src/test/java/org/onosproject/codec/impl/GroupCodecTest.java +++ b/core/common/src/test/java/org/onosproject/codec/impl/GroupCodecTest.java @@ -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)); } diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/invalid-group.json b/core/common/src/test/resources/org/onosproject/codec/impl/invalid-group.json new file mode 100644 index 0000000000..c6758cf99a --- /dev/null +++ b/core/common/src/test/resources/org/onosproject/codec/impl/invalid-group.json @@ -0,0 +1,18 @@ +{ + "type": "ALL", + "deviceId": "of:0000000000000001", + "appCookie": "1234abCD", + "groupId": "1", + "buckets": [ + { + "treatment": { + "instructions": [ + { + "type": "OUTPUT", + "port": 2 + } + ] + } + } + ] +} \ No newline at end of file diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/simple-group.json b/core/common/src/test/resources/org/onosproject/codec/impl/simple-group.json index 675f24413f..865ae23a02 100644 --- a/core/common/src/test/resources/org/onosproject/codec/impl/simple-group.json +++ b/core/common/src/test/resources/org/onosproject/codec/impl/simple-group.json @@ -1,7 +1,7 @@ { "type": "ALL", "deviceId": "of:0000000000000001", - "appCookie": "1", + "appCookie": "0x1234abCD", "groupId": "1", "buckets": [ { diff --git a/web/api/src/main/resources/definitions/Group.json b/web/api/src/main/resources/definitions/Group.json index 8a689a3d27..53d226e9ff 100644 --- a/web/api/src/main/resources/definitions/Group.json +++ b/web/api/src/main/resources/definitions/Group.json @@ -67,7 +67,7 @@ "appCookie": { "type": "string", "description": "application cookie", - "example": "1" + "example": "0x1234abcd" }, "buckets": { "type": "array", diff --git a/web/api/src/main/resources/definitions/Groups.json b/web/api/src/main/resources/definitions/Groups.json index 3f9accad49..22f36fbe8f 100644 --- a/web/api/src/main/resources/definitions/Groups.json +++ b/web/api/src/main/resources/definitions/Groups.json @@ -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", diff --git a/web/api/src/main/resources/definitions/GroupsPost.json b/web/api/src/main/resources/definitions/GroupsPost.json index f096201b7c..7ea7b5022b 100644 --- a/web/api/src/main/resources/definitions/GroupsPost.json +++ b/web/api/src/main/resources/definitions/GroupsPost.json @@ -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", diff --git a/web/api/src/test/resources/org/onosproject/rest/resources/post-group.json b/web/api/src/test/resources/org/onosproject/rest/resources/post-group.json index 675f24413f..f062bf939b 100644 --- a/web/api/src/test/resources/org/onosproject/rest/resources/post-group.json +++ b/web/api/src/test/resources/org/onosproject/rest/resources/post-group.json @@ -1,7 +1,7 @@ { "type": "ALL", "deviceId": "of:0000000000000001", - "appCookie": "1", + "appCookie": "0x1", "groupId": "1", "buckets": [ {