Remove dependency on LinkResourceService from Constraint

Change-Id: Ib9c488331b22eef6769a767c6186ef7d2e8b1501
diff --git a/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java b/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
index 5cc1b4f..f92be7b 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
@@ -16,7 +16,6 @@
 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;
@@ -36,20 +35,13 @@
 import org.onosproject.net.flow.instructions.Instruction;
 import org.onosproject.net.flow.instructions.Instructions;
 import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction;
-import org.onosproject.net.resource.ResourceAllocation;
-import org.onosproject.net.resource.ResourceRequest;
-import org.onosproject.net.resource.ResourceType;
-import org.onosproject.net.resource.link.BandwidthResource;
-import org.onosproject.net.resource.link.BandwidthResourceRequest;
-import org.onosproject.net.resource.link.LambdaResource;
-import org.onosproject.net.resource.link.LambdaResourceAllocation;
-import org.onosproject.net.resource.link.LambdaResourceRequest;
-import org.onosproject.net.resource.link.LinkResourceAllocations;
-import org.onosproject.net.resource.link.LinkResourceListener;
-import org.onosproject.net.resource.link.LinkResourceRequest;
-import org.onosproject.net.resource.link.LinkResourceService;
-import org.onosproject.net.resource.link.MplsLabel;
-import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
+import org.onosproject.net.newresource.DiscreteResourceId;
+import org.onosproject.net.newresource.Resource;
+import org.onosproject.net.newresource.ResourceAllocation;
+import org.onosproject.net.newresource.ResourceConsumer;
+import org.onosproject.net.newresource.ResourceId;
+import org.onosproject.net.newresource.ResourceListener;
+import org.onosproject.net.newresource.ResourceService;
 import org.onosproject.net.topology.DefaultTopologyEdge;
 import org.onosproject.net.topology.DefaultTopologyVertex;
 import org.onosproject.net.topology.LinkWeight;
@@ -61,9 +53,9 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -184,145 +176,84 @@
         }
     }
 
