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