HIGUCHI Yuta
Committed by Gerrit Code Review

Add DataRateUnit to help specifying Bandwidth.

Change-Id: I2b83922d98cab5571408b920a89bb8b704934255
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
16 package org.onosproject.net.intent.constraint; 16 package org.onosproject.net.intent.constraint;
17 17
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 +
20 +import org.onlab.util.DataRateUnit;
19 import org.onosproject.net.Link; 21 import org.onosproject.net.Link;
20 import org.onosproject.net.resource.link.BandwidthResource; 22 import org.onosproject.net.resource.link.BandwidthResource;
21 import org.onosproject.net.resource.link.BandwidthResourceRequest; 23 import org.onosproject.net.resource.link.BandwidthResourceRequest;
...@@ -32,7 +34,7 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -32,7 +34,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
32 * Constraint that evaluates links based on available bandwidths. 34 * Constraint that evaluates links based on available bandwidths.
33 */ 35 */
34 @Beta 36 @Beta
35 -public class BandwidthConstraint extends BooleanConstraint { 37 +public final class BandwidthConstraint extends BooleanConstraint {
36 38
37 private final BandwidthResource bandwidth; 39 private final BandwidthResource bandwidth;
38 40
...@@ -45,6 +47,17 @@ public class BandwidthConstraint extends BooleanConstraint { ...@@ -45,6 +47,17 @@ public class BandwidthConstraint extends BooleanConstraint {
45 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null"); 47 this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
46 } 48 }
47 49
50 + /**
51 + * Creates a new bandwidth constraint.
52 + *
53 + * @param v required amount of bandwidth
54 + * @param unit {@link DataRateUnit} of {@code v}
55 + * @return {@link BandwidthConstraint} instance with given bandwidth requirement
56 + */
57 + public static BandwidthConstraint of(double v, DataRateUnit unit) {
58 + return new BandwidthConstraint(BandwidthResource.of(v, unit));
59 + }
60 +
48 // Constructor for serialization 61 // Constructor for serialization
49 private BandwidthConstraint() { 62 private BandwidthConstraint() {
50 this.bandwidth = null; 63 this.bandwidth = null;
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 package org.onosproject.net.resource.link; 16 package org.onosproject.net.resource.link;
17 17
18 import org.onlab.util.Bandwidth; 18 import org.onlab.util.Bandwidth;
19 - 19 +import org.onlab.util.DataRateUnit;
20 import java.util.Objects; 20 import java.util.Objects;
21 21
22 import static com.google.common.base.Preconditions.checkNotNull; 22 import static com.google.common.base.Preconditions.checkNotNull;
...@@ -43,6 +43,17 @@ public final class BandwidthResource implements LinkResource { ...@@ -43,6 +43,17 @@ public final class BandwidthResource implements LinkResource {
43 } 43 }
44 44
45 /** 45 /**
46 + * Creates a new bandwidth resource.
47 + *
48 + * @param v amount of bandwidth to request
49 + * @param unit {@link DataRateUnit} of {@code v}
50 + * @return {@link BandwidthResource} instance with given bandwidth
51 + */
52 + public static BandwidthResource of(double v, DataRateUnit unit) {
53 + return new BandwidthResource(Bandwidth.of(v, unit));
54 + }
55 +
56 + /**
46 * Returns bandwidth as a double value. 57 * Returns bandwidth as a double value.
47 * 58 *
48 * @return bandwidth as a double value 59 * @return bandwidth as a double value
......
...@@ -16,14 +16,12 @@ ...@@ -16,14 +16,12 @@
16 package org.onosproject.net.intent; 16 package org.onosproject.net.intent;
17 17
18 import org.junit.Test; 18 import org.junit.Test;
19 -import org.onlab.util.Bandwidth; 19 +import org.onlab.util.DataRateUnit;
20 import org.onosproject.TestApplicationId; 20 import org.onosproject.TestApplicationId;
21 import org.onosproject.core.ApplicationId; 21 import org.onosproject.core.ApplicationId;
22 import org.onosproject.net.HostId; 22 import org.onosproject.net.HostId;
23 import org.onosproject.net.flow.TrafficSelector; 23 import org.onosproject.net.flow.TrafficSelector;
24 import org.onosproject.net.intent.constraint.BandwidthConstraint; 24 import org.onosproject.net.intent.constraint.BandwidthConstraint;
25 -import org.onosproject.net.resource.link.BandwidthResource;
26 -
27 import com.google.common.collect.ImmutableList; 25 import com.google.common.collect.ImmutableList;
28 import com.google.common.testing.EqualsTester; 26 import com.google.common.testing.EqualsTester;
29 27
...@@ -109,8 +107,7 @@ public class HostToHostIntentTest extends IntentTest { ...@@ -109,8 +107,7 @@ public class HostToHostIntentTest extends IntentTest {
109 107
110 @Test 108 @Test
111 public void testImplicitConstraintsAreAdded() { 109 public void testImplicitConstraintsAreAdded() {
112 - final BandwidthConstraint other = new BandwidthConstraint( 110 + final Constraint other = BandwidthConstraint.of(1, DataRateUnit.GBPS);
113 - new BandwidthResource(Bandwidth.gbps(1)));
114 final HostToHostIntent intent = HostToHostIntent.builder() 111 final HostToHostIntent intent = HostToHostIntent.builder()
115 .appId(APPID) 112 .appId(APPID)
116 .one(id1) 113 .one(id1)
......
...@@ -42,6 +42,17 @@ public final class Bandwidth implements RichComparable<Bandwidth> { ...@@ -42,6 +42,17 @@ public final class Bandwidth implements RichComparable<Bandwidth> {
42 } 42 }
43 43
44 /** 44 /**
45 + * Creates a new instance with given bandwidth.
46 + *
47 + * @param v bandwidth value
48 + * @param unit {@link DataRateUnit} of {@code v}
49 + * @return {@link Bandwidth} instance with given bandwidth
50 + */
51 + public static Bandwidth of(double v, DataRateUnit unit) {
52 + return new Bandwidth(unit.toBitsPerSecond(v));
53 + }
54 +
55 + /**
45 * Creates a new instance with given bandwidth in bps. 56 * Creates a new instance with given bandwidth in bps.
46 * 57 *
47 * @param bps bandwidth value to be assigned 58 * @param bps bandwidth value to be assigned
......
1 +package org.onlab.util;
2 +
3 +import com.google.common.annotations.Beta;
4 +
5 +/**
6 + * Data rate unit.
7 + */
8 +@Beta
9 +public enum DataRateUnit {
10 + /**
11 + * Bit per second.
12 + */
13 + BPS(1L),
14 + /**
15 + * Kilobit per second.
16 + * (Decimal/SI)
17 + */
18 + KBPS(1_000L),
19 + /**
20 + * Megabit per second.
21 + * (Decimal/SI)
22 + */
23 + MBPS(1_000_000L),
24 + /**
25 + * Gigabit per second.
26 + * (Decimal/SI)
27 + */
28 + GBPS(1_000_000_000L);
29 +
30 + private final long multiplier;
31 +
32 + DataRateUnit(long multiplier) {
33 + this.multiplier = multiplier;
34 + }
35 +
36 + /**
37 + * Returns the multiplier to use, when converting value of this unit to bps.
38 + *
39 + * @return multiplier
40 + */
41 + public long multiplier() {
42 + return multiplier;
43 + }
44 +
45 + /**
46 + * Converts given value in this unit to bits per seconds.
47 + *
48 + * @param v data rate value
49 + * @return {@code v} in bits per seconds
50 + */
51 + public long toBitsPerSecond(long v) {
52 + return v * multiplier;
53 + }
54 +
55 + /**
56 + * Converts given value in this unit to bits per seconds.
57 + *
58 + * @param v data rate value
59 + * @return {@code v} in bits per seconds
60 + */
61 + public double toBitsPerSecond(double v) {
62 + return v * multiplier;
63 + }
64 +}