Sho SHIMIZU
Committed by Gerrit Code Review

Move MPLS label selection logic to MplsPathIntentCompiler

Change-Id: I2c64ca2a6ac363a0138e6940471893673ce3b83f
...@@ -47,6 +47,7 @@ import org.onosproject.net.intent.IntentCompiler; ...@@ -47,6 +47,7 @@ import org.onosproject.net.intent.IntentCompiler;
47 import org.onosproject.net.intent.IntentExtensionService; 47 import org.onosproject.net.intent.IntentExtensionService;
48 import org.onosproject.net.intent.MplsPathIntent; 48 import org.onosproject.net.intent.MplsPathIntent;
49 import org.onosproject.net.link.LinkStore; 49 import org.onosproject.net.link.LinkStore;
50 +import org.onosproject.net.resource.ResourceRequest;
50 import org.onosproject.net.resource.link.DefaultLinkResourceRequest; 51 import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
51 import org.onosproject.net.resource.link.LinkResourceAllocations; 52 import org.onosproject.net.resource.link.LinkResourceAllocations;
52 import org.onosproject.net.resource.link.LinkResourceRequest; 53 import org.onosproject.net.resource.link.LinkResourceRequest;
...@@ -55,13 +56,18 @@ import org.onosproject.net.resource.link.MplsLabel; ...@@ -55,13 +56,18 @@ import org.onosproject.net.resource.link.MplsLabel;
55 import org.onosproject.net.resource.link.MplsLabelResourceAllocation; 56 import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
56 import org.onosproject.net.resource.ResourceAllocation; 57 import org.onosproject.net.resource.ResourceAllocation;
57 import org.onosproject.net.resource.ResourceType; 58 import org.onosproject.net.resource.ResourceType;
59 +import org.onosproject.net.resource.link.MplsLabelResourceRequest;
58 import org.slf4j.Logger; 60 import org.slf4j.Logger;
59 61
60 import java.util.Collections; 62 import java.util.Collections;
63 +import java.util.HashMap;
61 import java.util.Iterator; 64 import java.util.Iterator;
62 import java.util.LinkedList; 65 import java.util.LinkedList;
63 import java.util.List; 66 import java.util.List;
67 +import java.util.Map;
68 +import java.util.Optional;
64 import java.util.Set; 69 import java.util.Set;
70 +import java.util.stream.StreamSupport;
65 71
66 import static com.google.common.base.Preconditions.checkNotNull; 72 import static com.google.common.base.Preconditions.checkNotNull;
67 import static org.slf4j.LoggerFactory.getLogger; 73 import static org.slf4j.LoggerFactory.getLogger;
...@@ -117,11 +123,34 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> { ...@@ -117,11 +123,34 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> {
117 linkRequest.add(linkStore.getLink(link.dst(), link.src())); 123 linkRequest.add(linkStore.getLink(link.dst(), link.src()));
118 } 124 }
119 125
120 - LinkResourceRequest.Builder request = DefaultLinkResourceRequest 126 + Map<Link, MplsLabel> labels = findMplsLabels(linkRequest);
121 - .builder(intent.id(), linkRequest).addMplsRequest(); 127 +
122 - LinkResourceAllocations reqMpls = resourceService 128 + LinkResourceRequest request = DefaultLinkResourceRequest
123 - .requestResources(request.build()); 129 + .builder(intent.id(), linkRequest)
124 - return reqMpls; 130 + .addMplsRequest(labels)
131 + .build();
132 + return resourceService.requestResources(request);
133 + }
134 +
135 + private Map<Link, MplsLabel> findMplsLabels(Set<Link> links) {
136 + Map<Link, MplsLabel> labels = new HashMap<>();
137 + for (Link link : links) {
138 + Optional<MplsLabel> label = findMplsLabel(link);
139 + if (label.isPresent()) {
140 + labels.put(link, label.get());
141 + }
142 + }
143 +
144 + return labels;
145 + }
146 +
147 + private Optional<MplsLabel> findMplsLabel(Link link) {
148 + Iterable<ResourceRequest> freeLabels = resourceService.getAvailableResources(link);
149 + return StreamSupport.stream(freeLabels.spliterator(), false)
150 + .filter(x -> x instanceof MplsLabelResourceRequest)
151 + .map(x -> (MplsLabelResourceRequest) x)
152 + .map(MplsLabelResourceRequest::mplsLabel)
153 + .findFirst();
125 } 154 }
126 155
127 private MplsLabel getMplsLabel(LinkResourceAllocations allocations, Link link) { 156 private MplsLabel getMplsLabel(LinkResourceAllocations allocations, Link link) {
......
...@@ -45,7 +45,6 @@ import org.onosproject.net.resource.link.MplsLabelResourceAllocation; ...@@ -45,7 +45,6 @@ import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
45 import org.onosproject.net.resource.link.MplsLabelResourceRequest; 45 import org.onosproject.net.resource.link.MplsLabelResourceRequest;
46 import org.slf4j.Logger; 46 import org.slf4j.Logger;
47 47
48 -import java.util.Collections;
49 import java.util.HashMap; 48 import java.util.HashMap;
50 import java.util.HashSet; 49 import java.util.HashSet;
51 import java.util.Map; 50 import java.util.Map;
...@@ -84,28 +83,6 @@ public class LinkResourceManager ...@@ -84,28 +83,6 @@ public class LinkResourceManager
84 } 83 }
85 84
86 85
87 - /**
88 - * Returns available MPLS label on specified link.
89 - *
90 - * @param link the link
91 - * @return available MPLS labels on specified link
92 - */
93 - private Iterable<MplsLabel> getAvailableMplsLabels(Link link) {
94 - Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
95 - if (resAllocs == null) {
96 - return Collections.emptySet();
97 - }
98 - Set<MplsLabel> mplsLabels = new HashSet<>();
99 - for (ResourceAllocation res : resAllocs) {
100 - if (res.type() == ResourceType.MPLS_LABEL) {
101 -
102 - mplsLabels.add(((MplsLabelResourceAllocation) res).mplsLabel());
103 - }
104 - }
105 -
106 - return mplsLabels;
107 - }
108 -
109 @Override 86 @Override
110 public LinkResourceAllocations requestResources(LinkResourceRequest req) { 87 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
111 checkPermission(LINK_WRITE); 88 checkPermission(LINK_WRITE);
......