Move MPLS label selection logic to MplsPathIntentCompiler

Change-Id: I2c64ca2a6ac363a0138e6940471893673ce3b83f
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
index 5fd1c85..449b898 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
@@ -47,6 +47,7 @@
 import org.onosproject.net.intent.IntentExtensionService;
 import org.onosproject.net.intent.MplsPathIntent;
 import org.onosproject.net.link.LinkStore;
+import org.onosproject.net.resource.ResourceRequest;
 import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
 import org.onosproject.net.resource.link.LinkResourceAllocations;
 import org.onosproject.net.resource.link.LinkResourceRequest;
@@ -55,13 +56,18 @@
 import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
 import org.onosproject.net.resource.ResourceAllocation;
 import org.onosproject.net.resource.ResourceType;
+import org.onosproject.net.resource.link.MplsLabelResourceRequest;
 import org.slf4j.Logger;
 
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import java.util.stream.StreamSupport;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.slf4j.LoggerFactory.getLogger;
@@ -117,11 +123,34 @@
             linkRequest.add(linkStore.getLink(link.dst(), link.src()));
         }
 
-        LinkResourceRequest.Builder request = DefaultLinkResourceRequest
-                .builder(intent.id(), linkRequest).addMplsRequest();
-        LinkResourceAllocations reqMpls = resourceService
-                .requestResources(request.build());
-        return reqMpls;
+        Map<Link, MplsLabel> labels = findMplsLabels(linkRequest);
+
+        LinkResourceRequest request = DefaultLinkResourceRequest
+                .builder(intent.id(), linkRequest)
+                .addMplsRequest(labels)
+                .build();
+        return resourceService.requestResources(request);
+    }
+
+    private Map<Link, MplsLabel> findMplsLabels(Set<Link> links) {
+        Map<Link, MplsLabel> labels = new HashMap<>();
+        for (Link link : links) {
+            Optional<MplsLabel> label = findMplsLabel(link);
+            if (label.isPresent()) {
+                labels.put(link, label.get());
+            }
+        }
+
+        return labels;
+    }
+
+    private Optional<MplsLabel> findMplsLabel(Link link) {
+        Iterable<ResourceRequest> freeLabels = resourceService.getAvailableResources(link);
+        return StreamSupport.stream(freeLabels.spliterator(), false)
+                .filter(x -> x instanceof MplsLabelResourceRequest)
+                .map(x -> (MplsLabelResourceRequest) x)
+                .map(MplsLabelResourceRequest::mplsLabel)
+                .findFirst();
     }
 
     private MplsLabel getMplsLabel(LinkResourceAllocations allocations, Link link) {
diff --git a/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java b/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java
index fe05379..7eb189e 100644
--- a/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java
@@ -45,7 +45,6 @@
 import org.onosproject.net.resource.link.MplsLabelResourceRequest;
 import org.slf4j.Logger;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -84,28 +83,6 @@
     }
 
 
-    /**
-     * Returns available MPLS label on specified link.
-     *
-     * @param link the link
-     * @return available MPLS labels on specified link
-     */
-    private Iterable<MplsLabel> getAvailableMplsLabels(Link link) {
-        Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
-        if (resAllocs == null) {
-            return Collections.emptySet();
-        }
-        Set<MplsLabel> mplsLabels = new HashSet<>();
-        for (ResourceAllocation res : resAllocs) {
-            if (res.type() == ResourceType.MPLS_LABEL) {
-
-                mplsLabels.add(((MplsLabelResourceAllocation) res).mplsLabel());
-            }
-        }
-
-        return mplsLabels;
-    }
-
     @Override
     public LinkResourceAllocations requestResources(LinkResourceRequest req) {
         checkPermission(LINK_WRITE);