Jian Li | 7eb2078 | 2021-02-27 01:10:50 +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; |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 26 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterService; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_FLAG_LENGTH; |
| 34 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH; |
| 35 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH; |
| 36 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 37 | import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH; |
| 38 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString; |
| 39 | import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson; |
| 40 | |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 41 | /** |
| 42 | * Lists kubevirt routers. |
| 43 | */ |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 44 | @Service |
| 45 | @Command(scope = "onos", name = "kubevirt-routers", |
| 46 | description = "Lists all kubevirt routers") |
| 47 | public class KubevirtListRouterCommand extends AbstractShellCommand { |
| 48 | |
| 49 | @Override |
| 50 | protected void doExecute() throws Exception { |
| 51 | KubevirtRouterService service = get(KubevirtRouterService.class); |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 52 | List<KubevirtRouter> routers = Lists.newArrayList(service.routers()); |
| 53 | routers.sort(Comparator.comparing(KubevirtRouter::name)); |
| 54 | |
| 55 | String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, |
| 56 | CLI_FLAG_LENGTH, CLI_IP_ADDRESSES_LENGTH, CLI_IP_ADDRESS_LENGTH)); |
| 57 | |
| 58 | if (outputJson()) { |
| 59 | print("%s", json(routers)); |
| 60 | } else { |
| 61 | print(format, "Name", "SNAT", "Internal", "External"); |
| 62 | |
| 63 | for (KubevirtRouter router : routers) { |
| 64 | Set<String> internalCidrs = router.internal(); |
| 65 | Set<String> externalIps = router.external().keySet(); |
| 66 | |
| 67 | String internal = internalCidrs.size() == 0 ? "[]" : internalCidrs.toString(); |
| 68 | String external = externalIps.size() == 0 ? "[]" : externalIps.toString(); |
| 69 | |
| 70 | print(format, StringUtils.substring(router.name(), 0, |
| 71 | CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
| 72 | StringUtils.substring(String.valueOf(router.enableSnat()), 0, |
| 73 | CLI_FLAG_LENGTH - CLI_MARGIN_LENGTH), |
| 74 | StringUtils.substring(internal, 0, |
| 75 | CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH), |
| 76 | StringUtils.substring(external, 0, |
| 77 | CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH) |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private String json(List<KubevirtRouter> routers) { |
| 84 | ObjectMapper mapper = new ObjectMapper(); |
| 85 | ArrayNode result = mapper.createArrayNode(); |
| 86 | |
| 87 | for (KubevirtRouter router : routers) { |
| 88 | result.add(jsonForEntity(router, KubevirtRouter.class)); |
| 89 | } |
| 90 | |
| 91 | return prettyJson(mapper, result.toString()); |
| 92 | } |
| 93 | } |