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