Update SimpleLinkResourceStore to obtain capacities from link annotations

Change-Id: I98f8959fdc00953c98a151ad7b0bfa1041b118d7
diff --git a/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStore.java b/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStore.java
index 78cd341..52f60af 100644
--- a/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStore.java
+++ b/core/store/trivial/src/main/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStore.java
@@ -29,6 +29,8 @@
 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 @@
      * @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++) {
-            allocations.add(new LambdaResourceAllocation(Lambda.valueOf(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)));
+            }
+        } catch (NumberFormatException e) {
+            log.debug("No optical.wave annotation on link %s", link);
         }
-        allocations.add(new BandwidthResourceAllocation(Bandwidth.valueOf(1000000)));
+
+        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 @@
      *         {@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 @@
      * @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 @@
      * @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);
diff --git a/core/store/trivial/src/test/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStoreTest.java b/core/store/trivial/src/test/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStoreTest.java
index 1daad2f..f28bb63 100644
--- a/core/store/trivial/src/test/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStoreTest.java
+++ b/core/store/trivial/src/test/java/org/onlab/onos/store/trivial/impl/SimpleLinkResourceStoreTest.java
@@ -28,7 +28,10 @@
 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 @@
      * @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 @@
 
         final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
         assertNotNull(res);
-        assertEquals(100, res.size());
+        assertEquals(80, res.size());
     }
 }