Jian Li | 534f3f2 | 2021-02-22 21:48:32 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnode.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.Completion; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 25 | import org.onosproject.kubevirtnode.api.KubevirtNodeService; |
| 26 | import org.onosproject.kubevirtnode.api.KubevirtPhyInterface; |
| 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | import 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") |
| 38 | public 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 Li | 8c553eb | 2021-03-04 18:48:39 +0900 | [diff] [blame^] | 49 | if (names == null || names.size() == 0) { |
| 50 | print("Need to specify at least one node name using --name option."); |
| 51 | return; |
| 52 | } |
| 53 | |
Jian Li | 534f3f2 | 2021-02-22 21:48:32 +0900 | [diff] [blame] | 54 | 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) { |
| 70 | print("Name: %s", node.hostname()); |
| 71 | print(" Type: %s", node.type()); |
| 72 | print(" State: %s", node.state()); |
| 73 | print(" Management IP: %s", node.managementIp().toString()); |
| 74 | print(" Data IP: %s", node.dataIp().toString()); |
| 75 | print(" Integration Bridge: %s", node.intgBridge()); |
| 76 | print(" Tunneling Bridge: %s", node.tunBridge()); |
| 77 | |
| 78 | int counter = 1; |
| 79 | for (KubevirtPhyInterface intf: node.phyIntfs()) { |
| 80 | print(" Physical interfaces #%d:", counter); |
| 81 | print(" Network: %s", intf.network()); |
| 82 | print(" Interface: %s", intf.intf()); |
| 83 | counter++; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private String json(KubevirtNode node) { |
| 88 | return prettyJson(new ObjectMapper(), |
| 89 | jsonForEntity(node, KubevirtNode.class).toString()); |
| 90 | } |
| 91 | } |