blob: d4acfca3b109cbce3ee069af225f37384e216626 [file] [log] [blame]
Davide Sanvito05983ba2017-12-01 11:46:44 +01001/*
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
17package org.onosproject.imr.cli;
18
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey19f2d9b2018-10-09 13:23:58 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
Davide Sanvito05983ba2017-12-01 11:46:44 +010024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.DefaultApplicationId;
26import org.onosproject.imr.IntentMonitorAndRerouteService;
27import org.onosproject.net.intent.Intent;
28import org.onosproject.net.intent.IntentService;
29import org.onosproject.net.intent.Key;
30
31/**
32 * Starts monitoring of an intent submitting its key to the IMR service.
33 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070034@Service
Davide Sanvito05983ba2017-12-01 11:46:44 +010035@Command(scope = "imr", name = "startmon",
36 description = "Submit an intent to the IMR application to start monitoring")
37public 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 Milkey19f2d9b2018-10-09 13:23:58 -070046 @Completion(ApplicationIdImrCompleter.class)
Davide Sanvito05983ba2017-12-01 11:46:44 +010047 private Short appId;
48
49 @Argument(index = 1, name = "applicationName",
50 description = "Application Name that submitted the intent",
51 required = true)
Ray Milkey19f2d9b2018-10-09 13:23:58 -070052 @Completion(ApplicationNameImrCompleter.class)
Davide Sanvito05983ba2017-12-01 11:46:44 +010053 private String appName;
54
55 @Argument(index = 2, name = "intentKey",
56 description = "String representation of the key of the intent",
57 required = false)
Ray Milkey19f2d9b2018-10-09 13:23:58 -070058 @Completion(IntentKeyImrCompleter.class)
Davide Sanvito05983ba2017-12-01 11:46:44 +010059 private String key;
60
61 private IntentMonitorAndRerouteService imrService;
62 private IntentService intentService;
63
64 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070065 protected void doExecute() {
Davide Sanvito05983ba2017-12-01 11:46:44 +010066 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}