blob: 868566ce0abef8d643fc7c0810fb25b850042fb0 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Yi Tseng46f3edd2017-05-26 15:14:53 -070022import org.onlab.util.Tools;
Yuta HIGUCHId8119802017-12-20 13:59:38 -080023import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.Device;
25import org.onosproject.net.Host;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070026import org.onosproject.net.Link;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080027import org.onosproject.net.config.NetworkConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.device.DeviceAdminService;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053029import org.onosproject.net.flow.FlowRuleService;
30import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.host.HostAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.Intent;
Yi Tseng46f3edd2017-05-26 15:14:53 -070033import org.onosproject.net.intent.IntentEvent;
34import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.intent.IntentService;
Yi Tseng46f3edd2017-05-26 15:14:53 -070036import org.onosproject.net.intent.Key;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070037import org.onosproject.net.link.LinkAdminService;
Andrea Campanella4e4084c2020-05-13 15:36:57 +020038import org.onosproject.net.meter.MeterService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070039import org.onosproject.net.region.RegionAdminService;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080040import org.onosproject.ui.UiExtensionService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070041import org.onosproject.ui.UiTopoLayoutService;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080042
Yi Tseng46f3edd2017-05-26 15:14:53 -070043import java.util.Set;
44import java.util.concurrent.CompletableFuture;
45import java.util.concurrent.ExecutionException;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053046import java.util.concurrent.TimeUnit;
Yi Tseng46f3edd2017-05-26 15:14:53 -070047import java.util.concurrent.TimeoutException;
48import java.util.stream.Collectors;
49
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053050import static org.onosproject.net.intent.IntentState.WITHDRAWN;
tom89b63c52014-09-16 09:19:51 -070051
52/**
tome2555ff2014-10-07 18:47:58 -070053 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070054 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055@Service
tom89b63c52014-09-16 09:19:51 -070056@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070057 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
Yuta HIGUCHId8119802017-12-20 13:59:38 -080058public class WipeOutCommand extends AbstractShellCommand {
tom89b63c52014-09-16 09:19:51 -070059
tom1679e182014-10-09 13:50:45 -070060 private static final String PLEASE = "please";
Yi Tseng46f3edd2017-05-26 15:14:53 -070061 @Argument(name = "please", description = "Confirmation phrase")
tom1679e182014-10-09 13:50:45 -070062 String please = null;
tom22288032014-10-07 08:16:53 -070063
tom89b63c52014-09-16 09:19:51 -070064 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 protected void doExecute() {
tom1679e182014-10-09 13:50:45 -070066 if (please == null || !please.equals(PLEASE)) {
67 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070068 return;
69 }
70
Thomas Vachuskad35f8472015-08-04 10:06:48 -070071 wipeOutIntents();
72 wipeOutHosts();
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053073 wipeOutFlows();
74 wipeOutGroups();
Andrea Campanella4e4084c2020-05-13 15:36:57 +020075 wipeOutMeters();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070076 wipeOutDevices();
77 wipeOutLinks();
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080078 wipeOutNetworkConfig();
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070079
80 wipeOutLayouts();
81 wipeOutRegions();
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080082 wipeOutUiCache();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070083 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070084
Thomas Vachuskad35f8472015-08-04 10:06:48 -070085 private void wipeOutIntents() {
86 print("Wiping intents");
87 IntentService intentService = get(IntentService.class);
Yi Tseng46f3edd2017-05-26 15:14:53 -070088 Set<Key> keysToWithdrawn = Sets.newConcurrentHashSet();
89 Set<Intent> intentsToWithdrawn = Tools.stream(intentService.getIntents())
90 .filter(intent -> intentService.getIntentState(intent.key()) != WITHDRAWN)
91 .collect(Collectors.toSet());
92 intentsToWithdrawn.stream()
93 .map(Intent::key)
94 .forEach(keysToWithdrawn::add);
95 CompletableFuture<Void> completableFuture = new CompletableFuture<>();
96 IntentListener listener = e -> {
97 if (e.type() == IntentEvent.Type.WITHDRAWN) {
98 keysToWithdrawn.remove(e.subject().key());
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070099 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700100 if (keysToWithdrawn.isEmpty()) {
101 completableFuture.complete(null);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530102 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700103 };
104 intentService.addListener(listener);
105 intentsToWithdrawn.forEach(intentService::withdraw);
106 try {
Thomas Vachuska1b1355d2018-02-06 16:53:58 -0800107 if (!intentsToWithdrawn.isEmpty()) {
108 // Wait 1.5 seconds for each Intent
109 completableFuture.get(intentsToWithdrawn.size() * 1500L, TimeUnit.MILLISECONDS);
110 }
111 } catch (InterruptedException | ExecutionException | TimeoutException e) {
112 print("Encountered exception while withdrawing intents: " + e.toString());
Yi Tseng46f3edd2017-05-26 15:14:53 -0700113 } finally {
114 intentService.removeListener(listener);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530115 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700116 intentsToWithdrawn.forEach(intentService::purge);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530117 }
118
119 private void wipeOutFlows() {
120 print("Wiping Flows");
121 FlowRuleService flowRuleService = get(FlowRuleService.class);
122 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
123 for (Device device : deviceAdminService.getDevices()) {
124 flowRuleService.purgeFlowRules(device.id());
125 }
126 }
127
128 private void wipeOutGroups() {
129 print("Wiping groups");
130 GroupService groupService = get(GroupService.class);
131 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
132 for (Device device : deviceAdminService.getDevices()) {
133 groupService.purgeGroupEntries(device.id());
tom89b63c52014-09-16 09:19:51 -0700134 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700135 }
tom89b63c52014-09-16 09:19:51 -0700136
Andrea Campanella4e4084c2020-05-13 15:36:57 +0200137 private void wipeOutMeters() {
138 print("Wiping meters");
139 MeterService meterService = get(MeterService.class);
140 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
141 for (Device device : deviceAdminService.getDevices()) {
142 meterService.purgeMeters(device.id());
143 }
144 }
145
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700146 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -0700147 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -0700148 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700149 while (hostAdminService.getHostCount() > 0) {
150 try {
151 for (Host host : hostAdminService.getHosts()) {
152 hostAdminService.removeHost(host.id());
153 }
154 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700155 log.info("Unable to wipe-out hosts", e);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700156 }
tom89b63c52014-09-16 09:19:51 -0700157 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700158 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700159
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700160 private void wipeOutDevices() {
161 print("Wiping devices");
162 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
163 while (deviceAdminService.getDeviceCount() > 0) {
164 try {
165 for (Device device : deviceAdminService.getDevices()) {
166 deviceAdminService.removeDevice(device.id());
167 }
168 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700169 log.info("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -0700170 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700171 }
172 }
173
174 private void wipeOutLinks() {
175 print("Wiping links");
176 LinkAdminService linkAdminService = get(LinkAdminService.class);
177 while (linkAdminService.getLinkCount() > 0) {
178 try {
179 for (Link link : linkAdminService.getLinks()) {
180 linkAdminService.removeLinks(link.src());
181 linkAdminService.removeLinks(link.dst());
182 }
183 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700184 log.info("Unable to wipe-out links", e);
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700185 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700186 }
tom89b63c52014-09-16 09:19:51 -0700187 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700188
189 private void wipeOutLayouts() {
190 print("Wiping UI layouts");
191 UiTopoLayoutService service = get(UiTopoLayoutService.class);
Simon Huntb1ce2602016-07-23 14:04:31 -0700192 // wipe out all layouts except the default, which should always be there
193 service.getLayouts().forEach(l -> {
194 if (!l.id().isDefault()) {
195 service.removeLayout(l);
196 }
197 });
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700198 }
199
200 private void wipeOutRegions() {
201 print("Wiping regions");
202 RegionAdminService service = get(RegionAdminService.class);
203 service.getRegions().forEach(r -> service.removeRegion(r.id()));
204 }
Thomas Vachuska1b1355d2018-02-06 16:53:58 -0800205
206 private void wipeOutNetworkConfig() {
207 print("Wiping network configs");
208 get(NetworkConfigService.class).removeConfig();
209 }
210
211 private void wipeOutUiCache() {
212 print("Wiping ui model cache");
213 get(UiExtensionService.class).refreshModel();
214 }
215
tom89b63c52014-09-16 09:19:51 -0700216}