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