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