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