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