Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snode.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.k8snode.api.K8sNode; |
| 24 | import org.onosproject.k8snode.api.K8sNodeService; |
| 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 | |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 30 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 31 | |
| 32 | /** |
| 33 | * Checks detailed node init state. |
| 34 | */ |
| 35 | @Service |
| 36 | @Command(scope = "onos", name = "k8s-node-check", |
| 37 | description = "Shows detailed kubernetes node init state") |
| 38 | public class K8sNodeCheckCommand extends AbstractShellCommand { |
| 39 | |
| 40 | @Argument(index = 0, name = "hostname", description = "Hostname", |
| 41 | required = true, multiValued = false) |
| 42 | @Completion(K8sHostnameCompleter.class) |
| 43 | private String hostname = null; |
| 44 | |
| 45 | private static final String MSG_OK = "OK"; |
| 46 | private static final String MSG_ERROR = "ERROR"; |
| 47 | |
| 48 | @Override |
| 49 | protected void doExecute() { |
| 50 | K8sNodeService nodeService = get(K8sNodeService.class); |
| 51 | DeviceService deviceService = get(DeviceService.class); |
| 52 | |
| 53 | K8sNode node = nodeService.node(hostname); |
| 54 | if (node == null) { |
| 55 | print("Cannot find %s from registered nodes", hostname); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | print("[Integration Bridge Status]"); |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 60 | Device intgBridge = deviceService.getDevice(node.intgBridge()); |
| 61 | if (intgBridge != null) { |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 62 | print("%s %s=%s available=%s %s", |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 63 | deviceService.isAvailable(intgBridge.id()) ? MSG_OK : MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 64 | node.intgBridgeName(), |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 65 | intgBridge.id(), |
| 66 | deviceService.isAvailable(intgBridge.id()), |
| 67 | intgBridge.annotations()); |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 68 | printPortState(deviceService, node.intgBridge(), node.intgBridgePortName()); |
| 69 | printPortState(deviceService, node.intgBridge(), node.intgToExtPatchPortName()); |
| 70 | printPortState(deviceService, node.intgBridge(), node.intgToLocalPatchPortName()); |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 71 | } else { |
| 72 | print("%s %s=%s is not available", |
| 73 | MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 74 | node.intgBridgeName(), |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 75 | node.intgBridge()); |
| 76 | } |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 77 | |
Jian Li | 619fa28 | 2020-09-02 14:45:35 +0900 | [diff] [blame] | 78 | print(""); |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 79 | print("[External Bridge Status]"); |
| 80 | Device extBridge = deviceService.getDevice(node.extBridge()); |
| 81 | if (extBridge != null) { |
| 82 | print("%s %s=%s available=%s %s", |
| 83 | deviceService.isAvailable(extBridge.id()) ? MSG_OK : MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 84 | node.extBridgeName(), |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 85 | extBridge.id(), |
| 86 | deviceService.isAvailable(extBridge.id()), |
| 87 | extBridge.annotations()); |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 88 | printPortState(deviceService, node.extBridge(), node.extToIntgPatchPortName()); |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 89 | } else { |
| 90 | print("%s %s=%s is not available", |
| 91 | MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 92 | node.extBridgeName(), |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 93 | node.extBridge()); |
| 94 | } |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 95 | |
Jian Li | 619fa28 | 2020-09-02 14:45:35 +0900 | [diff] [blame] | 96 | print(""); |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 97 | print("[Local Bridge Status]"); |
| 98 | Device localBridge = deviceService.getDevice(node.localBridge()); |
| 99 | if (localBridge != null) { |
| 100 | print("%s %s=%s available=%s %s", |
| 101 | deviceService.isAvailable(localBridge.id()) ? MSG_OK : MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 102 | node.localBridgeName(), |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 103 | localBridge.id(), |
| 104 | deviceService.isAvailable(localBridge.id()), |
| 105 | localBridge.annotations()); |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 106 | printPortState(deviceService, node.localBridge(), node.localToIntgPatchPortName()); |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 107 | } |
Jian Li | 619fa28 | 2020-09-02 14:45:35 +0900 | [diff] [blame] | 108 | |
| 109 | print(""); |
| 110 | print("[Tunnel Bridge Status]"); |
| 111 | Device tunBridge = deviceService.getDevice(node.tunBridge()); |
| 112 | if (tunBridge != null) { |
| 113 | print("%s %s=%s available=%s %s", |
| 114 | deviceService.isAvailable(tunBridge.id()) ? MSG_OK : MSG_ERROR, |
| 115 | node.tunBridgeName(), |
| 116 | tunBridge.id(), |
| 117 | deviceService.isAvailable(tunBridge.id()), |
| 118 | tunBridge.annotations()); |
| 119 | printPortState(deviceService, node.tunBridge(), node.tunToIntgPatchPortName()); |
| 120 | |
| 121 | if (node.dataIp() != null) { |
| 122 | printPortState(deviceService, node.tunBridge(), node.vxlanPortName()); |
| 123 | printPortState(deviceService, node.tunBridge(), node.grePortName()); |
| 124 | printPortState(deviceService, node.tunBridge(), node.genevePortName()); |
| 125 | } |
| 126 | } |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private void printPortState(DeviceService deviceService, |
| 130 | DeviceId deviceId, String portName) { |
| 131 | Port port = deviceService.getPorts(deviceId).stream() |
| 132 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 133 | p.isEnabled()) |
| 134 | .findAny().orElse(null); |
| 135 | |
| 136 | if (port != null) { |
| 137 | print("%s %s portNum=%s enabled=%s %s", |
| 138 | port.isEnabled() ? MSG_OK : MSG_ERROR, |
| 139 | portName, |
| 140 | port.number(), |
| 141 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 142 | port.annotations()); |
| 143 | } else { |
| 144 | print("%s %s does not exist", MSG_ERROR, portName); |
| 145 | } |
| 146 | } |
| 147 | } |