blob: 39a18f005a02d552f90d889d68e19cc6715a35e6 [file] [log] [blame]
Jian Li534f3f22021-02-22 21:48:32 +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.kubevirtnode.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Completion;
21import org.apache.karaf.shell.api.action.Option;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.kubevirtnode.api.KubevirtNode;
25import org.onosproject.kubevirtnode.api.KubevirtNodeService;
26import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
27
28import java.util.List;
29
30import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.prettyJson;
31
32/**
33 * Show a node info registered to the service.
34 */
35@Service
36@Command(scope = "onos", name = "kubevirt-node",
37 description = "Displays a node details")
38public class KubevirtShowNodeCommand extends AbstractShellCommand {
39
40 @Option(name = "--name",
41 description = "Filter kubevirt node by specific name", multiValued = true)
42 @Completion(KubevirtHostnameCompleter.class)
43 private List<String> names;
44
45 @Override
46 protected void doExecute() throws Exception {
47 KubevirtNodeService service = get(KubevirtNodeService.class);
48
Jian Li8c553eb2021-03-04 18:48:39 +090049 if (names == null || names.size() == 0) {
50 print("Need to specify at least one node name using --name option.");
51 return;
52 }
53
Jian Li534f3f22021-02-22 21:48:32 +090054 for (String name : names) {
55 KubevirtNode node = service.node(name);
56 if (node == null) {
57 print("Unable to find %s", name);
58 continue;
59 }
60
61 if (outputJson()) {
62 print("%s", json(node));
63 } else {
64 printNode(node);
65 }
66 }
67 }
68
69 private void printNode(KubevirtNode node) {
Daniel Park5ff76b72022-09-26 22:58:53 +090070 print("Node: %s", node.toString());
Jian Li534f3f22021-02-22 21:48:32 +090071 print("Name: %s", node.hostname());
72 print(" Type: %s", node.type());
73 print(" State: %s", node.state());
74 print(" Management IP: %s", node.managementIp().toString());
75 print(" Data IP: %s", node.dataIp().toString());
76 print(" Integration Bridge: %s", node.intgBridge());
77 print(" Tunneling Bridge: %s", node.tunBridge());
78
79 int counter = 1;
80 for (KubevirtPhyInterface intf: node.phyIntfs()) {
81 print(" Physical interfaces #%d:", counter);
82 print(" Network: %s", intf.network());
83 print(" Interface: %s", intf.intf());
84 counter++;
85 }
Daniel Park734b5532022-09-26 15:13:59 +090086
87 if (node.gatewayBridgeName() != null) {
88 print(" GatewayBridgeName: %s", node.gatewayBridgeName());
89 }
90
Daniel Park5ff76b72022-09-26 22:58:53 +090091 if (node.kubernetesExternalLbInterface() != null) {
92 print(" ElbBridgeName: %s", node.kubernetesExternalLbInterface().externalLbBridgeName());
93 print(" ElbIp: %s", node.kubernetesExternalLbInterface().externalLbIp().toString());
94 print(" ElbGwIp: %s", node.kubernetesExternalLbInterface().externalLbGwIp().toString());
95 print(" ElbGwMac: %s", node.kubernetesExternalLbInterface().externalLbGwMac().toString());
Daniel Park734b5532022-09-26 15:13:59 +090096 }
Jian Li534f3f22021-02-22 21:48:32 +090097 }
98
99 private String json(KubevirtNode node) {
100 return prettyJson(new ObjectMapper(),
101 jsonForEntity(node, KubevirtNode.class).toString());
102 }
103}