blob: 97c6eaf88abc117f18dad695e0c74fc316ca8f29 [file] [log] [blame]
Jian Lib230e07c2020-12-21 11:25:12 +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.ArrayNode;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
22import org.apache.commons.lang.StringUtils;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.kubevirtnode.api.KubevirtNode;
27import org.onosproject.kubevirtnode.api.KubevirtNodeService;
28
29import java.util.Comparator;
30import java.util.List;
31
32import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.genFormatString;
33import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.prettyJson;
34
35
36/**
37 * Lists all nodes registered to the service.
38 */
39@Service
40@Command(scope = "onos", name = "kubevirt-nodes",
41 description = "Lists all nodes registered in KubeVirt node service")
42public class KubevirtNodeListCommand extends AbstractShellCommand {
43
44 private static final int HOSTNAME_LENGTH = 35;
45 private static final int TYPE_LENGTH = 15;
46 private static final int MANAGEMENT_IP_LENGTH = 25;
47 private static final int DATA_IP_LENGTH = 25;
48 private static final int STATUS = 15;
49 private static final int MARGIN_LENGTH = 2;
50
51 @Override
52 protected void doExecute() throws Exception {
53 KubevirtNodeService nodeService = get(KubevirtNodeService.class);
54 List<KubevirtNode> nodes = Lists.newArrayList(nodeService.nodes());
55 nodes.sort(Comparator.comparing(KubevirtNode::hostname));
56
57 String format = genFormatString(ImmutableList.of(HOSTNAME_LENGTH,
58 TYPE_LENGTH, MANAGEMENT_IP_LENGTH, DATA_IP_LENGTH, STATUS));
59
60 if (outputJson()) {
61 print("%s", json(nodes));
62 } else {
63 print(format, "Hostname", "Type", "Management IP", "Data IP", "State");
64 for (KubevirtNode node : nodes) {
65 print(format,
66 StringUtils.substring(node.hostname(), 0,
67 HOSTNAME_LENGTH - MARGIN_LENGTH),
68 node.type(),
69 StringUtils.substring(node.managementIp().toString(), 0,
70 MANAGEMENT_IP_LENGTH - MARGIN_LENGTH),
71 node.dataIp() != null ? StringUtils.substring(
72 node.dataIp().toString(), 0,
73 DATA_IP_LENGTH - MARGIN_LENGTH) : "",
74 node.state());
75 }
76 print("Total %s nodes", nodeService.nodes().size());
77 }
78 }
79
80 private String json(List<KubevirtNode> nodes) {
81 ObjectMapper mapper = new ObjectMapper();
82 ArrayNode result = mapper.createArrayNode();
83 for (KubevirtNode node : nodes) {
84 result.add(jsonForEntity(node, KubevirtNode.class));
85 }
86 return prettyJson(mapper, result.toString());
87 }
88}