Sho SHIMIZU
Committed by Gerrit Code Review

Avoid double wrap of Bandwidth class

Change-Id: I6bb95beafd55364499df64429c0788153a0b1624
......@@ -39,7 +39,6 @@ import org.onosproject.net.intent.constraint.BandwidthConstraint;
import org.onosproject.net.intent.constraint.LambdaConstraint;
import org.onosproject.net.intent.constraint.LinkTypeConstraint;
import org.onosproject.net.intent.constraint.PartialFailureConstraint;
import org.onosproject.net.resource.link.BandwidthResource;
import java.util.LinkedList;
import java.util.List;
......@@ -365,7 +364,7 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
// Check for a bandwidth specification
if (!isNullOrEmpty(bandwidthString)) {
final Bandwidth bandwidth = Bandwidth.bps(Double.parseDouble(bandwidthString));
constraints.add(new BandwidthConstraint(new BandwidthResource(bandwidth)));
constraints.add(new BandwidthConstraint(bandwidth));
}
// Check for a lambda specification
......
......@@ -17,9 +17,9 @@ package org.onosproject.net.intent.constraint;
import com.google.common.annotations.Beta;
import org.onlab.util.Bandwidth;
import org.onlab.util.DataRateUnit;
import org.onosproject.net.Link;
import org.onosproject.net.resource.link.BandwidthResource;
import org.onosproject.net.resource.link.BandwidthResourceRequest;
import org.onosproject.net.resource.link.LinkResourceService;
import org.onosproject.net.resource.ResourceRequest;
......@@ -36,14 +36,14 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Beta
public final class BandwidthConstraint extends BooleanConstraint {
private final BandwidthResource bandwidth;
private final Bandwidth bandwidth;
/**
* Creates a new bandwidth constraint.
*
* @param bandwidth required bandwidth
*/
public BandwidthConstraint(BandwidthResource bandwidth) {
public BandwidthConstraint(Bandwidth bandwidth) {
this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
}
......@@ -55,7 +55,7 @@ public final class BandwidthConstraint extends BooleanConstraint {
* @return {@link BandwidthConstraint} instance with given bandwidth requirement
*/
public static BandwidthConstraint of(double v, DataRateUnit unit) {
return new BandwidthConstraint(BandwidthResource.of(v, unit));
return new BandwidthConstraint(Bandwidth.of(v, unit));
}
// Constructor for serialization
......@@ -68,7 +68,7 @@ public final class BandwidthConstraint extends BooleanConstraint {
for (ResourceRequest request : resourceService.getAvailableResources(link)) {
if (request.type() == ResourceType.BANDWIDTH) {
BandwidthResourceRequest brr = (BandwidthResourceRequest) request;
if (brr.bandwidth().toDouble() >= bandwidth.toDouble()) {
if (brr.bandwidth().toDouble() >= bandwidth.bps()) {
return true;
}
}
......@@ -81,7 +81,7 @@ public final class BandwidthConstraint extends BooleanConstraint {
*
* @return required bandwidth
*/
public BandwidthResource bandwidth() {
public Bandwidth bandwidth() {
return bandwidth;
}
......
......@@ -201,7 +201,7 @@ public final class DefaultLinkResourceRequest implements LinkResourceRequest {
return addLambdaRequest();
} else if (constraint instanceof BandwidthConstraint) {
BandwidthConstraint bw = (BandwidthConstraint) constraint;
return addBandwidthRequest(bw.bandwidth().toDouble());
return addBandwidthRequest(bw.bandwidth().bps());
}
return this;
}
......
......@@ -18,7 +18,6 @@ package org.onosproject.net.intent.constraint;
import org.junit.Test;
import org.onlab.util.Bandwidth;
import org.onosproject.net.Link;
import org.onosproject.net.resource.link.BandwidthResource;
import org.onosproject.net.resource.link.LambdaResource;
import com.google.common.testing.EqualsTester;
......@@ -39,21 +38,18 @@ public class ConstraintObjectsTest {
private final Bandwidth sameAsBandwidth1 = Bandwidth.bps(100.0);
private final Bandwidth bandwidth2 = Bandwidth.bps(200.0);
final BandwidthConstraint bandwidthConstraint1 =
new BandwidthConstraint(new BandwidthResource(bandwidth1));
final BandwidthConstraint bandwidthConstraintSameAs1 =
new BandwidthConstraint(new BandwidthResource(sameAsBandwidth1));
final BandwidthConstraint bandwidthConstraint2 =
new BandwidthConstraint(new BandwidthResource(bandwidth2));
final BandwidthConstraint bandwidthConstraint1 = new BandwidthConstraint(bandwidth1);
final BandwidthConstraint bandwidthConstraintSameAs1 = new BandwidthConstraint(sameAsBandwidth1);
final BandwidthConstraint bandwidthConstraint2 = new BandwidthConstraint(bandwidth2);
/**
* Checks that the objects were created properly.
*/
@Test
public void testBandwidthConstraintCreation() {
assertThat(bandwidthConstraint1.bandwidth().toDouble(), is(equalTo(100.0)));
assertThat(bandwidthConstraintSameAs1.bandwidth().toDouble(), is(equalTo(100.0)));
assertThat(bandwidthConstraint2.bandwidth().toDouble(), is(equalTo(200.0)));
assertThat(bandwidthConstraint1.bandwidth().bps(), is(equalTo(100.0)));
assertThat(bandwidthConstraintSameAs1.bandwidth().bps(), is(equalTo(100.0)));
assertThat(bandwidthConstraint2.bandwidth().bps(), is(equalTo(200.0)));
}
/**
......
......@@ -32,7 +32,6 @@ import org.onosproject.net.intent.constraint.LatencyConstraint;
import org.onosproject.net.intent.constraint.LinkTypeConstraint;
import org.onosproject.net.intent.constraint.ObstacleConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import org.onosproject.net.resource.link.BandwidthResource;
import org.onosproject.net.resource.link.LambdaResource;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -185,7 +184,7 @@ public final class DecodeConstraintCodecHelper {
ConstraintCodec.BANDWIDTH + ConstraintCodec.MISSING_MEMBER_MESSAGE)
.asDouble();
return new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(bandwidth)));
return new BandwidthConstraint(Bandwidth.bps(bandwidth));
}
/**
......
......@@ -128,7 +128,7 @@ public final class EncodeConstraintCodecHelper {
final BandwidthConstraint bandwidthConstraint =
(BandwidthConstraint) constraint;
return context.mapper().createObjectNode()
.put("bandwidth", bandwidthConstraint.bandwidth().toDouble());
.put("bandwidth", bandwidthConstraint.bandwidth().bps());
}
/**
......
......@@ -134,7 +134,7 @@ public class ConstraintCodecTest {
assertThat(constraint, instanceOf(BandwidthConstraint.class));
BandwidthConstraint bandwidthConstraint = (BandwidthConstraint) constraint;
assertThat(bandwidthConstraint.bandwidth().toDouble(), is(345.678D));
assertThat(bandwidthConstraint.bandwidth().bps(), is(345.678D));
}
/**
......
......@@ -63,7 +63,6 @@ import org.onosproject.net.intent.constraint.LambdaConstraint;
import org.onosproject.net.intent.constraint.LatencyConstraint;
import org.onosproject.net.intent.constraint.ObstacleConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import org.onosproject.net.resource.link.BandwidthResource;
import org.onosproject.net.resource.link.LambdaResource;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -181,7 +180,7 @@ public class IntentCodecTest extends AbstractIntentTest {
final List<Constraint> constraints =
ImmutableList.of(
new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
new BandwidthConstraint(Bandwidth.bps(1.0)),
new LambdaConstraint(LambdaResource.valueOf(3)),
new AnnotationConstraint("key", 33.0),
new AsymmetricPathConstraint(),
......
......@@ -141,7 +141,7 @@ public final class IntentJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode>
final JsonNode bandwidthJson = constraintJson.get("bandwidth");
return bandwidthJson != null
&& constraintJson.get("bandwidth").asDouble()
== bandwidthConstraint.bandwidth().toDouble();
== bandwidthConstraint.bandwidth().bps();
}
/**
......
......@@ -34,7 +34,6 @@ import org.onosproject.net.intent.PointToPointIntent;
import org.onosproject.net.intent.constraint.BandwidthConstraint;
import org.onosproject.net.intent.constraint.LambdaConstraint;
import org.onosproject.net.intent.impl.PathNotFoundException;
import org.onosproject.net.resource.link.BandwidthResource;
import org.onosproject.net.resource.link.LambdaResource;
import org.onosproject.net.resource.link.LinkResourceService;
......@@ -229,7 +228,7 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest {
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
final List<Constraint> constraints =
Collections.singletonList(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
......@@ -251,7 +250,7 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest {
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
final List<Constraint> constraints =
Collections.singletonList(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
try {
final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
......
......@@ -400,7 +400,7 @@ public class KryoSerializerTest {
@Test
public void testBandwidthConstraint() {
testSerializable(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1000.0))));
testSerializable(new BandwidthConstraint(Bandwidth.bps(1000.0)));
}
@Test
......