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; ...@@ -29,6 +29,8 @@ import org.apache.felix.scr.annotations.Activate;
29 import org.apache.felix.scr.annotations.Component; 29 import org.apache.felix.scr.annotations.Component;
30 import org.apache.felix.scr.annotations.Deactivate; 30 import org.apache.felix.scr.annotations.Deactivate;
31 import org.apache.felix.scr.annotations.Service; 31 import org.apache.felix.scr.annotations.Service;
32 +import org.onlab.onos.net.AnnotationKeys;
33 +import org.onlab.onos.net.Annotations;
32 import org.onlab.onos.net.Link; 34 import org.onlab.onos.net.Link;
33 import org.onlab.onos.net.intent.IntentId; 35 import org.onlab.onos.net.intent.IntentId;
34 import org.onlab.onos.net.resource.Bandwidth; 36 import org.onlab.onos.net.resource.Bandwidth;
...@@ -74,12 +76,26 @@ public class SimpleLinkResourceStore implements LinkResourceStore { ...@@ -74,12 +76,26 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
74 * @return free resources 76 * @return free resources
75 */ 77 */
76 private synchronized Set<ResourceAllocation> readOriginalFreeResources(Link link) { 78 private synchronized Set<ResourceAllocation> readOriginalFreeResources(Link link) {
77 - // TODO read capacity and lambda resources from topology 79 + Annotations annotations = link.annotations();
78 Set<ResourceAllocation> allocations = new HashSet<>(); 80 Set<ResourceAllocation> allocations = new HashSet<>();
79 - for (int i = 1; i <= 100; i++) { 81 +
80 - allocations.add(new LambdaResourceAllocation(Lambda.valueOf(i))); 82 + try {
83 + int waves = Integer.parseInt(annotations.value(AnnotationKeys.OPTICAL_WAVES));
84 + for (int i = 1; i <= waves; i++) {
85 + allocations.add(new LambdaResourceAllocation(Lambda.valueOf(i)));
86 + }
87 + } catch (NumberFormatException e) {
88 + log.debug("No optical.wave annotation on link %s", link);
81 } 89 }
82 - allocations.add(new BandwidthResourceAllocation(Bandwidth.valueOf(1000000))); 90 +
91 + try {
92 + int bandwidth = Integer.parseInt(annotations.value(AnnotationKeys.BANDWIDTH));
93 + allocations.add(
94 + new BandwidthResourceAllocation(Bandwidth.valueOf(bandwidth)));
95 + } catch (NumberFormatException e) {
96 + log.debug("No bandwidth annotation on link %s", link);
97 + }
98 +
83 return allocations; 99 return allocations;
84 } 100 }
85 101
...@@ -92,7 +108,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore { ...@@ -92,7 +108,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
92 * {@link BandwidthResourceAllocation} object with 0 bandwidth 108 * {@link BandwidthResourceAllocation} object with 0 bandwidth
93 * 109 *
94 */ 110 */
95 - private synchronized BandwidthResourceAllocation getBandwidth(Set<ResourceAllocation> freeRes) { 111 + private synchronized BandwidthResourceAllocation getBandwidth(
112 + Set<ResourceAllocation> freeRes) {
96 for (ResourceAllocation res : freeRes) { 113 for (ResourceAllocation res : freeRes) {
97 if (res.type() == ResourceType.BANDWIDTH) { 114 if (res.type() == ResourceType.BANDWIDTH) {
98 return (BandwidthResourceAllocation) res; 115 return (BandwidthResourceAllocation) res;
...@@ -107,7 +124,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore { ...@@ -107,7 +124,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
107 * @param link the target link 124 * @param link the target link
108 * @param allocations the resources to be subtracted 125 * @param allocations the resources to be subtracted
109 */ 126 */
110 - private synchronized void subtractFreeResources(Link link, LinkResourceAllocations allocations) { 127 + private synchronized void subtractFreeResources(Link link,
128 + LinkResourceAllocations allocations) {
111 // TODO Use lock or version for updating freeResources. 129 // TODO Use lock or version for updating freeResources.
112 checkNotNull(link); 130 checkNotNull(link);
113 Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link)); 131 Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
...@@ -141,7 +159,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore { ...@@ -141,7 +159,8 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
141 * @param link the target link 159 * @param link the target link
142 * @param allocations the resources to be added 160 * @param allocations the resources to be added
143 */ 161 */
144 - private synchronized void addFreeResources(Link link, LinkResourceAllocations allocations) { 162 + private synchronized void addFreeResources(Link link,
163 + LinkResourceAllocations allocations) {
145 // TODO Use lock or version for updating freeResources. 164 // TODO Use lock or version for updating freeResources.
146 Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link)); 165 Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link));
147 Set<ResourceAllocation> addRes = allocations.getResourceAllocation(link); 166 Set<ResourceAllocation> addRes = allocations.getResourceAllocation(link);
......
...@@ -28,7 +28,10 @@ import java.util.Set; ...@@ -28,7 +28,10 @@ import java.util.Set;
28 import org.junit.After; 28 import org.junit.After;
29 import org.junit.Before; 29 import org.junit.Before;
30 import org.junit.Test; 30 import org.junit.Test;
31 +import org.onlab.onos.net.AnnotationKeys;
32 +import org.onlab.onos.net.Annotations;
31 import org.onlab.onos.net.ConnectPoint; 33 import org.onlab.onos.net.ConnectPoint;
34 +import org.onlab.onos.net.DefaultAnnotations;
32 import org.onlab.onos.net.DefaultLink; 35 import org.onlab.onos.net.DefaultLink;
33 import org.onlab.onos.net.Link; 36 import org.onlab.onos.net.Link;
34 import org.onlab.onos.net.provider.ProviderId; 37 import org.onlab.onos.net.provider.ProviderId;
...@@ -61,11 +64,15 @@ public class SimpleLinkResourceStoreTest { ...@@ -61,11 +64,15 @@ public class SimpleLinkResourceStoreTest {
61 * @return created {@link Link} object 64 * @return created {@link Link} object
62 */ 65 */
63 private Link newLink(String dev1, int port1, String dev2, int port2) { 66 private Link newLink(String dev1, int port1, String dev2, int port2) {
67 + Annotations annotations = DefaultAnnotations.builder()
68 + .set(AnnotationKeys.OPTICAL_WAVES, "80")
69 + .set(AnnotationKeys.BANDWIDTH, "1000000")
70 + .build();
64 return new DefaultLink( 71 return new DefaultLink(
65 new ProviderId("of", "foo"), 72 new ProviderId("of", "foo"),
66 new ConnectPoint(deviceId(dev1), portNumber(port1)), 73 new ConnectPoint(deviceId(dev1), portNumber(port1)),
67 new ConnectPoint(deviceId(dev2), portNumber(port2)), 74 new ConnectPoint(deviceId(dev2), portNumber(port2)),
68 - DIRECT); 75 + DIRECT, annotations);
69 } 76 }
70 77
71 @Before 78 @Before
...@@ -158,6 +165,6 @@ public class SimpleLinkResourceStoreTest { ...@@ -158,6 +165,6 @@ public class SimpleLinkResourceStoreTest {
158 165
159 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes); 166 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
160 assertNotNull(res); 167 assertNotNull(res);
161 - assertEquals(100, res.size()); 168 + assertEquals(80, res.size());
162 } 169 }
163 } 170 }
......