blob: d0189e4616ec48a3d0d734bdc894bf1870f7a32a [file] [log] [blame]
Hyunsun Moonde372572016-01-14 03:42:47 -08001/*
Brian O'Connor8e57fd52016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Hyunsun Moonde372572016-01-14 03:42:47 -08003 *
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
alshabibb4d31712016-06-01 18:51:03 -070017package org.opencord.cordvtn.cli;
Hyunsun Moonde372572016-01-14 03:42:47 -080018
Hyunsun Moon81a13562016-08-04 13:48:08 -070019import com.jcraft.jsch.Session;
Hyunsun Moonde372572016-01-14 03:42:47 -080020import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
Hyunsun Moon81a13562016-08-04 13:48:08 -070022import org.onlab.packet.IpAddress;
Hyunsun Moonde372572016-01-14 03:42:47 -080023import org.onosproject.cli.AbstractShellCommand;
Hyunsun Moon81a13562016-08-04 13:48:08 -070024import org.onosproject.net.DeviceId;
25import org.onosproject.net.Port;
alshabibb4d31712016-06-01 18:51:03 -070026import org.opencord.cordvtn.api.CordVtnNode;
27import org.opencord.cordvtn.impl.CordVtnNodeManager;
Hyunsun Moon479b7752016-05-06 20:13:28 -070028import org.onosproject.net.Device;
29import org.onosproject.net.device.DeviceService;
Hyunsun Moon81a13562016-08-04 13:48:08 -070030
31import java.util.Set;
32
33import static org.onosproject.net.AnnotationKeys.PORT_NAME;
34import static org.opencord.cordvtn.api.Constants.*;
35import static org.opencord.cordvtn.impl.RemoteIpCommandUtil.*;
Hyunsun Moonde372572016-01-14 03:42:47 -080036
37/**
38 * Checks detailed node init state.
39 */
40@Command(scope = "onos", name = "cordvtn-node-check",
41 description = "Shows detailed node init state")
42public 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 Mooncb799442016-01-15 20:03:18 -080050 CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class);
Hyunsun Moon479b7752016-05-06 20:13:28 -070051 DeviceService deviceService = AbstractShellCommand.get(DeviceService.class);
52
Hyunsun Mooncb799442016-01-15 20:03:18 -080053 CordVtnNode node = nodeManager.getNodes()
Hyunsun Moonde372572016-01-14 03:42:47 -080054 .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 Moon81a13562016-08-04 13:48:08 -070064 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 Moon479b7752016-05-06 20:13:28 -070073
Hyunsun Moon81a13562016-08-04 13:48:08 -070074 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 Moon479b7752016-05-06 20:13:28 -070082
Hyunsun Moon81a13562016-08-04 13:48:08 -070083 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 Moonde372572016-01-14 03:42:47 -0800133 }
134}