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; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 17 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.Intent; |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 26 | import org.onosproject.net.intent.IntentEvent; |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 27 | import org.onosproject.net.intent.IntentListener; |
| 28 | import org.onosproject.net.intent.IntentService; |
| 29 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.Key; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 31 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 32 | import java.io.BufferedReader; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStreamReader; |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 35 | import java.math.BigInteger; |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 36 | import java.util.EnumSet; |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 37 | import java.util.Objects; |
| 38 | import java.util.concurrent.CountDownLatch; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 41 | import static com.google.common.base.Strings.isNullOrEmpty; |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 42 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 43 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 44 | |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 45 | /** |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 46 | * Removes an intent. |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 47 | */ |
tom | 6db1f0a | 2014-10-07 09:12:29 -0700 | [diff] [blame] | 48 | @Command(scope = "onos", name = "remove-intent", |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 49 | description = "Removes the specified intent") |
tom | 6db1f0a | 2014-10-07 09:12:29 -0700 | [diff] [blame] | 50 | public class IntentRemoveCommand extends AbstractShellCommand { |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 51 | |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 52 | @Argument(index = 0, name = "app", |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 53 | description = "Application ID", |
| 54 | required = false, multiValued = false) |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 55 | String applicationIdString = null; |
| 56 | |
Brian O'Connor | 422b875 | 2015-06-04 16:16:49 -0700 | [diff] [blame] | 57 | @Argument(index = 1, name = "key", |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 58 | description = "Intent Key", |
| 59 | required = false, multiValued = false) |
Brian O'Connor | 422b875 | 2015-06-04 16:16:49 -0700 | [diff] [blame] | 60 | String keyString = null; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 61 | |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 62 | @Option(name = "-p", aliases = "--purge", |
| 63 | description = "Purge the intent from the store after removal", |
| 64 | required = false, multiValued = false) |
| 65 | private boolean purgeAfterRemove = false; |
| 66 | |
| 67 | @Option(name = "-s", aliases = "--sync", |
| 68 | description = "Waits for the removal before returning", |
| 69 | required = false, multiValued = false) |
| 70 | private boolean sync = false; |
| 71 | |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 72 | private static final EnumSet<IntentState> CAN_PURGE = EnumSet.of(WITHDRAWN, FAILED); |
| 73 | |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 74 | @Override |
| 75 | protected void execute() { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 76 | IntentService intentService = get(IntentService.class); |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 77 | removeIntent(intentService.getIntents(), |
| 78 | applicationIdString, keyString, |
| 79 | purgeAfterRemove, sync); |
| 80 | } |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 81 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 82 | /** |
| 83 | * Purges the intents passed as argument. |
| 84 | * |
| 85 | * @param intents list of intents to purge |
| 86 | */ |
| 87 | private void purgeIntents(Iterable<Intent> intents) { |
| 88 | IntentService intentService = get(IntentService.class); |
| 89 | this.purgeAfterRemove = true; |
| 90 | removeIntentsByAppId(intentService, intents, null); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Purges the intents passed as argument after confirmation is provided |
| 95 | * for each of them. |
| 96 | * If no explicit confirmation is provided, the intent is not purged. |
| 97 | * |
| 98 | * @param intents list of intents to purge |
| 99 | */ |
| 100 | public void purgeIntentsInteractive(Iterable<Intent> intents) { |
| 101 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 102 | intents.forEach(intent -> { |
| 103 | System.out.print(String.format("Id=%s, Key=%s, AppId=%s. Remove? [y/N]: ", |
| 104 | intent.id(), intent.key(), intent.appId().name())); |
| 105 | String response; |
| 106 | try { |
| 107 | response = br.readLine(); |
| 108 | response = response.trim().replace("\n", ""); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 109 | if ("y".equals(response)) { |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 110 | this.purgeIntents(ImmutableList.of(intent)); |
| 111 | } |
| 112 | } catch (IOException e) { |
| 113 | response = ""; |
| 114 | } |
| 115 | print(response); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Removes the intents passed as argument, assuming these |
| 121 | * belong to the application's ID provided (if any) and |
| 122 | * contain a key string. |
| 123 | * |
| 124 | * If an application ID is provided, it will be used to further |
| 125 | * filter the intents to be removed. |
| 126 | * |
| 127 | * @param intents list of intents to remove |
| 128 | * @param applicationIdString application ID to filter intents |
| 129 | * @param keyString string to filter intents |
| 130 | * @param purgeAfterRemove states whether the intents should be also purged |
| 131 | * @param sync states whether the cli should wait for the operation to finish |
| 132 | * before returning |
| 133 | */ |
| 134 | private void removeIntent(Iterable<Intent> intents, |
| 135 | String applicationIdString, String keyString, |
| 136 | boolean purgeAfterRemove, boolean sync) { |
| 137 | IntentService intentService = get(IntentService.class); |
| 138 | CoreService coreService = get(CoreService.class); |
| 139 | this.applicationIdString = applicationIdString; |
| 140 | this.keyString = keyString; |
| 141 | this.purgeAfterRemove = purgeAfterRemove; |
| 142 | this.sync = sync; |
suibin zhang | 5e897b7 | 2015-08-06 10:02:18 -0700 | [diff] [blame] | 143 | if (purgeAfterRemove || sync) { |
| 144 | print("Using \"sync\" to remove/purge intents - this may take a while..."); |
| 145 | print("Check \"summary\" to see remove/purge progress."); |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 148 | ApplicationId appId = appId(); |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 149 | if (!isNullOrEmpty(applicationIdString)) { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 150 | appId = coreService.getAppId(applicationIdString); |
| 151 | if (appId == null) { |
| 152 | print("Cannot find application Id %s", applicationIdString); |
| 153 | return; |
| 154 | } |
| 155 | } |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 156 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 157 | if (isNullOrEmpty(keyString)) { |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 158 | removeIntentsByAppId(intentService, intents, appId); |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 159 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 160 | } else { |
| 161 | final Key key; |
| 162 | if (keyString.startsWith("0x")) { |
| 163 | // The intent uses a LongKey |
| 164 | keyString = keyString.replaceFirst("0x", ""); |
| 165 | key = Key.of(new BigInteger(keyString, 16).longValue(), appId); |
| 166 | } else { |
| 167 | // The intent uses a StringKey |
| 168 | key = Key.of(keyString, appId); |
| 169 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 170 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 171 | Intent intent = intentService.getIntent(key); |
| 172 | if (intent != null) { |
| 173 | removeIntent(intentService, intent); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 178 | /** |
| 179 | * Removes the intents passed as argument. |
| 180 | * |
| 181 | * If an application ID is provided, it will be used to further |
| 182 | * filter the intents to be removed. |
| 183 | * |
| 184 | * @param intentService IntentService object |
| 185 | * @param intents intents to remove |
| 186 | * @param appId application ID to filter intents |
| 187 | */ |
| 188 | private void removeIntentsByAppId(IntentService intentService, |
| 189 | Iterable<Intent> intents, |
| 190 | ApplicationId appId) { |
| 191 | for (Intent intent : intents) { |
| 192 | if (appId == null || intent.appId().equals(appId)) { |
| 193 | removeIntent(intentService, intent); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Removes the intent passed as argument. |
| 200 | * |
| 201 | * @param intentService IntentService object |
| 202 | * @param intent intent to remove |
| 203 | */ |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 204 | private void removeIntent(IntentService intentService, Intent intent) { |
| 205 | IntentListener listener = null; |
| 206 | Key key = intent.key(); |
| 207 | final CountDownLatch withdrawLatch, purgeLatch; |
| 208 | if (purgeAfterRemove || sync) { |
| 209 | // set up latch and listener to track uninstall progress |
| 210 | withdrawLatch = new CountDownLatch(1); |
| 211 | purgeLatch = purgeAfterRemove ? new CountDownLatch(1) : null; |
| 212 | listener = (IntentEvent event) -> { |
| 213 | if (Objects.equals(event.subject().key(), key)) { |
| 214 | if (event.type() == IntentEvent.Type.WITHDRAWN || |
| 215 | event.type() == IntentEvent.Type.FAILED) { |
| 216 | withdrawLatch.countDown(); |
| 217 | } else if (purgeAfterRemove && |
| 218 | event.type() == IntentEvent.Type.PURGED) { |
| 219 | purgeLatch.countDown(); |
| 220 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 221 | } |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 222 | }; |
| 223 | intentService.addListener(listener); |
| 224 | } else { |
| 225 | purgeLatch = null; |
| 226 | withdrawLatch = null; |
| 227 | } |
| 228 | |
| 229 | // request the withdraw |
| 230 | intentService.withdraw(intent); |
| 231 | |
| 232 | if (purgeAfterRemove || sync) { |
| 233 | try { // wait for withdraw event |
| 234 | withdrawLatch.await(5, TimeUnit.SECONDS); |
| 235 | } catch (InterruptedException e) { |
| 236 | print("Timed out waiting for intent {} withdraw", key); |
| 237 | } |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 238 | if (purgeAfterRemove && CAN_PURGE.contains(intentService.getIntentState(key))) { |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 239 | intentService.purge(intent); |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 240 | if (sync) { // wait for purge event |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 241 | /* TODO |
| 242 | Technically, the event comes before map.remove() is called. |
| 243 | If we depend on sync and purge working together, we will |
| 244 | need to address this. |
| 245 | */ |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 246 | try { |
| 247 | purgeLatch.await(5, TimeUnit.SECONDS); |
| 248 | } catch (InterruptedException e) { |
| 249 | print("Timed out waiting for intent {} purge", key); |
| 250 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 253 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 254 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 255 | if (listener != null) { |
| 256 | // clean up the listener |
| 257 | intentService.removeListener(listener); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 258 | } |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 259 | } |
| 260 | } |