blob: 00699a090559430ef5b67a54fc60243261e2889f [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
49 for (String name : names) {
50 KubevirtNode node = service.node(name);
51 if (node == null) {
52 print("Unable to find %s", name);
53 continue;
54 }
55
56 if (outputJson()) {
57 print("%s", json(node));
58 } else {
59 printNode(node);
60 }
61 }
62 }
63
64 private void printNode(KubevirtNode node) {
65 print("Name: %s", node.hostname());
66 print(" Type: %s", node.type());
67 print(" State: %s", node.state());
68 print(" Management IP: %s", node.managementIp().toString());
69 print(" Data IP: %s", node.dataIp().toString());
70 print(" Integration Bridge: %s", node.intgBridge());
71 print(" Tunneling Bridge: %s", node.tunBridge());
72
73 int counter = 1;
74 for (KubevirtPhyInterface intf: node.phyIntfs()) {
75 print(" Physical interfaces #%d:", counter);
76 print(" Network: %s", intf.network());
77 print(" Interface: %s", intf.intf());
78 counter++;
79 }
80 }
81
82 private String json(KubevirtNode node) {
83 return prettyJson(new ObjectMapper(),
84 jsonForEntity(node, KubevirtNode.class).toString());
85 }
86}