Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +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 org.apache.karaf.shell.api.action.Argument; |
| 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.lifecycle.Service; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 24 | import org.onosproject.kubevirtnode.api.KubevirtNodeService; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | |
| 30 | import static org.onosproject.kubevirtnode.api.Constants.GENEVE; |
| 31 | import static org.onosproject.kubevirtnode.api.Constants.GRE; |
| 32 | import static org.onosproject.kubevirtnode.api.Constants.INTEGRATION_BRIDGE; |
| 33 | import static org.onosproject.kubevirtnode.api.Constants.TUNNEL_BRIDGE; |
| 34 | import static org.onosproject.kubevirtnode.api.Constants.VXLAN; |
| 35 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 36 | |
| 37 | /** |
| 38 | * Checks detailed node init state. |
| 39 | */ |
| 40 | @Service |
| 41 | @Command(scope = "onos", name = "kubevirt-check-node", |
| 42 | description = "Shows detailed kubevirt nodes status") |
| 43 | public class KubevirtCheckNodeCommand extends AbstractShellCommand { |
| 44 | |
| 45 | @Argument(index = 0, name = "hostname", description = "Hostname", |
| 46 | required = true, multiValued = false) |
| 47 | @Completion(KubevirtHostnameCompleter.class) |
| 48 | private String hostname = null; |
| 49 | |
| 50 | private static final String MSG_PASS = "PASS"; |
| 51 | private static final String MSG_FAIL = "FAIL"; |
| 52 | |
| 53 | @Override |
| 54 | protected void doExecute() throws Exception { |
| 55 | KubevirtNodeService nodeService = get(KubevirtNodeService.class); |
| 56 | DeviceService deviceService = get(DeviceService.class); |
| 57 | |
| 58 | KubevirtNode node = nodeService.node(hostname); |
| 59 | if (node == null) { |
| 60 | print("Cannot find %s from registered nodes", hostname); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | print("[Integration Bridge Status]"); |
| 65 | Device intgBridge = deviceService.getDevice(node.intgBridge()); |
| 66 | if (intgBridge != null) { |
| 67 | print("%s %s=%s available=%s %s", |
| 68 | deviceService.isAvailable(intgBridge.id()) ? MSG_PASS : MSG_FAIL, |
| 69 | INTEGRATION_BRIDGE, |
| 70 | intgBridge.id(), |
| 71 | deviceService.isAvailable(intgBridge.id()), |
| 72 | intgBridge.annotations()); |
| 73 | } |
| 74 | |
| 75 | print(""); |
| 76 | print("[Tunnel Bridge Status]"); |
| 77 | Device tunBridge = deviceService.getDevice(node.tunBridge()); |
| 78 | if (tunBridge != null) { |
| 79 | print("%s %s=%s available=%s %s", |
| 80 | deviceService.isAvailable(tunBridge.id()) ? MSG_PASS : MSG_FAIL, |
| 81 | TUNNEL_BRIDGE, |
| 82 | tunBridge.id(), |
| 83 | deviceService.isAvailable(tunBridge.id()), |
| 84 | tunBridge.annotations()); |
| 85 | |
| 86 | if (node.dataIp() != null) { |
| 87 | printPortState(deviceService, node.tunBridge(), VXLAN); |
| 88 | printPortState(deviceService, node.tunBridge(), GRE); |
| 89 | printPortState(deviceService, node.tunBridge(), GENEVE); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private void printPortState(DeviceService deviceService, |
| 95 | DeviceId deviceId, String portName) { |
| 96 | Port port = deviceService.getPorts(deviceId).stream() |
| 97 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 98 | p.isEnabled()) |
| 99 | .findAny().orElse(null); |
| 100 | |
| 101 | if (port != null) { |
| 102 | print("%s %s portNum=%s enabled=%s %s", |
| 103 | port.isEnabled() ? MSG_PASS : MSG_FAIL, |
| 104 | portName, |
| 105 | port.number(), |
| 106 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 107 | port.annotations()); |
| 108 | } else { |
| 109 | print("%s %s does not exist", MSG_FAIL, portName); |
| 110 | } |
| 111 | } |
| 112 | } |