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