blob: f64a304de78a3b28c73f27939e95de5382b755eb [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Hari Krishnaa9293632015-07-16 16:43:40 -070027import org.onosproject.net.intent.IntentState;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070028import org.onosproject.net.link.LinkAdminService;
tom89b63c52014-09-16 09:19:51 -070029
30/**
tome2555ff2014-10-07 18:47:58 -070031 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070032 */
33@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070034 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
tom89b63c52014-09-16 09:19:51 -070035public class WipeOutCommand extends ClustersListCommand {
36
tom1679e182014-10-09 13:50:45 -070037 private static final String PLEASE = "please";
tom22288032014-10-07 08:16:53 -070038
tom1679e182014-10-09 13:50:45 -070039 @Argument(index = 0, name = "please", description = "Confirmation phrase",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070040 required = false, multiValued = false)
tom1679e182014-10-09 13:50:45 -070041 String please = null;
tom22288032014-10-07 08:16:53 -070042
tom89b63c52014-09-16 09:19:51 -070043 @Override
tom0872a172014-09-23 11:24:26 -070044 protected void execute() {
tom1679e182014-10-09 13:50:45 -070045 if (please == null || !please.equals(PLEASE)) {
46 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070047 return;
48 }
49
Thomas Vachuskad35f8472015-08-04 10:06:48 -070050 wipeOutIntents();
51 wipeOutHosts();
52 wipeOutDevices();
53 wipeOutLinks();
54 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070055
Thomas Vachuskad35f8472015-08-04 10:06:48 -070056 private void wipeOutIntents() {
57 print("Wiping intents");
58 IntentService intentService = get(IntentService.class);
59 for (Intent intent : intentService.getIntents()) {
60 if (intentService.getIntentState(intent.key()) != IntentState.WITHDRAWN) {
61 intentService.withdraw(intent);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070062 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070063 intentService.purge(intent);
tom89b63c52014-09-16 09:19:51 -070064 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070065 }
tom89b63c52014-09-16 09:19:51 -070066
Thomas Vachuskad35f8472015-08-04 10:06:48 -070067 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -070068 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -070069 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070070 while (hostAdminService.getHostCount() > 0) {
71 try {
72 for (Host host : hostAdminService.getHosts()) {
73 hostAdminService.removeHost(host.id());
74 }
75 } catch (Exception e) {
76 log.warn("Unable to wipe-out hosts", e);
77 }
tom89b63c52014-09-16 09:19:51 -070078 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070079 }
Brian O'Connor958d3812014-10-03 19:46:23 -070080
Thomas Vachuskad35f8472015-08-04 10:06:48 -070081 private void wipeOutDevices() {
82 print("Wiping devices");
83 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
84 while (deviceAdminService.getDeviceCount() > 0) {
85 try {
86 for (Device device : deviceAdminService.getDevices()) {
87 deviceAdminService.removeDevice(device.id());
88 }
89 } catch (Exception e) {
90 log.warn("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -070091 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070092 }
93 }
94
95 private void wipeOutLinks() {
96 print("Wiping links");
97 LinkAdminService linkAdminService = get(LinkAdminService.class);
98 while (linkAdminService.getLinkCount() > 0) {
99 try {
100 for (Link link : linkAdminService.getLinks()) {
101 linkAdminService.removeLinks(link.src());
102 linkAdminService.removeLinks(link.dst());
103 }
104 } catch (Exception e) {
105 log.warn("Unable to wipe-out links", e);
106 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700107 }
tom89b63c52014-09-16 09:19:51 -0700108 }
tom89b63c52014-09-16 09:19:51 -0700109}