Madan Jampani
Committed by Gerrit Code Review

Leaders cli improvements:

 -c option now returns term and term start time.
 -j option returns all info (including candidates)
 -fixes the NPE identified in ONOS-3846

Change-Id: Ifbbd1aab92df78113f5abab09d360cee7f43c2d0
...@@ -41,7 +41,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; ...@@ -41,7 +41,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
41 public class LeaderCommand extends AbstractShellCommand { 41 public class LeaderCommand extends AbstractShellCommand {
42 42
43 private static final String FMT = "%-30s | %-15s | %-5s | %-10s |"; 43 private static final String FMT = "%-30s | %-15s | %-5s | %-10s |";
44 - private static final String FMT_C = "%-30s | %-15s | %-19s |"; 44 + private static final String FMT_C = "%-30s | %-15s | %-5s | %-10s | %-19s |";
45 private boolean allTopics; 45 private boolean allTopics;
46 private Pattern pattern; 46 private Pattern pattern;
47 47
...@@ -83,74 +83,54 @@ public class LeaderCommand extends AbstractShellCommand { ...@@ -83,74 +83,54 @@ public class LeaderCommand extends AbstractShellCommand {
83 print("------------------------------------------------------------------------"); 83 print("------------------------------------------------------------------------");
84 } 84 }
85 85
86 - private void displayCandidates(Map<String, Leadership> leaderBoard, 86 + private void displayCandidates(Map<String, Leadership> leaderBoard) {
87 - Map<String, List<NodeId>> candidates) { 87 + print("--------------------------------------------------------------------------------------------");
88 - print("------------------------------------------------------------------------"); 88 + print(FMT_C, "Topic", "Leader", "Term", "Elected", "Candidates");
89 - print(FMT_C, "Topic", "Leader", "Candidates"); 89 + print("--------------------------------------------------------------------------------------------");
90 - print("------------------------------------------------------------------------"); 90 + leaderBoard.entrySet()
91 - candidates
92 - .entrySet()
93 .stream() 91 .stream()
94 .filter(es -> allTopics || pattern.matcher(es.getKey()).matches()) 92 .filter(es -> allTopics || pattern.matcher(es.getKey()).matches())
93 + .sorted((a, b) -> leadershipComparator.compare(a.getValue(), b.getValue()))
95 .forEach(es -> { 94 .forEach(es -> {
96 - List<NodeId> list = es.getValue(); 95 + Leadership l = es.getValue();
97 - if (list == null || list.isEmpty()) { 96 + List<NodeId> candidateList = l.candidates();
97 + if (candidateList == null || candidateList.isEmpty()) {
98 return; 98 return;
99 } 99 }
100 - Leadership l = leaderBoard.get(es.getKey());
101 print(FMT_C, 100 print(FMT_C,
102 es.getKey(), 101 es.getKey(),
103 String.valueOf(l.leaderNodeId()), 102 String.valueOf(l.leaderNodeId()),
103 + l.leader().term(),
104 + Tools.timeAgo(l.leader().termStartTime()),
104 // formatting hacks to get it into a table 105 // formatting hacks to get it into a table
105 - list.get(0).toString()); 106 + candidateList.get(0).toString());
106 - list.subList(1, list.size()).forEach(n -> print(FMT_C, " ", " ", n)); 107 + candidateList.subList(1, candidateList.size())
107 - print(FMT_C, " ", " ", " "); 108 + .forEach(n -> print(FMT_C, " ", " ", " ", " ", n));
109 + print(FMT_C, " ", " ", " ", " ", " ");
108 }); 110 });
109 - print("------------------------------------------------------------------------"); 111 + print("--------------------------------------------------------------------------------------------");
110 } 112 }
111 113
112 /** 114 /**
113 - * Returns JSON node representing the leaders. 115 + * Returns JSON node representing the leaders and candidates.
114 * 116 *
115 * @param leaderBoard map of leaders 117 * @param leaderBoard map of leaders
116 */ 118 */
117 private JsonNode json(Map<String, Leadership> leaderBoard) { 119 private JsonNode json(Map<String, Leadership> leaderBoard) {
118 ObjectMapper mapper = new ObjectMapper(); 120 ObjectMapper mapper = new ObjectMapper();
119 ArrayNode result = mapper.createArrayNode(); 121 ArrayNode result = mapper.createArrayNode();
120 - leaderBoard.values() 122 + leaderBoard.forEach((topic, leadership) -> {
121 - .stream() 123 + result.add(
122 - .sorted(leadershipComparator)
123 - .forEach(l ->
124 - result.add(
125 - mapper.createObjectNode()
126 - .put("topic", l.topic())
127 - .put("leader", String.valueOf(l.leaderNodeId()))
128 - .put("candidates", l.candidates().toString())
129 - .put("epoch", l.leader().term())
130 - .put("epochStartTime", Tools.timeAgo(l.leader().termStartTime()))));
131 -
132 - return result;
133 - }
134 -
135 - /**
136 - * Returns JSON node representing the leaders.
137 - *
138 - * @param leaderBoard map of leaders
139 - */
140 - private JsonNode json(Map<String, Leadership> leaderBoard,
141 - Map<String, List<NodeId>> candidateBoard) {
142 - ObjectMapper mapper = new ObjectMapper();
143 - ArrayNode result = mapper.createArrayNode();
144 - candidateBoard.entrySet()
145 - .stream()
146 - .forEach(es -> {
147 - Leadership l = leaderBoard.get(es.getKey());
148 - result.add(
149 mapper.createObjectNode() 124 mapper.createObjectNode()
150 - .put("topic", es.getKey()) 125 + .put("topic", topic)
151 - .put("leader", l == null ? "none" : l.leader().toString()) 126 + .put("leader", leadership.leaderNodeId() == null ?
152 - .put("candidates", es.getValue().toString())); 127 + "none" : leadership.leaderNodeId().toString())
153 - }); 128 + .put("term", leadership.leader() != null ?
129 + leadership.leader().term() : 0)
130 + .put("termStartTime", leadership.leader() != null ?
131 + leadership.leader().termStartTime() : 0)
132 + .put("candidates", leadership.candidates().toString()));
133 + });
154 return result; 134 return result;
155 } 135 }
156 136
...@@ -165,20 +145,15 @@ public class LeaderCommand extends AbstractShellCommand { ...@@ -165,20 +145,15 @@ public class LeaderCommand extends AbstractShellCommand {
165 pattern = Pattern.compile(topicPattern); 145 pattern = Pattern.compile(topicPattern);
166 } 146 }
167 147
148 + if (outputJson()) {
149 + print("%s", json(leaderBoard));
150 + return;
151 + }
152 +
168 if (showCandidates) { 153 if (showCandidates) {
169 - Map<String, List<NodeId>> candidates = leaderService 154 + displayCandidates(leaderBoard);
170 - .getCandidates();
171 - if (outputJson()) {
172 - print("%s", json(leaderBoard, candidates));
173 - } else {
174 - displayCandidates(leaderBoard, candidates);
175 - }
176 } else { 155 } else {
177 - if (outputJson()) { 156 + displayLeaders(leaderBoard);
178 - print("%s", json(leaderBoard));
179 - } else {
180 - displayLeaders(leaderBoard);
181 - }
182 } 157 }
183 } 158 }
184 } 159 }
......