blob: 5b9c268471f2999053facbee83be93f2aa2db75e [file] [log] [blame]
Jian Lif2483072020-12-25 02:24:16 +09001/*
2 * Copyright 2020-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.kubevirtnode.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
24import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
25
26import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.prettyJson;
27
28/**
29 * Lists all KubeVirt API server configs registered to the service.
30 */
31@Service
32@Command(scope = "onos", name = "kubevirt-api-configs",
33 description = "Lists all KubeVirt API server configs registered to the service")
Jian Li4fe40e52021-01-06 03:29:58 +090034public class KubevirtListApiConfigsCommand extends AbstractShellCommand {
Jian Lif2483072020-12-25 02:24:16 +090035
Jian Lie0eaf5c2021-09-06 10:02:13 +090036 private static final String FORMAT = "%-10s%-20s%-10s%-25s%-10s";
Jian Lif2483072020-12-25 02:24:16 +090037
38 @Override
39 protected void doExecute() throws Exception {
40 KubevirtApiConfigService service = get(KubevirtApiConfigService.class);
41 KubevirtApiConfig config = service.apiConfig();
42
43 if (outputJson()) {
44 print("%s", json(config));
45 } else {
Jian Lie0eaf5c2021-09-06 10:02:13 +090046 print(FORMAT, "Scheme", "Server IP", "Port", "Controller IP", "State");
47 String controllerIp = "N/A";
Jian Liaaf44b52020-12-27 23:22:46 +090048 if (config != null) {
Jian Lie0eaf5c2021-09-06 10:02:13 +090049 if (config.controllerIp() != null) {
50 controllerIp = config.controllerIp().toString();
51 }
Jian Liaaf44b52020-12-27 23:22:46 +090052 print(FORMAT, config.scheme().name(), config.ipAddress().toString(),
Jian Lie0eaf5c2021-09-06 10:02:13 +090053 config.port(), controllerIp, config.state().name());
Jian Liaaf44b52020-12-27 23:22:46 +090054 } else {
55 print("Kubevirt config not found!");
56 }
57
Jian Lif2483072020-12-25 02:24:16 +090058 }
59 }
60
61 private String json(KubevirtApiConfig config) {
62 ObjectMapper mapper = new ObjectMapper();
63 ObjectNode node = jsonForEntity(config, KubevirtApiConfig.class);
64 return prettyJson(mapper, node.toString());
65 }
66}