blob: caaf2444373fc758e819a3c679cf28b899b7be00 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
tom1380eee2014-09-24 09:22:02 -070016package org.onlab.onos.cli;
17
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
tom1380eee2014-09-24 09:22:02 -070021import com.google.common.collect.Lists;
22import org.apache.karaf.shell.commands.Command;
tom1380eee2014-09-24 09:22:02 -070023import org.onlab.onos.cluster.ClusterService;
24import org.onlab.onos.cluster.ControllerNode;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070025import org.onlab.onos.mastership.MastershipService;
tom1380eee2014-09-24 09:22:02 -070026import org.onlab.onos.net.DeviceId;
27
28import java.util.Collections;
29import java.util.List;
30
31import static com.google.common.collect.Lists.newArrayList;
32
33/**
34 * Lists device mastership information.
35 */
36@Command(scope = "onos", name = "masters",
37 description = "Lists device mastership information")
38public class MastersListCommand extends AbstractShellCommand {
39
40 @Override
41 protected void execute() {
42 ClusterService service = get(ClusterService.class);
43 MastershipService mastershipService = get(MastershipService.class);
44 List<ControllerNode> nodes = newArrayList(service.getNodes());
45 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070046
47 if (outputJson()) {
48 print("%s", json(service, mastershipService, nodes));
49 } else {
50 for (ControllerNode node : nodes) {
51 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
52 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
53 print("%s: %d devices", node.id(), ids.size());
54 for (DeviceId deviceId : ids) {
55 print(" %s", deviceId);
56 }
57 }
58 }
59 }
60
61 // Produces JSON structure.
62 private JsonNode json(ClusterService service, MastershipService mastershipService,
63 List<ControllerNode> nodes) {
64 ObjectMapper mapper = new ObjectMapper();
65 ArrayNode result = mapper.createArrayNode();
tom1380eee2014-09-24 09:22:02 -070066 ControllerNode self = service.getLocalNode();
67 for (ControllerNode node : nodes) {
68 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
tom32085cf2014-10-16 00:04:33 -070069 result.add(mapper.createObjectNode()
70 .put("id", node.id().toString())
71 .put("size", ids.size())
72 .set("devices", json(mapper, ids)));
tom1380eee2014-09-24 09:22:02 -070073 }
tom32085cf2014-10-16 00:04:33 -070074 return result;
75 }
76
77 /**
78 * Produces a JSON array containing the specified device identifiers.
79 *
80 * @param mapper object mapper
81 * @param ids collection of device identifiers
82 * @return JSON array
83 */
84 public static JsonNode json(ObjectMapper mapper, Iterable<DeviceId> ids) {
85 ArrayNode result = mapper.createArrayNode();
86 for (DeviceId deviceId : ids) {
87 result.add(deviceId.toString());
88 }
89 return result;
tom1380eee2014-09-24 09:22:02 -070090 }
91
92}