Jian Li | 073f1ba | 2021-02-28 03:50:15 +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.KubevirtFloatingIp; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterService; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH; |
| 33 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 34 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH; |
| 35 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString; |
| 36 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson; |
| 37 | |
| 38 | /** |
| 39 | * Lists kubevirt floating IPs. |
| 40 | */ |
| 41 | @Service |
| 42 | @Command(scope = "onos", name = "kubevirt-fips", |
| 43 | description = "Lists all kubevirt floating IPs") |
| 44 | public class KubevirtListFloatingIpCommand extends AbstractShellCommand { |
| 45 | |
| 46 | @Override |
| 47 | protected void doExecute() throws Exception { |
| 48 | KubevirtRouterService service = get(KubevirtRouterService.class); |
| 49 | List<KubevirtFloatingIp> fips = Lists.newArrayList(service.floatingIps()); |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 50 | fips.sort(Comparator.comparing(KubevirtFloatingIp::networkName)); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 51 | |
| 52 | String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, |
| 53 | CLI_IP_ADDRESS_LENGTH, CLI_NAME_LENGTH, CLI_IP_ADDRESS_LENGTH)); |
| 54 | |
| 55 | if (outputJson()) { |
| 56 | print("%s", json(fips)); |
| 57 | } else { |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 58 | print(format, "Network Name", "Floating IP", "POD Name", "Fixed IP"); |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 59 | for (KubevirtFloatingIp fip : fips) { |
| 60 | |
| 61 | String fixedIp = fip.fixedIp() == null ? "N/A" : fip.fixedIp().toString(); |
| 62 | String podName = fip.podName() == null ? "N/A" : fip.podName(); |
| 63 | |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 64 | print(format, StringUtils.substring(fip.networkName(), 0, |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 65 | CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
| 66 | StringUtils.substring(fip.floatingIp().toString(), 0, |
| 67 | CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH), |
| 68 | StringUtils.substring(podName, 0, |
| 69 | CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
| 70 | StringUtils.substring(fixedIp, 0, |
| 71 | CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH) |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private String json(List<KubevirtFloatingIp> fips) { |
| 78 | ObjectMapper mapper = new ObjectMapper(); |
| 79 | ArrayNode result = mapper.createArrayNode(); |
| 80 | |
| 81 | for (KubevirtFloatingIp fip : fips) { |
| 82 | result.add(jsonForEntity(fip, KubevirtFloatingIp.class)); |
| 83 | } |
| 84 | |
| 85 | return prettyJson(mapper, result.toString()); |
| 86 | } |
| 87 | } |