helenyrwu
Committed by Gerrit Code Review

Filters intent list before installation

Change-Id: I6f73ff65ab7e31fac8e9a6f28f20f0119e24793d
...@@ -36,6 +36,7 @@ import org.slf4j.Logger; ...@@ -36,6 +36,7 @@ import org.slf4j.Logger;
36 36
37 import java.util.ArrayList; 37 import java.util.ArrayList;
38 import java.util.Collection; 38 import java.util.Collection;
39 +import java.util.Iterator;
39 import java.util.List; 40 import java.util.List;
40 import java.util.Optional; 41 import java.util.Optional;
41 import java.util.Set; 42 import java.util.Set;
...@@ -200,8 +201,64 @@ class IntentInstaller { ...@@ -200,8 +201,64 @@ class IntentInstaller {
200 this.toInstall = toInstall; 201 this.toInstall = toInstall;
201 this.successConsumer = successConsumer; 202 this.successConsumer = successConsumer;
202 this.errorConsumer = errorConsumer; 203 this.errorConsumer = errorConsumer;
203 - prepareIntentData(toUninstall, Direction.REMOVE); 204 + prepareIntentData(toUninstall, toInstall);
204 - prepareIntentData(toInstall, Direction.ADD); 205 + }
206 +
207 + private void prepareIntentData(Optional<IntentData> uninstallData,
208 + Optional<IntentData> installData) {
209 + if (!installData.isPresent() && !uninstallData.isPresent()) {
210 + return;
211 + } else if (!installData.isPresent()) {
212 + prepareIntentData(uninstallData, Direction.REMOVE);
213 + } else if (!uninstallData.isPresent()) {
214 + prepareIntentData(installData, Direction.ADD);
215 + } else {
216 + IntentData uninstall = uninstallData.get();
217 + IntentData install = installData.get();
218 + List<Intent> uninstallIntents = new ArrayList<>();
219 + uninstallIntents.addAll(uninstall.installables());
220 + List<Intent> installIntents = new ArrayList<>();
221 + installIntents.addAll(install.installables());
222 +
223 + checkState(uninstallIntents.stream().allMatch(this::isSupported),
224 + "Unsupported installable intents detected");
225 + checkState(installIntents.stream().allMatch(this::isSupported),
226 + "Unsupported installable intents detected");
227 +
228 + //TODO: Filter FlowObjective intents
229 + // Filter out same intents and intents with same flow rules
230 + Iterator<Intent> iterator = installIntents.iterator();
231 + while (iterator.hasNext()) {
232 + Intent installIntent = iterator.next();
233 + uninstallIntents.stream().filter(uIntent -> {
234 + if (uIntent.equals(installIntent)) {
235 + return true;
236 + } else if (uIntent instanceof FlowRuleIntent && installIntent instanceof FlowRuleIntent) {
237 + return ((FlowRuleIntent) uIntent).flowRules()
238 + .containsAll(((FlowRuleIntent) installIntent).flowRules());
239 + } else {
240 + return false;
241 + }
242 + }).findFirst().ifPresent(common -> {
243 + uninstallIntents.remove(common);
244 + iterator.remove();
245 + });
246 + }
247 +
248 + final IntentData newUninstall = new IntentData(uninstall, uninstallIntents);
249 + final IntentData newInstall = new IntentData(install, installIntents);
250 +
251 + trackerService.removeTrackedResources(newUninstall.key(), newUninstall.intent().resources());
252 + uninstallIntents.forEach(installable ->
253 + trackerService.removeTrackedResources(newUninstall.intent().key(),
254 + installable.resources()));
255 + trackerService.addTrackedResources(newInstall.key(), newInstall.intent().resources());
256 + installIntents.forEach(installable ->
257 + trackerService.addTrackedResources(newInstall.key(),
258 + installable.resources()));
259 + prepareIntents(uninstallIntents, Direction.REMOVE);
260 + prepareIntents(installIntents, Direction.ADD);
261 + }
205 } 262 }
206 263
207 /** 264 /**
......