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