blob: 2c6e27a7a500e2c6451ba67ef7e027a22459b562 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom89b63c52014-09-16 09:19:51 -070017
Yi Tseng46f3edd2017-05-26 15:14:53 -070018import com.google.common.collect.Sets;
tom22288032014-10-07 08:16:53 -070019import org.apache.karaf.shell.commands.Argument;
tom89b63c52014-09-16 09:19:51 -070020import org.apache.karaf.shell.commands.Command;
Yi Tseng46f3edd2017-05-26 15:14:53 -070021import org.onlab.util.Tools;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.Device;
23import org.onosproject.net.Host;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070024import org.onosproject.net.Link;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.device.DeviceAdminService;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053026import org.onosproject.net.flow.FlowRuleService;
27import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.host.HostAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.intent.Intent;
Yi Tseng46f3edd2017-05-26 15:14:53 -070030import org.onosproject.net.intent.IntentEvent;
31import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.IntentService;
Yi Tseng46f3edd2017-05-26 15:14:53 -070033import org.onosproject.net.intent.Key;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070034import org.onosproject.net.link.LinkAdminService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070035import org.onosproject.net.region.RegionAdminService;
36import org.onosproject.ui.UiTopoLayoutService;
Yi Tseng46f3edd2017-05-26 15:14:53 -070037import java.util.Set;
38import java.util.concurrent.CompletableFuture;
39import java.util.concurrent.ExecutionException;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053040import java.util.concurrent.TimeUnit;
Yi Tseng46f3edd2017-05-26 15:14:53 -070041import java.util.concurrent.TimeoutException;
42import java.util.stream.Collectors;
43
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053044import static org.onosproject.net.intent.IntentState.WITHDRAWN;
tom89b63c52014-09-16 09:19:51 -070045
46/**
tome2555ff2014-10-07 18:47:58 -070047 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070048 */
49@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070050 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
tom89b63c52014-09-16 09:19:51 -070051public class WipeOutCommand extends ClustersListCommand {
52
tom1679e182014-10-09 13:50:45 -070053 private static final String PLEASE = "please";
Yi Tseng46f3edd2017-05-26 15:14:53 -070054 @Argument(name = "please", description = "Confirmation phrase")
tom1679e182014-10-09 13:50:45 -070055 String please = null;
tom22288032014-10-07 08:16:53 -070056
tom89b63c52014-09-16 09:19:51 -070057 @Override
tom0872a172014-09-23 11:24:26 -070058 protected void execute() {
tom1679e182014-10-09 13:50:45 -070059 if (please == null || !please.equals(PLEASE)) {
60 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070061 return;
62 }
63
Thomas Vachuskad35f8472015-08-04 10:06:48 -070064 wipeOutIntents();
65 wipeOutHosts();
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053066 wipeOutFlows();
67 wipeOutGroups();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070068 wipeOutDevices();
69 wipeOutLinks();
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070070
71 wipeOutLayouts();
72 wipeOutRegions();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070073 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070074
Thomas Vachuskad35f8472015-08-04 10:06:48 -070075 private void wipeOutIntents() {
76 print("Wiping intents");
77 IntentService intentService = get(IntentService.class);
Yi Tseng46f3edd2017-05-26 15:14:53 -070078 Set<Key> keysToWithdrawn = Sets.newConcurrentHashSet();
79 Set<Intent> intentsToWithdrawn = Tools.stream(intentService.getIntents())
80 .filter(intent -> intentService.getIntentState(intent.key()) != WITHDRAWN)
81 .collect(Collectors.toSet());
82 intentsToWithdrawn.stream()
83 .map(Intent::key)
84 .forEach(keysToWithdrawn::add);
85 CompletableFuture<Void> completableFuture = new CompletableFuture<>();
86 IntentListener listener = e -> {
87 if (e.type() == IntentEvent.Type.WITHDRAWN) {
88 keysToWithdrawn.remove(e.subject().key());
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070089 }
Yi Tseng46f3edd2017-05-26 15:14:53 -070090 if (keysToWithdrawn.isEmpty()) {
91 completableFuture.complete(null);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053092 }
Yi Tseng46f3edd2017-05-26 15:14:53 -070093 };
94 intentService.addListener(listener);
95 intentsToWithdrawn.forEach(intentService::withdraw);
96 try {
97 // Wait 1.5 seconds for each Intent
98 completableFuture.get(intentsToWithdrawn.size() * 1500, TimeUnit.MILLISECONDS);
99 } catch (InterruptedException e) {
100 print("Got interrupted exception while withdrawn Intents " + e.toString());
101 } catch (ExecutionException e) {
102 print("Got execution exception while withdrawn Intents " + e.toString());
103 } catch (TimeoutException e) {
104 print("Got timeout exception while withdrawn Intents " + e.toString());
105 } finally {
106 intentService.removeListener(listener);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530107 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700108 intentsToWithdrawn.forEach(intentService::purge);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530109 }
110
111 private void wipeOutFlows() {
112 print("Wiping Flows");
113 FlowRuleService flowRuleService = get(FlowRuleService.class);
114 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
115 for (Device device : deviceAdminService.getDevices()) {
116 flowRuleService.purgeFlowRules(device.id());
117 }
118 }
119
120 private void wipeOutGroups() {
121 print("Wiping groups");
122 GroupService groupService = get(GroupService.class);
123 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
124 for (Device device : deviceAdminService.getDevices()) {
125 groupService.purgeGroupEntries(device.id());
tom89b63c52014-09-16 09:19:51 -0700126 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700127 }
tom89b63c52014-09-16 09:19:51 -0700128
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700129 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -0700130 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -0700131 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700132 while (hostAdminService.getHostCount() > 0) {
133 try {
134 for (Host host : hostAdminService.getHosts()) {
135 hostAdminService.removeHost(host.id());
136 }
137 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700138 log.info("Unable to wipe-out hosts", e);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700139 }
tom89b63c52014-09-16 09:19:51 -0700140 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700141 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700142
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700143 private void wipeOutDevices() {
144 print("Wiping devices");
145 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
146 while (deviceAdminService.getDeviceCount() > 0) {
147 try {
148 for (Device device : deviceAdminService.getDevices()) {
149 deviceAdminService.removeDevice(device.id());
150 }
151 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700152 log.info("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -0700153 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700154 }
155 }
156
157 private void wipeOutLinks() {
158 print("Wiping links");
159 LinkAdminService linkAdminService = get(LinkAdminService.class);
160 while (linkAdminService.getLinkCount() > 0) {
161 try {
162 for (Link link : linkAdminService.getLinks()) {
163 linkAdminService.removeLinks(link.src());
164 linkAdminService.removeLinks(link.dst());
165 }
166 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700167 log.info("Unable to wipe-out links", e);
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700168 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700169 }
tom89b63c52014-09-16 09:19:51 -0700170 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700171
172 private void wipeOutLayouts() {
173 print("Wiping UI layouts");
174 UiTopoLayoutService service = get(UiTopoLayoutService.class);
Simon Huntb1ce2602016-07-23 14:04:31 -0700175 // wipe out all layouts except the default, which should always be there
176 service.getLayouts().forEach(l -> {
177 if (!l.id().isDefault()) {
178 service.removeLayout(l);
179 }
180 });
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700181 }
182
183 private void wipeOutRegions() {
184 print("Wiping regions");
185 RegionAdminService service = get(RegionAdminService.class);
186 service.getRegions().forEach(r -> service.removeRegion(r.id()));
187 }
tom89b63c52014-09-16 09:19:51 -0700188}