Fix for ONOS-5175. GroupId refactoring.
Change-Id: I951392bdc69fe1ef694d321164b0b49032617d6b
diff --git a/core/api/src/main/java/org/onosproject/core/DefaultGroupId.java b/core/api/src/main/java/org/onosproject/core/DefaultGroupId.java
index bb5695f..41dc7a2 100644
--- a/core/api/src/main/java/org/onosproject/core/DefaultGroupId.java
+++ b/core/api/src/main/java/org/onosproject/core/DefaultGroupId.java
@@ -22,28 +22,21 @@
/**
* Default implementation of {@link GroupId}.
*/
-// TODO: require refactor to extend from Identifier base class
-public class DefaultGroupId implements GroupId {
-
- private final int id;
+@Deprecated
+public class DefaultGroupId extends GroupId {
public DefaultGroupId(int id) {
- this.id = id;
+ super(id);
}
// Constructor for serialization
private DefaultGroupId() {
- this.id = 0;
- }
-
- @Override
- public int id() {
- return this.id;
+ super(0);
}
@Override
public int hashCode() {
- return id;
+ return identifier;
}
@Override
@@ -55,13 +48,13 @@
return false;
}
final DefaultGroupId other = (DefaultGroupId) obj;
- return Objects.equals(this.id, other.id);
+ return Objects.equals(this.identifier, other.identifier);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
- .add("id", "0x" + Integer.toHexString(id))
+ .add("id", "0x" + Integer.toHexString(identifier))
.toString();
}
}
diff --git a/core/api/src/main/java/org/onosproject/core/GroupId.java b/core/api/src/main/java/org/onosproject/core/GroupId.java
index 2ab398b..c2a9d26 100644
--- a/core/api/src/main/java/org/onosproject/core/GroupId.java
+++ b/core/api/src/main/java/org/onosproject/core/GroupId.java
@@ -15,18 +15,39 @@
*/
package org.onosproject.core;
+import com.google.common.base.MoreObjects;
+import org.onlab.util.Identifier;
+
/**
* Group identifier.
*/
-// TODO: require refactor to extend from Identifier base class
-public interface GroupId {
+public class GroupId extends Identifier<Integer> {
+
+ public GroupId(int id) {
+ super(id);
+ }
+
+ // Constructor for serialization
+ private GroupId() {
+ super(0);
+ }
/**
* Returns a group ID as an integer value.
* The method is not intended for use by application developers.
* Return data type may change in the future release.
*
- * @return a group ID as integer value
+ * @param id int value
+ * @return group ID
*/
- int id();
+ public static GroupId valueOf(int id) {
+ return new GroupId(id);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("id", "0x" + Integer.toHexString(identifier))
+ .toString();
+ }
}
diff --git a/core/api/src/test/java/org/onosproject/core/GroupIdTest.java b/core/api/src/test/java/org/onosproject/core/GroupIdTest.java
new file mode 100644
index 0000000..5cd31f0
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/core/GroupIdTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.core;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+/**
+ * Test for GroupId.
+ */
+public class GroupIdTest {
+
+ /**
+ * Tests the equality of the instances.
+ */
+ @Test
+ public void testEquality() {
+ GroupId id1 = new GroupId((short) 1);
+ GroupId id2 = new GroupId((short) 1);
+ GroupId id3 = new GroupId((short) 2);
+
+ new EqualsTester()
+ .addEqualityGroup(id1, id2)
+ .addEqualityGroup(id3)
+ .testEquals();
+ }
+
+}
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 1fa9618..0292008 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
@@ -23,7 +23,6 @@
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
-import org.onosproject.core.DefaultGroupId;
import org.onosproject.core.GroupId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.group.DefaultGroup;
@@ -72,7 +71,7 @@
public ObjectNode encode(Group group, CodecContext context) {
checkNotNull(group, "Group cannot be null");
ObjectNode result = context.mapper().createObjectNode()
- .put(ID, group.id().toString())
+ .put(ID, group.id().id().toString())
.put(STATE, group.state().toString())
.put(LIFE, group.life())
.put(PACKETS, group.packets())
@@ -90,7 +89,7 @@
}
if (group.givenGroupId() != null) {
- result.put(GIVEN_GROUP_ID, group.givenGroupId());
+ result.put(GIVEN_GROUP_ID, group.givenGroupId().toString());
}
ArrayNode buckets = context.mapper().createArrayNode();
@@ -114,7 +113,7 @@
// parse group id
int groupIdInt = nullIsIllegal(json.get(GROUP_ID),
GROUP_ID + MISSING_MEMBER_MESSAGE).asInt();
- GroupId groupId = new DefaultGroupId(groupIdInt);
+ GroupId groupId = new GroupId(groupIdInt);
// parse group key (appCookie)
String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE),
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/GroupJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/GroupJsonMatcher.java
index 445d0c2..f1c9068 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/GroupJsonMatcher.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/GroupJsonMatcher.java
@@ -38,7 +38,7 @@
public boolean matchesSafely(JsonNode jsonGroup, Description description) {
// check id
String jsonGroupId = jsonGroup.get("id").asText();
- String groupId = group.id().toString();
+ String groupId = group.id().id().toString();
if (!jsonGroupId.equals(groupId)) {
description.appendText("group id was " + jsonGroupId);
return false;
diff --git a/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java b/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java
index d1930eb..cee9823 100644
--- a/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java
@@ -361,8 +361,8 @@
GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Group createdGroup = groupService.getGroup(deviceId, key);
int createdGroupId = createdGroup.id().id();
- assertNotEquals(gId1.id(), createdGroupId);
- assertNotEquals(gId2.id(), createdGroupId);
+ assertNotEquals(gId1.id().intValue(), createdGroupId);
+ assertNotEquals(gId2.id().intValue(), createdGroupId);
List<GroupOperation> expectedGroupOps = Arrays.asList(
GroupOperation.createDeleteGroupOperation(gId1,
diff --git a/web/api/src/test/java/org/onosproject/rest/resources/GroupsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/resources/GroupsResourceTest.java
index 67cf11b..0e494ef 100644
--- a/web/api/src/test/java/org/onosproject/rest/resources/GroupsResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/resources/GroupsResourceTest.java
@@ -272,9 +272,9 @@
public boolean matchesSafely(JsonObject jsonGroup) {
// check id
final String jsonId = jsonGroup.get("id").asString();
- final String groupId = group.id().toString();
+ final String groupId = group.id().id().toString();
if (!jsonId.equals(groupId)) {
- reason = "id " + group.id().toString();
+ reason = "id " + group.id().id().toString();
return false;
}
@@ -356,7 +356,7 @@
final JsonObject jsonGroup = json.get(jsonGroupIndex).asObject();
- final String groupId = group.id().toString();
+ final String groupId = group.id().id().toString();
final String jsonGroupId = jsonGroup.get("id").asString();
if (jsonGroupId.equals(groupId)) {
groupFound = true;
@@ -366,7 +366,7 @@
}
}
if (!groupFound) {
- reason = "Group with id " + group.id().toString() + " not found";
+ reason = "Group with id " + group.id().id().toString() + " not found";
return false;
} else {
return true;