blob: 8d510547d688f5b5f16a2caf829178725f62cd4c [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 Campanella23250502020-05-13 15:36:57 +020038import org.onosproject.net.meter.MeterService;
Danny Chang1a0e18932020-08-20 11:53:30 +080039import org.onosproject.net.packet.PacketService;
40import org.onosproject.net.packet.PacketRequest;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070041import org.onosproject.net.region.RegionAdminService;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080042import org.onosproject.ui.UiExtensionService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070043import org.onosproject.ui.UiTopoLayoutService;
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080044
Yi Tseng46f3edd2017-05-26 15:14:53 -070045import java.util.Set;
46import java.util.concurrent.CompletableFuture;
47import java.util.concurrent.ExecutionException;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053048import java.util.concurrent.TimeUnit;
Yi Tseng46f3edd2017-05-26 15:14:53 -070049import java.util.concurrent.TimeoutException;
50import java.util.stream.Collectors;
51
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053052import static org.onosproject.net.intent.IntentState.WITHDRAWN;
tom89b63c52014-09-16 09:19:51 -070053
54/**
tome2555ff2014-10-07 18:47:58 -070055 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070056 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057@Service
tom89b63c52014-09-16 09:19:51 -070058@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070059 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
Yuta HIGUCHId8119802017-12-20 13:59:38 -080060public class WipeOutCommand extends AbstractShellCommand {
tom89b63c52014-09-16 09:19:51 -070061
tom1679e182014-10-09 13:50:45 -070062 private static final String PLEASE = "please";
Yi Tseng46f3edd2017-05-26 15:14:53 -070063 @Argument(name = "please", description = "Confirmation phrase")
tom1679e182014-10-09 13:50:45 -070064 String please = null;
tom22288032014-10-07 08:16:53 -070065
tom89b63c52014-09-16 09:19:51 -070066 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 protected void doExecute() {
tom1679e182014-10-09 13:50:45 -070068 if (please == null || !please.equals(PLEASE)) {
69 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070070 return;
71 }
72
Thomas Vachuskad35f8472015-08-04 10:06:48 -070073 wipeOutIntents();
74 wipeOutHosts();
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053075 wipeOutFlows();
76 wipeOutGroups();
Andrea Campanella23250502020-05-13 15:36:57 +020077 wipeOutMeters();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070078 wipeOutDevices();
79 wipeOutLinks();
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080080 wipeOutNetworkConfig();
Danny Chang1a0e18932020-08-20 11:53:30 +080081 wipeOutPacketRequests();
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070082
83 wipeOutLayouts();
84 wipeOutRegions();
Thomas Vachuska1b1355d2018-02-06 16:53:58 -080085 wipeOutUiCache();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070086 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070087
Thomas Vachuskad35f8472015-08-04 10:06:48 -070088 private void wipeOutIntents() {
89 print("Wiping intents");
90 IntentService intentService = get(IntentService.class);
Yi Tseng46f3edd2017-05-26 15:14:53 -070091 Set<Key> keysToWithdrawn = Sets.newConcurrentHashSet();
92 Set<Intent> intentsToWithdrawn = Tools.stream(intentService.getIntents())
93 .filter(intent -> intentService.getIntentState(intent.key()) != WITHDRAWN)
94 .collect(Collectors.toSet());
95 intentsToWithdrawn.stream()
96 .map(Intent::key)
97 .forEach(keysToWithdrawn::add);
98 CompletableFuture<Void> completableFuture = new CompletableFuture<>();
99 IntentListener listener = e -> {
100 if (e.type() == IntentEvent.Type.WITHDRAWN) {
101 keysToWithdrawn.remove(e.subject().key());
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700102 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700103 if (keysToWithdrawn.isEmpty()) {
104 completableFuture.complete(null);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530105 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700106 };
107 intentService.addListener(listener);
108 intentsToWithdrawn.forEach(intentService::withdraw);
109 try {
Thomas Vachuska1b1355d2018-02-06 16:53:58 -0800110 if (!intentsToWithdrawn.isEmpty()) {
111 // Wait 1.5 seconds for each Intent
112 completableFuture.get(intentsToWithdrawn.size() * 1500L, TimeUnit.MILLISECONDS);
113 }
114 } catch (InterruptedException | ExecutionException | TimeoutException e) {
115 print("Encountered exception while withdrawing intents: " + e.toString());
Yi Tseng46f3edd2017-05-26 15:14:53 -0700116 } finally {
117 intentService.removeListener(listener);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530118 }
Yi Tseng46f3edd2017-05-26 15:14:53 -0700119 intentsToWithdrawn.forEach(intentService::purge);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530120 }
121
122 private void wipeOutFlows() {
123 print("Wiping Flows");
124 FlowRuleService flowRuleService = get(FlowRuleService.class);
125 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
126 for (Device device : deviceAdminService.getDevices()) {
127 flowRuleService.purgeFlowRules(device.id());
128 }
129 }
130
131 private void wipeOutGroups() {
132 print("Wiping groups");
133 GroupService groupService = get(GroupService.class);
134 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
135 for (Device device : deviceAdminService.getDevices()) {
136 groupService.purgeGroupEntries(device.id());
tom89b63c52014-09-16 09:19:51 -0700137 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700138 }
tom89b63c52014-09-16 09:19:51 -0700139
Andrea Campanella23250502020-05-13 15:36:57 +0200140 private void wipeOutMeters() {
141 print("Wiping meters");
142 MeterService meterService = get(MeterService.class);
143 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
144 for (Device device : deviceAdminService.getDevices()) {
145 meterService.purgeMeters(device.id());
146 }
147 }
148
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700149 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -0700150 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -0700151 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700152 while (hostAdminService.getHostCount() > 0) {
153 try {
154 for (Host host : hostAdminService.getHosts()) {
155 hostAdminService.removeHost(host.id());
156 }
157 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700158 log.info("Unable to wipe-out hosts", e);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700159 }
tom89b63c52014-09-16 09:19:51 -0700160 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700161 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700162
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700163 private void wipeOutDevices() {
164 print("Wiping devices");
165 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
166 while (deviceAdminService.getDeviceCount() > 0) {
167 try {
168 for (Device device : deviceAdminService.getDevices()) {
169 deviceAdminService.removeDevice(device.id());
170 }
171 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700172 log.info("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -0700173 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700174 }
175 }
176
177 private void wipeOutLinks() {
178 print("Wiping links");
179 LinkAdminService linkAdminService = get(LinkAdminService.class);
180 while (linkAdminService.getLinkCount() > 0) {
181 try {
182 for (Link link : linkAdminService.getLinks()) {
183 linkAdminService.removeLinks(link.src());
184 linkAdminService.removeLinks(link.dst());
185 }
186 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700187 log.info("Unable to wipe-out links", e);
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700188 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700189 }
tom89b63c52014-09-16 09:19:51 -0700190 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700191
Danny Chang1a0e18932020-08-20 11:53:30 +0800192 private void wipeOutPacketRequests() {
193 print("Wiping packet requests");
194 PacketService service = get(PacketService.class);
195 for (PacketRequest r : service.getRequests()) {
196 service.cancelPackets(r.selector(), r.priority(), r.appId(), r.deviceId());
197 }
198 }
199
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700200 private void wipeOutLayouts() {
201 print("Wiping UI layouts");
202 UiTopoLayoutService service = get(UiTopoLayoutService.class);
Simon Huntb1ce2602016-07-23 14:04:31 -0700203 // wipe out all layouts except the default, which should always be there
204 service.getLayouts().forEach(l -> {
205 if (!l.id().isDefault()) {
206 service.removeLayout(l);
207 }
208 });
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700209 }
210
211 private void wipeOutRegions() {
212 print("Wiping regions");
213 RegionAdminService service = get(RegionAdminService.class);
214 service.getRegions().forEach(r -> service.removeRegion(r.id()));
215 }
Thomas Vachuska1b1355d2018-02-06 16:53:58 -0800216
217 private void wipeOutNetworkConfig() {
218 print("Wiping network configs");
219 get(NetworkConfigService.class).removeConfig();
220 }
221
222 private void wipeOutUiCache() {
223 print("Wiping ui model cache");
224 get(UiExtensionService.class).refreshModel();
225 }
226
tom89b63c52014-09-16 09:19:51 -0700227}