Jian Li | f4523d8 | 2019-07-07 01:06:09 +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; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Jian Li | f4523d8 | 2019-07-07 01:06:09 +0900 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
| 23 | import io.fabric8.kubernetes.api.model.networking.NetworkPolicy; |
| 24 | import io.fabric8.kubernetes.client.utils.Serialization; |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 25 | import org.apache.commons.lang.StringUtils; |
Jian Li | f4523d8 | 2019-07-07 01:06:09 +0900 | [diff] [blame] | 26 | import org.apache.karaf.shell.api.action.Command; |
| 27 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 28 | import org.onosproject.cli.AbstractShellCommand; |
| 29 | import org.onosproject.k8snetworking.api.K8sNetworkPolicyService; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.util.Comparator; |
| 33 | import java.util.List; |
| 34 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 35 | import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH; |
| 36 | import static org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH; |
| 37 | import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH; |
| 38 | import static org.onosproject.k8snetworking.api.Constants.CLI_TYPES_LENGTH; |
| 39 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString; |
Jian Li | f4523d8 | 2019-07-07 01:06:09 +0900 | [diff] [blame] | 40 | import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson; |
| 41 | |
| 42 | /** |
| 43 | * Lists kubernetes network policies. |
| 44 | */ |
| 45 | @Service |
| 46 | @Command(scope = "onos", name = "k8s-network-policies", |
| 47 | description = "Lists all kubernetes network policies") |
| 48 | public class K8sNetworkPolicyListCommand extends AbstractShellCommand { |
| 49 | |
Jian Li | f4523d8 | 2019-07-07 01:06:09 +0900 | [diff] [blame] | 50 | @Override |
| 51 | protected void doExecute() { |
| 52 | K8sNetworkPolicyService service = get(K8sNetworkPolicyService.class); |
| 53 | List<NetworkPolicy> policies = Lists.newArrayList(service.networkPolicies()); |
| 54 | policies.sort(Comparator.comparing(p -> p.getMetadata().getName())); |
| 55 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 56 | String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, |
| 57 | CLI_NAMESPACE_LENGTH, CLI_TYPES_LENGTH)); |
| 58 | |
Jian Li | f4523d8 | 2019-07-07 01:06:09 +0900 | [diff] [blame] | 59 | if (outputJson()) { |
| 60 | print("%s", json(policies)); |
| 61 | } else { |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 62 | print(format, "Name", "Namespace", "Types"); |
Jian Li | f4523d8 | 2019-07-07 01:06:09 +0900 | [diff] [blame] | 63 | |
| 64 | for (NetworkPolicy policy : policies) { |
| 65 | |
Jian Li | a6f5838 | 2019-12-16 14:22:13 +0900 | [diff] [blame] | 66 | print(format, |
| 67 | StringUtils.substring(policy.getMetadata().getName(), |
| 68 | 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), |
| 69 | StringUtils.substring(policy.getMetadata().getNamespace(), |
| 70 | 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH), |
Jian Li | f4523d8 | 2019-07-07 01:06:09 +0900 | [diff] [blame] | 71 | policy.getSpec().getPolicyTypes().isEmpty() ? |
| 72 | "" : policy.getSpec().getPolicyTypes()); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private String json(List<NetworkPolicy> policies) { |
| 78 | ObjectMapper mapper = new ObjectMapper(); |
| 79 | ArrayNode result = mapper.createArrayNode(); |
| 80 | |
| 81 | try { |
| 82 | for (NetworkPolicy policy : policies) { |
| 83 | ObjectNode json = (ObjectNode) new ObjectMapper() |
| 84 | .readTree(Serialization.asJson(policy)); |
| 85 | result.add(json); |
| 86 | } |
| 87 | return prettyJson(mapper, result.toString()); |
| 88 | } catch (IOException e) { |
| 89 | log.warn("Failed to parse Network Policy's JSON string."); |
| 90 | return ""; |
| 91 | } |
| 92 | } |
| 93 | } |