blob: 010fcc102191484b71060eb52f5fd278b0c8f9d1 [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;
Jian Li019ce6a2020-09-09 10:23:21 +090027import org.onosproject.k8snode.api.K8sRouterBridge;
Jian Li077b07e2020-09-01 16:55:25 +090028import org.onosproject.k8snode.api.K8sTunnelBridge;
29
30import java.util.Comparator;
31import java.util.List;
32import java.util.stream.Collectors;
33
34import static org.onosproject.k8snode.util.K8sNodeUtil.genFormatString;
35import static org.onosproject.k8snode.util.K8sNodeUtil.prettyJson;
36
37/**
38 * Lists all host registered to the service.
39 */
40@Service
41@Command(scope = "onos", name = "k8s-hosts",
42 description = "Lists all hosts registered in kubernetes host service")
43public class K8sHostListCommand extends AbstractShellCommand {
44
45 private static final int HOST_IP_LENGTH = 15;
Jian Li077b07e2020-09-01 16:55:25 +090046 private static final int TUNBRS_LENGTH = 40;
Jian Li019ce6a2020-09-09 10:23:21 +090047 private static final int RTRBRS_LENGTH = 40;
Jian Li077b07e2020-09-01 16:55:25 +090048 private static final int STATUS_LENGTH = 15;
49
50 @Override
51 protected void doExecute() {
52 K8sHostService hostService = get(K8sHostService.class);
53 List<K8sHost> hosts = Lists.newArrayList(hostService.hosts());
54 hosts.sort(Comparator.comparing(K8sHost::hostIp));
55
56 String format = genFormatString(
Jian Li019ce6a2020-09-09 10:23:21 +090057 ImmutableList.of(HOST_IP_LENGTH, TUNBRS_LENGTH, RTRBRS_LENGTH, STATUS_LENGTH));
Jian Li077b07e2020-09-01 16:55:25 +090058
59 if (outputJson()) {
60 print("%s", json(hosts));
61 } else {
Jian Li019ce6a2020-09-09 10:23:21 +090062 print(format, "Host IP", "Tunnel Bridges", "Router Bridges", "State");
Jian Li077b07e2020-09-01 16:55:25 +090063 for (K8sHost host : hosts) {
64 print(format,
65 host.hostIp().toString(),
Jian Li077b07e2020-09-01 16:55:25 +090066 host.tunBridges().stream().map(K8sTunnelBridge::name)
67 .collect(Collectors.toSet()).toString(),
Jian Li019ce6a2020-09-09 10:23:21 +090068 host.routerBridges().stream().map(K8sRouterBridge::name)
69 .collect(Collectors.toSet()).toString(),
Jian Li077b07e2020-09-01 16:55:25 +090070 host.state().toString());
71 }
72 print("Total %s hosts", hosts.size());
73 }
74 }
75
76 private String json(List<K8sHost> hosts) {
77 ObjectMapper mapper = new ObjectMapper();
78 ArrayNode result = mapper.createArrayNode();
79 for (K8sHost host: hosts) {
80 result.add(jsonForEntity(host, K8sHost.class));
81 }
82 return prettyJson(mapper, result.toString());
83 }
84}