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