Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.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.kubevirtnetworking.api.KubevirtLoadBalancer; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerService; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH; |
| 33 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH; |
| 34 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 35 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH; |
| 36 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString; |
| 37 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson; |
| 38 | |
| 39 | /** |
| 40 | * Lists kubevirt load balancers. |
| 41 | */ |
| 42 | @Service |
| 43 | @Command(scope = "onos", name = "kubevirt-lbs", |
| 44 | description = "Lists all kubevirt load balancers") |
| 45 | public class KubevirtListLoadBalancerCommand extends AbstractShellCommand { |
| 46 | @Override |
| 47 | protected void doExecute() throws Exception { |
| 48 | KubevirtLoadBalancerService service = get(KubevirtLoadBalancerService.class); |
| 49 | List<KubevirtLoadBalancer> lbs = Lists.newArrayList(service.loadBalancers()); |
| 50 | lbs.sort(Comparator.comparing(KubevirtLoadBalancer::name)); |
| 51 | |
| 52 | String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_NAME_LENGTH, |
| 53 | CLI_IP_ADDRESS_LENGTH, CLI_IP_ADDRESSES_LENGTH)); |
| 54 | |
| 55 | if (outputJson()) { |
| 56 | print("%s", json(lbs)); |
| 57 | } else { |
| 58 | print(format, "Name", "NetworkId", "Virtual IP", "Members"); |
| 59 | |
| 60 | for (KubevirtLoadBalancer lb : lbs) { |
| 61 | print(format, |
| 62 | StringUtils.substring(lb.name(), |
| 63 | 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
| 64 | StringUtils.substring(lb.networkId(), |
| 65 | 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
| 66 | StringUtils.substring(lb.vip().toString(), |
| 67 | 0, CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), |
| 68 | lb.members().toString()); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private String json(List<KubevirtLoadBalancer> lbs) { |
| 74 | ObjectMapper mapper = new ObjectMapper(); |
| 75 | ArrayNode result = mapper.createArrayNode(); |
| 76 | |
| 77 | for (KubevirtLoadBalancer lb : lbs) { |
| 78 | result.add(jsonForEntity(lb, KubevirtLoadBalancer.class)); |
| 79 | } |
| 80 | |
| 81 | return prettyJson(mapper, result.toString()); |
| 82 | } |
| 83 | } |