-    public static class MockLinkResourceAllocations implements LinkResourceAllocations {
-        @Override
-        public Set<ResourceAllocation> getResourceAllocation(Link link) {
-            return ImmutableSet.of(
-                    new LambdaResourceAllocation(LambdaResource.valueOf(77)),
-                    new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
+    public static final class MockResourceService implements ResourceService {
+
+        private final double bandwidth;
+
+        public static ResourceService makeBandwidthResourceService(double bandwidth) {
+            return new MockResourceService(bandwidth);
+        }
+
+        private MockResourceService(double bandwidth) {
+            this.bandwidth = bandwidth;
         }
 
         @Override
-        public IntentId intentId() {
+        public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) {
             return null;
         }
 
         @Override
-        public Collection<Link> links() {
+        public boolean release(List<ResourceAllocation> allocations) {
+            return false;
+        }
+
+        @Override
+        public boolean release(ResourceConsumer consumer) {
+            return false;
+        }
+
+        @Override
+        public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
             return null;
         }
 
         @Override
-        public Set<ResourceRequest> resources() {
+        public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
             return null;
         }
 
         @Override
-        public ResourceType type() {
+        public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
             return null;
         }
-    }
-
-    public static class MockedAllocationFailure extends RuntimeException { }
-
-    public static class MockResourceService implements LinkResourceService {
-
-        double availableBandwidth = -1.0;
-        int availableLambda = -1;
-
-        /**
-         * Allocates a resource service that will allow bandwidth allocations
-         * up to a limit.
-         *
-         * @param bandwidth available bandwidth limit
-         * @return resource manager for bandwidth requests
-         */
-        public static MockResourceService makeBandwidthResourceService(double bandwidth) {
-            final MockResourceService result = new MockResourceService();
-            result.availableBandwidth = bandwidth;
-            return result;
-        }
-
-        /**
-         * Allocates a resource service that will allow lambda allocations.
-         *
-         * @param lambda Lambda to return for allocation requests. Currently unused
-         * @return resource manager for lambda requests
-         */
-        public static MockResourceService makeLambdaResourceService(int lambda) {
-            final MockResourceService result = new MockResourceService();
-            result.availableLambda = lambda;
-            return result;
-        }
 
         @Override
-        public LinkResourceAllocations requestResources(LinkResourceRequest req) {
-            int lambda = -1;
-            double bandwidth = -1.0;
+        public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
+            return null;
+        }
 
-            for (ResourceRequest resourceRequest : req.resources()) {
-                if (resourceRequest.type() == ResourceType.BANDWIDTH) {
-                    final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
-                    bandwidth = brr.bandwidth().toDouble();
-                } else if (resourceRequest.type() == ResourceType.LAMBDA) {
-                    lambda = 1;
-                }
+        @Override
+        public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
+            return null;
+        }
+
+        @Override
+        public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
+            return null;
+        }
+
+        @Override
+        public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
+            return null;
+        }
+
+        @Override
+        public boolean isAvailable(Resource resource) {
+            if (!resource.isTypeOf(Bandwidth.class)) {
+                return false;
             }
 
-            if (availableBandwidth < bandwidth) {
-                throw new MockedAllocationFailure();
-            }
-            if (lambda > 0 && availableLambda == 0) {
-                throw new MockedAllocationFailure();
-            }
-
-            return new IntentTestsMocks.MockLinkResourceAllocations();
+            Optional<Double> value = resource.valueAs(Double.class);
+            return value.filter(requested -> requested <= bandwidth).isPresent();
         }
 
         @Override
-        public void releaseResources(LinkResourceAllocations allocations) {
-            // Mock
+        public void addListener(ResourceListener listener) {
         }
 
         @Override
-        public LinkResourceAllocations updateResources(LinkResourceRequest req,
-                                                       LinkResourceAllocations oldAllocations) {
-            return null;
-        }
-
-        @Override
-        public Iterable<LinkResourceAllocations> getAllocations() {
-            return ImmutableSet.of(
-                    new IntentTestsMocks.MockLinkResourceAllocations());
-        }
-
-        @Override
-        public Iterable<LinkResourceAllocations> getAllocations(Link link) {
-            return ImmutableSet.of(
-                    new IntentTestsMocks.MockLinkResourceAllocations());
-        }
-
-        @Override
-        public LinkResourceAllocations getAllocations(IntentId intentId) {
-            return new IntentTestsMocks.MockLinkResourceAllocations();
-        }
-
-        @Override
-        public Iterable<ResourceRequest> getAvailableResources(Link link) {
-            final List<ResourceRequest> result = new LinkedList<>();
-            if (availableBandwidth > 0.0) {
-                result.add(new BandwidthResourceRequest(
-                        new BandwidthResource(Bandwidth.bps(availableBandwidth))));
-            }
-            if (availableLambda > 0) {
-                result.add(new LambdaResourceRequest());
-            }
-            return result;
-        }
-
-        @Override
-        public Iterable<ResourceRequest> getAvailableResources(Link link, LinkResourceAllocations allocations) {
-            return null;
-        }
-
-        @Override
-        public void addListener(LinkResourceListener listener) {
-
-        }
-
-        @Override
-        public void removeListener(LinkResourceListener listener) {
-
+        public void removeListener(ResourceListener listener) {
         }
     }
 
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
index 299eaef..480d63c 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
@@ -23,8 +23,8 @@
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
 import org.onosproject.net.PortNumber;
+import org.onosproject.net.intent.ResourceContext;
 import org.onosproject.net.provider.ProviderId;
-import org.onosproject.net.resource.link.LinkResourceService;
 
 import static org.easymock.EasyMock.createMock;
 import static org.hamcrest.Matchers.closeTo;
@@ -51,11 +51,11 @@
 
     private AnnotationConstraint sut;
     private Link link;
-    private LinkResourceService linkResourceService;
+    private ResourceContext resourceContext;
 
     @Before
     public void setUp() {
-        linkResourceService = createMock(LinkResourceService.class);
+        resourceContext = createMock(ResourceContext.class);
 
         DefaultAnnotations annotations = DefaultAnnotations.builder().set(KEY, String.valueOf(VALUE)).build();
 
@@ -75,8 +75,8 @@
         double value = 120;
         sut = new AnnotationConstraint(KEY, value);
 
-        assertThat(sut.isValid(link, linkResourceService), is(true));
-        assertThat(sut.cost(link, linkResourceService), is(closeTo(VALUE, 1.0e-6)));
+        assertThat(sut.isValid(link, resourceContext), is(true));
+        assertThat(sut.cost(link, resourceContext), is(closeTo(VALUE, 1.0e-6)));
     }
 
     /**
@@ -87,8 +87,8 @@
         double value = 80;
         sut = new AnnotationConstraint(KEY, value);
 
-        assertThat(sut.isValid(link, linkResourceService), is(false));
-        assertThat(sut.cost(link, linkResourceService), is(lessThan(0.0)));
+        assertThat(sut.isValid(link, resourceContext), is(false));
+        assertThat(sut.cost(link, resourceContext), is(lessThan(0.0)));
     }
 
     @Test
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
index a08caab..ca45d8c 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
@@ -26,8 +26,8 @@
 import org.onosproject.net.Link;
 import org.onosproject.net.Path;
 import org.onosproject.net.PortNumber;
+import org.onosproject.net.intent.ResourceContext;
 import org.onosproject.net.provider.ProviderId;
-import org.onosproject.net.resource.link.LinkResourceService;
 
 import java.time.Duration;
 import java.time.temporal.ChronoUnit;
@@ -56,7 +56,7 @@
     private static final String LATENCY2 = "4.0";
 
     private LatencyConstraint sut;
-    private LinkResourceService linkResourceService;
+    private ResourceContext resourceContext;
 
     private Path path;
     private Link link1;
@@ -64,7 +64,7 @@
 
     @Before
     public void setUp() {
-        linkResourceService = createMock(LinkResourceService.class);
+        resourceContext = createMock(ResourceContext.class);
 
         Annotations annotations1 = DefaultAnnotations.builder().set(LATENCY, LATENCY1).build();
         Annotations annotations2 = DefaultAnnotations.builder().set(LATENCY, LATENCY2).build();
@@ -93,7 +93,7 @@
     public void testLessThanLatency() {
         sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS));
 
-        assertThat(sut.validate(path, linkResourceService), is(true));
+        assertThat(sut.validate(path, resourceContext), is(true));
     }
 
     /**
@@ -103,7 +103,7 @@
     public void testMoreThanLatency() {
         sut = new LatencyConstraint(Duration.of(3, ChronoUnit.MICROS));
 
-        assertThat(sut.validate(path, linkResourceService), is(false));
+        assertThat(sut.validate(path, resourceContext), is(false));
     }
 
     /**
@@ -113,8 +113,8 @@
     public void testCost() {
         sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS));
 
-        assertThat(sut.cost(link1, linkResourceService), is(closeTo(Double.parseDouble(LATENCY1), 1.0e-6)));
-        assertThat(sut.cost(link2, linkResourceService), is(closeTo(Double.parseDouble(LATENCY2), 1.0e-6)));
+        assertThat(sut.cost(link1, resourceContext), is(closeTo(Double.parseDouble(LATENCY1), 1.0e-6)));
+        assertThat(sut.cost(link2, resourceContext), is(closeTo(Double.parseDouble(LATENCY2), 1.0e-6)));
     }
 
     /**
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
index 99cd338..c9b301a 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
@@ -26,8 +26,8 @@
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Path;
 import org.onosproject.net.PortNumber;
+import org.onosproject.net.intent.ResourceContext;
 import org.onosproject.net.provider.ProviderId;
-import org.onosproject.net.resource.link.LinkResourceService;
 
 import java.util.Arrays;
 
@@ -50,7 +50,7 @@
     private static final PortNumber PN4 = PortNumber.portNumber(4);
     private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
 
-    private LinkResourceService linkResourceService;
+    private ResourceContext resourceContext;
 
     private Path path;
     private DefaultLink link2;
@@ -60,7 +60,7 @@
 
     @Before
     public void setUp() {
-        linkResourceService = createMock(LinkResourceService.class);
+        resourceContext = createMock(ResourceContext.class);
 
         link1 = DefaultLink.builder()
                 .providerId(PROVIDER_ID)
@@ -97,7 +97,7 @@
     public void testPathNotThroughObstacles() {
         sut = new ObstacleConstraint(DID4);
 
-        assertThat(sut.validate(path, linkResourceService), is(true));
+        assertThat(sut.validate(path, resourceContext), is(true));
     }
 
     /**
@@ -107,6 +107,6 @@
     public void testPathThroughObstacle() {
         sut = new ObstacleConstraint(DID1);
 
-        assertThat(sut.validate(path, linkResourceService), is(false));
+        assertThat(sut.validate(path, resourceContext), is(false));
     }
 }
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java
index bbd10a6..668614a 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java
@@ -24,8 +24,8 @@
 import org.onosproject.net.Path;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.intent.Constraint;
+import org.onosproject.net.intent.ResourceContext;
 import org.onosproject.net.provider.ProviderId;
-import org.onosproject.net.resource.link.LinkResourceService;
 
 import java.util.Arrays;
 
@@ -52,7 +52,7 @@
     private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
 
     private WaypointConstraint sut;
-    private LinkResourceService linkResourceService;
+    private ResourceContext resourceContext;
 
     private Path path;
     private DefaultLink link2;
@@ -60,7 +60,7 @@
 
     @Before
     public void setUp() {
-        linkResourceService = createMock(LinkResourceService.class);
+        resourceContext = createMock(ResourceContext.class);
 
         link1 = DefaultLink.builder()
                 .providerId(PROVIDER_ID)
@@ -85,7 +85,7 @@
     public void testSatisfyWaypoints() {
         sut = new WaypointConstraint(DID1, DID2, DID3);
 
-        assertThat(sut.validate(path, linkResourceService), is(true));
+        assertThat(sut.validate(path, resourceContext), is(true));
     }
 
     /**
@@ -95,7 +95,7 @@
     public void testNotSatisfyWaypoint() {
         sut = new WaypointConstraint(DID4);
 
-        assertThat(sut.validate(path, linkResourceService), is(false));
+        assertThat(sut.validate(path, resourceContext), is(false));
     }
 
     @Test