blob: da42790a04820ce8752ef4ce32eeaa626ec73697 [file] [log] [blame]
Yuta HIGUCHI60731cb2014-11-11 01:34:46 -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 */
16package org.onlab.onos.cli;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21
22import org.apache.karaf.shell.commands.Command;
23import org.onlab.onos.cluster.ClusterService;
24import org.onlab.onos.cluster.ControllerNode;
25import org.onlab.onos.store.service.DatabaseAdminService;
26
27import java.util.Collections;
28import java.util.List;
29
30import 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")
37public 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}