Kavitha Alagesan

Fix for wipe-out please command ONOS-4653

Change-Id: I26c841f2d67bc915b7e02e6d1e792a7e2d9ea329
...@@ -21,11 +21,18 @@ import org.onosproject.net.Device; ...@@ -21,11 +21,18 @@ import org.onosproject.net.Device;
21 import org.onosproject.net.Host; 21 import org.onosproject.net.Host;
22 import org.onosproject.net.Link; 22 import org.onosproject.net.Link;
23 import org.onosproject.net.device.DeviceAdminService; 23 import org.onosproject.net.device.DeviceAdminService;
24 +import org.onosproject.net.flow.FlowRuleService;
25 +import org.onosproject.net.group.GroupService;
24 import org.onosproject.net.host.HostAdminService; 26 import org.onosproject.net.host.HostAdminService;
25 import org.onosproject.net.intent.Intent; 27 import org.onosproject.net.intent.Intent;
26 import org.onosproject.net.intent.IntentService; 28 import org.onosproject.net.intent.IntentService;
27 import org.onosproject.net.intent.IntentState; 29 import org.onosproject.net.intent.IntentState;
28 import org.onosproject.net.link.LinkAdminService; 30 import org.onosproject.net.link.LinkAdminService;
31 +import java.util.EnumSet;
32 +import java.util.concurrent.CountDownLatch;
33 +import java.util.concurrent.TimeUnit;
34 +import static org.onosproject.net.intent.IntentState.FAILED;
35 +import static org.onosproject.net.intent.IntentState.WITHDRAWN;
29 36
30 /** 37 /**
31 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents. 38 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
...@@ -35,7 +42,7 @@ import org.onosproject.net.link.LinkAdminService; ...@@ -35,7 +42,7 @@ import org.onosproject.net.link.LinkAdminService;
35 public class WipeOutCommand extends ClustersListCommand { 42 public class WipeOutCommand extends ClustersListCommand {
36 43
37 private static final String PLEASE = "please"; 44 private static final String PLEASE = "please";
38 - 45 + private static final EnumSet<IntentState> CAN_PURGE = EnumSet.of(WITHDRAWN, FAILED);
39 @Argument(index = 0, name = "please", description = "Confirmation phrase", 46 @Argument(index = 0, name = "please", description = "Confirmation phrase",
40 required = false, multiValued = false) 47 required = false, multiValued = false)
41 String please = null; 48 String please = null;
...@@ -49,6 +56,8 @@ public class WipeOutCommand extends ClustersListCommand { ...@@ -49,6 +56,8 @@ public class WipeOutCommand extends ClustersListCommand {
49 56
50 wipeOutIntents(); 57 wipeOutIntents();
51 wipeOutHosts(); 58 wipeOutHosts();
59 + wipeOutFlows();
60 + wipeOutGroups();
52 wipeOutDevices(); 61 wipeOutDevices();
53 wipeOutLinks(); 62 wipeOutLinks();
54 } 63 }
...@@ -56,11 +65,38 @@ public class WipeOutCommand extends ClustersListCommand { ...@@ -56,11 +65,38 @@ public class WipeOutCommand extends ClustersListCommand {
56 private void wipeOutIntents() { 65 private void wipeOutIntents() {
57 print("Wiping intents"); 66 print("Wiping intents");
58 IntentService intentService = get(IntentService.class); 67 IntentService intentService = get(IntentService.class);
68 + final CountDownLatch withdrawLatch;
69 + withdrawLatch = new CountDownLatch(1);
59 for (Intent intent : intentService.getIntents()) { 70 for (Intent intent : intentService.getIntents()) {
60 if (intentService.getIntentState(intent.key()) != IntentState.WITHDRAWN) { 71 if (intentService.getIntentState(intent.key()) != IntentState.WITHDRAWN) {
61 intentService.withdraw(intent); 72 intentService.withdraw(intent);
73 + try { // wait for withdraw event
74 + withdrawLatch.await(5, TimeUnit.SECONDS);
75 + } catch (InterruptedException e) {
76 + print("Timed out waiting for intent {} withdraw");
77 + }
78 + }
79 + if (CAN_PURGE.contains(intentService.getIntentState(intent.key()))) {
80 + intentService.purge(intent);
81 + }
62 } 82 }
63 - intentService.purge(intent); 83 + }
84 +
85 + private void wipeOutFlows() {
86 + print("Wiping Flows");
87 + FlowRuleService flowRuleService = get(FlowRuleService.class);
88 + DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
89 + for (Device device : deviceAdminService.getDevices()) {
90 + flowRuleService.purgeFlowRules(device.id());
91 + }
92 + }
93 +
94 + private void wipeOutGroups() {
95 + print("Wiping groups");
96 + GroupService groupService = get(GroupService.class);
97 + DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
98 + for (Device device : deviceAdminService.getDevices()) {
99 + groupService.purgeGroupEntries(device.id());
64 } 100 }
65 } 101 }
66 102
......
...@@ -61,6 +61,12 @@ public interface FlowRuleService ...@@ -61,6 +61,12 @@ public interface FlowRuleService
61 void applyFlowRules(FlowRule... flowRules); 61 void applyFlowRules(FlowRule... flowRules);
62 62
63 /** 63 /**
64 + * Purges all the flow rules on the specified device.
65 + * @param deviceId device identifier
66 + */
67 + void purgeFlowRules(DeviceId deviceId);
68 +
69 + /**
64 * Removes the specified flow rules from their respective devices. If the 70 * Removes the specified flow rules from their respective devices. If the
65 * device is not presently connected to the controller, these flow will 71 * device is not presently connected to the controller, these flow will
66 * be removed once the device reconnects. 72 * be removed once the device reconnects.
......
...@@ -107,6 +107,12 @@ public interface GroupService ...@@ -107,6 +107,12 @@ public interface GroupService
107 ApplicationId appId); 107 ApplicationId appId);
108 108
109 /** 109 /**
110 + * Purges all the group entries on the specified device.
111 + * @param deviceId device identifier
112 + */
113 + void purgeGroupEntries(DeviceId deviceId);
114 +
115 + /**
110 * Deletes a group associated to an application cookie. 116 * Deletes a group associated to an application cookie.
111 * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be 117 * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be
112 * provided along with cookie depending on the result of the 118 * provided along with cookie depending on the result of the
......
...@@ -38,6 +38,10 @@ public class FlowRuleServiceAdapter implements FlowRuleService { ...@@ -38,6 +38,10 @@ public class FlowRuleServiceAdapter implements FlowRuleService {
38 } 38 }
39 39
40 @Override 40 @Override
41 + public void purgeFlowRules(DeviceId deviceId){
42 + }
43 +
44 + @Override
41 public void removeFlowRules(FlowRule... flowRules) { 45 public void removeFlowRules(FlowRule... flowRules) {
42 } 46 }
43 47
......
...@@ -244,6 +244,12 @@ public class FlowRuleManager ...@@ -244,6 +244,12 @@ public class FlowRuleManager
244 } 244 }
245 245
246 @Override 246 @Override
247 + public void purgeFlowRules(DeviceId deviceId) {
248 + checkPermission(FLOWRULE_WRITE);
249 + store.purgeFlowRule(deviceId);
250 + }
251 +
252 + @Override
247 public void removeFlowRules(FlowRule... flowRules) { 253 public void removeFlowRules(FlowRule... flowRules) {
248 checkPermission(FLOWRULE_WRITE); 254 checkPermission(FLOWRULE_WRITE);
249 255
......
...@@ -224,6 +224,13 @@ public class GroupManager ...@@ -224,6 +224,13 @@ public class GroupManager
224 newCookie); 224 newCookie);
225 } 225 }
226 226
227 + @Override
228 + public void purgeGroupEntries(DeviceId deviceId) {
229 + checkPermission(GROUP_WRITE);
230 + store.purgeGroupEntry(deviceId);
231 + }
232 +
233 +
227 /** 234 /**
228 * Delete a group associated to an application cookie. 235 * Delete a group associated to an application cookie.
229 * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be 236 * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be
......
...@@ -260,6 +260,22 @@ public class FlowRuleManagerTest { ...@@ -260,6 +260,22 @@ public class FlowRuleManagerTest {
260 } 260 }
261 261
262 @Test 262 @Test
263 + public void purgeFlowRules() {
264 + FlowRule f1 = addFlowRule(1);
265 + FlowRule f2 = addFlowRule(2);
266 + FlowRule f3 = addFlowRule(3);
267 + assertEquals("3 rules should exist", 3, flowCount());
268 + FlowEntry fe1 = new DefaultFlowEntry(f1);
269 + FlowEntry fe2 = new DefaultFlowEntry(f2);
270 + FlowEntry fe3 = new DefaultFlowEntry(f3);
271 + providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3));
272 + validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
273 + RULE_ADDED, RULE_ADDED, RULE_ADDED);
274 + mgr.purgeFlowRules(DID);
275 + assertEquals("0 rule should exist", 0, flowCount());
276 + }
277 +
278 + @Test
263 public void removeFlowRules() { 279 public void removeFlowRules() {
264 FlowRule f1 = addFlowRule(1); 280 FlowRule f1 = addFlowRule(1);
265 FlowRule f2 = addFlowRule(2); 281 FlowRule f2 = addFlowRule(2);
......
...@@ -188,6 +188,21 @@ public class GroupManagerTest { ...@@ -188,6 +188,21 @@ public class GroupManagerTest {
188 } 188 }
189 189
190 /** 190 /**
191 + * Tests group Purge Operation.
192 + */
193 + @Test
194 + public void testPurgeGroups() {
195 + //Test Group creation before AUDIT process
196 + testGroupCreationBeforeAudit(DID);
197 + programmableTestCleanUp();
198 + testAuditWithExtraneousMissingGroups(DID);
199 + // Test group add bucket operations
200 + testAddBuckets(DID);
201 + // Test group Purge operations
202 + testPurgeGroupEntry(DID);
203 + }
204 +
205 + /**
191 * Tests group bucket modifications (additions and deletions) and 206 * Tests group bucket modifications (additions and deletions) and
192 * Tests group deletion. 207 * Tests group deletion.
193 */ 208 */
...@@ -507,6 +522,13 @@ public class GroupManagerTest { ...@@ -507,6 +522,13 @@ public class GroupManagerTest {
507 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED)); 522 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED));
508 } 523 }
509 524
525 + // Test purge group entry operations
526 + private void testPurgeGroupEntry(DeviceId deviceId) {
527 + assertEquals(1, Iterables.size(groupService.getGroups(deviceId, appId)));
528 + groupService.purgeGroupEntries(deviceId);
529 + assertEquals(0, Iterables.size(groupService.getGroups(deviceId, appId)));
530 + }
531 +
510 // Test group remove operations 532 // Test group remove operations
511 private void testRemoveGroup(DeviceId deviceId) { 533 private void testRemoveGroup(DeviceId deviceId) {
512 GroupKey currKey = new DefaultGroupKey("group1RemoveBuckets".getBytes()); 534 GroupKey currKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
......