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