blob: 17ba3f387bab004a65faeca721ed162b3aa2516d [file] [log] [blame]
Jian Li16f1f532018-12-27 17:45:09 +09001/*
2 * Copyright 2018-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 */
16package org.onosproject.openstacktelemetry.cli;
17
18import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Completion;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService;
24import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
25
Jian Li667c6eb2019-01-07 23:01:12 +090026import static java.lang.Thread.sleep;
27import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.DISABLED;
28
Jian Li16f1f532018-12-27 17:45:09 +090029/**
30 * Disables a telemetry service.
31 */
32@Service
33@Command(scope = "onos", name = "telemetry-disable",
34 description = "Disable a specific telemetry service")
35public class TelemetryServiceDisableCommand extends AbstractShellCommand {
36
37 @Argument(index = 0, name = "config name", description = "telemetry config name",
38 required = true, multiValued = false)
39 @Completion(TelemetryConfigNameCompleter.class)
40 private String configName = null;
41
Jian Li667c6eb2019-01-07 23:01:12 +090042 private static final long SLEEP_MS = 2000; // wait 2s for checking status
43 private static final String SUCCESS_FORMAT = "Successfully disabled telemetry service %s!";
44 private static final String FAIL_FORMAT = "Failed to disable telemetry service %s!";
Jian Li16f1f532018-12-27 17:45:09 +090045 private static final String NO_ELEMENT =
46 "No telemetry config is found with the given name";
47
48 @Override
49 protected void doExecute() {
50 TelemetryConfigAdminService service = get(TelemetryConfigAdminService.class);
51 TelemetryConfig config = service.getConfig(configName);
52
53 if (config == null) {
54 print(NO_ELEMENT);
55 return;
56 }
57
Jian Li667c6eb2019-01-07 23:01:12 +090058 TelemetryConfig updatedConfig = config.updateStatus(DISABLED);
Jian Li16f1f532018-12-27 17:45:09 +090059
60 service.updateTelemetryConfig(updatedConfig);
Jian Li667c6eb2019-01-07 23:01:12 +090061
62 try {
63 sleep(SLEEP_MS);
64 } catch (InterruptedException e) {
65 error("Exception caused during status checking...");
66 }
67
68 TelemetryConfig finalConfig = service.getConfig(configName);
69
70 if (finalConfig.status() == DISABLED) {
71 print(SUCCESS_FORMAT, config.name());
72 } else {
73 print(FAIL_FORMAT, config.name());
74 }
Jian Li16f1f532018-12-27 17:45:09 +090075 }
76}