Davide Sanvito | 05983ba | 2017-12-01 11:46:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.imr.cli; |
| 18 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 19f2d9b | 2018-10-09 13:23:58 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Option; |
| 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Davide Sanvito | 05983ba | 2017-12-01 11:46:44 +0100 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.core.DefaultApplicationId; |
| 26 | import org.onosproject.imr.IntentMonitorAndRerouteService; |
| 27 | import org.onosproject.net.intent.IntentService; |
| 28 | import org.onosproject.net.intent.Key; |
| 29 | |
| 30 | /** |
| 31 | * Stops monitoring of an intent by the IMR service. |
| 32 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 33 | @Service |
Davide Sanvito | 05983ba | 2017-12-01 11:46:44 +0100 | [diff] [blame] | 34 | @Command(scope = "imr", name = "stopmon", |
| 35 | description = "Stop monitoring and intent already submitted to the IMR") |
| 36 | public class StopMonitorCommand extends AbstractShellCommand { |
| 37 | |
| 38 | @Option(name = "-l", aliases = "--longkey", description = "Treat intentKey as LongKey", |
| 39 | required = false, multiValued = false) |
| 40 | private boolean treatAsLongKey = false; |
| 41 | |
| 42 | @Argument(index = 0, name = "applicationId", |
| 43 | description = "Application ID that submitted the intent", |
| 44 | required = true) |
Ray Milkey | 19f2d9b | 2018-10-09 13:23:58 -0700 | [diff] [blame] | 45 | @Completion(ApplicationIdImrCompleter.class) |
Davide Sanvito | 05983ba | 2017-12-01 11:46:44 +0100 | [diff] [blame] | 46 | private Short appId = null; |
| 47 | |
| 48 | @Argument(index = 1, name = "applicationName", |
| 49 | description = "Application Name that submitted the intent", |
| 50 | required = true) |
Ray Milkey | 19f2d9b | 2018-10-09 13:23:58 -0700 | [diff] [blame] | 51 | @Completion(ApplicationNameImrCompleter.class) |
Davide Sanvito | 05983ba | 2017-12-01 11:46:44 +0100 | [diff] [blame] | 52 | private String appName = null; |
| 53 | |
| 54 | @Argument(index = 2, name = "intentKey", |
| 55 | description = "String representation of the key of the intent", |
| 56 | required = false) |
Ray Milkey | 19f2d9b | 2018-10-09 13:23:58 -0700 | [diff] [blame] | 57 | @Completion(IntentKeyImrCompleter.class) |
Davide Sanvito | 05983ba | 2017-12-01 11:46:44 +0100 | [diff] [blame] | 58 | private String key = null; |
| 59 | |
| 60 | private IntentMonitorAndRerouteService imrService; |
| 61 | private IntentService intentService; |
| 62 | |
| 63 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 64 | protected void doExecute() { |
Davide Sanvito | 05983ba | 2017-12-01 11:46:44 +0100 | [diff] [blame] | 65 | imrService = get(IntentMonitorAndRerouteService.class); |
| 66 | intentService = get(IntentService.class); |
| 67 | |
| 68 | if (appId != null && appName != null) { |
| 69 | if (key != null) { |
| 70 | /* |
| 71 | Intent key might be a StringKey or a LongKey, but in any case is |
| 72 | provided via CLI as a string. To solve only ambiguity we check if |
| 73 | "--longkey" CLI parameter has been set. |
| 74 | */ |
| 75 | if (treatAsLongKey) { |
| 76 | try { |
| 77 | Key intentKeyLong = Key.of(Integer.decode(key), new DefaultApplicationId(appId, appName)); |
| 78 | if (imrService.stopMonitorIntent(intentKeyLong)) { |
| 79 | print("Stopped monitoring of intent with LongKey %s", intentKeyLong); |
| 80 | return; |
| 81 | } |
| 82 | } catch (NumberFormatException nfe) { |
| 83 | print("\"%s\" is not a valid LongKey", key); |
| 84 | return; |
| 85 | } |
| 86 | } else { |
| 87 | Key intentKeyString = Key.of(key, new DefaultApplicationId(appId, appName)); |
| 88 | if (imrService.stopMonitorIntent(intentKeyString)) { |
| 89 | print("Stopped monitoring of intent with StringKey %s", intentKeyString); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | //No intent found in IMR |
| 95 | print("No monitored intent with key %s found", key); |
| 96 | } else { |
| 97 | intentService.getIntents().forEach(i -> { |
| 98 | if (i.appId().equals(new DefaultApplicationId(appId, appName))) { |
| 99 | imrService.stopMonitorIntent(i.key()); |
| 100 | print("Stopped monitoring of intent with key %s", i.key()); |
| 101 | } |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | } |