Yuta HIGUCHI | 60731cb | 2014-11-11 01:34:46 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli; |
Yuta HIGUCHI | 60731cb | 2014-11-11 01:34:46 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | |
| 22 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.ControllerNode; |
| 25 | import org.onosproject.store.service.DatabaseAdminService; |
Yuta HIGUCHI | 60731cb | 2014-11-11 01:34:46 -0800 | [diff] [blame] | 26 | |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.collect.Lists.newArrayList; |
| 31 | |
| 32 | /** |
| 33 | * Lists all controller cluster nodes. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "tablet-member", |
| 36 | description = "Lists all member nodes") |
| 37 | public class TabletMemberCommand extends AbstractShellCommand { |
| 38 | |
| 39 | // TODO add tablet name argument when we support multiple tablets |
| 40 | |
| 41 | @Override |
| 42 | protected void execute() { |
| 43 | DatabaseAdminService service = get(DatabaseAdminService.class); |
| 44 | ClusterService clusterService = get(ClusterService.class); |
| 45 | List<ControllerNode> nodes = newArrayList(service.listMembers()); |
| 46 | Collections.sort(nodes, Comparators.NODE_COMPARATOR); |
| 47 | if (outputJson()) { |
| 48 | print("%s", json(service, nodes)); |
| 49 | } else { |
| 50 | ControllerNode self = clusterService.getLocalNode(); |
| 51 | for (ControllerNode node : nodes) { |
| 52 | print("id=%s, address=%s:%s %s", |
| 53 | node.id(), node.ip(), node.tcpPort(), |
| 54 | node.equals(self) ? "*" : ""); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Produces JSON structure. |
| 60 | private JsonNode json(DatabaseAdminService service, List<ControllerNode> nodes) { |
| 61 | ObjectMapper mapper = new ObjectMapper(); |
| 62 | ArrayNode result = mapper.createArrayNode(); |
| 63 | for (ControllerNode node : nodes) { |
| 64 | result.add(mapper.createObjectNode() |
| 65 | .put("id", node.id().toString()) |
| 66 | .put("ip", node.ip().toString()) |
| 67 | .put("tcpPort", node.tcpPort())); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | } |