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