Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame^] | 1 | /* |
| 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 Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli; |
| 20 | |
| 21 | import static com.google.common.collect.Lists.newArrayList; |
| 22 | |
| 23 | import java.util.Collections; |
| 24 | import java.util.List; |
| 25 | |
| 26 | import org.apache.karaf.shell.commands.Command; |
| 27 | import org.onlab.onos.cluster.NodeId; |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 28 | import org.onlab.onos.cluster.RoleInfo; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 29 | import org.onlab.onos.mastership.MastershipService; |
| 30 | import org.onlab.onos.net.Device; |
| 31 | import org.onlab.onos.net.DeviceId; |
| 32 | import 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.") |
| 40 | public class RolesCommand extends AbstractShellCommand { |
| 41 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 42 | private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]"; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 43 | |
| 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 Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 74 | RoleInfo nodes = service.getNodesFor(deviceId); |
| 75 | StringBuilder builder = new StringBuilder(); |
| 76 | for (NodeId nid : nodes.backups()) { |
| 77 | builder.append(nid).append(" "); |
| 78 | } |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 79 | |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 80 | print(FMT_HDR, deviceId, |
| 81 | nodes.master() == null ? "NONE" : nodes.master(), |
| 82 | builder.toString()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 83 | } |
| 84 | } |