Luca Prete

ONOS-4403 Remove intents using appId when apps get deactivated.

Change-Id: Ieaea8aeff2f2c29287a3e2eea33654fa22d38c09
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 16
17 package org.onosproject.routing; 17 package org.onosproject.routing;
18 18
19 +import org.onosproject.core.ApplicationId;
19 import org.onosproject.net.intent.Intent; 20 import org.onosproject.net.intent.Intent;
20 21
21 /** 22 /**
...@@ -48,4 +49,12 @@ public interface IntentSynchronizationService { ...@@ -48,4 +49,12 @@ public interface IntentSynchronizationService {
48 * @param intent intent to withdraw 49 * @param intent intent to withdraw
49 */ 50 */
50 void withdraw(Intent intent); 51 void withdraw(Intent intent);
52 +
53 + /**
54 + * Withdraws intents by app Id.
55 + *
56 + * @param applicationId the Id of the application that created the intents
57 + * to be removed
58 + */
59 + void removeIntentsByAppId(ApplicationId applicationId);
51 } 60 }
......
...@@ -143,6 +143,28 @@ public class IntentSynchronizer implements IntentSynchronizationService, ...@@ -143,6 +143,28 @@ public class IntentSynchronizer implements IntentSynchronizationService,
143 } 143 }
144 144
145 @Override 145 @Override
146 + public void removeIntentsByAppId(ApplicationId appId) {
147 + if (!isElectedLeader) {
148 + // Only leader will withdraw intents
149 + return;
150 + }
151 +
152 + log.debug("Withdrawing intents for app {}...",
153 + appId);
154 +
155 + intents.entrySet()
156 + .stream()
157 + .filter(intent -> intent.getValue().appId().equals(appId))
158 + .forEach(intent -> {
159 + log.debug("Intent Synchronizer withdrawing intent: {}",
160 + intent);
161 + intentService.withdraw(intent.getValue());
162 + intents.remove(intent.getKey(), intent.getValue());
163 + log.info("Tried to clean intents for app: {}", appId);
164 + });
165 + }
166 +
167 + @Override
146 public void submit(Intent intent) { 168 public void submit(Intent intent) {
147 synchronized (this) { 169 synchronized (this) {
148 intents.put(intent.key(), intent); 170 intents.put(intent.key(), intent);
......
...@@ -89,9 +89,9 @@ public class SdnIp { ...@@ -89,9 +89,9 @@ public class SdnIp {
89 interfaceService); 89 interfaceService);
90 peerConnectivity.start(); 90 peerConnectivity.start();
91 91
92 - // TODO fix removing intents 92 + applicationService.registerDeactivateHook(appId, () -> {
93 - applicationService.registerDeactivateHook(appId, 93 + intentSynchronizer.removeIntentsByAppId(appId);
94 - intentSynchronizerAdmin::removeIntents); 94 + });
95 95
96 log.info("SDN-IP started"); 96 log.info("SDN-IP started");
97 } 97 }
......
...@@ -92,8 +92,9 @@ public class Vpls { ...@@ -92,8 +92,9 @@ public class Vpls {
92 intentService, 92 intentService,
93 intentSynchronizer); 93 intentSynchronizer);
94 94
95 - applicationService.registerDeactivateHook(appId, 95 + applicationService.registerDeactivateHook(appId, () -> {
96 - intentSynchronizerAdmin::removeIntents); 96 + intentSynchronizer.removeIntentsByAppId(appId);
97 + });
97 98
98 hostService.addListener(hostListener); 99 hostService.addListener(hostListener);
99 interfaceService.addListener(interfaceListener); 100 interfaceService.addListener(interfaceListener);
......
...@@ -666,12 +666,14 @@ public class VplsTest { ...@@ -666,12 +666,14 @@ public class VplsTest {
666 666
667 @Override 667 @Override
668 public void modifyPrimary(boolean isPrimary) { 668 public void modifyPrimary(boolean isPrimary) {
669 -
670 } 669 }
671 670
672 @Override 671 @Override
673 public void removeIntents() { 672 public void removeIntents() {
673 + }
674 674
675 + @Override
676 + public void removeIntentsByAppId(ApplicationId applicationId) {
675 } 677 }
676 } 678 }
677 679
......