Sho SHIMIZU

Inline static factory methods in BandwidthResource

Change-Id: I8ac7fbaea5c81c9558f88f6ac88bcf581466d073
......@@ -24,6 +24,7 @@ import java.util.List;
import org.apache.karaf.shell.commands.Option;
import org.onlab.packet.Ip6Address;
import org.onlab.packet.IpAddress;
import org.onlab.util.Bandwidth;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
......@@ -306,8 +307,8 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
// Check for a bandwidth specification
if (!isNullOrEmpty(bandwidthString)) {
final double bandwidthValue = Double.parseDouble(bandwidthString);
constraints.add(new BandwidthConstraint(BandwidthResource.bps(bandwidthValue)));
final Bandwidth bandwidth = Bandwidth.bps(Double.parseDouble(bandwidthString));
constraints.add(new BandwidthConstraint(new BandwidthResource(bandwidth)));
}
// Check for a lambda specification
......
......@@ -33,7 +33,7 @@ public final class BandwidthResource extends LinkResource {
*
* @param bandwidth bandwidth value to be assigned
*/
private BandwidthResource(Bandwidth bandwidth) {
public BandwidthResource(Bandwidth bandwidth) {
this.bandwidth = checkNotNull(bandwidth);
}
......@@ -43,61 +43,6 @@ public final class BandwidthResource extends LinkResource {
}
/**
* Creates a new instance with given bandwidth in bps.
*
* @param bandwidth bandwidth value to be assigned
* @return {@link BandwidthResource} instance with given bandwidth
*/
@Deprecated
public static BandwidthResource valueOf(double bandwidth) {
return bps(bandwidth);
}
public static BandwidthResource from(Bandwidth bandwidth) {
return new BandwidthResource(bandwidth);
}
/**
* Creates a new instance with given bandwidth in bps.
*
* @param bps bandwidth value to be assigned
* @return {@link BandwidthResource} instance with given bandwidth
*/
public static BandwidthResource bps(double bps) {
return from(Bandwidth.bps(bps));
}
/**
* Creates a new instance with given bandwidth in Kbps.
*
* @param kbps bandwidth value to be assigned
* @return {@link BandwidthResource} instance with given bandwidth
*/
public static BandwidthResource kbps(double kbps) {
return from(Bandwidth.kbps(kbps));
}
/**
* Creates a new instance with given bandwidth in Mbps.
*
* @param mbps bandwidth value to be assigned
* @return {@link BandwidthResource} instance with given bandwidth
*/
public static BandwidthResource mbps(double mbps) {
return from(Bandwidth.mbps(mbps));
}
/**
* Creates a new instance with given bandwidth in Gbps.
*
* @param gbps bandwidth value to be assigned
* @return {@link BandwidthResource} instance with given bandwidth
*/
public static BandwidthResource gbps(double gbps) {
return from(Bandwidth.gbps(gbps));
}
/**
* Returns bandwidth as a double value.
*
* @return bandwidth as a double value
......
......@@ -20,6 +20,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.Objects;
import org.onlab.util.Bandwidth;
import org.onosproject.net.Link;
import org.onosproject.net.intent.Constraint;
import org.onosproject.net.intent.IntentId;
......@@ -137,7 +138,7 @@ public final class DefaultLinkResourceRequest implements LinkResourceRequest {
*/
@Override
public Builder addBandwidthRequest(double bandwidth) {
resources.add(new BandwidthResourceRequest(BandwidthResource.bps(bandwidth)));
resources.add(new BandwidthResourceRequest(new BandwidthResource(Bandwidth.bps(bandwidth))));
return this;
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.net.intent;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import org.onlab.util.Bandwidth;
import org.onosproject.core.DefaultGroupId;
import org.onosproject.core.GroupId;
import org.onosproject.net.DeviceId;
......@@ -302,7 +303,7 @@ public class IntentTestsMocks {
final List<ResourceRequest> result = new LinkedList<>();
if (availableBandwidth > 0.0) {
result.add(new BandwidthResourceRequest(
BandwidthResource.bps(availableBandwidth)));
new BandwidthResource(Bandwidth.bps(availableBandwidth))));
}
if (availableLambda > 0) {
result.add(new LambdaResourceRequest());
......
......@@ -16,6 +16,7 @@
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.BandwidthResource;
import org.onosproject.net.resource.LambdaResource;
......@@ -34,12 +35,16 @@ public class ConstraintObjectsTest {
// Bandwidth Constraint
private final Bandwidth bandwidth1 = Bandwidth.bps(100.0);
private final Bandwidth sameAsBandwidth1 = Bandwidth.bps(100.0);
private final Bandwidth bandwidth2 = Bandwidth.bps(200.0);
final BandwidthConstraint bandwidthConstraint1 =
new BandwidthConstraint(BandwidthResource.bps(100.0));
new BandwidthConstraint(new BandwidthResource(bandwidth1));
final BandwidthConstraint bandwidthConstraintSameAs1 =
new BandwidthConstraint(BandwidthResource.bps(100.0));
new BandwidthConstraint(new BandwidthResource(sameAsBandwidth1));
final BandwidthConstraint bandwidthConstraint2 =
new BandwidthConstraint(BandwidthResource.bps(200.0));
new BandwidthConstraint(new BandwidthResource(bandwidth2));
/**
* Checks that the objects were created properly.
......
......@@ -22,6 +22,7 @@ import org.junit.Test;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.MplsLabel;
import org.onlab.util.Bandwidth;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
......@@ -145,7 +146,7 @@ public class IntentCodecTest extends AbstractIntentTest {
final List<Constraint> constraints =
ImmutableList.of(
new BandwidthConstraint(BandwidthResource.bps(1.0)),
new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
new LambdaConstraint(LambdaResource.valueOf(3)),
new AnnotationConstraint("key", 33.0),
new AsymmetricPathConstraint(),
......
......@@ -17,6 +17,7 @@ package org.onosproject.net.intent.impl.compiler;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.onlab.util.Bandwidth;
import org.onosproject.TestApplicationId;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
......@@ -227,7 +228,8 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest {
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
final List<Constraint> constraints = Arrays.asList(new BandwidthConstraint(BandwidthResource.bps(100.0)));
final List<Constraint> constraints = Arrays.asList(
new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
......@@ -248,7 +250,8 @@ public class PointToPointIntentCompilerTest extends AbstractIntentTest {
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
final List<Constraint> constraints = Arrays.asList(new BandwidthConstraint(BandwidthResource.bps(100.0)));
final List<Constraint> constraints = Arrays.asList(
new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
try {
final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
......
......@@ -16,6 +16,7 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Deactivate;
import org.onlab.util.Bandwidth;
import org.slf4j.Logger;
import org.onlab.util.KryoNamespace;
import org.onlab.util.PositionalParameterStringFormatter;
......@@ -67,8 +68,8 @@ public class ConsistentLinkResourceStore extends
private final Logger log = getLogger(getClass());
private static final BandwidthResource DEFAULT_BANDWIDTH = BandwidthResource.mbps(1_000);
private static final BandwidthResource EMPTY_BW = BandwidthResource.bps(0);
private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0));
// Smallest non-reserved MPLS label
private static final int MIN_UNRESERVED_LABEL = 0x10;
......@@ -154,7 +155,7 @@ public class ConsistentLinkResourceStore extends
String strBw = link.annotations().value(BANDWIDTH);
if (strBw != null) {
try {
bandwidth = BandwidthResource.mbps(Double.parseDouble(strBw));
bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw)));
} catch (NumberFormatException e) {
// do nothings
bandwidth = null;
......@@ -237,7 +238,8 @@ public class ConsistentLinkResourceStore extends
}
}
free.put(type, Sets.newHashSet(new BandwidthResourceAllocation(BandwidthResource.bps(freeBw))));
free.put(type, Sets.newHashSet(
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw)))));
break;
case LAMBDA:
Set<? extends ResourceAllocation> lmd = caps.get(type);
......
......@@ -30,6 +30,7 @@ import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.Bandwidth;
import org.onlab.util.PositionalParameterStringFormatter;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.Link;
......@@ -80,9 +81,9 @@ public class HazelcastLinkResourceStore
private final Logger log = getLogger(getClass());
private static final BandwidthResource DEFAULT_BANDWIDTH = BandwidthResource.mbps(1_000);
private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
private static final BandwidthResource EMPTY_BW = BandwidthResource.bps(0);
private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0));
// table to store current allocations
/** LinkKey -> List<LinkResourceAllocations>. */
......@@ -174,7 +175,7 @@ public class HazelcastLinkResourceStore
String strBw = link.annotations().value(bandwidthAnnotation);
if (strBw != null) {
try {
bandwidth = BandwidthResource.mbps(Double.parseDouble(strBw));
bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw)));
} catch (NumberFormatException e) {
// do nothings
bandwidth = null;
......@@ -262,7 +263,8 @@ public class HazelcastLinkResourceStore
}
}
free.put(type, Sets.newHashSet(new BandwidthResourceAllocation(BandwidthResource.bps(freeBw))));
free.put(type, Sets.newHashSet(
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw)))));
break;
}
......
......@@ -21,6 +21,7 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.util.Bandwidth;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.Annotations;
import org.onosproject.net.ConnectPoint;
......@@ -172,7 +173,7 @@ public class HazelcastLinkResourceStoreTest {
final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
assertNotNull(alloc);
assertEquals(BandwidthResource.mbps(1000.0), alloc.bandwidth());
assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
}
/**
......@@ -209,7 +210,7 @@ public class HazelcastLinkResourceStoreTest {
ImmutableSet.of(link))
.build();
final ResourceAllocation allocation =
new BandwidthResourceAllocation(BandwidthResource.mbps(900.0));
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0)));
final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
final LinkResourceAllocations allocations =
......@@ -230,7 +231,7 @@ public class HazelcastLinkResourceStoreTest {
ImmutableSet.of(link))
.build();
final ResourceAllocation allocation =
new BandwidthResourceAllocation(BandwidthResource.mbps(9000.0));
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(9000.0)));
final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
final LinkResourceAllocations allocations =
......@@ -258,7 +259,7 @@ public class HazelcastLinkResourceStoreTest {
ImmutableSet.of(link))
.build();
final ResourceAllocation allocation =
new BandwidthResourceAllocation(BandwidthResource.mbps(900.0));
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0)));
final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
final LinkResourceAllocations allocations =
......
......@@ -355,7 +355,7 @@ public class KryoSerializerTest {
.build();
Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
ImmutableSet.of(new BandwidthResourceAllocation(BandwidthResource.bps(10.0)),
ImmutableSet.of(new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(10.0))),
new LambdaResourceAllocation(LambdaResource.valueOf(1))));
testSerializable(new DefaultLinkResourceAllocations(request, allocations));
}
......@@ -378,7 +378,7 @@ public class KryoSerializerTest {
@Test
public void testBandwidthConstraint() {
testSerializable(new BandwidthConstraint(BandwidthResource.bps(1000.0)));
testSerializable(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1000.0))));
}
@Test
......
......@@ -26,6 +26,7 @@ import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.Bandwidth;
import org.onlab.util.PositionalParameterStringFormatter;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.Annotations;
......@@ -55,7 +56,7 @@ import static org.slf4j.LoggerFactory.getLogger;
@Component(immediate = true)
@Service
public class SimpleLinkResourceStore implements LinkResourceStore {
private static final BandwidthResource DEFAULT_BANDWIDTH = BandwidthResource.mbps(1_000);
private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
private final Logger log = getLogger(getClass());
private Map<IntentId, LinkResourceAllocations> linkResourceAllocationsMap;
......@@ -98,7 +99,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
BandwidthResource bandwidth = DEFAULT_BANDWIDTH;
try {
bandwidth = BandwidthResource.mbps((Double.parseDouble(annotations.value(AnnotationKeys.BANDWIDTH))));
bandwidth = new BandwidthResource(
Bandwidth.mbps((Double.parseDouble(annotations.value(AnnotationKeys.BANDWIDTH)))));
} catch (NumberFormatException e) {
log.debug("No bandwidth annotation on link %s", link);
}
......@@ -123,7 +125,7 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
return (BandwidthResourceAllocation) res;
}
}
return new BandwidthResourceAllocation(BandwidthResource.bps(0));
return new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(0)));
}
/**
......@@ -156,7 +158,7 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
}
freeRes.remove(ba);
freeRes.add(new BandwidthResourceAllocation(
BandwidthResource.bps(newBandwidth)));
new BandwidthResource(Bandwidth.bps(newBandwidth))));
break;
case LAMBDA:
final boolean lambdaAvailable = freeRes.remove(res);
......@@ -198,7 +200,7 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
double newBandwidth = ba.bandwidth().toDouble() + requestedBandwidth;
freeRes.remove(ba);
freeRes.add(new BandwidthResourceAllocation(
BandwidthResource.bps(newBandwidth)));
new BandwidthResource(Bandwidth.bps(newBandwidth))));
break;
case LAMBDA:
checkState(freeRes.add(res));
......
......@@ -22,6 +22,7 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.util.Bandwidth;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.Annotations;
import org.onosproject.net.ConnectPoint;
......@@ -159,7 +160,7 @@ public class SimpleLinkResourceStoreTest {
final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
assertNotNull(alloc);
assertEquals(BandwidthResource.mbps(1000.0), alloc.bandwidth());
assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
}
/**
......@@ -184,7 +185,7 @@ public class SimpleLinkResourceStoreTest {
@Override
public Set<ResourceAllocation> getResourceAllocation(Link link) {
final ResourceAllocation allocation =
new BandwidthResourceAllocation(BandwidthResource.bps(allocationAmount));
new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(allocationAmount)));
final Set<ResourceAllocation> allocations = new HashSet<>();
allocations.add(allocation);
return allocations;
......