Prince Pereira
Committed by Gerrit Code Review

Fix for ONOS-5175. GroupId refactoring.

Change-Id: I951392bdc69fe1ef694d321164b0b49032617d6b
......@@ -22,28 +22,21 @@ import java.util.Objects;
/**
* 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 @@ public class DefaultGroupId implements GroupId {
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();
}
}
......
......@@ -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();
}
}
......
/*
* 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();
}
}
......@@ -23,7 +23,6 @@ import org.onosproject.codec.CodecContext;
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 final class GroupCodec extends JsonCodec<Group> {
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 @@ public final class GroupCodec extends JsonCodec<Group> {
}
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 @@ public final class GroupCodec extends JsonCodec<Group> {
// 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),
......
......@@ -38,7 +38,7 @@ public final class GroupJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode>
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;
......
......@@ -361,8 +361,8 @@ public class GroupManagerTest {
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,
......
......@@ -272,9 +272,9 @@ public class GroupsResourceTest extends ResourceTest {
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 @@ public class GroupsResourceTest extends ResourceTest {
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 @@ public class GroupsResourceTest extends ResourceTest {
}
}
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;
......