Jon Hall

Fix for ONOS-1715: NPE in nodes cli command

Change-Id: I5a1c4e3a73162ee34ede1ea1aba641898822adea
...@@ -15,19 +15,21 @@ ...@@ -15,19 +15,21 @@
15 */ 15 */
16 package org.onosproject.cli; 16 package org.onosproject.cli;
17 17
18 -import static com.google.common.collect.Lists.newArrayList; 18 +import com.fasterxml.jackson.databind.JsonNode;
19 - 19 +import com.fasterxml.jackson.databind.ObjectMapper;
20 -import java.util.Collections; 20 +import com.fasterxml.jackson.databind.node.ArrayNode;
21 -import java.util.List; 21 +import com.fasterxml.jackson.databind.node.ObjectNode;
22 -
23 import org.apache.karaf.shell.commands.Command; 22 import org.apache.karaf.shell.commands.Command;
23 +import org.joda.time.DateTime;
24 import org.onlab.util.Tools; 24 import org.onlab.util.Tools;
25 import org.onosproject.cluster.ClusterService; 25 import org.onosproject.cluster.ClusterService;
26 import org.onosproject.cluster.ControllerNode; 26 import org.onosproject.cluster.ControllerNode;
27 27
28 -import com.fasterxml.jackson.databind.JsonNode; 28 +import java.util.Collections;
29 -import com.fasterxml.jackson.databind.ObjectMapper; 29 +import java.util.List;
30 -import com.fasterxml.jackson.databind.node.ArrayNode; 30 +
31 +import static com.google.common.collect.Lists.newArrayList;
32 +
31 33
32 /** 34 /**
33 * Lists all controller cluster nodes. 35 * Lists all controller cluster nodes.
...@@ -49,9 +51,14 @@ public class NodesListCommand extends AbstractShellCommand { ...@@ -49,9 +51,14 @@ public class NodesListCommand extends AbstractShellCommand {
49 } else { 51 } else {
50 ControllerNode self = service.getLocalNode(); 52 ControllerNode self = service.getLocalNode();
51 for (ControllerNode node : nodes) { 53 for (ControllerNode node : nodes) {
54 + DateTime lastUpdated = service.getLastUpdated(node.id());
55 + String timeAgo = "Never";
56 + if (lastUpdated != null) {
57 + timeAgo = Tools.timeAgo(lastUpdated.getMillis());
58 + }
52 print(FMT, node.id(), node.ip(), node.tcpPort(), 59 print(FMT, node.id(), node.ip(), node.tcpPort(),
53 service.getState(node.id()), 60 service.getState(node.id()),
54 - Tools.timeAgo(service.getLastUpdated(node.id()).getMillis()), 61 + timeAgo,
55 node.equals(self) ? "*" : ""); 62 node.equals(self) ? "*" : "");
56 } 63 }
57 } 64 }
...@@ -63,12 +70,17 @@ public class NodesListCommand extends AbstractShellCommand { ...@@ -63,12 +70,17 @@ public class NodesListCommand extends AbstractShellCommand {
63 ArrayNode result = mapper.createArrayNode(); 70 ArrayNode result = mapper.createArrayNode();
64 ControllerNode self = service.getLocalNode(); 71 ControllerNode self = service.getLocalNode();
65 for (ControllerNode node : nodes) { 72 for (ControllerNode node : nodes) {
66 - result.add(mapper.createObjectNode() 73 + ControllerNode.State nodeState = service.getState(node.id());
74 + ObjectNode newNode = mapper.createObjectNode()
67 .put("id", node.id().toString()) 75 .put("id", node.id().toString())
68 .put("ip", node.ip().toString()) 76 .put("ip", node.ip().toString())
69 .put("tcpPort", node.tcpPort()) 77 .put("tcpPort", node.tcpPort())
70 - .put("state", service.getState(node.id()).toString()) 78 + .put("self", node.equals(self));
71 - .put("self", node.equals(self))); 79 +
80 + if (nodeState != null) {
81 + newNode.put("state", nodeState.toString());
82 + }
83 + result.add(newNode);
72 } 84 }
73 return result; 85 return result;
74 } 86 }
......