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