blob: ded79aaa6eba91363febea576a5a177b550a3932 [file] [log] [blame]
Jian Lif97a07e2021-01-13 18:05:00 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
22import org.apache.commons.lang.StringUtils;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
27import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService;
28
29import java.util.Comparator;
30import java.util.List;
31
32import static org.onosproject.kubevirtnetworking.api.Constants.CLI_ID_LENGTH;
33import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
34import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
35import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
36import static org.onosproject.kubevirtnetworking.api.Constants.CLI_SEG_ID_LENGTH;
37import static org.onosproject.kubevirtnetworking.api.Constants.CLI_TYPE_LENGTH;
38import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
39import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
40
41/**
42 * Lists kubevirt networks.
43 */
44@Service
45@Command(scope = "onos", name = "kubevirt-networks",
46 description = "Lists all kubevirt networks")
47public class KubevirtListNetworkCommand extends AbstractShellCommand {
48 @Override
49 protected void doExecute() throws Exception {
50 KubevirtNetworkService service = get(KubevirtNetworkService.class);
51 List<KubevirtNetwork> networks = Lists.newArrayList(service.networks());
52 networks.sort(Comparator.comparing(KubevirtNetwork::name));
53
54 String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH,
55 CLI_TYPE_LENGTH, CLI_SEG_ID_LENGTH, CLI_IP_ADDRESS_LENGTH));
56
57 if (outputJson()) {
58 print("%s", json(networks));
59 } else {
60 print(format, "ID", "Name", "Type", "SegId", "Gateway");
61
62 for (KubevirtNetwork net: networks) {
63 print(format,
64 StringUtils.substring(net.networkId(),
65 0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH),
66 StringUtils.substring(net.name(),
67 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
68 net.type().toString(),
69 net.segmentId() == null ? "N/A" : net.segmentId(),
Jian Lie2abe812021-08-12 18:03:30 +090070 net.gatewayIp() == null ? "N/A" : net.gatewayIp().toString());
Jian Lif97a07e2021-01-13 18:05:00 +090071 }
72 }
73 }
74
75 private String json(List<KubevirtNetwork> networks) {
76 ObjectMapper mapper = new ObjectMapper();
77 ArrayNode result = mapper.createArrayNode();
78
79 for (KubevirtNetwork network : networks) {
80 result.add(jsonForEntity(network, KubevirtNetwork.class));
81 }
82 return prettyJson(mapper, result.toString());
83 }
84}