blob: 9e988cae2294d9db0bb74ea400f09c874951545f [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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.cli;
Hyunsun Moonde372572016-01-14 03:42:47 -080017
Hyunsun Moon81a13562016-08-04 13:48:08 -070018import com.jcraft.jsch.Session;
Hyunsun Moonde372572016-01-14 03:42:47 -080019import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Hyunsun Moon81a13562016-08-04 13:48:08 -070021import org.onlab.packet.IpAddress;
Hyunsun Moonde372572016-01-14 03:42:47 -080022import org.onosproject.cli.AbstractShellCommand;
Hyunsun Moon81a13562016-08-04 13:48:08 -070023import org.onosproject.net.DeviceId;
24import org.onosproject.net.Port;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070025import org.opencord.cordvtn.api.node.CordVtnNode;
alshabibb4d31712016-06-01 18:51:03 -070026import org.opencord.cordvtn.impl.CordVtnNodeManager;
Hyunsun Moon479b7752016-05-06 20:13:28 -070027import org.onosproject.net.Device;
28import org.onosproject.net.device.DeviceService;
Hyunsun Moon81a13562016-08-04 13:48:08 -070029
30import java.util.Set;
31
32import static org.onosproject.net.AnnotationKeys.PORT_NAME;
33import static org.opencord.cordvtn.api.Constants.*;
34import static org.opencord.cordvtn.impl.RemoteIpCommandUtil.*;
Hyunsun Moonde372572016-01-14 03:42:47 -080035
36/**
37 * Checks detailed node init state.
38 */
39@Command(scope = "onos", name = "cordvtn-node-check",
40 description = "Shows detailed node init state")
41public 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 Moon6066bd32016-10-24 15:35:34 -070047 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 Moonde372572016-01-14 03:42:47 -080052 @Override
53 protected void execute() {
Hyunsun Mooncb799442016-01-15 20:03:18 -080054 CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class);
Hyunsun Moon479b7752016-05-06 20:13:28 -070055 DeviceService deviceService = AbstractShellCommand.get(DeviceService.class);
56
Hyunsun Mooncb799442016-01-15 20:03:18 -080057 CordVtnNode node = nodeManager.getNodes()
Hyunsun Moonde372572016-01-14 03:42:47 -080058 .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 Moon6066bd32016-10-24 15:35:34 -070067 print("Current state: %s (%s)", getState(nodeManager, node), HINT);
Hyunsun Moon81a13562016-08-04 13:48:08 -070068 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 Moon479b7752016-05-06 20:13:28 -070077
Hyunsun Moon81a13562016-08-04 13:48:08 -070078 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 Moon479b7752016-05-06 20:13:28 -070086
Hyunsun Moon81a13562016-08-04 13:48:08 -070087 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 Moonde372572016-01-14 03:42:47 -0800137 }
Hyunsun Moon6066bd32016-10-24 15:35:34 -0700138
139 private String getState(CordVtnNodeManager nodeManager, CordVtnNode node) {
140 return nodeManager.isNodeInitComplete(node) ? COMPLETE : INCOMPLETE;
141 }
Hyunsun Moonde372572016-01-14 03:42:47 -0800142}