blob: 550710eeaa826203eda1d462ff6b3f8be20c9c05 [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;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053024import org.onosproject.net.flow.FlowRuleService;
25import org.onosproject.net.group.GroupService;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.host.HostAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.intent.Intent;
28import org.onosproject.net.intent.IntentService;
Hari Krishnaa9293632015-07-16 16:43:40 -070029import org.onosproject.net.intent.IntentState;
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070030import org.onosproject.net.link.LinkAdminService;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070031import org.onosproject.net.region.RegionAdminService;
32import org.onosproject.ui.UiTopoLayoutService;
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053033import java.util.EnumSet;
34import java.util.concurrent.CountDownLatch;
35import java.util.concurrent.TimeUnit;
36import static org.onosproject.net.intent.IntentState.FAILED;
37import static org.onosproject.net.intent.IntentState.WITHDRAWN;
tom89b63c52014-09-16 09:19:51 -070038
39/**
tome2555ff2014-10-07 18:47:58 -070040 * Wipes-out the entire network information base, i.e. devices, links, hosts, intents.
tom89b63c52014-09-16 09:19:51 -070041 */
42@Command(scope = "onos", name = "wipe-out",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070043 description = "Wipes-out the entire network information base, i.e. devices, links, hosts")
tom89b63c52014-09-16 09:19:51 -070044public class WipeOutCommand extends ClustersListCommand {
45
tom1679e182014-10-09 13:50:45 -070046 private static final String PLEASE = "please";
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053047 private static final EnumSet<IntentState> CAN_PURGE = EnumSet.of(WITHDRAWN, FAILED);
tom1679e182014-10-09 13:50:45 -070048 @Argument(index = 0, name = "please", description = "Confirmation phrase",
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070049 required = false, multiValued = false)
tom1679e182014-10-09 13:50:45 -070050 String please = null;
tom22288032014-10-07 08:16:53 -070051
tom89b63c52014-09-16 09:19:51 -070052 @Override
tom0872a172014-09-23 11:24:26 -070053 protected void execute() {
tom1679e182014-10-09 13:50:45 -070054 if (please == null || !please.equals(PLEASE)) {
55 print("I'm afraid I can't do that!\nSay: %s", PLEASE);
tom22288032014-10-07 08:16:53 -070056 return;
57 }
58
Thomas Vachuskad35f8472015-08-04 10:06:48 -070059 wipeOutIntents();
60 wipeOutHosts();
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053061 wipeOutFlows();
62 wipeOutGroups();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070063 wipeOutDevices();
64 wipeOutLinks();
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070065
66 wipeOutLayouts();
67 wipeOutRegions();
Thomas Vachuskad35f8472015-08-04 10:06:48 -070068 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070069
Thomas Vachuskad35f8472015-08-04 10:06:48 -070070 private void wipeOutIntents() {
71 print("Wiping intents");
72 IntentService intentService = get(IntentService.class);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053073 final CountDownLatch withdrawLatch;
74 withdrawLatch = new CountDownLatch(1);
Thomas Vachuskad35f8472015-08-04 10:06:48 -070075 for (Intent intent : intentService.getIntents()) {
76 if (intentService.getIntentState(intent.key()) != IntentState.WITHDRAWN) {
77 intentService.withdraw(intent);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053078 try { // wait for withdraw event
79 withdrawLatch.await(5, TimeUnit.SECONDS);
80 } catch (InterruptedException e) {
81 print("Timed out waiting for intent {} withdraw");
82 }
Thomas Vachuskaf1c42082015-07-10 16:41:31 -070083 }
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +053084 if (CAN_PURGE.contains(intentService.getIntentState(intent.key()))) {
85 intentService.purge(intent);
86 }
87 }
88 }
89
90 private void wipeOutFlows() {
91 print("Wiping Flows");
92 FlowRuleService flowRuleService = get(FlowRuleService.class);
93 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
94 for (Device device : deviceAdminService.getDevices()) {
95 flowRuleService.purgeFlowRules(device.id());
96 }
97 }
98
99 private void wipeOutGroups() {
100 print("Wiping groups");
101 GroupService groupService = get(GroupService.class);
102 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
103 for (Device device : deviceAdminService.getDevices()) {
104 groupService.purgeGroupEntries(device.id());
tom89b63c52014-09-16 09:19:51 -0700105 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700106 }
tom89b63c52014-09-16 09:19:51 -0700107
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700108 private void wipeOutHosts() {
tome2555ff2014-10-07 18:47:58 -0700109 print("Wiping hosts");
tom89b63c52014-09-16 09:19:51 -0700110 HostAdminService hostAdminService = get(HostAdminService.class);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700111 while (hostAdminService.getHostCount() > 0) {
112 try {
113 for (Host host : hostAdminService.getHosts()) {
114 hostAdminService.removeHost(host.id());
115 }
116 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700117 log.info("Unable to wipe-out hosts", e);
Thomas Vachuskaf1c42082015-07-10 16:41:31 -0700118 }
tom89b63c52014-09-16 09:19:51 -0700119 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700120 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700121
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700122 private void wipeOutDevices() {
123 print("Wiping devices");
124 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
125 while (deviceAdminService.getDeviceCount() > 0) {
126 try {
127 for (Device device : deviceAdminService.getDevices()) {
128 deviceAdminService.removeDevice(device.id());
129 }
130 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700131 log.info("Unable to wipe-out devices", e);
Hari Krishnaa9293632015-07-16 16:43:40 -0700132 }
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700133 }
134 }
135
136 private void wipeOutLinks() {
137 print("Wiping links");
138 LinkAdminService linkAdminService = get(LinkAdminService.class);
139 while (linkAdminService.getLinkCount() > 0) {
140 try {
141 for (Link link : linkAdminService.getLinks()) {
142 linkAdminService.removeLinks(link.src());
143 linkAdminService.removeLinks(link.dst());
144 }
145 } catch (Exception e) {
Ray Milkeyab87ac42016-08-26 13:13:19 -0700146 log.info("Unable to wipe-out links", e);
Thomas Vachuskad35f8472015-08-04 10:06:48 -0700147 }
Brian O'Connor958d3812014-10-03 19:46:23 -0700148 }
tom89b63c52014-09-16 09:19:51 -0700149 }
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700150
151 private void wipeOutLayouts() {
152 print("Wiping UI layouts");
153 UiTopoLayoutService service = get(UiTopoLayoutService.class);
Simon Huntb1ce2602016-07-23 14:04:31 -0700154 // wipe out all layouts except the default, which should always be there
155 service.getLayouts().forEach(l -> {
156 if (!l.id().isDefault()) {
157 service.removeLayout(l);
158 }
159 });
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -0700160 }
161
162 private void wipeOutRegions() {
163 print("Wiping regions");
164 RegionAdminService service = get(RegionAdminService.class);
165 service.getRegions().forEach(r -> service.removeRegion(r.id()));
166 }
tom89b63c52014-09-16 09:19:51 -0700167}