Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 17 | |
Ray Milkey | 3bceb01 | 2018-09-07 13:42:46 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
| 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 24 | import org.apache.karaf.shell.api.action.Option; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 25 | import org.onlab.util.Tools; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.cluster.Leadership; |
Ray Milkey | 3bceb01 | 2018-09-07 13:42:46 -0700 | [diff] [blame] | 28 | import org.onosproject.cluster.LeadershipAdminService; |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 29 | import org.onosproject.cluster.NodeId; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 30 | |
Ray Milkey | 3bceb01 | 2018-09-07 13:42:46 -0700 | [diff] [blame] | 31 | import java.util.Comparator; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.regex.Pattern; |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 35 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Prints the leader for every topic. |
| 38 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | @Service |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 40 | @Command(scope = "onos", name = "leaders", |
| 41 | description = "Finds the leader for particular topic.") |
| 42 | public class LeaderCommand extends AbstractShellCommand { |
| 43 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 44 | private static final String FMT = "%-30s | %-15s | %-5s | %-10s |"; |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 45 | private static final String FMT_C = "%-30s | %-15s | %-5s | %-10s | %-19s |"; |
Ayaka Koshibe | 941f860 | 2015-04-15 14:17:08 -0700 | [diff] [blame] | 46 | private boolean allTopics; |
| 47 | private Pattern pattern; |
| 48 | |
| 49 | @Argument(index = 0, name = "topic", description = "A leadership topic. Can be a regex", |
| 50 | required = false, multiValued = false) |
| 51 | String topicPattern = null; |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 52 | |
| 53 | @Option(name = "-c", aliases = "--candidates", |
| 54 | description = "List candidate Nodes for each topic's leadership race", |
| 55 | required = false, multiValued = false) |
| 56 | private boolean showCandidates = false; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 57 | |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Compares leaders, sorting by toString() output. |
| 60 | */ |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 61 | private Comparator<Leadership> leadershipComparator = (l1, l2) -> |
| 62 | String.valueOf(l1.leaderNodeId()).compareTo(String.valueOf(l2.leaderNodeId())); |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Displays text representing the leaders. |
| 66 | * |
| 67 | * @param leaderBoard map of leaders |
| 68 | */ |
| 69 | private void displayLeaders(Map<String, Leadership> leaderBoard) { |
Madan Jampani | 5756c35 | 2015-04-29 00:23:58 -0700 | [diff] [blame] | 70 | print("------------------------------------------------------------------------"); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 71 | print(FMT, "Topic", "Leader", "Term", "Elected"); |
Madan Jampani | 5756c35 | 2015-04-29 00:23:58 -0700 | [diff] [blame] | 72 | print("------------------------------------------------------------------------"); |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 73 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 74 | leaderBoard.values() |
| 75 | .stream() |
Ayaka Koshibe | 941f860 | 2015-04-15 14:17:08 -0700 | [diff] [blame] | 76 | .filter(l -> allTopics || pattern.matcher(l.topic()).matches()) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 77 | .filter(l -> l.leader() != null) |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 78 | .sorted(leadershipComparator) |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 79 | .forEach(l -> print(FMT, |
| 80 | l.topic(), |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 81 | l.leaderNodeId(), |
| 82 | l.leader().term(), |
| 83 | Tools.timeAgo(l.leader().termStartTime()))); |
Madan Jampani | 5756c35 | 2015-04-29 00:23:58 -0700 | [diff] [blame] | 84 | print("------------------------------------------------------------------------"); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 85 | } |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 86 | |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 87 | private void displayCandidates(Map<String, Leadership> leaderBoard) { |
| 88 | print("--------------------------------------------------------------------------------------------"); |
| 89 | print(FMT_C, "Topic", "Leader", "Term", "Elected", "Candidates"); |
| 90 | print("--------------------------------------------------------------------------------------------"); |
| 91 | leaderBoard.entrySet() |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 92 | .stream() |
Ayaka Koshibe | 0d886fc | 2015-04-23 11:53:41 -0700 | [diff] [blame] | 93 | .filter(es -> allTopics || pattern.matcher(es.getKey()).matches()) |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 94 | .sorted((a, b) -> leadershipComparator.compare(a.getValue(), b.getValue())) |
Ayaka Koshibe | 0d886fc | 2015-04-23 11:53:41 -0700 | [diff] [blame] | 95 | .forEach(es -> { |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 96 | Leadership l = es.getValue(); |
| 97 | List<NodeId> candidateList = l.candidates(); |
| 98 | if (candidateList == null || candidateList.isEmpty()) { |
Ayaka Koshibe | 0d886fc | 2015-04-23 11:53:41 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 101 | print(FMT_C, |
Ayaka Koshibe | 0d886fc | 2015-04-23 11:53:41 -0700 | [diff] [blame] | 102 | es.getKey(), |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 103 | String.valueOf(l.leaderNodeId()), |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 104 | l.leader().term(), |
| 105 | Tools.timeAgo(l.leader().termStartTime()), |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 106 | // formatting hacks to get it into a table |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 107 | candidateList.get(0).toString()); |
| 108 | candidateList.subList(1, candidateList.size()) |
| 109 | .forEach(n -> print(FMT_C, " ", " ", " ", " ", n)); |
| 110 | print(FMT_C, " ", " ", " ", " ", " "); |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 111 | }); |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 112 | print("--------------------------------------------------------------------------------------------"); |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 115 | /** |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 116 | * Returns JSON node representing the leaders and candidates. |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 117 | * |
| 118 | * @param leaderBoard map of leaders |
| 119 | */ |
| 120 | private JsonNode json(Map<String, Leadership> leaderBoard) { |
| 121 | ObjectMapper mapper = new ObjectMapper(); |
| 122 | ArrayNode result = mapper.createArrayNode(); |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 123 | leaderBoard.forEach((topic, leadership) -> { |
| 124 | result.add( |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 125 | mapper.createObjectNode() |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 126 | .put("topic", topic) |
| 127 | .put("leader", leadership.leaderNodeId() == null ? |
| 128 | "none" : leadership.leaderNodeId().toString()) |
| 129 | .put("term", leadership.leader() != null ? |
| 130 | leadership.leader().term() : 0) |
| 131 | .put("termStartTime", leadership.leader() != null ? |
| 132 | leadership.leader().termStartTime() : 0) |
| 133 | .put("candidates", leadership.candidates().toString())); |
| 134 | }); |
Ayaka Koshibe | 0d886fc | 2015-04-23 11:53:41 -0700 | [diff] [blame] | 135 | return result; |
| 136 | } |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 137 | |
| 138 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 139 | protected void doExecute() { |
Ray Milkey | 3bceb01 | 2018-09-07 13:42:46 -0700 | [diff] [blame] | 140 | LeadershipAdminService leaderService = get(LeadershipAdminService.class); |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 141 | Map<String, Leadership> leaderBoard = leaderService.getLeaderBoard(); |
Ayaka Koshibe | 941f860 | 2015-04-15 14:17:08 -0700 | [diff] [blame] | 142 | if (topicPattern == null) { |
| 143 | allTopics = true; |
| 144 | } else { |
| 145 | allTopics = false; |
| 146 | pattern = Pattern.compile(topicPattern); |
| 147 | } |
| 148 | |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 149 | if (outputJson()) { |
| 150 | print("%s", json(leaderBoard)); |
| 151 | return; |
| 152 | } |
| 153 | |
Ayaka Koshibe | 0d886fc | 2015-04-23 11:53:41 -0700 | [diff] [blame] | 154 | if (showCandidates) { |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 155 | displayCandidates(leaderBoard); |
Ayaka Koshibe | 0d886fc | 2015-04-23 11:53:41 -0700 | [diff] [blame] | 156 | } else { |
Madan Jampani | 0872767 | 2016-02-01 11:57:14 -0800 | [diff] [blame] | 157 | displayLeaders(leaderBoard); |
Ray Milkey | 1c198d6 | 2015-03-04 14:03:52 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |