Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 17 | |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.commands.Argument; |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.Host; |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Link; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.device.DeviceAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.host.HostAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.Intent; |
| 26 | import org.onosproject.net.intent.IntentService; |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 27 | import org.onosproject.net.link.LinkAdminService; |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 30 | * Wipes-out the entire network information base, i.e. devices, links, hosts, intents. |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 31 | */ |
| 32 | @Command(scope = "onos", name = "wipe-out", |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 33 | description = "Wipes-out the entire network information base, i.e. devices, links, hosts") |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 34 | public class WipeOutCommand extends ClustersListCommand { |
| 35 | |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 36 | private static final String PLEASE = "please"; |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 37 | |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 38 | @Argument(index = 0, name = "please", description = "Confirmation phrase", |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 39 | required = false, multiValued = false) |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 40 | String please = null; |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 41 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 42 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 43 | protected void execute() { |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 44 | if (please == null || !please.equals(PLEASE)) { |
| 45 | print("I'm afraid I can't do that!\nSay: %s", PLEASE); |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 49 | print("Wiping devices"); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 50 | DeviceAdminService deviceAdminService = get(DeviceAdminService.class); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 51 | 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 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 72 | } |
| 73 | |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 74 | print("Wiping hosts"); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 75 | HostAdminService hostAdminService = get(HostAdminService.class); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 76 | 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 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 84 | } |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 85 | |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 86 | print("Wiping intents"); |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 87 | IntentService intentService = get(IntentService.class); |
| 88 | for (Intent intent : intentService.getIntents()) { |
Thomas Vachuska | 18046d4 | 2015-07-14 09:17:26 -0700 | [diff] [blame] | 89 | intentService.withdraw(intent); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 90 | intentService.purge(intent); |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 91 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 92 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 93 | } |