Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snode.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.karaf.shell.api.action.Command; |
| 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.k8snode.api.K8sHost; |
| 26 | import org.onosproject.k8snode.api.K8sHostService; |
Jian Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 27 | import org.onosproject.k8snode.api.K8sRouterBridge; |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 28 | import org.onosproject.k8snode.api.K8sTunnelBridge; |
| 29 | |
| 30 | import java.util.Comparator; |
| 31 | import java.util.List; |
| 32 | import java.util.stream.Collectors; |
| 33 | |
| 34 | import static org.onosproject.k8snode.util.K8sNodeUtil.genFormatString; |
| 35 | import 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") |
| 43 | public class K8sHostListCommand extends AbstractShellCommand { |
| 44 | |
| 45 | private static final int HOST_IP_LENGTH = 15; |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 46 | private static final int TUNBRS_LENGTH = 40; |
Jian Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 47 | private static final int RTRBRS_LENGTH = 40; |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 48 | 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 Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 57 | ImmutableList.of(HOST_IP_LENGTH, TUNBRS_LENGTH, RTRBRS_LENGTH, STATUS_LENGTH)); |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 58 | |
| 59 | if (outputJson()) { |
| 60 | print("%s", json(hosts)); |
| 61 | } else { |
Jian Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 62 | print(format, "Host IP", "Tunnel Bridges", "Router Bridges", "State"); |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 63 | for (K8sHost host : hosts) { |
| 64 | print(format, |
| 65 | host.hostIp().toString(), |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 66 | host.tunBridges().stream().map(K8sTunnelBridge::name) |
| 67 | .collect(Collectors.toSet()).toString(), |
Jian Li | 019ce6a | 2020-09-09 10:23:21 +0900 | [diff] [blame] | 68 | host.routerBridges().stream().map(K8sRouterBridge::name) |
| 69 | .collect(Collectors.toSet()).toString(), |
Jian Li | 077b07e | 2020-09-01 16:55:25 +0900 | [diff] [blame] | 70 | 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 | } |