blob: e3562e26d36130edc70932c0ec11016bfa389cd5 [file] [log] [blame]
Jian Libde20bf2019-01-25 17:34:43 +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;
Jian Lia6f58382019-12-16 14:22:13 +090020import com.google.common.collect.ImmutableList;
Jian Libde20bf2019-01-25 17:34:43 +090021import com.google.common.collect.Lists;
Jian Lia6f58382019-12-16 14:22:13 +090022import org.apache.commons.lang.StringUtils;
Jian Libde20bf2019-01-25 17:34:43 +090023import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.k8snetworking.api.K8sNetwork;
27import org.onosproject.k8snetworking.api.K8sNetworkService;
28
29import java.util.Comparator;
30import java.util.List;
31
Jian Lia6f58382019-12-16 14:22:13 +090032import static org.onosproject.k8snetworking.api.Constants.CLI_ID_LENGTH;
33import static org.onosproject.k8snetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
34import static org.onosproject.k8snetworking.api.Constants.CLI_MARGIN_LENGTH;
35import static org.onosproject.k8snetworking.api.Constants.CLI_NAME_LENGTH;
36import static org.onosproject.k8snetworking.api.Constants.CLI_SEG_ID_LENGTH;
37import static org.onosproject.k8snetworking.api.Constants.CLI_TYPE_LENGTH;
38import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.genFormatString;
Jian Libde20bf2019-01-25 17:34:43 +090039import 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")
47public class K8sNetworkListCommand extends AbstractShellCommand {
48
Jian Libde20bf2019-01-25 17:34:43 +090049 @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 Lia6f58382019-12-16 14:22:13 +090055 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 Libde20bf2019-01-25 17:34:43 +090058 if (outputJson()) {
59 print("%s", json(networks));
60 } else {
Jian Lia6f58382019-12-16 14:22:13 +090061 print(format, "ID", "Name", "Type", "SegId", "Gateway");
Jian Libde20bf2019-01-25 17:34:43 +090062
63 for (K8sNetwork net: networks) {
Jian Lia6f58382019-12-16 14:22:13 +090064 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 Libde20bf2019-01-25 17:34:43 +090069 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}