blob: d71e5076bfef840dda39863133601dd3651db753 [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.IntentService;
28import org.onosproject.net.intent.Key;
29
30/**
31 * Stops monitoring of an intent by the IMR service.
32 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070033@Service
Davide Sanvito05983ba2017-12-01 11:46:44 +010034@Command(scope = "imr", name = "stopmon",
35 description = "Stop monitoring and intent already submitted to the IMR")
36public class StopMonitorCommand 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)
Ray Milkey19f2d9b2018-10-09 13:23:58 -070045 @Completion(ApplicationIdImrCompleter.class)
Davide Sanvito05983ba2017-12-01 11:46:44 +010046 private Short appId = null;
47
48 @Argument(index = 1, name = "applicationName",
49 description = "Application Name that submitted the intent",
50 required = true)
Ray Milkey19f2d9b2018-10-09 13:23:58 -070051 @Completion(ApplicationNameImrCompleter.class)
Davide Sanvito05983ba2017-12-01 11:46:44 +010052 private String appName = null;
53
54 @Argument(index = 2, name = "intentKey",
55 description = "String representation of the key of the intent",
56 required = false)
Ray Milkey19f2d9b2018-10-09 13:23:58 -070057 @Completion(IntentKeyImrCompleter.class)
Davide Sanvito05983ba2017-12-01 11:46:44 +010058 private String key = null;
59
60 private IntentMonitorAndRerouteService imrService;
61 private IntentService intentService;
62
63 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070064 protected void doExecute() {
Davide Sanvito05983ba2017-12-01 11:46:44 +010065 imrService = get(IntentMonitorAndRerouteService.class);
66 intentService = get(IntentService.class);
67
68 if (appId != null && appName != null) {
69 if (key != null) {
70 /*
71 Intent key might be a StringKey or a LongKey, but in any case is
72 provided via CLI as a string. To solve only ambiguity we check if
73 "--longkey" CLI parameter has been set.
74 */
75 if (treatAsLongKey) {
76 try {
77 Key intentKeyLong = Key.of(Integer.decode(key), new DefaultApplicationId(appId, appName));
78 if (imrService.stopMonitorIntent(intentKeyLong)) {
79 print("Stopped monitoring of intent with LongKey %s", intentKeyLong);
80 return;
81 }
82 } catch (NumberFormatException nfe) {
83 print("\"%s\" is not a valid LongKey", key);
84 return;
85 }
86 } else {
87 Key intentKeyString = Key.of(key, new DefaultApplicationId(appId, appName));
88 if (imrService.stopMonitorIntent(intentKeyString)) {
89 print("Stopped monitoring of intent with StringKey %s", intentKeyString);
90 return;
91 }
92 }
93
94 //No intent found in IMR
95 print("No monitored intent with key %s found", key);
96 } else {
97 intentService.getIntents().forEach(i -> {
98 if (i.appId().equals(new DefaultApplicationId(appId, appName))) {
99 imrService.stopMonitorIntent(i.key());
100 print("Stopped monitoring of intent with key %s", i.key());
101 }
102 });
103 }
104 }
105
106 }
107}