blob: dad481d2dd72cf66c19dfeb7be72b22e146c6748 [file] [log] [blame]
Hyunsun Moonde372572016-01-14 03:42:47 -08001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090026import org.opencord.cordvtn.api.node.CordVtnNodeService;
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 Moon2c3f0ee2017-04-06 16:47:21 +090047 private static final String HINT = "hint: try init again if the state is not" +
48 " COMPLETE but all settings are OK";
Hyunsun Moon6066bd32016-10-24 15:35:34 -070049
Hyunsun Moonde372572016-01-14 03:42:47 -080050 @Override
51 protected void execute() {
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090052 CordVtnNodeService nodeService = AbstractShellCommand.get(CordVtnNodeService.class);
Hyunsun Moon479b7752016-05-06 20:13:28 -070053 DeviceService deviceService = AbstractShellCommand.get(DeviceService.class);
54
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090055 CordVtnNode node = nodeService.nodes().stream()
Hyunsun Moonde372572016-01-14 03:42:47 -080056 .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 Moon2c3f0ee2017-04-06 16:47:21 +090064 print("Current state: %s (%s)", node.state().name(), HINT);
Hyunsun Moon81a13562016-08-04 13:48:08 -070065 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 Moon479b7752016-05-06 20:13:28 -070074
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090075 node.systemInterfaces().forEach(iface -> print(
Hyunsun Moon81a13562016-08-04 13:48:08 -070076 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 Moon479b7752016-05-06 20:13:28 -070083
Hyunsun Moon81a13562016-08-04 13:48:08 -070084 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 Moon2c3f0ee2017-04-06 16:47:21 +090089 boolean isIp = ips.contains(node.dataIp().ip()) &&
90 ips.contains(node.localManagementIp().ip());
Hyunsun Moon81a13562016-08-04 13:48:08 -070091
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 Moon2c3f0ee2017-04-06 16:47:21 +090098 print(getSystemIfaceState(session, node.dataInterface()));
99 if (node.hostManagementInterface() != null) {
100 print(getSystemIfaceState(session, node.hostManagementInterface()));
Hyunsun Moon81a13562016-08-04 13:48:08 -0700101 }
102
103 disconnect(session);
104 } else {
105 print("%s Unable to SSH to %s", MSG_NO, node.hostname());
106 }
107 }
108
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +0900109 private String getPortState(DeviceService deviceService, DeviceId deviceId,
110 String portName) {
Hyunsun Moon81a13562016-08-04 13:48:08 -0700111 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 Moon2c3f0ee2017-04-06 16:47:21 +0900118 port.isEnabled() ? MSG_OK : MSG_NO,
119 portName,
120 port.number(),
121 port.isEnabled() ? Boolean.TRUE : Boolean.FALSE,
122 port.annotations());
Hyunsun Moon81a13562016-08-04 13:48:08 -0700123 } 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 Moonde372572016-01-14 03:42:47 -0800136 }
Hyunsun Moonde372572016-01-14 03:42:47 -0800137}