Toshio Koide
Committed by Gerrit Code Review

Update SimpleLinkResourceStore to obtain capacities from link annotations

Change-Id: I98f8959fdc00953c98a151ad7b0bfa1041b118d7
......@@ -29,6 +29,8 @@ 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.onos.net.AnnotationKeys;
import org.onlab.onos.net.Annotations;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.intent.IntentId;
import org.onlab.onos.net.resource.Bandwidth;
......@@ -74,12 +76,26 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
* @return free resources
*/
private synchronized Set<ResourceAllocation> readOriginalFreeResources(Link link) {
// TODO read capacity and lambda resources from topology
Annotations annotations = link.annotations();
Set<ResourceAllocation> allocations = new HashSet<>();
for (int i = 1; i <= 100; i++) {
try {
int waves = Integer.parseInt(annotations.value(AnnotationKeys.OPTICAL_WAVES));
for (int i = 1; i <= waves; i++) {
allocations.add(new LambdaResourceAllocation(Lambda.valueOf(i)));
}
allocations.add(new BandwidthResourceAllocation(Bandwidth.valueOf(1000000)));
} catch (NumberFormatException e) {
log.debug("No optical.wave annotation on link %s", link);
}
try {
int bandwidth = Integer.parseInt(annotations.value(AnnotationKeys.BANDWIDTH));
allocations.add(
new BandwidthResourceAllocation(Bandwidth.valueOf(bandwidth)));
} catch (NumberFormatException e) {
log.debug("No bandwidth annotation on link %s", link);
}
return allocations;
}
......@@ -92,7 +108,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
* {@link BandwidthResourceAllocation} object with 0 bandwidth
*
*/
private synchronized BandwidthResourceAllocation getBandwidth(Set<ResourceAllocation> freeRes) {
private synchronized BandwidthResourceAllocation getBandwidth(
Set<ResourceAllocation> freeRes) {
for (ResourceAllocation res : freeRes) {
if (res.type() == ResourceType.BANDWIDTH) {
return (BandwidthResourceAllocation) res;
......@@ -107,7 +124,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
* @param link the target link
* @param allocations the resources to be subtracted
*/
private synchronized void subtractFreeResources(Link link, LinkResourceAllocations allocations) {
private synchronized void subtractFreeResources(Link link,
LinkResourceAllocations allocations) {
// TODO Use lock or version for updating freeResources.
checkNotNull(link);
Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
......@@ -141,7 +159,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
* @param link the target link
* @param allocations the resources to be added
*/
private synchronized void addFreeResources(Link link, LinkResourceAllocations allocations) {
private synchronized void addFreeResources(Link link,
LinkResourceAllocations allocations) {
// TODO Use lock or version for updating freeResources.
Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
Set<ResourceAllocation> addRes = allocations.getResourceAllocation(link);
......
......@@ -28,7 +28,10 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.onos.net.AnnotationKeys;
import org.onlab.onos.net.Annotations;
import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.DefaultAnnotations;
import org.onlab.onos.net.DefaultLink;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.provider.ProviderId;
......@@ -61,11 +64,15 @@ public class SimpleLinkResourceStoreTest {
* @return created {@link Link} object
*/
private Link newLink(String dev1, int port1, String dev2, int port2) {
Annotations annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.OPTICAL_WAVES, "80")
.set(AnnotationKeys.BANDWIDTH, "1000000")
.build();
return new DefaultLink(
new ProviderId("of", "foo"),
new ConnectPoint(deviceId(dev1), portNumber(port1)),
new ConnectPoint(deviceId(dev2), portNumber(port2)),
DIRECT);
DIRECT, annotations);
}
@Before
......@@ -158,6 +165,6 @@ public class SimpleLinkResourceStoreTest {
final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
assertNotNull(res);
assertEquals(100, res.size());
assertEquals(80, res.size());
}
}
......