blob: bd5ba3a8c4102c8167a2118b852a7258fd4b294d [file] [log] [blame]
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080017
Madan Jampania14047d2015-02-25 12:23:02 -080018import java.util.Comparator;
19import java.util.Map;
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070020import java.util.List;
Madan Jampania14047d2015-02-25 12:23:02 -080021
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080022import org.apache.karaf.shell.commands.Command;
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070023import org.apache.karaf.shell.commands.Option;
Madan Jampani30a57f82015-03-02 12:19:41 -080024import org.onlab.util.Tools;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.cluster.Leadership;
27import org.onosproject.cluster.LeadershipService;
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070028import org.onosproject.cluster.NodeId;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080029
Ray Milkey1c198d62015-03-04 14:03:52 -080030import com.fasterxml.jackson.databind.JsonNode;
31import com.fasterxml.jackson.databind.ObjectMapper;
32import com.fasterxml.jackson.databind.node.ArrayNode;
33
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080034/**
35 * Prints the leader for every topic.
36 */
37@Command(scope = "onos", name = "leaders",
38 description = "Finds the leader for particular topic.")
39public class LeaderCommand extends AbstractShellCommand {
40
Madan Jampani30a57f82015-03-02 12:19:41 -080041 private static final String FMT = "%-20s | %-15s | %-6s | %-10s |";
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070042 private static final String FMT_C = "%-20s | %-15s | %-19s |";
43
44 @Option(name = "-c", aliases = "--candidates",
45 description = "List candidate Nodes for each topic's leadership race",
46 required = false, multiValued = false)
47 private boolean showCandidates = false;
Brian O'Connora0d31002014-12-01 13:43:18 -080048
Ray Milkey1c198d62015-03-04 14:03:52 -080049 /**
50 * Compares leaders, sorting by toString() output.
51 */
52 private Comparator<Leadership> leadershipComparator =
53 (e1, e2) -> {
54 if (e1.leader() == null && e2.leader() == null) {
55 return 0;
56 }
57 if (e1.leader() == null) {
58 return 1;
59 }
60 if (e2.leader() == null) {
61 return -1;
62 }
63 return e1.leader().toString().compareTo(e2.leader().toString());
64 };
65
66 /**
67 * Displays text representing the leaders.
68 *
69 * @param leaderBoard map of leaders
70 */
71 private void displayLeaders(Map<String, Leadership> leaderBoard) {
Madan Jampani30a57f82015-03-02 12:19:41 -080072 print("--------------------------------------------------------------");
73 print(FMT, "Topic", "Leader", "Epoch", "Elected");
74 print("--------------------------------------------------------------");
75
Jonathan Hartf2fda812015-02-17 15:21:03 -080076 leaderBoard.values()
77 .stream()
78 .sorted(leadershipComparator)
Madan Jampani30a57f82015-03-02 12:19:41 -080079 .forEach(l -> print(FMT,
80 l.topic(),
81 l.leader(),
82 l.epoch(),
83 Tools.timeAgo(l.electedTime())));
84 print("--------------------------------------------------------------");
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080085 }
Ray Milkey1c198d62015-03-04 14:03:52 -080086
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070087 private void displayCandidates(Map<String, Leadership> leaderBoard,
Ayaka Koshibefd26a302015-04-13 13:59:54 -070088 Map<String, Leadership> candidates) {
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070089 print("--------------------------------------------------------------");
90 print(FMT_C, "Topic", "Leader", "Candidates");
91 print("--------------------------------------------------------------");
92 leaderBoard
93 .values()
94 .stream()
95 .sorted(leadershipComparator)
96 .forEach(l -> {
Ayaka Koshibefd26a302015-04-13 13:59:54 -070097 List<NodeId> list = candidates.get(l.topic()).candidates();
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070098 print(FMT_C,
99 l.topic(),
100 l.leader(),
Ayaka Koshibefd26a302015-04-13 13:59:54 -0700101 list.get(0).toString());
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700102 // formatting hacks to get it into a table
Ayaka Koshibefd26a302015-04-13 13:59:54 -0700103 list.subList(1, list.size()).forEach(n -> print(FMT_C, " ", " ", n));
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700104 print(FMT_C, " ", " ", " ");
105 });
106 print("--------------------------------------------------------------");
107 }
108
Ray Milkey1c198d62015-03-04 14:03:52 -0800109 /**
110 * Returns JSON node representing the leaders.
111 *
112 * @param leaderBoard map of leaders
113 */
114 private JsonNode json(Map<String, Leadership> leaderBoard) {
115 ObjectMapper mapper = new ObjectMapper();
116 ArrayNode result = mapper.createArrayNode();
117 leaderBoard.values()
118 .stream()
119 .sorted(leadershipComparator)
120 .forEach(l ->
121 result.add(
122 mapper.createObjectNode()
123 .put("topic", l.topic())
124 .put("leader", l.leader().toString())
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700125 .put("candidates", l.candidates().toString())
Ray Milkey1c198d62015-03-04 14:03:52 -0800126 .put("epoch", l.epoch())
127 .put("electedTime", Tools.timeAgo(l.electedTime()))));
128
129 return result;
130 }
131
132
133 @Override
134 protected void execute() {
135 LeadershipService leaderService = get(LeadershipService.class);
136 Map<String, Leadership> leaderBoard = leaderService.getLeaderBoard();
137
138 if (outputJson()) {
139 print("%s", json(leaderBoard));
140 } else {
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700141 if (showCandidates) {
Ayaka Koshibefd26a302015-04-13 13:59:54 -0700142 Map<String, Leadership> candidates = leaderService.getCandidates();
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700143 displayCandidates(leaderBoard, candidates);
144 } else {
145 displayLeaders(leaderBoard);
146 }
Ray Milkey1c198d62015-03-04 14:03:52 -0800147 }
148 }
149}