blob: 6eeb220fb084243d0cf2e4b78bed1f5b684b5ff6 [file] [log] [blame]
Daniel Park5ff76b72022-09-26 22:58:53 +09001/*
2 * Copyright 2022-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.kubevirtnode.cli;
17
18import com.google.common.collect.ImmutableList;
19import org.apache.commons.lang.StringUtils;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onlab.packet.IpAddress;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfig;
25import org.onosproject.kubevirtnode.api.KubernetesExternalLbConfigService;
26
27import static org.onosproject.kubevirtnode.api.Constants.CLI_IP_ADDRESSES_LENGTH;
28import static org.onosproject.kubevirtnode.api.Constants.CLI_IP_ADDRESS_LENGTH;
29import static org.onosproject.kubevirtnode.api.Constants.CLI_MAC_ADDRESS_LENGTH;
30import static org.onosproject.kubevirtnode.api.Constants.CLI_MARGIN_LENGTH;
31import static org.onosproject.kubevirtnode.api.Constants.CLI_NAME_LENGTH;
32import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.genFormatString;
33
34/**
35 * Lists all Kubernetes External LB config registered to the service.
36 */
37@Service
38@Command(scope = "onos", name = "kubernetes-lb-configs",
39 description = "Lists all Kubernetes External LB config registered to the service")
40public class KubernetesExternalLbConfigCommand extends AbstractShellCommand {
41
42 private static final String KUBE_VIP = "kubevip";
43
44 @Override
45 protected void doExecute() throws Exception {
46 KubernetesExternalLbConfigService service = get(KubernetesExternalLbConfigService.class);
47
48 String format = genFormatString(ImmutableList.of(CLI_NAME_LENGTH,
49 org.onosproject.kubevirtnode.api.Constants.CLI_IP_ADDRESS_LENGTH,
50 CLI_MAC_ADDRESS_LENGTH, CLI_IP_ADDRESSES_LENGTH));
51
52 KubernetesExternalLbConfig lbConfig = service.lbConfig(KUBE_VIP);
53
54 if (lbConfig == null) {
55 print("LB config not found!");
56 } else {
57 print(format, "ConfigName", "Gateway IP", "Gateway MAC", "Global-Range");
58
59 String configName = lbConfig.configName();
60 IpAddress gatewayIp = lbConfig.loadBalancerGwIp();
61 String gatewayMac = lbConfig.loadBalancerGwMac() == null ? "N/A" : lbConfig.loadBalancerGwMac().toString();
62 String globalRange = lbConfig.globalIpRange() == null ? "N/A" : lbConfig.globalIpRange();
63
64 print(format, StringUtils.substring(configName, 0,
65 CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
66 StringUtils.substring(gatewayIp.toString(), 0,
67 CLI_IP_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
68 StringUtils.substring(gatewayMac, 0,
69 CLI_MAC_ADDRESS_LENGTH - CLI_MARGIN_LENGTH),
70 StringUtils.substring(globalRange, 0,
71 CLI_IP_ADDRESSES_LENGTH - CLI_MARGIN_LENGTH)
72 );
73 }
74 }
75}