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 | if (node.dataIp() != null) { |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 72 | printPortState(deviceService, node.intgBridge(), node.vxlanPortName()); |
| 73 | printPortState(deviceService, node.intgBridge(), node.grePortName()); |
| 74 | printPortState(deviceService, node.intgBridge(), node.genevePortName()); |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 75 | } |
| 76 | } else { |
| 77 | print("%s %s=%s is not available", |
| 78 | MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 79 | node.intgBridgeName(), |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 80 | node.intgBridge()); |
| 81 | } |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 82 | |
| 83 | print("[External Bridge Status]"); |
| 84 | Device extBridge = deviceService.getDevice(node.extBridge()); |
| 85 | if (extBridge != null) { |
| 86 | print("%s %s=%s available=%s %s", |
| 87 | deviceService.isAvailable(extBridge.id()) ? MSG_OK : MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 88 | node.extBridgeName(), |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 89 | extBridge.id(), |
| 90 | deviceService.isAvailable(extBridge.id()), |
| 91 | extBridge.annotations()); |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 92 | printPortState(deviceService, node.extBridge(), node.extBridgePortName()); |
| 93 | printPortState(deviceService, node.extBridge(), node.extToIntgPatchPortName()); |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 94 | } else { |
| 95 | print("%s %s=%s is not available", |
| 96 | MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 97 | node.extBridgeName(), |
Jian Li | 8a98804 | 2019-05-03 23:41:19 +0900 | [diff] [blame] | 98 | node.extBridge()); |
| 99 | } |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 100 | |
| 101 | print("[Local Bridge Status]"); |
| 102 | Device localBridge = deviceService.getDevice(node.localBridge()); |
| 103 | if (localBridge != null) { |
| 104 | print("%s %s=%s available=%s %s", |
| 105 | deviceService.isAvailable(localBridge.id()) ? MSG_OK : MSG_ERROR, |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 106 | node.localBridgeName(), |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 107 | localBridge.id(), |
| 108 | deviceService.isAvailable(localBridge.id()), |
| 109 | localBridge.annotations()); |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 110 | printPortState(deviceService, node.localBridge(), node.localBridgePortName()); |
| 111 | printPortState(deviceService, node.localBridge(), node.localToIntgPatchPortName()); |
Jian Li | 1a2eb5d | 2019-08-27 02:07:05 +0900 | [diff] [blame] | 112 | } |
Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private void printPortState(DeviceService deviceService, |
| 116 | DeviceId deviceId, String portName) { |
| 117 | Port port = deviceService.getPorts(deviceId).stream() |
| 118 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 119 | p.isEnabled()) |
| 120 | .findAny().orElse(null); |
| 121 | |
| 122 | if (port != null) { |
| 123 | print("%s %s portNum=%s enabled=%s %s", |
| 124 | port.isEnabled() ? MSG_OK : MSG_ERROR, |
| 125 | portName, |
| 126 | port.number(), |
| 127 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 128 | port.annotations()); |
| 129 | } else { |
| 130 | print("%s %s does not exist", MSG_ERROR, portName); |
| 131 | } |
| 132 | } |
| 133 | } |