blob: cd4275299c1dd76fe8565ac2604c862b80fd3e4a [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Yuta HIGUCHId8119802017-12-20 13:59:38 -080022import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.Device;
24import org.onosproject.net.Host;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070025import org.onosproject.net.Link;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.device.DeviceAdminService;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053027import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.host.HostAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.Intent;
Yi Tseng46f3edd2017-05-26 15:14:53 -070031import org.onosproject.net.intent.IntentEvent;
32import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.IntentService;
Yi Tseng46f3edd2017-05-26 15:14:53 -070034import org.onosproject.net.intent.Key;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070035import org.onosproject.net.link.LinkAdminService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070036import org.onosproject.net.region.RegionAdminService;
37import org.onosproject.ui.UiTopoLayoutService;
Yi Tseng46f3edd2017-05-26 15:14:53 -070038import java.util.Set;
39import java.util.concurrent.CompletableFuture;
40import java.util.concurrent.ExecutionException;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053041import java.util.concurrent.TimeUnit;
Yi Tseng46f3edd2017-05-26 15:14:53 -070042import java.util.concurrent.TimeoutException;
43import java.util.stream.Collectors;
44
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053045import static org.onosproject.net.intent.IntentState.WITHDRAWN;
tom89b63c52014-09-16 09:19:51 -070046
47/**
tome2555ff2014-10-07 18:47:58 -070048 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070049 */
50@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070051 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
Yuta HIGUCHId8119802017-12-20 13:59:38 -080052public class WipeOutCommand extends AbstractShellCommand {
tom89b63c52014-09-16 09:19:51 -070053
tom1679e182014-10-09 13:50:45 -070054 private static final String PLEASE = "please";
Yi Tseng46f3edd2017-05-26 15:14:53 -070055 @Argument(name = "please", description = "Confirmation phrase")
tom1679e182014-10-09 13:50:45 -070056 String please = null;
tom22288032014-10-07 08:16:53 -070057
tom89b63c52014-09-16 09:19:51 -070058 @Override
tom0872a172014-09-23 11:24:26 -070059 protected void execute() {
tom1679e182014-10-09 13:50:45 -070060 if (please == null || !please.equals(PLEASE)) {
61 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070062 return;
63 }
64
Thomas Vachuskad35f8472015-08-04 10:06:48 -070065 wipeOutIntents();
66 wipeOutHosts();
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053067 wipeOutFlows();
68 wipeOutGroups();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070069 wipeOutDevices();
70 wipeOutLinks();
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070071
72 wipeOutLayouts();
73 wipeOutRegions();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070074 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070075
Thomas Vachuskad35f8472015-08-04 10:06:48 -070076 private void wipeOutIntents() {
77 print("Wiping intents");
78 IntentService intentService = get(IntentService.class);
Yi Tseng46f3edd2017-05-26 15:14:53 -070079 Set<Key> keysToWithdrawn = Sets.newConcurrentHashSet();
80 Set<Intent> intentsToWithdrawn = Tools.stream(intentService.getIntents())
81 .filter(intent -> intentService.getIntentState(intent.key()) != WITHDRAWN)
82 .collect(Collectors.toSet());
83 intentsToWithdrawn.stream()
84 .map(Intent::key)
85 .forEach(keysToWithdrawn::add);
86 CompletableFuture<Void> completableFuture = new CompletableFuture<>();
87 IntentListener listener = e -> {
88 if (e.type() == IntentEvent.Type.WITHDRAWN) {
89 keysToWithdrawn.remove(e.subject().key());
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070090 }
Yi Tseng46f3edd2017-05-26 15:14:53 -070091 if (keysToWithdrawn.isEmpty()) {
92 completableFuture.complete(null);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053093 }
Yi Tseng46f3edd2017-05-26 15:14:53 -070094 };
95 intentService.addListener(listener);
96 intentsToWithdrawn.forEach(intentService::withdraw);
97 try {
98 // Wait 1.5 seconds for each Intent
Ray Milkey3717e602018-02-01 13:49:47 -080099 completableFuture.get(intentsToWithdrawn.size() * 1500L, TimeUnit.MILLISECONDS);
Yi Tseng46f3edd2017-05-26 15:14:53 -0700100 } catch (InterruptedException e) {
101 print("Got interrupted exception while withdrawn Intents " + e.toString());
102 } catch (ExecutionException e) {
103 print("Got execution exception while withdrawn Intents " + e.toString());
104 } catch (TimeoutException e) {
105 print("Got timeout exception while withdrawn Intents " + e.toString());
106 } finally {
107 intentService.removeListener(listener);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530108 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700109 intentsToWithdrawn.forEach(intentService::purge);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530110 }
111
112 private void wipeOutFlows() {
113 print("Wiping Flows");
114 FlowRuleService flowRuleService = get(FlowRuleService.class);
115 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
116 for (Device device : deviceAdminService.getDevices()) {
117 flowRuleService.purgeFlowRules(device.id());
118 }
119 }
120
121 private void wipeOutGroups() {
122 print("Wiping groups");
123 GroupService groupService = get(GroupService.class);
124 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
125 for (Device device : deviceAdminService.getDevices()) {
126 groupService.purgeGroupEntries(device.id());
tom89b63c52014-09-16 09:19:51 -0700127 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700128 }
tom89b63c52014-09-16 09:19:51 -0700129
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700130 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -0700131 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -0700132 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700133 while (hostAdminService.getHostCount() > 0) {
134 try {
135 for (Host host : hostAdminService.getHosts()) {
136 hostAdminService.removeHost(host.id());
137 }
138 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700139 log.info("Unable to wipe-out hosts", e);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700140 }
tom89b63c52014-09-16 09:19:51 -0700141 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700142 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700143
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700144 private void wipeOutDevices() {
145 print("Wiping devices");
146 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
147 while (deviceAdminService.getDeviceCount() > 0) {
148 try {
149 for (Device device : deviceAdminService.getDevices()) {
150 deviceAdminService.removeDevice(device.id());
151 }
152 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700153 log.info("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -0700154 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700155 }
156 }
157
158 private void wipeOutLinks() {
159 print("Wiping links");
160 LinkAdminService linkAdminService = get(LinkAdminService.class);
161 while (linkAdminService.getLinkCount() > 0) {
162 try {
163 for (Link link : linkAdminService.getLinks()) {
164 linkAdminService.removeLinks(link.src());
165 linkAdminService.removeLinks(link.dst());
166 }
167 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700168 log.info("Unable to wipe-out links", e);
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700169 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700170 }
tom89b63c52014-09-16 09:19:51 -0700171 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700172
173 private void wipeOutLayouts() {
174 print("Wiping UI layouts");
175 UiTopoLayoutService service = get(UiTopoLayoutService.class);
Simon Huntb1ce2602016-07-23 14:04:31 -0700176 // wipe out all layouts except the default, which should always be there
177 service.getLayouts().forEach(l -> {
178 if (!l.id().isDefault()) {
179 service.removeLayout(l);
180 }
181 });
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700182 }
183
184 private void wipeOutRegions() {
185 print("Wiping regions");
186 RegionAdminService service = get(RegionAdminService.class);
187 service.getRegions().forEach(r -> service.removeRegion(r.id()));
188 }
tom89b63c52014-09-16 09:19:51 -0700189}