blob: 6659a38d2d62502463c87ecd0ee92253585ffc95 [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 */
Ayaka Koshibee8e45352014-10-16 00:37:19 -070019package org.onlab.onos.cli;
20
21import static com.google.common.collect.Lists.newArrayList;
22
23import java.util.Collections;
24import java.util.List;
25
26import org.apache.karaf.shell.commands.Command;
27import org.onlab.onos.cluster.NodeId;
Ayaka Koshibeabedb092014-10-20 17:01:31 -070028import org.onlab.onos.cluster.RoleInfo;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070029import org.onlab.onos.mastership.MastershipService;
30import org.onlab.onos.net.Device;
31import org.onlab.onos.net.DeviceId;
32import org.onlab.onos.net.device.DeviceService;
33
34
35/**
36 * Lists mastership roles of nodes for each device.
37 */
38@Command(scope = "onos", name = "roles",
39 description = "Lists mastership roles of nodes for each device.")
40public class RolesCommand extends AbstractShellCommand {
41
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070042 private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]";
Ayaka Koshibee8e45352014-10-16 00:37:19 -070043
44 @Override
45 protected void execute() {
46 DeviceService deviceService = get(DeviceService.class);
47 MastershipService roleService = get(MastershipService.class);
48
49 for (Device d : getSortedDevices(deviceService)) {
50 DeviceId did = d.id();
51 printRoles(roleService, did);
52 }
53 }
54
55 /**
56 * Returns the list of devices sorted using the device ID URIs.
57 *
58 * @param service device service
59 * @return sorted device list
60 */
61 protected static List<Device> getSortedDevices(DeviceService service) {
62 List<Device> devices = newArrayList(service.getDevices());
63 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR);
64 return devices;
65 }
66
67 /**
68 * Prints the role information for a device.
69 *
70 * @param deviceId the ID of the device
71 * @param master the current master
72 */
73 protected void printRoles(MastershipService service, DeviceId deviceId) {
Ayaka Koshibeabedb092014-10-20 17:01:31 -070074 RoleInfo nodes = service.getNodesFor(deviceId);
75 StringBuilder builder = new StringBuilder();
76 for (NodeId nid : nodes.backups()) {
77 builder.append(nid).append(" ");
78 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070079
Ayaka Koshibeabedb092014-10-20 17:01:31 -070080 print(FMT_HDR, deviceId,
81 nodes.master() == null ? "NONE" : nodes.master(),
82 builder.toString());
Ayaka Koshibee8e45352014-10-16 00:37:19 -070083 }
84}