Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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; |
Hari Krishna | a929363 | 2015-07-16 16:43:40 -0700 | [diff] [blame] | 27 | import org.onosproject.net.intent.IntentState; |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 28 | import org.onosproject.net.link.LinkAdminService; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 29 | import org.onosproject.net.region.RegionAdminService; |
| 30 | import org.onosproject.ui.UiTopoLayoutService; |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 33 | * Wipes-out the entire network information base, i.e. devices, links, hosts, intents. |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 34 | */ |
| 35 | @Command(scope = "onos", name = "wipe-out", |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 36 | description = "Wipes-out the entire network information base, i.e. devices, links, hosts") |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 37 | public class WipeOutCommand extends ClustersListCommand { |
| 38 | |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 39 | private static final String PLEASE = "please"; |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 40 | |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 41 | @Argument(index = 0, name = "please", description = "Confirmation phrase", |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 42 | required = false, multiValued = false) |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 43 | String please = null; |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 44 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 45 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 46 | protected void execute() { |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 47 | if (please == null || !please.equals(PLEASE)) { |
| 48 | print("I'm afraid I can't do that!\nSay: %s", PLEASE); |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 52 | wipeOutIntents(); |
| 53 | wipeOutHosts(); |
| 54 | wipeOutDevices(); |
| 55 | wipeOutLinks(); |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 56 | |
| 57 | wipeOutLayouts(); |
| 58 | wipeOutRegions(); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 59 | } |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 60 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 61 | 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 Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 67 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 68 | intentService.purge(intent); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 69 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 70 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 71 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 72 | private void wipeOutHosts() { |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 73 | print("Wiping hosts"); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 74 | HostAdminService hostAdminService = get(HostAdminService.class); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 75 | 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 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 83 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 84 | } |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 85 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 86 | 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 Krishna | a929363 | 2015-07-16 16:43:40 -0700 | [diff] [blame] | 96 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 97 | } |
| 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'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 112 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 113 | } |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 114 | |
| 115 | private void wipeOutLayouts() { |
| 116 | print("Wiping UI layouts"); |
| 117 | UiTopoLayoutService service = get(UiTopoLayoutService.class); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame^] | 118 | // 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 Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 124 | } |
| 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 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 131 | } |