blob: ff86eaaaa5c49e22b8afda59723e931ebdffa4b2 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom1380eee2014-09-24 09:22:02 -070019package org.onlab.onos.cli;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
tom1380eee2014-09-24 09:22:02 -070024import com.google.common.collect.Lists;
25import org.apache.karaf.shell.commands.Command;
tom1380eee2014-09-24 09:22:02 -070026import org.onlab.onos.cluster.ClusterService;
27import org.onlab.onos.cluster.ControllerNode;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070028import org.onlab.onos.mastership.MastershipService;
tom1380eee2014-09-24 09:22:02 -070029import org.onlab.onos.net.DeviceId;
30
31import java.util.Collections;
32import java.util.List;
33
34import static com.google.common.collect.Lists.newArrayList;
35
36/**
37 * Lists device mastership information.
38 */
39@Command(scope = "onos", name = "masters",
40 description = "Lists device mastership information")
41public class MastersListCommand extends AbstractShellCommand {
42
43 @Override
44 protected void execute() {
45 ClusterService service = get(ClusterService.class);
46 MastershipService mastershipService = get(MastershipService.class);
47 List<ControllerNode> nodes = newArrayList(service.getNodes());
48 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070049
50 if (outputJson()) {
51 print("%s", json(service, mastershipService, nodes));
52 } else {
53 for (ControllerNode node : nodes) {
54 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
55 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
56 print("%s: %d devices", node.id(), ids.size());
57 for (DeviceId deviceId : ids) {
58 print(" %s", deviceId);
59 }
60 }
61 }
62 }
63
64 // Produces JSON structure.
65 private JsonNode json(ClusterService service, MastershipService mastershipService,
66 List<ControllerNode> nodes) {
67 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
tom1380eee2014-09-24 09:22:02 -070069 ControllerNode self = service.getLocalNode();
70 for (ControllerNode node : nodes) {
71 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
tom32085cf2014-10-16 00:04:33 -070072 result.add(mapper.createObjectNode()
73 .put("id", node.id().toString())
74 .put("size", ids.size())
75 .set("devices", json(mapper, ids)));
tom1380eee2014-09-24 09:22:02 -070076 }
tom32085cf2014-10-16 00:04:33 -070077 return result;
78 }
79
80 /**
81 * Produces a JSON array containing the specified device identifiers.
82 *
83 * @param mapper object mapper
84 * @param ids collection of device identifiers
85 * @return JSON array
86 */
87 public static JsonNode json(ObjectMapper mapper, Iterable<DeviceId> ids) {
88 ArrayNode result = mapper.createArrayNode();
89 for (DeviceId deviceId : ids) {
90 result.add(deviceId.toString());
91 }
92 return result;
tom1380eee2014-09-24 09:22:02 -070093 }
94
95}