Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.cli; |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 17 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 18 | import com.jcraft.jsch.Session; |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Port; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 25 | import org.opencord.cordvtn.api.node.CordVtnNode; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 26 | import org.opencord.cordvtn.api.node.CordVtnNodeService; |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 33 | import static org.opencord.cordvtn.api.Constants.*; |
| 34 | import static org.opencord.cordvtn.impl.RemoteIpCommandUtil.*; |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Checks detailed node init state. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "cordvtn-node-check", |
| 40 | description = "Shows detailed node init state") |
| 41 | public class CordVtnNodeCheckCommand extends AbstractShellCommand { |
| 42 | |
| 43 | @Argument(index = 0, name = "hostname", description = "Hostname", |
| 44 | required = true, multiValued = false) |
| 45 | private String hostname = null; |
| 46 | |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 47 | private static final String HINT = "hint: try init again if the state is not" + |
| 48 | " COMPLETE but all settings are OK"; |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 49 | |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 50 | @Override |
| 51 | protected void execute() { |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 52 | CordVtnNodeService nodeService = AbstractShellCommand.get(CordVtnNodeService.class); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 53 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
| 54 | |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 55 | CordVtnNode node = nodeService.nodes().stream() |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 56 | .filter(n -> n.hostname().equals(hostname)) |
| 57 | .findFirst() |
| 58 | .orElse(null); |
| 59 | |
| 60 | if (node == null) { |
| 61 | print("Cannot find %s from registered nodes", hostname); |
| 62 | return; |
| 63 | } |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 64 | print("Current state: %s (%s)", node.state().name(), HINT); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 65 | print("%n[Integration Bridge Status]"); |
| 66 | Device device = deviceService.getDevice(node.integrationBridgeId()); |
| 67 | if (device != null) { |
| 68 | print("%s %s=%s available=%s %s", |
| 69 | deviceService.isAvailable(device.id()) ? MSG_OK : MSG_NO, |
| 70 | INTEGRATION_BRIDGE, |
| 71 | device.id(), |
| 72 | deviceService.isAvailable(device.id()), |
| 73 | device.annotations()); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 74 | |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 75 | node.systemInterfaces().forEach(iface -> print( |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 76 | getPortState(deviceService, node.integrationBridgeId(), iface))); |
| 77 | } else { |
| 78 | print("%s %s=%s is not available", |
| 79 | MSG_NO, |
| 80 | INTEGRATION_BRIDGE, |
| 81 | node.integrationBridgeId()); |
| 82 | } |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 83 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 84 | print("%n[Interfaces and IP setup]"); |
| 85 | Session session = connect(node.sshInfo()); |
| 86 | if (session != null) { |
| 87 | Set<IpAddress> ips = getCurrentIps(session, INTEGRATION_BRIDGE); |
| 88 | boolean isUp = isInterfaceUp(session, INTEGRATION_BRIDGE); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 89 | boolean isIp = ips.contains(node.dataIp().ip()) && |
| 90 | ips.contains(node.localManagementIp().ip()); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 91 | |
| 92 | print("%s %s up=%s Ips=%s", |
| 93 | isUp && isIp ? MSG_OK : MSG_NO, |
| 94 | INTEGRATION_BRIDGE, |
| 95 | isUp ? Boolean.TRUE : Boolean.FALSE, |
| 96 | getCurrentIps(session, INTEGRATION_BRIDGE)); |
| 97 | |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 98 | print(getSystemIfaceState(session, node.dataInterface())); |
| 99 | if (node.hostManagementInterface() != null) { |
| 100 | print(getSystemIfaceState(session, node.hostManagementInterface())); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | disconnect(session); |
| 104 | } else { |
| 105 | print("%s Unable to SSH to %s", MSG_NO, node.hostname()); |
| 106 | } |
| 107 | } |
| 108 | |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 109 | private String getPortState(DeviceService deviceService, DeviceId deviceId, |
| 110 | String portName) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 111 | Port port = deviceService.getPorts(deviceId).stream() |
| 112 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 113 | p.isEnabled()) |
| 114 | .findAny().orElse(null); |
| 115 | |
| 116 | if (port != null) { |
| 117 | return String.format("%s %s portNum=%s enabled=%s %s", |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 118 | port.isEnabled() ? MSG_OK : MSG_NO, |
| 119 | portName, |
| 120 | port.number(), |
| 121 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 122 | port.annotations()); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 123 | } else { |
| 124 | return String.format("%s %s does not exist", MSG_NO, portName); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private String getSystemIfaceState(Session session, String iface) { |
| 129 | boolean isUp = isInterfaceUp(session, iface); |
| 130 | boolean isIp = getCurrentIps(session, iface).isEmpty(); |
| 131 | return String.format("%s %s up=%s IpFlushed=%s", |
| 132 | isUp && isIp ? MSG_OK : MSG_NO, |
| 133 | iface, |
| 134 | isUp ? Boolean.TRUE : Boolean.FALSE, |
| 135 | isIp ? Boolean.TRUE : Boolean.FALSE); |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 136 | } |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 137 | } |