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; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 26 | import org.opencord.cordvtn.impl.CordVtnNodeManager; |
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 | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 47 | private static final String COMPLETE = "COMPLETE"; |
| 48 | private static final String INCOMPLETE = "INCOMPLETE"; |
| 49 | private static final String HINT = "hint: try init again if the state is INCOMPLETE" + |
| 50 | " but all settings OK"; |
| 51 | |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 52 | @Override |
| 53 | protected void execute() { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 54 | CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 55 | DeviceService deviceService = AbstractShellCommand.get(DeviceService.class); |
| 56 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 57 | CordVtnNode node = nodeManager.getNodes() |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 58 | .stream() |
| 59 | .filter(n -> n.hostname().equals(hostname)) |
| 60 | .findFirst() |
| 61 | .orElse(null); |
| 62 | |
| 63 | if (node == null) { |
| 64 | print("Cannot find %s from registered nodes", hostname); |
| 65 | return; |
| 66 | } |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 67 | print("Current state: %s (%s)", getState(nodeManager, node), HINT); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 68 | print("%n[Integration Bridge Status]"); |
| 69 | Device device = deviceService.getDevice(node.integrationBridgeId()); |
| 70 | if (device != null) { |
| 71 | print("%s %s=%s available=%s %s", |
| 72 | deviceService.isAvailable(device.id()) ? MSG_OK : MSG_NO, |
| 73 | INTEGRATION_BRIDGE, |
| 74 | device.id(), |
| 75 | deviceService.isAvailable(device.id()), |
| 76 | device.annotations()); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 77 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 78 | node.systemIfaces().stream().forEach(iface -> print( |
| 79 | getPortState(deviceService, node.integrationBridgeId(), iface))); |
| 80 | } else { |
| 81 | print("%s %s=%s is not available", |
| 82 | MSG_NO, |
| 83 | INTEGRATION_BRIDGE, |
| 84 | node.integrationBridgeId()); |
| 85 | } |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 86 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 87 | print("%n[Interfaces and IP setup]"); |
| 88 | Session session = connect(node.sshInfo()); |
| 89 | if (session != null) { |
| 90 | Set<IpAddress> ips = getCurrentIps(session, INTEGRATION_BRIDGE); |
| 91 | boolean isUp = isInterfaceUp(session, INTEGRATION_BRIDGE); |
| 92 | boolean isIp = ips.contains(node.dataIp().ip()) && ips.contains(node.localMgmtIp().ip()); |
| 93 | |
| 94 | print("%s %s up=%s Ips=%s", |
| 95 | isUp && isIp ? MSG_OK : MSG_NO, |
| 96 | INTEGRATION_BRIDGE, |
| 97 | isUp ? Boolean.TRUE : Boolean.FALSE, |
| 98 | getCurrentIps(session, INTEGRATION_BRIDGE)); |
| 99 | |
| 100 | print(getSystemIfaceState(session, node.dataIface())); |
| 101 | if (node.hostMgmtIface().isPresent()) { |
| 102 | print(getSystemIfaceState(session, node.hostMgmtIface().get())); |
| 103 | } |
| 104 | |
| 105 | disconnect(session); |
| 106 | } else { |
| 107 | print("%s Unable to SSH to %s", MSG_NO, node.hostname()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private String getPortState(DeviceService deviceService, DeviceId deviceId, String portName) { |
| 112 | Port port = deviceService.getPorts(deviceId).stream() |
| 113 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
| 114 | p.isEnabled()) |
| 115 | .findAny().orElse(null); |
| 116 | |
| 117 | if (port != null) { |
| 118 | return String.format("%s %s portNum=%s enabled=%s %s", |
| 119 | port.isEnabled() ? MSG_OK : MSG_NO, |
| 120 | portName, |
| 121 | port.number(), |
| 122 | port.isEnabled() ? Boolean.TRUE : Boolean.FALSE, |
| 123 | port.annotations()); |
| 124 | } else { |
| 125 | return String.format("%s %s does not exist", MSG_NO, portName); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private String getSystemIfaceState(Session session, String iface) { |
| 130 | boolean isUp = isInterfaceUp(session, iface); |
| 131 | boolean isIp = getCurrentIps(session, iface).isEmpty(); |
| 132 | return String.format("%s %s up=%s IpFlushed=%s", |
| 133 | isUp && isIp ? MSG_OK : MSG_NO, |
| 134 | iface, |
| 135 | isUp ? Boolean.TRUE : Boolean.FALSE, |
| 136 | isIp ? Boolean.TRUE : Boolean.FALSE); |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 137 | } |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 138 | |
| 139 | private String getState(CordVtnNodeManager nodeManager, CordVtnNode node) { |
| 140 | return nodeManager.isNodeInitComplete(node) ? COMPLETE : INCOMPLETE; |
| 141 | } |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 142 | } |