blob: f86db47caf330e3ff8a6a54a15a9d9e0be67da27 [file] [log] [blame]
Jian Lif4523d82019-07-07 01:06:09 +09001/*
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 */
16package org.onosproject.k8snetworking.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lidad23432019-12-16 14:22:13 +090021import com.google.common.collect.ImmutableList;
Jian Lif4523d82019-07-07 01:06:09 +090022import com.google.common.collect.Lists;
Jian Li9ac73952021-01-15 14:53:22 +090023import io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy;
Jian Lif4523d82019-07-07 01:06:09 +090024import io.fabric8.kubernetes.client.utils.Serialization;
Jian Lidad23432019-12-16 14:22:13 +090025import org.apache.commons.lang.StringUtils;
Jian Lif4523d82019-07-07 01:06:09 +090026import org.apache.karaf.shell.api.action.Command;
27import org.apache.karaf.shell.api.action.lifecycle.Service;
28import org.onosproject.cli.AbstractShellCommand;
29import org.onosproject.k8snetworking.api.K8sNetworkPolicyService;
30
31import java.io.IOException;
32import java.util.Comparator;
33import java.util.List;
34
Jian Lidad23432019-12-16 14:22:13 +090035import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
36import static org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH;
37import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
38import static org.onosproject.k8snetworking.api.Constants.CLI_TYPES_LENGTH;
39import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
Jian Lif4523d82019-07-07 01:06:09 +090040import 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")
48public class K8sNetworkPolicyListCommand extends AbstractShellCommand {
49
Jian Lif4523d82019-07-07 01:06:09 +090050 @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 Lidad23432019-12-16 14:22:13 +090056 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
57 CLI_NAMESPACE_LENGTH, CLI_TYPES_LENGTH));
58
Jian Lif4523d82019-07-07 01:06:09 +090059 if (outputJson()) {
60 print("%s", json(policies));
61 } else {
Jian Lidad23432019-12-16 14:22:13 +090062 print(format, "Name", "Namespace", "Types");
Jian Lif4523d82019-07-07 01:06:09 +090063
64 for (NetworkPolicy policy : policies) {
65
Jian Lidad23432019-12-16 14:22:13 +090066 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 Lif4523d82019-07-07 01:06:09 +090071 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}