Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnode.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import org.apache.commons.lang.StringUtils; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 27 | import org.onosproject.kubevirtnode.api.KubevirtNodeService; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.genFormatString; |
| 33 | import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.prettyJson; |
| 34 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 35 | /** |
| 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") |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 41 | public class KubevirtListNodesCommand extends AbstractShellCommand { |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 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 | } |