blob: 880621d31f1f755f2f6478e57e57f935de289d30 [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;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080026import org.onosproject.net.config.NetworkConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.device.DeviceAdminService;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053028import org.onosproject.net.flow.FlowRuleService;
29import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.host.HostAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.intent.Intent;
Yi Tseng46f3edd2017-05-26 15:14:53 -070032import org.onosproject.net.intent.IntentEvent;
33import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.IntentService;
Yi Tseng46f3edd2017-05-26 15:14:53 -070035import org.onosproject.net.intent.Key;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070036import org.onosproject.net.link.LinkAdminService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070037import org.onosproject.net.region.RegionAdminService;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080038import org.onosproject.ui.UiExtensionService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070039import org.onosproject.ui.UiTopoLayoutService;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080040
Yi Tseng46f3edd2017-05-26 15:14:53 -070041import java.util.Set;
42import java.util.concurrent.CompletableFuture;
43import java.util.concurrent.ExecutionException;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053044import java.util.concurrent.TimeUnit;
Yi Tseng46f3edd2017-05-26 15:14:53 -070045import java.util.concurrent.TimeoutException;
46import java.util.stream.Collectors;
47
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053048import static org.onosproject.net.intent.IntentState.WITHDRAWN;
tom89b63c52014-09-16 09:19:51 -070049
50/**
tome2555ff2014-10-07 18:47:58 -070051 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070052 */
53@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070054 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
Yuta HIGUCHId8119802017-12-20 13:59:38 -080055public class WipeOutCommand extends AbstractShellCommand {
tom89b63c52014-09-16 09:19:51 -070056
tom1679e182014-10-09 13:50:45 -070057 private static final String PLEASE = "please";
Yi Tseng46f3edd2017-05-26 15:14:53 -070058 @Argument(name = "please", description = "Confirmation phrase")
tom1679e182014-10-09 13:50:45 -070059 String please = null;
tom22288032014-10-07 08:16:53 -070060
tom89b63c52014-09-16 09:19:51 -070061 @Override
tom0872a172014-09-23 11:24:26 -070062 protected void execute() {
tom1679e182014-10-09 13:50:45 -070063 if (please == null || !please.equals(PLEASE)) {
64 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070065 return;
66 }
67
Thomas Vachuskad35f8472015-08-04 10:06:48 -070068 wipeOutIntents();
69 wipeOutHosts();
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053070 wipeOutFlows();
71 wipeOutGroups();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070072 wipeOutDevices();
73 wipeOutLinks();
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080074 wipeOutNetworkConfig();
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070075
76 wipeOutLayouts();
77 wipeOutRegions();
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080078 wipeOutUiCache();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070079 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070080
Thomas Vachuskad35f8472015-08-04 10:06:48 -070081 private void wipeOutIntents() {
82 print("Wiping intents");
83 IntentService intentService = get(IntentService.class);
Yi Tseng46f3edd2017-05-26 15:14:53 -070084 Set<Key> keysToWithdrawn = Sets.newConcurrentHashSet();
85 Set<Intent> intentsToWithdrawn = Tools.stream(intentService.getIntents())
86 .filter(intent -> intentService.getIntentState(intent.key()) != WITHDRAWN)
87 .collect(Collectors.toSet());
88 intentsToWithdrawn.stream()
89 .map(Intent::key)
90 .forEach(keysToWithdrawn::add);
91 CompletableFuture<Void> completableFuture = new CompletableFuture<>();
92 IntentListener listener = e -> {
93 if (e.type() == IntentEvent.Type.WITHDRAWN) {
94 keysToWithdrawn.remove(e.subject().key());
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070095 }
Yi Tseng46f3edd2017-05-26 15:14:53 -070096 if (keysToWithdrawn.isEmpty()) {
97 completableFuture.complete(null);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053098 }
Yi Tseng46f3edd2017-05-26 15:14:53 -070099 };
100 intentService.addListener(listener);
101 intentsToWithdrawn.forEach(intentService::withdraw);
102 try {
Thomas Vachuska1b1355d2018-02-06 16:53:58 -0800103 if (!intentsToWithdrawn.isEmpty()) {
104 // Wait 1.5 seconds for each Intent
105 completableFuture.get(intentsToWithdrawn.size() * 1500L, TimeUnit.MILLISECONDS);
106 }
107 } catch (InterruptedException | ExecutionException | TimeoutException e) {
108 print("Encountered exception while withdrawing intents: " + e.toString());
Yi Tseng46f3edd2017-05-26 15:14:53 -0700109 } finally {
110 intentService.removeListener(listener);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530111 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700112 intentsToWithdrawn.forEach(intentService::purge);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530113 }
114
115 private void wipeOutFlows() {
116 print("Wiping Flows");
117 FlowRuleService flowRuleService = get(FlowRuleService.class);
118 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
119 for (Device device : deviceAdminService.getDevices()) {
120 flowRuleService.purgeFlowRules(device.id());
121 }
122 }
123
124 private void wipeOutGroups() {
125 print("Wiping groups");
126 GroupService groupService = get(GroupService.class);
127 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
128 for (Device device : deviceAdminService.getDevices()) {
129 groupService.purgeGroupEntries(device.id());
tom89b63c52014-09-16 09:19:51 -0700130 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700131 }
tom89b63c52014-09-16 09:19:51 -0700132
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700133 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -0700134 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -0700135 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700136 while (hostAdminService.getHostCount() > 0) {
137 try {
138 for (Host host : hostAdminService.getHosts()) {
139 hostAdminService.removeHost(host.id());
140 }
141 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700142 log.info("Unable to wipe-out hosts", e);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700143 }
tom89b63c52014-09-16 09:19:51 -0700144 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700145 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700146
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700147 private void wipeOutDevices() {
148 print("Wiping devices");
149 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
150 while (deviceAdminService.getDeviceCount() > 0) {
151 try {
152 for (Device device : deviceAdminService.getDevices()) {
153 deviceAdminService.removeDevice(device.id());
154 }
155 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700156 log.info("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -0700157 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700158 }
159 }
160
161 private void wipeOutLinks() {
162 print("Wiping links");
163 LinkAdminService linkAdminService = get(LinkAdminService.class);
164 while (linkAdminService.getLinkCount() > 0) {
165 try {
166 for (Link link : linkAdminService.getLinks()) {
167 linkAdminService.removeLinks(link.src());
168 linkAdminService.removeLinks(link.dst());
169 }
170 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700171 log.info("Unable to wipe-out links", e);
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700172 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700173 }
tom89b63c52014-09-16 09:19:51 -0700174 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700175
176 private void wipeOutLayouts() {
177 print("Wiping UI layouts");
178 UiTopoLayoutService service = get(UiTopoLayoutService.class);
Simon Huntb1ce2602016-07-23 14:04:31 -0700179 // wipe out all layouts except the default, which should always be there
180 service.getLayouts().forEach(l -> {
181 if (!l.id().isDefault()) {
182 service.removeLayout(l);
183 }
184 });
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700185 }
186
187 private void wipeOutRegions() {
188 print("Wiping regions");
189 RegionAdminService service = get(RegionAdminService.class);
190 service.getRegions().forEach(r -> service.removeRegion(r.id()));
191 }
Thomas Vachuska1b1355d2018-02-06 16:53:58 -0800192
193 private void wipeOutNetworkConfig() {
194 print("Wiping network configs");
195 get(NetworkConfigService.class).removeConfig();
196 }
197
198 private void wipeOutUiCache() {
199 print("Wiping ui model cache");
200 get(UiExtensionService.class).refreshModel();
201 }
202
tom89b63c52014-09-16 09:19:51 -0700203}