Search in sources :

Example 1 with K8sNetworkPolicyService

use of org.onosproject.k8snetworking.api.K8sNetworkPolicyService in project onos by opennetworkinglab.

the class K8sNetworkPolicyListCommand method doExecute.

@Override
protected void doExecute() {
    K8sNetworkPolicyService service = get(K8sNetworkPolicyService.class);
    List<NetworkPolicy> policies = Lists.newArrayList(service.networkPolicies());
    policies.sort(Comparator.comparing(p -> p.getMetadata().getName()));
    String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH, CLI_NAMESPACE_LENGTH, CLI_TYPES_LENGTH));
    if (outputJson()) {
        print("%s", json(policies));
    } else {
        print(format, "Name", "Namespace", "Types");
        for (NetworkPolicy policy : policies) {
            print(format, StringUtils.substring(policy.getMetadata().getName(), 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH), StringUtils.substring(policy.getMetadata().getNamespace(), 0, CLI_NAMESPACE_LENGTH - CLI_MARGIN_LENGTH), policy.getSpec().getPolicyTypes().isEmpty() ? "" : policy.getSpec().getPolicyTypes());
        }
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) CLI_TYPES_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_TYPES_LENGTH) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.apache.karaf.shell.api.action.Command) CLI_NAME_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) CLI_NAMESPACE_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Serialization(io.fabric8.kubernetes.client.utils.Serialization) K8sNetworkPolicyService(org.onosproject.k8snetworking.api.K8sNetworkPolicyService) K8sNetworkingUtil.prettyJson(org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson) Service(org.apache.karaf.shell.api.action.lifecycle.Service) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) CLI_MARGIN_LENGTH(org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH) Comparator(java.util.Comparator) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) K8sNetworkingUtil.genFormatString(org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString) K8sNetworkPolicyService(org.onosproject.k8snetworking.api.K8sNetworkPolicyService)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 NetworkPolicy (io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy)1 Serialization (io.fabric8.kubernetes.client.utils.Serialization)1 IOException (java.io.IOException)1 Comparator (java.util.Comparator)1 List (java.util.List)1 StringUtils (org.apache.commons.lang.StringUtils)1 Command (org.apache.karaf.shell.api.action.Command)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 AbstractShellCommand (org.onosproject.cli.AbstractShellCommand)1 CLI_MARGIN_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH)1 CLI_NAMESPACE_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_NAMESPACE_LENGTH)1 CLI_NAME_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH)1 CLI_TYPES_LENGTH (org.onosproject.k8snetworking.api.Constants.CLI_TYPES_LENGTH)1 K8sNetworkPolicyService (org.onosproject.k8snetworking.api.K8sNetworkPolicyService)1 K8sNetworkingUtil.genFormatString (org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString)1