Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 22 | import org.onlab.util.Tools; |
Yuta HIGUCHI | d811980 | 2017-12-20 13:59:38 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.Host; |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Link; |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 27 | import org.onosproject.net.config.NetworkConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceAdminService; |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 29 | import org.onosproject.net.flow.FlowRuleService; |
| 30 | import org.onosproject.net.group.GroupService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.host.HostAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.Intent; |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 33 | import org.onosproject.net.intent.IntentEvent; |
| 34 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.IntentService; |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 36 | import org.onosproject.net.intent.Key; |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 37 | import org.onosproject.net.link.LinkAdminService; |
Andrea Campanella | 2325050 | 2020-05-13 15:36:57 +0200 | [diff] [blame] | 38 | import org.onosproject.net.meter.MeterService; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 39 | import org.onosproject.net.region.RegionAdminService; |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 40 | import org.onosproject.ui.UiExtensionService; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 41 | import org.onosproject.ui.UiTopoLayoutService; |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 42 | |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 43 | import java.util.Set; |
| 44 | import java.util.concurrent.CompletableFuture; |
| 45 | import java.util.concurrent.ExecutionException; |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 46 | import java.util.concurrent.TimeUnit; |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 47 | import java.util.concurrent.TimeoutException; |
| 48 | import java.util.stream.Collectors; |
| 49 | |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 50 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 53 | * Wipes-out the entire network information base, i.e. devices, links, hosts, intents. |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 54 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Service |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 56 | @Command(scope = "onos", name = "wipe-out", |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 57 | description = "Wipes-out the entire network information base, i.e. devices, links, hosts") |
Yuta HIGUCHI | d811980 | 2017-12-20 13:59:38 -0800 | [diff] [blame] | 58 | public class WipeOutCommand extends AbstractShellCommand { |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 59 | |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 60 | private static final String PLEASE = "please"; |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 61 | @Argument(name = "please", description = "Confirmation phrase") |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 62 | String please = null; |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 63 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 64 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | protected void doExecute() { |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 66 | if (please == null || !please.equals(PLEASE)) { |
| 67 | print("I'm afraid I can't do that!\nSay: %s", PLEASE); |
tom | 2228803 | 2014-10-07 08:16:53 -0700 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 71 | wipeOutIntents(); |
| 72 | wipeOutHosts(); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 73 | wipeOutFlows(); |
| 74 | wipeOutGroups(); |
Andrea Campanella | 2325050 | 2020-05-13 15:36:57 +0200 | [diff] [blame] | 75 | wipeOutMeters(); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 76 | wipeOutDevices(); |
| 77 | wipeOutLinks(); |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 78 | wipeOutNetworkConfig(); |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 79 | |
| 80 | wipeOutLayouts(); |
| 81 | wipeOutRegions(); |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 82 | wipeOutUiCache(); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 83 | } |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 84 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 85 | private void wipeOutIntents() { |
| 86 | print("Wiping intents"); |
| 87 | IntentService intentService = get(IntentService.class); |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 88 | Set<Key> keysToWithdrawn = Sets.newConcurrentHashSet(); |
| 89 | Set<Intent> intentsToWithdrawn = Tools.stream(intentService.getIntents()) |
| 90 | .filter(intent -> intentService.getIntentState(intent.key()) != WITHDRAWN) |
| 91 | .collect(Collectors.toSet()); |
| 92 | intentsToWithdrawn.stream() |
| 93 | .map(Intent::key) |
| 94 | .forEach(keysToWithdrawn::add); |
| 95 | CompletableFuture<Void> completableFuture = new CompletableFuture<>(); |
| 96 | IntentListener listener = e -> { |
| 97 | if (e.type() == IntentEvent.Type.WITHDRAWN) { |
| 98 | keysToWithdrawn.remove(e.subject().key()); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 99 | } |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 100 | if (keysToWithdrawn.isEmpty()) { |
| 101 | completableFuture.complete(null); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 102 | } |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 103 | }; |
| 104 | intentService.addListener(listener); |
| 105 | intentsToWithdrawn.forEach(intentService::withdraw); |
| 106 | try { |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 107 | if (!intentsToWithdrawn.isEmpty()) { |
| 108 | // Wait 1.5 seconds for each Intent |
| 109 | completableFuture.get(intentsToWithdrawn.size() * 1500L, TimeUnit.MILLISECONDS); |
| 110 | } |
| 111 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 112 | print("Encountered exception while withdrawing intents: " + e.toString()); |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 113 | } finally { |
| 114 | intentService.removeListener(listener); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 115 | } |
Yi Tseng | 46f3edd | 2017-05-26 15:14:53 -0700 | [diff] [blame] | 116 | intentsToWithdrawn.forEach(intentService::purge); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private void wipeOutFlows() { |
| 120 | print("Wiping Flows"); |
| 121 | FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 122 | DeviceAdminService deviceAdminService = get(DeviceAdminService.class); |
| 123 | for (Device device : deviceAdminService.getDevices()) { |
| 124 | flowRuleService.purgeFlowRules(device.id()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private void wipeOutGroups() { |
| 129 | print("Wiping groups"); |
| 130 | GroupService groupService = get(GroupService.class); |
| 131 | DeviceAdminService deviceAdminService = get(DeviceAdminService.class); |
| 132 | for (Device device : deviceAdminService.getDevices()) { |
| 133 | groupService.purgeGroupEntries(device.id()); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 134 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 135 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 136 | |
Andrea Campanella | 2325050 | 2020-05-13 15:36:57 +0200 | [diff] [blame] | 137 | private void wipeOutMeters() { |
| 138 | print("Wiping meters"); |
| 139 | MeterService meterService = get(MeterService.class); |
| 140 | DeviceAdminService deviceAdminService = get(DeviceAdminService.class); |
| 141 | for (Device device : deviceAdminService.getDevices()) { |
| 142 | meterService.purgeMeters(device.id()); |
| 143 | } |
| 144 | } |
| 145 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 146 | private void wipeOutHosts() { |
tom | e2555ff | 2014-10-07 18:47:58 -0700 | [diff] [blame] | 147 | print("Wiping hosts"); |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 148 | HostAdminService hostAdminService = get(HostAdminService.class); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 149 | while (hostAdminService.getHostCount() > 0) { |
| 150 | try { |
| 151 | for (Host host : hostAdminService.getHosts()) { |
| 152 | hostAdminService.removeHost(host.id()); |
| 153 | } |
| 154 | } catch (Exception e) { |
Ray Milkey | ab87ac4 | 2016-08-26 13:13:19 -0700 | [diff] [blame] | 155 | log.info("Unable to wipe-out hosts", e); |
Thomas Vachuska | f1c4208 | 2015-07-10 16:41:31 -0700 | [diff] [blame] | 156 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 157 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 158 | } |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 159 | |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 160 | private void wipeOutDevices() { |
| 161 | print("Wiping devices"); |
| 162 | DeviceAdminService deviceAdminService = get(DeviceAdminService.class); |
| 163 | while (deviceAdminService.getDeviceCount() > 0) { |
| 164 | try { |
| 165 | for (Device device : deviceAdminService.getDevices()) { |
| 166 | deviceAdminService.removeDevice(device.id()); |
| 167 | } |
| 168 | } catch (Exception e) { |
Ray Milkey | ab87ac4 | 2016-08-26 13:13:19 -0700 | [diff] [blame] | 169 | log.info("Unable to wipe-out devices", e); |
Hari Krishna | a929363 | 2015-07-16 16:43:40 -0700 | [diff] [blame] | 170 | } |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | private void wipeOutLinks() { |
| 175 | print("Wiping links"); |
| 176 | LinkAdminService linkAdminService = get(LinkAdminService.class); |
| 177 | while (linkAdminService.getLinkCount() > 0) { |
| 178 | try { |
| 179 | for (Link link : linkAdminService.getLinks()) { |
| 180 | linkAdminService.removeLinks(link.src()); |
| 181 | linkAdminService.removeLinks(link.dst()); |
| 182 | } |
| 183 | } catch (Exception e) { |
Ray Milkey | ab87ac4 | 2016-08-26 13:13:19 -0700 | [diff] [blame] | 184 | log.info("Unable to wipe-out links", e); |
Thomas Vachuska | d35f847 | 2015-08-04 10:06:48 -0700 | [diff] [blame] | 185 | } |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 186 | } |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 187 | } |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 188 | |
| 189 | private void wipeOutLayouts() { |
| 190 | print("Wiping UI layouts"); |
| 191 | UiTopoLayoutService service = get(UiTopoLayoutService.class); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 192 | // wipe out all layouts except the default, which should always be there |
| 193 | service.getLayouts().forEach(l -> { |
| 194 | if (!l.id().isDefault()) { |
| 195 | service.removeLayout(l); |
| 196 | } |
| 197 | }); |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | private void wipeOutRegions() { |
| 201 | print("Wiping regions"); |
| 202 | RegionAdminService service = get(RegionAdminService.class); |
| 203 | service.getRegions().forEach(r -> service.removeRegion(r.id())); |
| 204 | } |
Thomas Vachuska | 1b1355d | 2018-02-06 16:53:58 -0800 | [diff] [blame] | 205 | |
| 206 | private void wipeOutNetworkConfig() { |
| 207 | print("Wiping network configs"); |
| 208 | get(NetworkConfigService.class).removeConfig(); |
| 209 | } |
| 210 | |
| 211 | private void wipeOutUiCache() { |
| 212 | print("Wiping ui model cache"); |
| 213 | get(UiExtensionService.class).refreshModel(); |
| 214 | } |
| 215 | |
tom | 89b63c5 | 2014-09-16 09:19:51 -0700 | [diff] [blame] | 216 | } |