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