blob: 61ea43dea2a39ffcb9ed66c2cb18b415a1d2bcaf [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;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070029import org.onosproject.net.region.RegionAdminService;
30import org.onosproject.ui.UiTopoLayoutService;
tom89b63c52014-09-16 09:19:51 -070031
32/**
tome2555ff2014-10-07 18:47:58 -070033 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070034 */
35@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070036 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
tom89b63c52014-09-16 09:19:51 -070037public class WipeOutCommand extends ClustersListCommand {
38
tom1679e182014-10-09 13:50:45 -070039 private static final String PLEASE = "please";
tom22288032014-10-07 08:16:53 -070040
tom1679e182014-10-09 13:50:45 -070041 @Argument(index = 0, name = "please", description = "Confirmation phrase",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070042 required = false, multiValued = false)
tom1679e182014-10-09 13:50:45 -070043 String please = null;
tom22288032014-10-07 08:16:53 -070044
tom89b63c52014-09-16 09:19:51 -070045 @Override
tom0872a172014-09-23 11:24:26 -070046 protected void execute() {
tom1679e182014-10-09 13:50:45 -070047 if (please == null || !please.equals(PLEASE)) {
48 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070049 return;
50 }
51
Thomas Vachuskad35f8472015-08-04 10:06:48 -070052 wipeOutIntents();
53 wipeOutHosts();
54 wipeOutDevices();
55 wipeOutLinks();
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070056
57 wipeOutLayouts();
58 wipeOutRegions();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070059 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070060
Thomas Vachuskad35f8472015-08-04 10:06:48 -070061 private void wipeOutIntents() {
62 print("Wiping intents");
63 IntentService intentService = get(IntentService.class);
64 for (Intent intent : intentService.getIntents()) {
65 if (intentService.getIntentState(intent.key()) != IntentState.WITHDRAWN) {
66 intentService.withdraw(intent);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070067 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070068 intentService.purge(intent);
tom89b63c52014-09-16 09:19:51 -070069 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070070 }
tom89b63c52014-09-16 09:19:51 -070071
Thomas Vachuskad35f8472015-08-04 10:06:48 -070072 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -070073 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -070074 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070075 while (hostAdminService.getHostCount() > 0) {
76 try {
77 for (Host host : hostAdminService.getHosts()) {
78 hostAdminService.removeHost(host.id());
79 }
80 } catch (Exception e) {
81 log.warn("Unable to wipe-out hosts", e);
82 }
tom89b63c52014-09-16 09:19:51 -070083 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070084 }
Brian O'Connor958d3812014-10-03 19:46:23 -070085
Thomas Vachuskad35f8472015-08-04 10:06:48 -070086 private void wipeOutDevices() {
87 print("Wiping devices");
88 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
89 while (deviceAdminService.getDeviceCount() > 0) {
90 try {
91 for (Device device : deviceAdminService.getDevices()) {
92 deviceAdminService.removeDevice(device.id());
93 }
94 } catch (Exception e) {
95 log.warn("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -070096 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -070097 }
98 }
99
100 private void wipeOutLinks() {
101 print("Wiping links");
102 LinkAdminService linkAdminService = get(LinkAdminService.class);
103 while (linkAdminService.getLinkCount() > 0) {
104 try {
105 for (Link link : linkAdminService.getLinks()) {
106 linkAdminService.removeLinks(link.src());
107 linkAdminService.removeLinks(link.dst());
108 }
109 } catch (Exception e) {
110 log.warn("Unable to wipe-out links", e);
111 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700112 }
tom89b63c52014-09-16 09:19:51 -0700113 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700114
115 private void wipeOutLayouts() {
116 print("Wiping UI layouts");
117 UiTopoLayoutService service = get(UiTopoLayoutService.class);
Simon Huntb1ce2602016-07-23 14:04:31 -0700118 // wipe out all layouts except the default, which should always be there
119 service.getLayouts().forEach(l -> {
120 if (!l.id().isDefault()) {
121 service.removeLayout(l);
122 }
123 });
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700124 }
125
126 private void wipeOutRegions() {
127 print("Wiping regions");
128 RegionAdminService service = get(RegionAdminService.class);
129 service.getRegions().forEach(r -> service.removeRegion(r.id()));
130 }
tom89b63c52014-09-16 09:19:51 -0700131}