blob: fa996ba10d557253e51eab8d886f017681f237b1 [file] [log] [blame]
Jian Li077b07e2020-09-01 16:55:25 +09001/*
2 * Copyright 2019-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.k8snode.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.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.k8snode.api.K8sHost;
26import org.onosproject.k8snode.api.K8sHostService;
27import org.onosproject.k8snode.api.K8sTunnelBridge;
28
29import java.util.Comparator;
30import java.util.List;
31import java.util.stream.Collectors;
32
33import static org.onosproject.k8snode.util.K8sNodeUtil.genFormatString;
34import static org.onosproject.k8snode.util.K8sNodeUtil.prettyJson;
35
36/**
37 * Lists all host registered to the service.
38 */
39@Service
40@Command(scope = "onos", name = "k8s-hosts",
41 description = "Lists all hosts registered in kubernetes host service")
42public class K8sHostListCommand extends AbstractShellCommand {
43
44 private static final int HOST_IP_LENGTH = 15;
45 private static final int NODES_LENGTH = 40;
46 private static final int TUNBRS_LENGTH = 40;
47 private static final int STATUS_LENGTH = 15;
48
49 @Override
50 protected void doExecute() {
51 K8sHostService hostService = get(K8sHostService.class);
52 List<K8sHost> hosts = Lists.newArrayList(hostService.hosts());
53 hosts.sort(Comparator.comparing(K8sHost::hostIp));
54
55 String format = genFormatString(
56 ImmutableList.of(HOST_IP_LENGTH, NODES_LENGTH, TUNBRS_LENGTH, STATUS_LENGTH));
57
58 if (outputJson()) {
59 print("%s", json(hosts));
60 } else {
61 print(format, "Host IP", "Nodes", "Tunnel Bridges", "State");
62 for (K8sHost host : hosts) {
63 print(format,
64 host.hostIp().toString(),
65 host.nodeNames().toString(),
66 host.tunBridges().stream().map(K8sTunnelBridge::name)
67 .collect(Collectors.toSet()).toString(),
68 host.state().toString());
69 }
70 print("Total %s hosts", hosts.size());
71 }
72 }
73
74 private String json(List<K8sHost> hosts) {
75 ObjectMapper mapper = new ObjectMapper();
76 ArrayNode result = mapper.createArrayNode();
77 for (K8sHost host: hosts) {
78 result.add(jsonForEntity(host, K8sHost.class));
79 }
80 return prettyJson(mapper, result.toString());
81 }
82}