blob: cbb98425f7f1f9b8341e00cd189dc53b7fcba940 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
tom22288032014-10-07 08:16:53 -070018import org.apache.karaf.shell.commands.Argument;
tom89b63c52014-09-16 09:19:51 -070019import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.Device;
21import org.onosproject.net.Host;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070022import org.onosproject.net.Link;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.host.HostAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.IntentService;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070027import org.onosproject.net.link.LinkAdminService;
tom89b63c52014-09-16 09:19:51 -070028
29/**
tome2555ff2014-10-07 18:47:58 -070030 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070031 */
32@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070033 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
tom89b63c52014-09-16 09:19:51 -070034public class WipeOutCommand extends ClustersListCommand {
35
tom1679e182014-10-09 13:50:45 -070036 private static final String PLEASE = "please";
tom22288032014-10-07 08:16:53 -070037
tom1679e182014-10-09 13:50:45 -070038 @Argument(index = 0, name = "please", description = "Confirmation phrase",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070039 required = false, multiValued = false)
tom1679e182014-10-09 13:50:45 -070040 String please = null;
tom22288032014-10-07 08:16:53 -070041
tom89b63c52014-09-16 09:19:51 -070042 @Override
tom0872a172014-09-23 11:24:26 -070043 protected void execute() {
tom1679e182014-10-09 13:50:45 -070044 if (please == null || !please.equals(PLEASE)) {
45 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070046 return;
47 }
48
tome2555ff2014-10-07 18:47:58 -070049 print("Wiping devices");
tom89b63c52014-09-16 09:19:51 -070050 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070051 while (deviceAdminService.getDeviceCount() > 0) {
52 try {
53 for (Device device : deviceAdminService.getDevices()) {
54 deviceAdminService.removeDevice(device.id());
55 }
56 } catch (Exception e) {
57 log.warn("Unable to wipe-out devices", e);
58 }
59 }
60
61 print("Wiping links");
62 LinkAdminService linkAdminService = get(LinkAdminService.class);
63 while (linkAdminService.getLinkCount() > 0) {
64 try {
65 for (Link link : linkAdminService.getLinks()) {
66 linkAdminService.removeLinks(link.src());
67 linkAdminService.removeLinks(link.dst());
68 }
69 } catch (Exception e) {
70 log.warn("Unable to wipe-out links", e);
71 }
tom89b63c52014-09-16 09:19:51 -070072 }
73
tome2555ff2014-10-07 18:47:58 -070074 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -070075 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070076 while (hostAdminService.getHostCount() > 0) {
77 try {
78 for (Host host : hostAdminService.getHosts()) {
79 hostAdminService.removeHost(host.id());
80 }
81 } catch (Exception e) {
82 log.warn("Unable to wipe-out hosts", e);
83 }
tom89b63c52014-09-16 09:19:51 -070084 }
Brian O'Connor958d3812014-10-03 19:46:23 -070085
tome2555ff2014-10-07 18:47:58 -070086 print("Wiping intents");
Brian O'Connor958d3812014-10-03 19:46:23 -070087 IntentService intentService = get(IntentService.class);
88 for (Intent intent : intentService.getIntents()) {
Thomas Vachuska18046d42015-07-14 09:17:26 -070089 intentService.withdraw(intent);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070090 intentService.purge(intent);
Brian O'Connor958d3812014-10-03 19:46:23 -070091 }
tom89b63c52014-09-16 09:19:51 -070092 }
tom89b63c52014-09-16 09:19:51 -070093}