blob: abda0275072febf2840a2467d073f476e9d55101 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli;
tom1380eee2014-09-24 09:22:02 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.ControllerNode;
25import org.onosproject.mastership.MastershipService;
26import org.onosproject.net.DeviceId;
Ray Milkeyc7477292016-03-11 10:53:43 -080027import org.onosproject.utils.Comparators;
tom1380eee2014-09-24 09:22:02 -070028
29import java.util.Collections;
30import java.util.List;
31
32import static com.google.common.collect.Lists.newArrayList;
33
34/**
35 * Lists device mastership information.
36 */
37@Command(scope = "onos", name = "masters",
38 description = "Lists device mastership information")
39public class MastersListCommand extends AbstractShellCommand {
40
41 @Override
42 protected void execute() {
43 ClusterService service = get(ClusterService.class);
44 MastershipService mastershipService = get(MastershipService.class);
45 List<ControllerNode> nodes = newArrayList(service.getNodes());
46 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070047
48 if (outputJson()) {
49 print("%s", json(service, mastershipService, nodes));
50 } else {
51 for (ControllerNode node : nodes) {
52 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
53 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
54 print("%s: %d devices", node.id(), ids.size());
55 for (DeviceId deviceId : ids) {
56 print(" %s", deviceId);
57 }
58 }
59 }
60 }
61
62 // Produces JSON structure.
63 private JsonNode json(ClusterService service, MastershipService mastershipService,
64 List<ControllerNode> nodes) {
65 ObjectMapper mapper = new ObjectMapper();
66 ArrayNode result = mapper.createArrayNode();
tom1380eee2014-09-24 09:22:02 -070067 ControllerNode self = service.getLocalNode();
68 for (ControllerNode node : nodes) {
69 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
tom32085cf2014-10-16 00:04:33 -070070 result.add(mapper.createObjectNode()
71 .put("id", node.id().toString())
72 .put("size", ids.size())
73 .set("devices", json(mapper, ids)));
tom1380eee2014-09-24 09:22:02 -070074 }
tom32085cf2014-10-16 00:04:33 -070075 return result;
76 }
77
78 /**
79 * Produces a JSON array containing the specified device identifiers.
80 *
81 * @param mapper object mapper
82 * @param ids collection of device identifiers
83 * @return JSON array
84 */
85 public static JsonNode json(ObjectMapper mapper, Iterable<DeviceId> ids) {
86 ArrayNode result = mapper.createArrayNode();
87 for (DeviceId deviceId : ids) {
88 result.add(deviceId.toString());
89 }
90 return result;
tom1380eee2014-09-24 09:22:02 -070091 }
92
93}