Sho SHIMIZU
Committed by Gerrit Code Review

Rename ResourcePath.Key to ResourceId for naming consistency

Change-Id: Idb34624f897b0fd5745f26758a007c171f4cd774
/*
* 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.net.newresource;
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Represents identifier of resource.
* This class is exposed to public, but intended to use only in ResourceStore implementations.
*/
@Beta
public final class ResourceId {
static final ResourceId ROOT = new ResourceId();
final ImmutableList<Object> components;
static ResourceId of(DeviceId device, Object... components) {
return new ResourceId(ImmutableList.builder()
.add(device)
.add(components)
.build());
}
static ResourceId of(DeviceId device, PortNumber port, Object... components) {
return new ResourceId(ImmutableList.builder()
.add(device)
.add(port)
.add(components)
.build());
}
private ResourceId(ImmutableList<Object> components) {
this.components = checkNotNull(components);
}
// for serializer
private ResourceId() {
this.components = ImmutableList.of();
}
// IndexOutOfBoundsException is raised when the instance is equal to ROOT
ResourceId parent() {
if (components.size() == 1) {
return ROOT;
} else {
return new ResourceId(components.subList(0, components.size() - 1));
}
}
ResourceId child(Object child) {
return new ResourceId(ImmutableList.builder()
.add(components)
.add(child)
.build());
}
@Override
public int hashCode() {
return components.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof ResourceId)) {
return false;
}
ResourceId other = (ResourceId) obj;
return Objects.equals(this.components, other.components);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("components", components)
.toString();
}
}
......@@ -17,7 +17,6 @@ package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
......@@ -49,12 +48,12 @@ import static com.google.common.base.Preconditions.checkState;
public abstract class ResourcePath {
private final Discrete parent;
private final Key key;
private final ResourceId id;
public static final Discrete ROOT = new Discrete();
public static ResourcePath discrete(DeviceId device) {
return new Discrete(Key.of(device));
return new Discrete(ResourceId.of(device));
}
/**
......@@ -65,7 +64,7 @@ public abstract class ResourcePath {
* @return resource path instance
*/
public static ResourcePath discrete(DeviceId device, Object... components) {
return new Discrete(Key.of(device, components));
return new Discrete(ResourceId.of(device, components));
}
/**
......@@ -77,7 +76,7 @@ public abstract class ResourcePath {
* @return resource path instance
*/
public static ResourcePath discrete(DeviceId device, PortNumber port, Object... components) {
return new Discrete(Key.of(device, port, components));
return new Discrete(ResourceId.of(device, port, components));
}
/**
......@@ -92,7 +91,7 @@ public abstract class ResourcePath {
checkArgument(components.length > 0,
"Length of components must be greater thant 0, but " + components.length);
return new Continuous(Key.of(device, components), value);
return new Continuous(ResourceId.of(device, components), value);
}
/**
......@@ -105,29 +104,29 @@ public abstract class ResourcePath {
* @return resource path instance
*/
public static ResourcePath continuous(double value, DeviceId device, PortNumber port, Object... components) {
return new Continuous(Key.of(device, port, components), value);
return new Continuous(ResourceId.of(device, port, components), value);
}
/**
* Creates an resource path from the specified key.
*
* @param key key of the path
* @param id key of the path
*/
protected ResourcePath(Key key) {
checkNotNull(key);
protected ResourcePath(ResourceId id) {
checkNotNull(id);
this.key = key;
if (key.components.size() == 1) {
this.id = id;
if (id.components.size() == 1) {
this.parent = ROOT;
} else {
this.parent = new Discrete(key.parent());
this.parent = new Discrete(id.parent());
}
}
// for serialization
private ResourcePath() {
this.parent = null;
this.key = Key.ROOT;
this.id = ResourceId.ROOT;
}
/**
......@@ -136,7 +135,7 @@ public abstract class ResourcePath {
* @return the components of this resource path
*/
public List<Object> components() {
return key.components;
return id.components;
}
/**
......@@ -160,7 +159,7 @@ public abstract class ResourcePath {
public ResourcePath child(Object child) {
checkState(this instanceof Discrete);
return new Discrete(key().child(child));
return new Discrete(id().child(child));
}
/**
......@@ -174,7 +173,7 @@ public abstract class ResourcePath {
public ResourcePath child(Object child, double value) {
checkState(this instanceof Discrete);
return new Continuous(key.child(child), value);
return new Continuous(id.child(child), value);
}
/**
......@@ -184,10 +183,10 @@ public abstract class ResourcePath {
* The return value is equal to the last object of {@code components()}.
*/
public Object last() {
if (key.components.isEmpty()) {
if (id.components.isEmpty()) {
return null;
}
return key.components.get(key.components.size() - 1);
return id.components.get(id.components.size() - 1);
}
/**
......@@ -195,13 +194,13 @@ public abstract class ResourcePath {
*
* @return the key of this resource path
*/
public Key key() {
return key;
public ResourceId id() {
return id;
}
@Override
public int hashCode() {
return key.hashCode();
return id.hashCode();
}
@Override
......@@ -213,13 +212,13 @@ public abstract class ResourcePath {
return false;
}
final ResourcePath that = (ResourcePath) obj;
return Objects.equals(this.key, that.key);
return Objects.equals(this.id, that.id);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("key", key)
.add("key", id)
.toString();
}
......@@ -237,7 +236,7 @@ public abstract class ResourcePath {
super();
}
private Discrete(Key key) {
private Discrete(ResourceId key) {
super(key);
}
}
......@@ -253,14 +252,14 @@ public abstract class ResourcePath {
public static final class Continuous extends ResourcePath {
private final double value;
private Continuous(Key key, double value) {
private Continuous(ResourceId key, double value) {
super(key);
this.value = value;
}
@Override
public int hashCode() {
return Objects.hash(this.key(), this.value);
return Objects.hash(this.id(), this.value);
}
@Override
......@@ -278,7 +277,7 @@ public abstract class ResourcePath {
}
final Continuous other = (Continuous) obj;
return Objects.equals(this.key(), other.key());
return Objects.equals(this.id(), other.id());
}
/**
......@@ -291,79 +290,4 @@ public abstract class ResourcePath {
}
}
/**
* Represents key of resource path used as a key in ResourceStore.
* This class is exposed to public, but intended to use only in ResourceStore implementations.
*/
@Beta
public static final class Key {
private static final Key ROOT = new Key();
private final ImmutableList<Object> components;
private static Key of(DeviceId device, Object... components) {
return new Key(ImmutableList.builder()
.add(device)
.add(components)
.build());
}
private static Key of(DeviceId device, PortNumber port, Object... components) {
return new Key(ImmutableList.builder()
.add(device)
.add(port)
.add(components)
.build());
}
private Key(ImmutableList<Object> components) {
this.components = checkNotNull(components);
}
// for serializer
private Key() {
this.components = ImmutableList.of();
}
// IndexOutOfBoundsException is raised when the instance is equal to ROOT
private Key parent() {
if (components.size() == 1) {
return ROOT;
} else {
return new Key(components.subList(0, components.size() - 1));
}
}
private Key child(Object child) {
return new Key(ImmutableList.builder()
.add(components)
.add(child)
.build());
}
@Override
public int hashCode() {
return components.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Key)) {
return false;
}
Key other = (Key) obj;
return Objects.equals(this.components, other.components);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("components", components)
.toString();
}
}
}
......
......@@ -60,18 +60,18 @@ public class ResourcePathTest {
}
@Test
public void testKeyEquality() {
ResourcePath.Key key1 = ResourcePath.discrete(D1, P1, VLAN1).key();
ResourcePath.Key sameAsKey1 = ResourcePath.discrete(D1, P1, VLAN1).key();
ResourcePath.Key key2 = ResourcePath.discrete(D2, P1, VLAN1).key();
ResourcePath.Key key3 = ResourcePath.continuous(BW1.bps(), D1, P1, BW1).key();
public void testIdEquality() {
ResourceId id1 = ResourcePath.discrete(D1, P1, VLAN1).id();
ResourceId sameAsId1 = ResourcePath.discrete(D1, P1, VLAN1).id();
ResourceId id2 = ResourcePath.discrete(D2, P1, VLAN1).id();
ResourceId id3 = ResourcePath.continuous(BW1.bps(), D1, P1, BW1).id();
// intentionally set a different value
ResourcePath.Key sameAsKey3 = ResourcePath.continuous(BW2.bps(), D1, P1, BW1).key();
ResourceId sameAsId3 = ResourcePath.continuous(BW2.bps(), D1, P1, BW1).id();
new EqualsTester()
.addEqualityGroup(key1, sameAsKey1)
.addEqualityGroup(key2)
.addEqualityGroup(key3, sameAsKey3);
.addEqualityGroup(id1, sameAsId1)
.addEqualityGroup(id2)
.addEqualityGroup(id3, sameAsId3);
}
@Test
......
......@@ -172,6 +172,7 @@ import org.onosproject.net.intent.constraint.WaypointConstraint;
import org.onosproject.net.link.DefaultLinkDescription;
import org.onosproject.net.meter.MeterId;
import org.onosproject.net.newresource.ResourceAllocation;
import org.onosproject.net.newresource.ResourceId;
import org.onosproject.net.newresource.ResourcePath;
import org.onosproject.net.packet.DefaultOutboundPacket;
import org.onosproject.net.packet.DefaultPacketRequest;
......@@ -430,7 +431,7 @@ public final class KryoNamespaces {
ResourcePath.class,
ResourcePath.Discrete.class,
ResourcePath.Continuous.class,
ResourcePath.Key.class,
ResourceId.class,
ResourceAllocation.class,
// Constraints
LambdaConstraint.class,
......
......@@ -379,7 +379,7 @@ public class KryoSerializerTest {
@Test
public void testResourceKey() {
testSerializedEquals(ResourcePath.discrete(DID1, P1).key());
testSerializedEquals(ResourcePath.discrete(DID1, P1).id());
}
@Test
......