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; |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 24 | import org.onosproject.net.flow.FlowRuleService; |
| 25 | import org.onosproject.net.group.GroupService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.host.HostAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.Intent; |
| 28 | import org.onosproject.net.intent.IntentService; |
Hari Krishna | a929363 | 2015-07-16 16:43:40 -0700 | [diff] [blame] | 29 | import org.onosproject.net.intent.IntentState; |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 30 | import org.onosproject.net.link.LinkAdminService; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 31 | import org.onosproject.net.region.RegionAdminService; |
| 32 | import org.onosproject.ui.UiTopoLayoutService; |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 33 | import java.util.EnumSet; |
| 34 | import java.util.concurrent.CountDownLatch; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 37 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 40 | * Wipes-out the entire network information base, i.e. devices, links, hosts, intents. |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 41 | */ |
| 42 | @Command(scope = "onos", name = "wipe-out", |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 43 | description = "Wipes-out the entire network information base, i.e. devices, links, hosts") |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 44 | public class WipeOutCommand extends ClustersListCommand { |
| 45 | |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 46 | private static final String PLEASE = "please"; |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 47 | private static final EnumSet<IntentState> CAN_PURGE = EnumSet.of(WITHDRAWN, FAILED); |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 48 | @Argument(index = 0, name = "please", description = "Confirmation phrase", |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 49 | required = false, multiValued = false) |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 50 | String please = null; |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 51 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 52 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 53 | protected void execute() { |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 54 | if (please == null || !please.equals(PLEASE)) { |
| 55 | print("I'm afraid I can't do that!\nSay: %s", PLEASE); |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 59 | wipeOutIntents(); |
| 60 | wipeOutHosts(); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 61 | wipeOutFlows(); |
| 62 | wipeOutGroups(); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 63 | wipeOutDevices(); |
| 64 | wipeOutLinks(); |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 65 | |
| 66 | wipeOutLayouts(); |
| 67 | wipeOutRegions(); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 68 | } |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 69 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 70 | private void wipeOutIntents() { |
| 71 | print("Wiping intents"); |
| 72 | IntentService intentService = get(IntentService.class); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 73 | final CountDownLatch withdrawLatch; |
| 74 | withdrawLatch = new CountDownLatch(1); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 75 | for (Intent intent : intentService.getIntents()) { |
| 76 | if (intentService.getIntentState(intent.key()) != IntentState.WITHDRAWN) { |
| 77 | intentService.withdraw(intent); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 78 | 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 Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 83 | } |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 84 | 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()); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 105 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 106 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 107 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 108 | private void wipeOutHosts() { |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 109 | print("Wiping hosts"); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 110 | HostAdminService hostAdminService = get(HostAdminService.class); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 111 | while (hostAdminService.getHostCount() > 0) { |
| 112 | try { |
| 113 | for (Host host : hostAdminService.getHosts()) { |
| 114 | hostAdminService.removeHost(host.id()); |
| 115 | } |
| 116 | } catch (Exception e) { |
Ray Milkey | ab87ac4 | 2016-08-26 13:13:19 -0700 | [diff] [blame] | 117 | log.info("Unable to wipe-out hosts", e); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 118 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 119 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 120 | } |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 121 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 122 | 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 Milkey | ab87ac4 | 2016-08-26 13:13:19 -0700 | [diff] [blame] | 131 | log.info("Unable to wipe-out devices", e); |
Hari Krishna | a929363 | 2015-07-16 16:43:40 -0700 | [diff] [blame] | 132 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 133 | } |
| 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 Milkey | ab87ac4 | 2016-08-26 13:13:19 -0700 | [diff] [blame] | 146 | log.info("Unable to wipe-out links", e); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 147 | } |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 148 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 149 | } |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 150 | |
| 151 | private void wipeOutLayouts() { |
| 152 | print("Wiping UI layouts"); |
| 153 | UiTopoLayoutService service = get(UiTopoLayoutService.class); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 154 | // 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 Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 160 | } |
| 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 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 167 | } |