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