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 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
| 26 | import org.onlab.onos.cluster.NodeId; |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 27 | import org.onlab.onos.cluster.RoleInfo; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 28 | import org.onlab.onos.mastership.MastershipService; |
| 29 | import org.onlab.onos.net.Device; |
| 30 | import org.onlab.onos.net.DeviceId; |
| 31 | import org.onlab.onos.net.device.DeviceService; |
| 32 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 33 | import java.util.List; |
| 34 | |
| 35 | import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Lists mastership roles of nodes for each device. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "roles", |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 41 | description = "Lists mastership roles of nodes for each device.") |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 42 | public class RolesCommand extends AbstractShellCommand { |
| 43 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 44 | private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]"; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 45 | |
| 46 | @Override |
| 47 | protected void execute() { |
| 48 | DeviceService deviceService = get(DeviceService.class); |
| 49 | MastershipService roleService = get(MastershipService.class); |
| 50 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 51 | if (outputJson()) { |
| 52 | print("%s", json(roleService, getSortedDevices(deviceService))); |
| 53 | } else { |
| 54 | for (Device d : getSortedDevices(deviceService)) { |
| 55 | DeviceId did = d.id(); |
| 56 | printRoles(roleService, did); |
| 57 | } |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 61 | // Produces JSON structure with role information for the given devices. |
| 62 | private JsonNode json(MastershipService service, List<Device> sortedDevices) { |
| 63 | ObjectMapper mapper = new ObjectMapper(); |
| 64 | ArrayNode results = mapper.createArrayNode(); |
| 65 | for (Device device : sortedDevices) { |
| 66 | results.add(json(service, mapper, device)); |
| 67 | } |
| 68 | return results; |
| 69 | } |
| 70 | |
| 71 | // Produces JSON structure with role information for the given device. |
| 72 | private JsonNode json(MastershipService service, ObjectMapper mapper, |
| 73 | Device device) { |
| 74 | NodeId master = service.getMasterFor(device.id()); |
| 75 | ObjectNode result = mapper.createObjectNode() |
| 76 | .put("id", device.id().toString()) |
| 77 | .put("master", master != null ? master.toString() : "none"); |
| 78 | RoleInfo nodes = service.getNodesFor(device.id()); |
| 79 | ArrayNode standbys = mapper.createArrayNode(); |
| 80 | for (NodeId nid : nodes.backups()) { |
| 81 | standbys.add(nid.toString()); |
| 82 | } |
| 83 | result.set("standbys", standbys); |
| 84 | return result; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Prints the role information for a device. |
| 89 | * |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 90 | * @param service mastership service |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 91 | * @param deviceId the ID of the device |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 92 | */ |
| 93 | protected void printRoles(MastershipService service, DeviceId deviceId) { |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 94 | RoleInfo nodes = service.getNodesFor(deviceId); |
| 95 | StringBuilder builder = new StringBuilder(); |
| 96 | for (NodeId nid : nodes.backups()) { |
| 97 | builder.append(nid).append(" "); |
| 98 | } |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 99 | |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 100 | print(FMT_HDR, deviceId, |
Thomas Vachuska | 444eda6 | 2014-10-28 13:09:42 -0700 | [diff] [blame] | 101 | nodes.master() == null ? "NONE" : nodes.master(), |
| 102 | builder.toString()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 103 | } |
| 104 | } |