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