blob: 772b841f22da1fa89fb40c40d82f3eb517c49e27 [file] [log] [blame]
Jian Li7eb20782021-02-27 01:10:50 +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;
Jian Li7eb20782021-02-27 01:10:50 +090026import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
27import org.onosproject.kubevirtnetworking.api.KubevirtRouterService;
28
29import java.util.Comparator;
30import java.util.List;
31import java.util.Set;
32
33import static org.onosproject.kubevirtnetworking.api.Constants.CLI_FLAG_LENGTH;
34import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESSES_LENGTH;
35import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
36import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
37import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
38import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
39import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
40
Jian Lie48a6172021-02-28 03:50:15 +090041/**
42 * Lists kubevirt routers.
43 */
Jian Li7eb20782021-02-27 01:10:50 +090044@Service
45@Command(scope = "onos", name = "kubevirt-routers",
46 description = "Lists all kubevirt routers")
47public class KubevirtListRouterCommand extends AbstractShellCommand {
48
49 @Override
50 protected void doExecute() throws Exception {
51 KubevirtRouterService service = get(KubevirtRouterService.class);
Jian Li7eb20782021-02-27 01:10:50 +090052 List<KubevirtRouter> routers = Lists.newArrayList(service.routers());
53 routers.sort(Comparator.comparing(KubevirtRouter::name));
54
55 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
56 CLI_FLAG_LENGTH, CLI_IP_ADDRESSES_LENGTH, CLI_IP_ADDRESS_LENGTH));
57
58 if (outputJson()) {
59 print("%s", json(routers));
60 } else {
61 print(format, "Name", "SNAT", "Internal", "External");
62
63 for (KubevirtRouter router : routers) {
64 Set<String> internalCidrs = router.internal();
65 Set<String> externalIps = router.external().keySet();
66
67 String internal = internalCidrs.size() == 0 ? "[]" : internalCidrs.toString();
68 String external = externalIps.size() == 0 ? "[]" : externalIps.toString();
69
70 print(format, StringUtils.substring(router.name(), 0,
71 CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
72 StringUtils.substring(String.valueOf(router.enableSnat()), 0,
73 CLI_FLAG_LENGTH - CLI_MARGIN_LENGTH),
74 StringUtils.substring(internal, 0,
75 CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH),
76 StringUtils.substring(external, 0,
77 CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH)
78 );
79 }
80 }
81 }
82
83 private String json(List<KubevirtRouter> routers) {
84 ObjectMapper mapper = new ObjectMapper();
85 ArrayNode result = mapper.createArrayNode();
86
87 for (KubevirtRouter router : routers) {
88 result.add(jsonForEntity(router, KubevirtRouter.class));
89 }
90
91 return prettyJson(mapper, result.toString());
92 }
93}