Sho SHIMIZU
Committed by Gerrit Code Review

Use Stream API instead of for loop

Change-Id: Ic5ce334a6d3891d2d43d1e5bb573e59efd0a9461
...@@ -17,6 +17,7 @@ package org.onosproject.net.intent.impl.compiler; ...@@ -17,6 +17,7 @@ package org.onosproject.net.intent.impl.compiler;
17 17
18 import com.google.common.collect.ImmutableList; 18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableSet; 19 import com.google.common.collect.ImmutableSet;
20 +import com.google.common.collect.Maps;
20 import com.google.common.collect.Sets; 21 import com.google.common.collect.Sets;
21 import org.apache.felix.scr.annotations.Activate; 22 import org.apache.felix.scr.annotations.Activate;
22 import org.apache.felix.scr.annotations.Component; 23 import org.apache.felix.scr.annotations.Component;
...@@ -56,6 +57,8 @@ import org.slf4j.LoggerFactory; ...@@ -56,6 +57,8 @@ import org.slf4j.LoggerFactory;
56 import java.util.ArrayList; 57 import java.util.ArrayList;
57 import java.util.Collections; 58 import java.util.Collections;
58 import java.util.List; 59 import java.util.List;
60 +import java.util.Map;
61 +import java.util.Optional;
59 import java.util.Set; 62 import java.util.Set;
60 import java.util.stream.Collectors; 63 import java.util.stream.Collectors;
61 import java.util.stream.Stream; 64 import java.util.stream.Stream;
...@@ -158,27 +161,25 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical ...@@ -158,27 +161,25 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical
158 return ImmutableList.of(createIntent(intent, firstPath, lambda)); 161 return ImmutableList.of(createIntent(intent, firstPath, lambda));
159 } 162 }
160 163
161 - // Use first path that can be successfully reserved 164 + // remaining cases
162 - for (Path path : paths) { 165 + // Use first path that the required resources are available
163 - // find common lambda on path 166 + Optional<Map.Entry<Path, List<OchSignal>>> found = paths.stream()
164 - // a list of OchSignal indicates consecutive OchSignals 167 + .map(path -> Maps.immutableEntry(path, findFirstAvailableOch(path)))
165 - List<OchSignal> lambda = findFirstAvailableOch(path); 168 + .filter(entry -> !entry.getValue().isEmpty())
166 - if (lambda.isEmpty()) { 169 + .filter(entry -> convertToResources(entry.getKey().links(),
167 - continue; 170 + entry.getValue()).stream().allMatch(resourceService::isAvailable))
168 - } 171 + .findFirst();
169 - List<Resource> lambdaResources = convertToResources(path.links(), lambda); 172 +
170 - if (!lambdaResources.stream().allMatch(resourceService::isAvailable)) { 173 + if (found.isPresent()) {
171 - continue; 174 + resources.addAll(convertToResources(found.get().getKey().links(), found.get().getValue()));
172 - }
173 - resources.addAll(lambdaResources);
174 175
175 allocateResources(intent, resources); 176 allocateResources(intent, resources);
176 177
177 - OchSignal ochSignal = OchSignal.toFixedGrid(lambda, ChannelSpacing.CHL_50GHZ); 178 + OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ);
178 - return ImmutableList.of(createIntent(intent, path, ochSignal)); 179 + return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal));
180 + } else {
181 + throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
179 } 182 }
180 -
181 - throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
182 } 183 }
183 184
184 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) { 185 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) {
......