Jian Li | bde20bf | 2019-01-25 17:34:43 +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.k8snetworking.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | dad2343 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Jian Li | dad2343 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 22 | import org.apache.commons.lang.StringUtils; |
Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame] | 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.k8snetworking.api.K8sNetwork; |
| 27 | import org.onosproject.k8snetworking.api.K8sNetworkService; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
| 31 | |
Jian Li | dad2343 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 32 | import static org.onosproject.k8snetworking.api.Constants.CLI_ID_LENGTH; |
| 33 | import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH; |
| 34 | import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 35 | import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH; |
| 36 | import static org.onosproject.k8snetworking.api.Constants.CLI_SEG_ID_LENGTH; |
| 37 | import static org.onosproject.k8snetworking.api.Constants.CLI_TYPE_LENGTH; |
| 38 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString; |
Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame] | 39 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson; |
| 40 | |
| 41 | /** |
| 42 | * Lists kubernetes networks. |
| 43 | */ |
| 44 | @Service |
| 45 | @Command(scope = "onos", name = "k8s-networks", |
| 46 | description = "Lists all kubernetes networks") |
| 47 | public class K8sNetworkListCommand extends AbstractShellCommand { |
| 48 | |
Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame] | 49 | @Override |
| 50 | protected void doExecute() { |
| 51 | K8sNetworkService service = get(K8sNetworkService.class); |
| 52 | List<K8sNetwork> networks = Lists.newArrayList(service.networks()); |
| 53 | networks.sort(Comparator.comparing(K8sNetwork::name)); |
| 54 | |
Jian Li | dad2343 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 55 | String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH, |
| 56 | CLI_TYPE_LENGTH, CLI_SEG_ID_LENGTH, CLI_IP_ADDRESS_LENGTH)); |
| 57 | |
Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame] | 58 | if (outputJson()) { |
| 59 | print("%s", json(networks)); |
| 60 | } else { |
Jian Li | dad2343 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 61 | print(format, "ID", "Name", "Type", "SegId", "Gateway"); |
Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame] | 62 | |
| 63 | for (K8sNetwork net: networks) { |
Jian Li | dad2343 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 64 | print(format, |
| 65 | StringUtils.substring(net.networkId(), |
| 66 | 0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH), |
| 67 | StringUtils.substring(net.name(), |
| 68 | 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
Jian Li | bde20bf | 2019-01-25 17:34:43 +0900 | [diff] [blame] | 69 | net.type().toString(), |
| 70 | net.segmentId(), |
| 71 | net.gatewayIp() == null ? "" : net.gatewayIp().toString()); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private String json(List<K8sNetwork> networks) { |
| 77 | ObjectMapper mapper = new ObjectMapper(); |
| 78 | ArrayNode result = mapper.createArrayNode(); |
| 79 | |
| 80 | for (K8sNetwork network : networks) { |
| 81 | result.add(jsonForEntity(network, K8sNetwork.class)); |
| 82 | } |
| 83 | return prettyJson(mapper, result.toString()); |
| 84 | } |
| 85 | } |