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 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.IntentState; |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 26 | import org.onosproject.net.intent.IntentService; |
| 27 | import org.onosproject.net.intent.IntentListener; |
| 28 | import org.onosproject.net.intent.IntentEvent; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.Key; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 30 | |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 31 | import java.math.BigInteger; |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 32 | import java.util.EnumSet; |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 33 | import java.util.Objects; |
| 34 | import java.util.concurrent.CountDownLatch; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 37 | import static com.google.common.base.Strings.isNullOrEmpty; |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 38 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 39 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
Thomas Vachuska | 4926c1b | 2014-10-21 00:44:10 -0700 | [diff] [blame] | 40 | |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 41 | /** |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 42 | * Removes an intent. |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 43 | */ |
tom | 6db1f0a | 2014-10-07 09:12:29 -0700 | [diff] [blame] | 44 | @Command(scope = "onos", name = "remove-intent", |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 45 | description = "Removes the specified intent") |
tom | 6db1f0a | 2014-10-07 09:12:29 -0700 | [diff] [blame] | 46 | public class IntentRemoveCommand extends AbstractShellCommand { |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 47 | |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 48 | @Argument(index = 0, name = "app", |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 49 | description = "Application ID", |
| 50 | required = false, multiValued = false) |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 51 | String applicationIdString = null; |
| 52 | |
Brian O'Connor | 422b875 | 2015-06-04 16:16:49 -0700 | [diff] [blame] | 53 | @Argument(index = 1, name = "key", |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 54 | description = "Intent Key", |
| 55 | required = false, multiValued = false) |
Brian O'Connor | 422b875 | 2015-06-04 16:16:49 -0700 | [diff] [blame] | 56 | String keyString = null; |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 57 | |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 58 | @Option(name = "-p", aliases = "--purge", |
| 59 | description = "Purge the intent from the store after removal", |
| 60 | required = false, multiValued = false) |
| 61 | private boolean purgeAfterRemove = false; |
| 62 | |
| 63 | @Option(name = "-s", aliases = "--sync", |
| 64 | description = "Waits for the removal before returning", |
| 65 | required = false, multiValued = false) |
| 66 | private boolean sync = false; |
| 67 | |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 68 | private static final EnumSet<IntentState> CAN_PURGE = EnumSet.of(WITHDRAWN, FAILED); |
| 69 | |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 70 | @Override |
| 71 | protected void execute() { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 72 | IntentService intentService = get(IntentService.class); |
| 73 | CoreService coreService = get(CoreService.class); |
| 74 | |
suibin zhang | 5e897b7 | 2015-08-06 10:02:18 -0700 | [diff] [blame] | 75 | if (purgeAfterRemove || sync) { |
| 76 | print("Using \"sync\" to remove/purge intents - this may take a while..."); |
| 77 | print("Check \"summary\" to see remove/purge progress."); |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 80 | ApplicationId appId = appId(); |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 81 | if (!isNullOrEmpty(applicationIdString)) { |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 82 | appId = coreService.getAppId(applicationIdString); |
| 83 | if (appId == null) { |
| 84 | print("Cannot find application Id %s", applicationIdString); |
| 85 | return; |
| 86 | } |
| 87 | } |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 88 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 89 | if (isNullOrEmpty(keyString)) { |
| 90 | for (Intent intent : intentService.getIntents()) { |
| 91 | if (intent.appId().equals(appId)) { |
| 92 | removeIntent(intentService, intent); |
| 93 | } |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 94 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 95 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 96 | } else { |
| 97 | final Key key; |
| 98 | if (keyString.startsWith("0x")) { |
| 99 | // The intent uses a LongKey |
| 100 | keyString = keyString.replaceFirst("0x", ""); |
| 101 | key = Key.of(new BigInteger(keyString, 16).longValue(), appId); |
| 102 | } else { |
| 103 | // The intent uses a StringKey |
| 104 | key = Key.of(keyString, appId); |
| 105 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 106 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 107 | Intent intent = intentService.getIntent(key); |
| 108 | if (intent != null) { |
| 109 | removeIntent(intentService, intent); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private void removeIntent(IntentService intentService, Intent intent) { |
| 115 | IntentListener listener = null; |
| 116 | Key key = intent.key(); |
| 117 | final CountDownLatch withdrawLatch, purgeLatch; |
| 118 | if (purgeAfterRemove || sync) { |
| 119 | // set up latch and listener to track uninstall progress |
| 120 | withdrawLatch = new CountDownLatch(1); |
| 121 | purgeLatch = purgeAfterRemove ? new CountDownLatch(1) : null; |
| 122 | listener = (IntentEvent event) -> { |
| 123 | if (Objects.equals(event.subject().key(), key)) { |
| 124 | if (event.type() == IntentEvent.Type.WITHDRAWN || |
| 125 | event.type() == IntentEvent.Type.FAILED) { |
| 126 | withdrawLatch.countDown(); |
| 127 | } else if (purgeAfterRemove && |
| 128 | event.type() == IntentEvent.Type.PURGED) { |
| 129 | purgeLatch.countDown(); |
| 130 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 131 | } |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 132 | }; |
| 133 | intentService.addListener(listener); |
| 134 | } else { |
| 135 | purgeLatch = null; |
| 136 | withdrawLatch = null; |
| 137 | } |
| 138 | |
| 139 | // request the withdraw |
| 140 | intentService.withdraw(intent); |
| 141 | |
| 142 | if (purgeAfterRemove || sync) { |
| 143 | try { // wait for withdraw event |
| 144 | withdrawLatch.await(5, TimeUnit.SECONDS); |
| 145 | } catch (InterruptedException e) { |
| 146 | print("Timed out waiting for intent {} withdraw", key); |
| 147 | } |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 148 | if (purgeAfterRemove && CAN_PURGE.contains(intentService.getIntentState(key))) { |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 149 | intentService.purge(intent); |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 150 | if (sync) { // wait for purge event |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 151 | /* TODO |
| 152 | Technically, the event comes before map.remove() is called. |
| 153 | If we depend on sync and purge working together, we will |
| 154 | need to address this. |
| 155 | */ |
suibin zhang | baa163f | 2015-08-06 09:22:24 -0700 | [diff] [blame] | 156 | try { |
| 157 | purgeLatch.await(5, TimeUnit.SECONDS); |
| 158 | } catch (InterruptedException e) { |
| 159 | print("Timed out waiting for intent {} purge", key); |
| 160 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 163 | } |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 164 | |
Thomas Vachuska | 33937c4 | 2015-07-15 17:40:32 -0700 | [diff] [blame] | 165 | if (listener != null) { |
| 166 | // clean up the listener |
| 167 | intentService.removeListener(listener); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 168 | } |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 169 | } |
| 170 | } |